This repository was archived by the owner on Apr 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathLayoutFrame.java
More file actions
306 lines (278 loc) · 9.67 KB
/
LayoutFrame.java
File metadata and controls
306 lines (278 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.openfin.desktop.demo;
/**
* Example of Java window that can be managed by OpenFin layout service for snap&dock
*/
import com.openfin.desktop.*;
import com.openfin.desktop.Window;
import com.openfin.desktop.channel.ChannelAction;
import com.openfin.desktop.channel.ChannelClient;
import com.openfin.desktop.win32.ExternalWindowObserver;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.System;
public class LayoutFrame extends JFrame {
private static String LayoutServiceChannelName = "of-layouts-service-v1";
private ExternalWindowObserver externalWindowObserver;
private JLabel labelName;
private JButton btnUndock;
private String windowName;
private ChannelClient channelClient;
private String appUuid;
private boolean frameless;
public LayoutFrame(DesktopConnection desktopConnection, String appUuid, String windowName) throws DesktopException {
this(desktopConnection, appUuid, windowName, false);
}
public LayoutFrame(DesktopConnection desktopConnection, String appUuid, String windowName, boolean frameless) throws DesktopException {
super();
this.setTitle(windowName);
System.out.println(windowName + " being created ");
this.appUuid = appUuid;
this.windowName = windowName;
this.frameless = frameless;
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setPreferredSize(new Dimension(640, 480));
JPanel pnl = new JPanel();
pnl.setLayout(new BoxLayout(pnl, BoxLayout.Y_AXIS));
this.labelName = new JLabel(windowName);
pnl.add(labelName);
this.btnUndock = new JButton("undock");
this.btnUndock.setEnabled(false);
pnl.add(btnUndock);
this.getContentPane().add(pnl);
if (frameless) {
this.setUndecorated(true);
JPanel titleBar = new JPanel(new BorderLayout());
titleBar.setBackground(Color.DARK_GRAY);
MouseAdapter myListener = new MouseAdapter() {
int pressedAtX, pressedAtY;
@Override
public void mousePressed(MouseEvent e) {
pressedAtX = e.getX();
pressedAtY = e.getY();
System.out.println("mouse pressed at x=" + pressedAtX + ", y=" + pressedAtY);
LayoutFrame.this.externalWindowObserver.enterSizeMove();
}
@Override
public void mouseDragged(MouseEvent e) {
int distanceX = e.getX() - pressedAtX;
int distanceY = e.getY() - pressedAtY;
System.out.println("dragged x=" + distanceX + ", y=" + distanceY);
Point frameLocation = LayoutFrame.this.getLocation();
Dimension dimension = LayoutFrame.this.getSize();
WindowBounds bounds = new WindowBounds(frameLocation.x + distanceX, frameLocation.y + distanceY,
dimension.width, dimension.height);
Point point = new Point(e.getX(), e.getY());
if (!LayoutFrame.this.externalWindowObserver.onMoving(bounds, point)) {
LayoutFrame.this.setLocation(frameLocation.x + distanceX, frameLocation.y + distanceY);
}
}
@Override
public void mouseReleased(MouseEvent e) {
LayoutFrame.this.externalWindowObserver.exitSizeMove();
}
};
titleBar.addMouseListener(myListener);
titleBar.addMouseMotionListener(myListener);
JButton btnClose = new JButton("X");
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LayoutFrame.this.dispose();
}});
titleBar.add(btnClose, BorderLayout.EAST);
this.getContentPane().add(titleBar, BorderLayout.NORTH);
}
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.externalWindowObserver = new ExternalWindowObserver(desktopConnection.getPort(), appUuid, windowName, this,
new AckListener() {
@Override
public void onSuccess(Ack ack) {
ExternalWindowObserver observer = (ExternalWindowObserver) ack.getSource();
observer.getDesktopConnection().getChannel(LayoutServiceChannelName).connect(
new AsyncCallback<ChannelClient>() {
@Override
public void onSuccess(ChannelClient client) {
LayoutFrame.this.channelClient = client;
btnUndock.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JSONObject payload = new JSONObject();
payload.put("uuid", appUuid);
payload.put("name", windowName);
client.dispatch("UNDOCK-WINDOW", payload, null);
}
});
client.register("event", new ChannelAction() {
@Override
public JSONObject invoke(String action, JSONObject payload, JSONObject senderIdentity) {
System.out.printf("channel event " + action);
return null;
}
});
}
});
}
@Override
public void onError(Ack ack) {
System.out.println(windowName + ": unable to register external window, " + ack.getReason());
}
});
// this.externalWindowObserver.setUserGesture(!this.frameless);
try {
// if (this.frameless) {
// WindowOptions options = new WindowOptions();
// options.setFrame(false);
// this.externalWindowObserver.setWindowOptions(options);
// }
this.externalWindowObserver.start();
} catch (Exception e) {
e.printStackTrace();
}
Window w = Window.wrap(appUuid, windowName, desktopConnection);
w.addEventListener("group-changed", new EventListener() {
@Override
public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) {
JSONObject eventObj = actionEvent.getEventObject();
w.getGroup(new AsyncCallback<java.util.List<Window>>() {
@Override
public void onSuccess(java.util.List<Window> result) {
if (result.size() > 0) {
checkTabbing();
} else {
LayoutFrame.this.btnUndock.setEnabled(false);
setHasFrame(LayoutFrame.this, true);
}
}
}, null);
}
}, null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
try {
LayoutFrame.this.cleanup();
LayoutFrame.this.dispose();
}
catch (Exception e1) {
e1.printStackTrace();
}
}
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
System.out.println(windowName + " closed ");
}
});
}
private void checkTabbing() {
JSONObject payload = new JSONObject();
payload.put("uuid", appUuid);
payload.put("name", windowName);
channelClient.dispatch("GETTABS", payload, new AckListener() {
@Override
public void onSuccess(Ack ack) {
System.out.printf("channel GETTABS ");
JSONObject data = (JSONObject) ack.getData();
Object result = data.get("result");
if (result != null && result instanceof JSONArray) {
JSONArray tabs = (JSONArray) result;
boolean enabled = !(tabs != null && tabs.length() > 0);
LayoutFrame.this.btnUndock.setEnabled(enabled);
setHasFrame(LayoutFrame.this, false);
} else {
LayoutFrame.this.btnUndock.setEnabled(true);
setHasFrame(LayoutFrame.this, true);
}
}
@Override
public void onError(Ack ack) {
System.out.printf("channel GETTABS error " + ack.getReason());
}
});
}
private void setHasFrame(JFrame frame, boolean hasFrame) {
if (!this.frameless) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println(windowName + " hasFrame=" + hasFrame);
WinDef.HWND hWnd = new WinDef.HWND();
hWnd.setPointer(Native.getComponentPointer(frame));
LayoutFrame.this.externalWindowObserver.setHasFrame(hWnd, hasFrame);
frame.setResizable(hasFrame);
frame.invalidate();
frame.validate();
frame.repaint();
SwingUtilities.updateComponentTreeUI(frame);
}
});
}
}
public String getWindowName() {
return windowName;
}
public void cleanup() {
try {
System.out.println(windowName + " cleaning up ");
this.externalWindowObserver.dispose();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(640, 480));
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
frame.setUndecorated(true);
JPanel titleBar = new JPanel(new BorderLayout());
titleBar.setBackground(Color.DARK_GRAY);
MouseAdapter myListener = new MouseAdapter() {
int pressedAtX, pressedAtY;
@Override
public void mousePressed(MouseEvent e) {
pressedAtX = e.getX();
pressedAtY = e.getY();
System.out.println("mouse pressed at x=" + pressedAtX + ", y=" + pressedAtY);
}
@Override
public void mouseDragged(MouseEvent e) {
int distanceX = e.getX() - pressedAtX;
int distanceY = e.getY() - pressedAtY;
System.out.println("dragged x=" + distanceX + ", y=" + distanceY);
Point frameLocation = frame.getLocation();
frame.setLocation(frameLocation.x + distanceX, frameLocation.y + distanceY);
}
@Override
public void mouseReleased(MouseEvent e) {
pressedAtX = e.getX();
pressedAtY = e.getY();
System.out.println("mouse released at x=" + pressedAtX + ", y=" + pressedAtY);
}
};
titleBar.addMouseListener(myListener);
titleBar.addMouseMotionListener(myListener);
JButton btnClose = new JButton("X");
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}});
titleBar.add(btnClose, BorderLayout.EAST);
frame.getContentPane().add(titleBar, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}