|
9 | 9 | import com.openfin.desktop.channel.ChannelAction; |
10 | 10 | import com.openfin.desktop.channel.ChannelClient; |
11 | 11 | import com.openfin.desktop.win32.ExternalWindowObserver; |
| 12 | +import com.sun.jna.Native; |
| 13 | +import com.sun.jna.platform.win32.User32; |
| 14 | +import com.sun.jna.platform.win32.WinDef; |
| 15 | + |
12 | 16 | import org.json.JSONArray; |
13 | 17 | import org.json.JSONObject; |
14 | 18 |
|
|
22 | 26 | import java.util.Iterator; |
23 | 27 |
|
24 | 28 | public class LayoutFrame extends JFrame { |
25 | | - private ExternalWindowObserver externalWindowObserver; |
26 | | - private JButton btnUndock; |
27 | | - private String windowName; |
28 | | - private ChannelClient channelClient; |
29 | | - private String appUuid; |
30 | | - |
31 | | - public LayoutFrame(DesktopConnection desktopConnection, String appUuid, String windowName) throws DesktopException { |
32 | | - super(); |
33 | | - System.out.println(windowName + " being created "); |
34 | | - this.appUuid = appUuid; |
35 | | - this.windowName = windowName; |
36 | | - this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
37 | | - this.setPreferredSize(new Dimension(640, 480)); |
38 | | - JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
39 | | - this.btnUndock = new JButton("undock"); |
40 | | - this.btnUndock.setEnabled(false); |
41 | | - pnl.add(btnUndock); |
42 | | - this.getContentPane().add(pnl); |
43 | | - this.pack(); |
44 | | - this.setLocationRelativeTo(null); |
45 | | - this.setVisible(true); |
46 | | - |
47 | | - this.externalWindowObserver = new ExternalWindowObserver(desktopConnection.getPort(), appUuid, windowName, |
48 | | - this, new AckListener() { |
49 | | - @Override |
50 | | - public void onSuccess(Ack ack) { |
51 | | - ExternalWindowObserver observer = (ExternalWindowObserver) ack.getSource(); |
52 | | - observer.getDesktopConnection().getChannel().connect("of-layouts-service-v1", |
53 | | - new AsyncCallback<ChannelClient>() { |
54 | | - @Override |
55 | | - public void onSuccess(ChannelClient client) { |
56 | | - LayoutFrame.this.channelClient = client; |
57 | | - btnUndock.addActionListener(new ActionListener() { |
58 | | - @Override |
59 | | - public void actionPerformed(ActionEvent e) { |
60 | | - JSONObject payload = new JSONObject(); |
61 | | - payload.put("uuid", appUuid); |
62 | | - payload.put("name", windowName); |
63 | | - client.dispatch("UNDOCK-WINDOW", payload, null); |
64 | | - } |
65 | | - }); |
66 | | - |
67 | | - client.register("event", new ChannelAction() { |
68 | | - @Override |
69 | | - public JSONObject invoke(String action, JSONObject payload) { |
70 | | - System.out.printf("channel event " + action); |
71 | | - return null; |
72 | | - } |
73 | | - }); |
74 | | - } |
75 | | - }); |
76 | | - } |
77 | | - @Override |
78 | | - public void onError(Ack ack) { |
79 | | - System.out.println(windowName + ": unable to register external window, " + ack.getReason()); |
80 | | - } |
81 | | - }); |
82 | | - |
83 | | - Window w = Window.wrap(appUuid, windowName, desktopConnection); |
84 | | - w.addEventListener("group-changed", new EventListener() { |
85 | | - @Override |
86 | | - public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) { |
87 | | - JSONObject eventObj = actionEvent.getEventObject(); |
88 | | - w.getGroup(new AsyncCallback<java.util.List<Window>>() { |
89 | | - @Override |
90 | | - public void onSuccess(java.util.List<Window> result) { |
91 | | - if (result.size() > 0) { |
92 | | - checkTabbing(); |
93 | | - } else { |
94 | | - LayoutFrame.this.btnUndock.setEnabled(false); |
95 | | - } |
96 | | - } |
97 | | - }, null); |
98 | | - } |
99 | | - }, null); |
100 | | - |
101 | | - this.addWindowListener(new WindowAdapter() { |
102 | | - public void windowClosing(WindowEvent e) { |
103 | | - super.windowClosing(e); |
104 | | - try { |
105 | | - LayoutFrame.this.cleanup(); |
106 | | - LayoutFrame.this.dispose(); |
107 | | - } catch (Exception e1) { |
108 | | - e1.printStackTrace(); |
109 | | - } |
110 | | - } |
111 | | - public void windowClosed(WindowEvent e) { |
112 | | - super.windowClosed(e); |
113 | | - System.out.println(windowName + " closed "); |
114 | | - } |
115 | | - }); |
116 | | - } |
117 | | - |
118 | | - private void checkTabbing() { |
119 | | - JSONObject payload = new JSONObject(); |
120 | | - payload.put("uuid", appUuid); |
121 | | - payload.put("name", windowName); |
122 | | - channelClient.dispatch("GETTABS", payload, new AckListener() { |
123 | | - @Override |
124 | | - public void onSuccess(Ack ack) { |
125 | | - System.out.printf("channel GETTABS "); |
126 | | - JSONObject data = (JSONObject) ack.getData(); |
127 | | - Object result = data.get("result"); |
128 | | - if (result != null && result instanceof JSONArray) { |
129 | | - JSONArray tabs = (JSONArray) result; |
130 | | - LayoutFrame.this.btnUndock.setEnabled(!(tabs != null && tabs.length() > 0)); |
131 | | - } else { |
132 | | - LayoutFrame.this.btnUndock.setEnabled(true); |
133 | | - } |
134 | | - } |
135 | | - @Override |
136 | | - public void onError(Ack ack) { |
137 | | - System.out.printf("channel GETTABS error " + ack.getReason()); |
138 | | - } |
139 | | - }); |
140 | | - } |
141 | | - |
142 | | - public String getWindowName() { |
143 | | - return windowName; |
144 | | - } |
145 | | - |
146 | | - public void cleanup() { |
147 | | - try { |
148 | | - System.out.println(windowName + " cleaning up "); |
149 | | - this.externalWindowObserver.dispose(); |
150 | | - } catch (Exception e) { |
151 | | - e.printStackTrace(); |
152 | | - } |
153 | | - } |
| 29 | + private ExternalWindowObserver externalWindowObserver; |
| 30 | + private JButton btnUndock; |
| 31 | + private String windowName; |
| 32 | + private ChannelClient channelClient; |
| 33 | + private String appUuid; |
| 34 | + |
| 35 | + public LayoutFrame(DesktopConnection desktopConnection, String appUuid, String windowName) throws DesktopException { |
| 36 | + super(); |
| 37 | + System.out.println(windowName + " being created "); |
| 38 | + this.appUuid = appUuid; |
| 39 | + this.windowName = windowName; |
| 40 | + this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 41 | + this.setPreferredSize(new Dimension(640, 480)); |
| 42 | + JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 43 | + this.btnUndock = new JButton("undock"); |
| 44 | + this.btnUndock.setEnabled(false); |
| 45 | + pnl.add(btnUndock); |
| 46 | + this.getContentPane().add(pnl); |
| 47 | + this.pack(); |
| 48 | + this.setLocationRelativeTo(null); |
| 49 | + this.setVisible(true); |
| 50 | + |
| 51 | + this.externalWindowObserver = new ExternalWindowObserver(desktopConnection.getPort(), appUuid, windowName, this, |
| 52 | + new AckListener() { |
| 53 | + @Override |
| 54 | + public void onSuccess(Ack ack) { |
| 55 | + ExternalWindowObserver observer = (ExternalWindowObserver) ack.getSource(); |
| 56 | + observer.getDesktopConnection().getChannel().connect("of-layouts-service-v1", |
| 57 | + new AsyncCallback<ChannelClient>() { |
| 58 | + @Override |
| 59 | + public void onSuccess(ChannelClient client) { |
| 60 | + LayoutFrame.this.channelClient = client; |
| 61 | + btnUndock.addActionListener(new ActionListener() { |
| 62 | + @Override |
| 63 | + public void actionPerformed(ActionEvent e) { |
| 64 | + JSONObject payload = new JSONObject(); |
| 65 | + payload.put("uuid", appUuid); |
| 66 | + payload.put("name", windowName); |
| 67 | + client.dispatch("UNDOCK-WINDOW", payload, null); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + client.register("event", new ChannelAction() { |
| 72 | + @Override |
| 73 | + public JSONObject invoke(String action, JSONObject payload) { |
| 74 | + System.out.printf("channel event " + action); |
| 75 | + return null; |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onError(Ack ack) { |
| 84 | + System.out.println(windowName + ": unable to register external window, " + ack.getReason()); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + Window w = Window.wrap(appUuid, windowName, desktopConnection); |
| 89 | + w.addEventListener("group-changed", new EventListener() { |
| 90 | + @Override |
| 91 | + public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) { |
| 92 | + JSONObject eventObj = actionEvent.getEventObject(); |
| 93 | + w.getGroup(new AsyncCallback<java.util.List<Window>>() { |
| 94 | + @Override |
| 95 | + public void onSuccess(java.util.List<Window> result) { |
| 96 | + if (result.size() > 0) { |
| 97 | + checkTabbing(); |
| 98 | + } else { |
| 99 | + LayoutFrame.this.btnUndock.setEnabled(false); |
| 100 | + setHasFrame(LayoutFrame.this, true); |
| 101 | + } |
| 102 | + } |
| 103 | + }, null); |
| 104 | + } |
| 105 | + }, null); |
| 106 | + |
| 107 | + this.addWindowListener(new WindowAdapter() { |
| 108 | + public void windowClosing(WindowEvent e) { |
| 109 | + super.windowClosing(e); |
| 110 | + try { |
| 111 | + LayoutFrame.this.cleanup(); |
| 112 | + LayoutFrame.this.dispose(); |
| 113 | + } |
| 114 | + catch (Exception e1) { |
| 115 | + e1.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public void windowClosed(WindowEvent e) { |
| 120 | + super.windowClosed(e); |
| 121 | + System.out.println(windowName + " closed "); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + private void checkTabbing() { |
| 127 | + JSONObject payload = new JSONObject(); |
| 128 | + payload.put("uuid", appUuid); |
| 129 | + payload.put("name", windowName); |
| 130 | + channelClient.dispatch("GETTABS", payload, new AckListener() { |
| 131 | + @Override |
| 132 | + public void onSuccess(Ack ack) { |
| 133 | + System.out.printf("channel GETTABS "); |
| 134 | + JSONObject data = (JSONObject) ack.getData(); |
| 135 | + Object result = data.get("result"); |
| 136 | + if (result != null && result instanceof JSONArray) { |
| 137 | + JSONArray tabs = (JSONArray) result; |
| 138 | + boolean enabled = !(tabs != null && tabs.length() > 0); |
| 139 | + LayoutFrame.this.btnUndock.setEnabled(enabled); |
| 140 | + setHasFrame(LayoutFrame.this, false); |
| 141 | + } else { |
| 142 | + LayoutFrame.this.btnUndock.setEnabled(true); |
| 143 | + setHasFrame(LayoutFrame.this, true); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onError(Ack ack) { |
| 149 | + System.out.printf("channel GETTABS error " + ack.getReason()); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + private void setHasFrame(JFrame frame, boolean hasFrame) { |
| 155 | + SwingUtilities.invokeLater(new Runnable() { |
| 156 | + @Override |
| 157 | + public void run() { |
| 158 | + System.out.println(windowName + " hasFrame=" + hasFrame); |
| 159 | + |
| 160 | + Dimension size = frame.getSize(); |
| 161 | + WinDef.HWND hWnd = new WinDef.HWND(); |
| 162 | + hWnd.setPointer(Native.getComponentPointer(frame)); |
| 163 | + |
| 164 | + int style = User32.INSTANCE.GetWindowLong(hWnd, User32.GWL_STYLE); |
| 165 | + |
| 166 | + if (hasFrame) { |
| 167 | + frame.setResizable(true); |
| 168 | +// style = style & ~User32.WS_CHILD; |
| 169 | + style = style | User32.WS_CAPTION | User32.WS_BORDER ; |
| 170 | + } else { |
| 171 | + frame.setResizable(false); |
| 172 | + style = style &~ User32.WS_CAPTION &~ User32.WS_BORDER; |
| 173 | +// style = style | User32.WS_CHILD; |
| 174 | + } |
| 175 | + User32.INSTANCE.SetWindowLong(hWnd, User32.GWL_STYLE, style); |
| 176 | + User32.INSTANCE.RedrawWindow(hWnd, null, null, new WinDef.DWORD((User32.RDW_FRAME | User32.RDW_INVALIDATE))); |
| 177 | + frame.setSize(size.width, size.height + 1); |
| 178 | + frame.invalidate(); |
| 179 | + frame.repaint(); |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + public String getWindowName() { |
| 185 | + return windowName; |
| 186 | + } |
| 187 | + |
| 188 | + public void cleanup() { |
| 189 | + try { |
| 190 | + System.out.println(windowName + " cleaning up "); |
| 191 | + this.externalWindowObserver.dispose(); |
| 192 | + } |
| 193 | + catch (Exception e) { |
| 194 | + e.printStackTrace(); |
| 195 | + } |
| 196 | + } |
154 | 197 | } |
0 commit comments