Skip to content

Commit 514454e

Browse files
committed
ADAP-142 docking demo using layout service
1 parent 65f7684 commit 514454e

1 file changed

Lines changed: 301 additions & 0 deletions

File tree

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
package com.openfin.desktop.demo;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Dimension;
5+
import java.awt.FlowLayout;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.awt.event.WindowAdapter;
9+
import java.awt.event.WindowEvent;
10+
import java.io.IOException;
11+
import java.util.UUID;
12+
import java.util.concurrent.CountDownLatch;
13+
14+
import javax.swing.BorderFactory;
15+
import javax.swing.JButton;
16+
import javax.swing.JFrame;
17+
import javax.swing.JLabel;
18+
import javax.swing.JPanel;
19+
import javax.swing.SwingUtilities;
20+
21+
import org.json.JSONObject;
22+
23+
import com.openfin.desktop.Ack;
24+
import com.openfin.desktop.AckListener;
25+
import com.openfin.desktop.Application;
26+
import com.openfin.desktop.ApplicationOptions;
27+
import com.openfin.desktop.AsyncCallback;
28+
import com.openfin.desktop.DesktopConnection;
29+
import com.openfin.desktop.DesktopException;
30+
import com.openfin.desktop.DesktopIOException;
31+
import com.openfin.desktop.DesktopStateListener;
32+
import com.openfin.desktop.RuntimeConfiguration;
33+
import com.openfin.desktop.WindowOptions;
34+
import com.openfin.desktop.channel.ChannelClient;
35+
import com.openfin.desktop.win32.ExternalWindowObserver;
36+
37+
public class LayoutServiceDemo implements DesktopStateListener {
38+
39+
private final static String appUuid = "layoutServiceDemo";
40+
41+
private DesktopConnection desktopConnection;
42+
private CountDownLatch latch = new CountDownLatch(1);
43+
private JFrame mainWindow;
44+
private JButton btnCreateOpenfinWindow;
45+
private JButton btnCreateJavaWindow;
46+
private Application application;
47+
private ChannelClient channelClient;
48+
49+
LayoutServiceDemo() {
50+
try {
51+
this.createMainWindow();
52+
this.launchOpenfin();
53+
}
54+
catch (DesktopException | DesktopIOException | IOException | InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
void createMainWindow() {
60+
this.mainWindow = new JFrame("Layout Service Demo");
61+
this.mainWindow.setResizable(false);
62+
this.mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
63+
this.mainWindow.addWindowListener(new WindowAdapter() {
64+
@Override
65+
public void windowClosing(WindowEvent we) {
66+
try {
67+
application.close(true, new AckListener() {
68+
69+
@Override
70+
public void onSuccess(Ack ack) {
71+
if (channelClient != null) {
72+
desktopConnection.getChannel().disconnect(channelClient, new AckListener() {
73+
@Override
74+
public void onSuccess(Ack ack) {
75+
}
76+
77+
@Override
78+
public void onError(Ack ack) {
79+
}
80+
});
81+
}
82+
mainWindow.dispose();
83+
try {
84+
desktopConnection.disconnect();
85+
}
86+
catch (DesktopException e) {
87+
e.printStackTrace();
88+
}
89+
}
90+
91+
@Override
92+
public void onError(Ack ack) {
93+
}
94+
});
95+
96+
}
97+
catch (DesktopException de) {
98+
de.printStackTrace();
99+
}
100+
}
101+
});
102+
103+
this.btnCreateOpenfinWindow = new JButton("Create Openfin Window");
104+
this.btnCreateOpenfinWindow.addActionListener(new ActionListener() {
105+
@Override
106+
public void actionPerformed(ActionEvent e) {
107+
createOpenfinWindow();
108+
}
109+
});
110+
this.btnCreateJavaWindow = new JButton("Create Java Window");
111+
this.btnCreateJavaWindow.addActionListener(new ActionListener() {
112+
@Override
113+
public void actionPerformed(ActionEvent e) {
114+
try {
115+
createJavaWindow(UUID.randomUUID().toString());
116+
}
117+
catch (DesktopException e1) {
118+
e1.printStackTrace();
119+
}
120+
}
121+
});
122+
this.btnCreateOpenfinWindow.setEnabled(false);
123+
this.btnCreateJavaWindow.setEnabled(false);
124+
JPanel contentPnl = new JPanel(new BorderLayout(10, 10));
125+
contentPnl.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
126+
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
127+
pnl.add(btnCreateOpenfinWindow);
128+
pnl.add(btnCreateJavaWindow);
129+
130+
contentPnl.add(new JLabel("Undock Openfin windows with global hotkey (CTRL+SHIFT+U or CMD+SHIFT+U)"), BorderLayout.NORTH);
131+
contentPnl.add(pnl, BorderLayout.CENTER);
132+
133+
this.mainWindow.getContentPane().add(contentPnl);
134+
135+
this.mainWindow.pack();
136+
this.mainWindow.setLocationRelativeTo(null);
137+
this.mainWindow.setVisible(true);
138+
}
139+
140+
void launchOpenfin() throws DesktopException, DesktopIOException, IOException, InterruptedException {
141+
RuntimeConfiguration config = new RuntimeConfiguration();
142+
config.setRuntimeVersion("stable");
143+
config.setAdditionalRuntimeArguments("--v=1");
144+
config.addService("layouts", "https://cdn.openfin.co/services/openfin/layouts/app.json");
145+
this.desktopConnection = new DesktopConnection("LayoutServiceDemo");
146+
this.desktopConnection.connect(config, this, 60);
147+
latch.await();
148+
}
149+
150+
void createApplication(String name, String uuid, String url, AckListener listener) {
151+
ApplicationOptions appOpt = new ApplicationOptions(name, uuid, url);
152+
WindowOptions mainWindowOptions = new WindowOptions();
153+
mainWindowOptions.setAutoShow(false);
154+
mainWindowOptions.setDefaultHeight(480);
155+
mainWindowOptions.setDefaultWidth(640);
156+
appOpt.setMainWindowOptions(mainWindowOptions);
157+
158+
this.application = new Application(appOpt, this.desktopConnection, new AckListener() {
159+
@Override
160+
public void onSuccess(Ack ack) {
161+
application.run(listener);
162+
SwingUtilities.invokeLater(new Runnable() {
163+
164+
@Override
165+
public void run() {
166+
btnCreateOpenfinWindow.setEnabled(true);
167+
btnCreateJavaWindow.setEnabled(true);
168+
}
169+
});
170+
}
171+
172+
@Override
173+
public void onError(Ack ack) {
174+
}
175+
});
176+
}
177+
178+
void createJavaWindow(String windowName) throws DesktopException {
179+
final JButton btnUndock = new JButton("undock");
180+
181+
JFrame f = new JFrame();
182+
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
183+
f.setPreferredSize(new Dimension(640, 480));
184+
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
185+
pnl.add(btnUndock);
186+
f.getContentPane().add(pnl);
187+
f.pack();
188+
f.setLocationRelativeTo(null);
189+
f.setVisible(true);
190+
191+
new ExternalWindowObserver(this.desktopConnection.getPort(), appUuid, windowName, f, new AckListener() {
192+
@Override
193+
public void onSuccess(Ack ack) {
194+
System.out.println(windowName + ": java window observered");
195+
ExternalWindowObserver observer = (ExternalWindowObserver) ack.getSource();
196+
observer.getDesktopConnection().getChannel().connect("of-layouts-service-v1", new AsyncCallback<ChannelClient>() {
197+
@Override
198+
public void onSuccess(ChannelClient client) {
199+
btnUndock.addActionListener(new ActionListener() {
200+
@Override
201+
public void actionPerformed(ActionEvent e) {
202+
JSONObject payload = new JSONObject();
203+
payload.put("uuid", appUuid);
204+
payload.put("name", windowName);
205+
client.dispatch("UNDOCK-WINDOW", payload, null);
206+
}});
207+
}});
208+
}
209+
210+
@Override
211+
public void onError(Ack ack) {
212+
System.out.println(windowName + ": unable to register external window, " + ack.getReason());
213+
}
214+
});
215+
}
216+
217+
void createOpenfinWindow() {
218+
try {
219+
WindowOptions winOpts = new WindowOptions();
220+
winOpts.setAutoShow(true);
221+
winOpts.setDefaultHeight(480);
222+
winOpts.setDefaultWidth(640);
223+
winOpts.setName(UUID.randomUUID().toString());
224+
winOpts.setUrl("https://www.google.com");
225+
application.createChildWindow(winOpts, new AckListener() {
226+
227+
@Override
228+
public void onSuccess(Ack ack) {
229+
}
230+
231+
@Override
232+
public void onError(Ack ack) {
233+
System.out.println("unable to create openfin window: " + ack.getReason());
234+
}
235+
});
236+
}
237+
catch (DesktopException e) {
238+
e.printStackTrace();
239+
}
240+
}
241+
242+
@Override
243+
public void onReady() {
244+
createApplication(appUuid, appUuid, "about:blank", new AckListener() {
245+
@Override
246+
public void onSuccess(Ack ack) {
247+
}
248+
249+
@Override
250+
public void onError(Ack ack) {
251+
}
252+
253+
});
254+
255+
// this.desktopConnection.getChannel().connect("of-layouts-service-v1", new
256+
// AsyncCallback<ChannelClient>() {
257+
//
258+
// @Override
259+
// public void onSuccess(ChannelClient client) {
260+
// channelClient = client;
261+
// System.out.println("channel connected: " + client.getChannelId());
262+
//
263+
// // try {
264+
// // desktopConnection.disconnect();
265+
// // }
266+
// // catch (DesktopException e) {
267+
// // // TODO Auto-generated catch block
268+
// // e.printStackTrace();
269+
// // }
270+
// }
271+
// });
272+
}
273+
274+
@Override
275+
public void onClose(String error) {
276+
latch.countDown();
277+
}
278+
279+
@Override
280+
public void onError(String reason) {
281+
// TODO Auto-generated method stub
282+
283+
}
284+
285+
@Override
286+
public void onMessage(String message) {
287+
// TODO Auto-generated method stub
288+
289+
}
290+
291+
@Override
292+
public void onOutgoingMessage(String message) {
293+
// TODO Auto-generated method stub
294+
295+
}
296+
297+
public static void main(String[] args) {
298+
new LayoutServiceDemo();
299+
}
300+
301+
}

0 commit comments

Comments
 (0)