Skip to content

Commit 5c65bfa

Browse files
author
Wenjun Che
committed
ADAP-16: Embedding example
1 parent b24b6bb commit 5c65bfa

File tree

5 files changed

+293
-0
lines changed

5 files changed

+293
-0
lines changed

release/embed.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -cp openfin-desktop-java-example-3.0.1.4.jar;openfin-desktop-java-adapter-3.0.1.4.jar;TableLayout-20050920.jar -DOpenFinOption=--config=\"https://demoappdirectory.openf.in/desktop/config/apps/OpenFin/HelloOpenFin/app.json\" -Djava.util.logging.config.file=logging.properties com.openfin.desktop.demo.WindowEmbedDemo
4.71 KB
Binary file not shown.
9.19 KB
Binary file not shown.

src/main/java/com/openfin/desktop/demo/OpenFinDesktopDemo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class OpenFinDesktopDemo extends JPanel implements ActionListener, Window
5454
InterApplicationBus bus;
5555

5656
protected DesktopConnection controller;
57+
protected System openfinSystem;
5758
protected AppCreateDialog appCreateDialog;
5859
protected LoadAppsDialog loadAppsDialog;
5960

@@ -378,6 +379,7 @@ public void run() {
378379
private void createAdminApplication() {
379380
updateMessagePanel("Creating InterAppBus");
380381
bus = controller.getInterApplicationBus();
382+
openfinSystem = new System(controller);
381383
updateMessagePanel("Connected to Desktop");
382384
setMainButtonsEnabled(true);
383385

@@ -395,6 +397,22 @@ public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) {
395397
}
396398
}, null);
397399
}
400+
401+
openfinSystem.addEventListener("desktop-icon-clicked", new EventListener() {
402+
@Override
403+
public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) {
404+
updateMessagePanel("desktop-icon-clicked");
405+
}
406+
}, new AckListener() {
407+
@Override
408+
public void onSuccess(Ack ack) {
409+
}
410+
@Override
411+
public void onError(Ack ack) {
412+
java.lang.System.out.println("error with desktop-icon-clicked " + ack.getJsonObject().toString());
413+
}
414+
});
415+
398416
}
399417

400418
public void init() {
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
package com.openfin.desktop.demo;
2+
3+
import com.openfin.desktop.*;
4+
import com.openfin.desktop.Window;
5+
import com.openfin.desktop.win32.WinMessageHelper;
6+
import com.sun.jna.Native;
7+
import info.clearthought.layout.TableLayout;
8+
9+
import javax.swing.*;
10+
import java.awt.*;
11+
import java.awt.event.ActionListener;
12+
import java.awt.event.WindowEvent;
13+
import java.awt.event.WindowListener;
14+
import java.lang.System;
15+
16+
/**
17+
* Java example to embed HTML5 window in Swing window
18+
*
19+
* Created by wche on 3/13/15.
20+
*
21+
*/
22+
public class WindowEmbedDemo extends JPanel implements ActionListener, WindowListener {
23+
24+
private static JFrame jFrame;
25+
protected String appUuid = "JavaEmbedding";
26+
protected String startupUuid = "OpenFinHelloWorld";
27+
protected String desktopOption;
28+
protected DesktopConnection controller;
29+
30+
protected JButton launch, close, embed;
31+
protected java.awt.Canvas embedCanvas;
32+
protected Long previousPrarentHwndId;
33+
34+
public WindowEmbedDemo(final String desktopOption, final String startupUuid) {
35+
this.startupUuid = startupUuid;
36+
this.desktopOption = desktopOption;
37+
try {
38+
this.controller = new DesktopConnection(appUuid, "localhost", 9696);
39+
} catch (DesktopException desktopError) {
40+
desktopError.printStackTrace();
41+
}
42+
setLayout(new BorderLayout());
43+
add(layoutCenterPanel(), BorderLayout.CENTER);
44+
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
45+
setMainButtonsEnabled(false);
46+
}
47+
48+
private JPanel layoutCenterPanel() {
49+
JPanel panel = new JPanel();
50+
double size[][] = {{TableLayout.FILL}, {120, TableLayout.FILL}};
51+
panel.setLayout(new TableLayout(size));
52+
panel.add(layoutActionButtonPanel(), "0,0,0,0");
53+
panel.add(layoutEmbedPanel(), "0, 1, 0, 1");
54+
return panel;
55+
}
56+
57+
private JPanel layoutActionButtonPanel() {
58+
JPanel buttonPanel = new JPanel();
59+
60+
JPanel topPanel = new JPanel();
61+
double size[][] = {{10, 190, 20, 190, 10}, {25, 10, 25, 10}};
62+
topPanel.setLayout(new TableLayout(size));
63+
topPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(2), "Desktop"));
64+
65+
launch = new JButton("Launch OpenFin");
66+
launch.setActionCommand("start");
67+
close = new JButton("Shutdown OpenFin");
68+
close.setActionCommand("close");
69+
topPanel.add(launch, "1,0,1,0");
70+
topPanel.add(close, "3,0,3,0");
71+
72+
embed = new JButton("Embed HTML5 app");
73+
embed.setActionCommand("embed-window");
74+
embed.setEnabled(false);
75+
topPanel.add(embed, "1,2,1,2");
76+
77+
close.addActionListener(this);
78+
launch.addActionListener(this);
79+
embed.addActionListener(this);
80+
81+
buttonPanel.add(topPanel, "0,0");
82+
return buttonPanel;
83+
}
84+
85+
protected JPanel layoutEmbedPanel() {
86+
JPanel panel = new JPanel();
87+
panel.setLayout(new BorderLayout());
88+
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(2), "HTML5 app"));
89+
90+
embedCanvas = new java.awt.Canvas();
91+
panel.add(embedCanvas, BorderLayout.CENTER);
92+
93+
94+
return panel;
95+
}
96+
97+
private void setMainButtonsEnabled(boolean enabled) {
98+
launch.setEnabled(!enabled);
99+
embed.setEnabled(enabled);
100+
close.setEnabled(enabled);
101+
}
102+
103+
private static void createAndShowGUI(final String desktopOption, final String startupUuid) {
104+
//Create and set up the window.
105+
jFrame = new JFrame("Java Docking Demo");
106+
jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
107+
//Create and set up the content pane.
108+
WindowEmbedDemo newContentPane = new WindowEmbedDemo(desktopOption, startupUuid);
109+
newContentPane.setOpaque(true); //content panes must be opaque
110+
jFrame.setContentPane(newContentPane);
111+
jFrame.addWindowListener(newContentPane);
112+
//Display the window.
113+
jFrame.pack();
114+
jFrame.setSize(600, 800);
115+
jFrame.setLocationRelativeTo(null);
116+
jFrame.setResizable(true);
117+
jFrame.setVisible(true);
118+
}
119+
120+
private void closeDesktop() {
121+
if (controller != null && controller.isConnected()) {
122+
try {
123+
new com.openfin.desktop.System(controller).exit();
124+
} catch (Exception e) {
125+
e.printStackTrace();
126+
}
127+
}
128+
129+
//closeWebSocket();
130+
SwingUtilities.invokeLater(new Runnable() {
131+
@Override
132+
public void run() {
133+
jFrame.dispose();
134+
}
135+
});
136+
try {
137+
Thread.sleep(1000);
138+
java.lang.System.exit(0);
139+
} catch (InterruptedException e) {
140+
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
141+
}
142+
}
143+
144+
@Override
145+
public void windowOpened(WindowEvent e) {
146+
}
147+
148+
@Override
149+
public void windowClosing(WindowEvent e) {
150+
closeDesktop();
151+
}
152+
153+
@Override
154+
public void windowClosed(WindowEvent e) {
155+
}
156+
157+
@Override
158+
public void windowIconified(WindowEvent e) {
159+
}
160+
161+
@Override
162+
public void windowDeiconified(WindowEvent e) {
163+
}
164+
165+
@Override
166+
public void windowActivated(WindowEvent e) {
167+
}
168+
169+
@Override
170+
public void windowDeactivated(WindowEvent e) {
171+
}
172+
173+
public void actionPerformed(java.awt.event.ActionEvent e) {
174+
if ("start".equals(e.getActionCommand())) {
175+
runStartAction();
176+
} else if ("close".equals(e.getActionCommand())) {
177+
closeDesktop();
178+
} else if ("embed-window".equals(e.getActionCommand())) {
179+
embedStartupApp();
180+
} else if ("release-window".equals(e.getActionCommand())) {
181+
releaseStartupApp();
182+
}
183+
}
184+
185+
private void runStartAction() {
186+
try {
187+
DesktopStateListener listener = new DesktopStateListener() {
188+
@Override
189+
public void onReady() {
190+
setMainButtonsEnabled(true);
191+
}
192+
@Override
193+
public void onError(String reason) {
194+
195+
}
196+
@Override
197+
public void onMessage(String message) {
198+
}
199+
@Override
200+
public void onOutgoingMessage(String message) {
201+
}
202+
};
203+
controller.launchAndConnect(null, desktopOption, listener, 10000);
204+
205+
} catch (Exception e) {
206+
e.printStackTrace();
207+
}
208+
}
209+
210+
private void embedStartupApp() {
211+
try {
212+
Window html5Wnd = Window.wrap(startupUuid, startupUuid, controller);
213+
long parentHWndId = Native.getComponentID(this.embedCanvas);
214+
System.out.println("Canvas HWND " + Long.toHexString(parentHWndId));
215+
WinMessageHelper.embedInto(parentHWndId, html5Wnd, 395, 525, 0, 0, new AckListener() {
216+
@Override
217+
public void onSuccess(Ack ack) {
218+
if (ack.isSuccessful()) {
219+
previousPrarentHwndId = ack.getJsonObject().getLong("hWndPreviousParent");
220+
} else {
221+
java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString());
222+
}
223+
}
224+
@Override
225+
public void onError(Ack ack) {
226+
java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString());
227+
}
228+
});
229+
} catch (Exception e) {
230+
e.printStackTrace();
231+
}
232+
}
233+
234+
private void releaseStartupApp() {
235+
Window html5Wnd = Window.wrap(startupUuid, startupUuid, controller);
236+
if (this.previousPrarentHwndId != null) {
237+
WinMessageHelper.embedInto(this.previousPrarentHwndId, html5Wnd, 395, 525, 0, 0, new AckListener() {
238+
@Override
239+
public void onSuccess(Ack ack) {
240+
java.lang.System.out.println("embedding result: " + ack.getJsonObject().toString());
241+
}
242+
@Override
243+
public void onError(Ack ack) {
244+
java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString());
245+
}
246+
});
247+
this.previousPrarentHwndId = null;
248+
}
249+
}
250+
251+
/**
252+
* To start OpenFin Desktop and Connect, pass full path of OpenFin with*
253+
* -DOpenFinOption=--config=\"RemoteConfigUrl\"
254+
*
255+
* Set UUID of startup HTML5 app to dock to
256+
* -DStartupUUID="550e8400-e29b-41d4-a716-4466333333000"
257+
*
258+
* @param args
259+
*/
260+
public static void main(String[] args) {
261+
final String desktop_option = java.lang.System.getProperty("OpenFinOption");
262+
final String startupUUID;
263+
if (java.lang.System.getProperty("StartupUUID") != null) {
264+
startupUUID = java.lang.System.getProperty("StartupUUID");
265+
} else {
266+
startupUUID = "OpenFinHelloWorld";
267+
}
268+
javax.swing.SwingUtilities.invokeLater(new Runnable() {
269+
public void run() {
270+
createAndShowGUI(desktop_option, startupUUID);
271+
}
272+
});
273+
}
274+
}

0 commit comments

Comments
 (0)