|
| 1 | +package com.openfin.desktop.demo; |
| 2 | + |
| 3 | +import com.openfin.desktop.*; |
| 4 | +import com.openfin.desktop.win32.WinMessageHelper; |
| 5 | +import com.sun.jna.Native; |
| 6 | +import info.clearthought.layout.TableLayout; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import javax.swing.*; |
| 13 | +import java.awt.*; |
| 14 | +import java.awt.event.ActionListener; |
| 15 | +import java.awt.event.WindowEvent; |
| 16 | +import java.awt.event.WindowListener; |
| 17 | +import java.lang.System; |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by wche on 3/26/2016. |
| 22 | + */ |
| 23 | +public class TVBEmbedDemo extends JPanel implements ActionListener, WindowListener { |
| 24 | + |
| 25 | + private final static Logger logger = LoggerFactory.getLogger(TVBEmbedDemo.class.getName()); |
| 26 | + |
| 27 | + private static JFrame jFrame; |
| 28 | + private String appUuid = "JavaEmbedding"; |
| 29 | + private String startupUuid = "TVBWebLocal-yeotwn3hvf6wh142"; |
| 30 | + protected DesktopConnection desktopConnection; |
| 31 | + private JButton launch, close; |
| 32 | + private java.awt.Canvas embedCanvas; |
| 33 | + private Long previousPrarentHwndId; |
| 34 | + private java.util.Timer timer = new java.util.Timer(); |
| 35 | + |
| 36 | + // bounds of HTML5 app |
| 37 | + private static int appHeigth = 1080; |
| 38 | + private static int appWidth = 1920; |
| 39 | + |
| 40 | + public TVBEmbedDemo() { |
| 41 | + try { |
| 42 | + this.desktopConnection = new DesktopConnection(appUuid); |
| 43 | + } catch (DesktopException desktopError) { |
| 44 | + desktopError.printStackTrace(); |
| 45 | + } |
| 46 | + setLayout(new BorderLayout()); |
| 47 | + add(layoutCenterPanel(), BorderLayout.CENTER); |
| 48 | + setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
| 49 | + setMainButtonsEnabled(false); |
| 50 | + } |
| 51 | + |
| 52 | + private JPanel layoutCenterPanel() { |
| 53 | + JPanel panel = new JPanel(); |
| 54 | + double size[][] = {{TableLayout.FILL}, {40, TableLayout.FILL}}; |
| 55 | + panel.setLayout(new TableLayout(size)); |
| 56 | + panel.add(layoutActionButtonPanel(), "0,0,0,0"); |
| 57 | + panel.add(layoutEmbedPanel(), "0, 1, 0, 1"); |
| 58 | + return panel; |
| 59 | + } |
| 60 | + |
| 61 | + private JPanel layoutActionButtonPanel() { |
| 62 | + JPanel buttonPanel = new JPanel(); |
| 63 | + |
| 64 | + JPanel topPanel = new JPanel(); |
| 65 | + double size[][] = {{10, 190, 20, 190, 10}, {25}}; |
| 66 | + topPanel.setLayout(new TableLayout(size)); |
| 67 | + |
| 68 | + launch = new JButton("Launch OpenFin"); |
| 69 | + launch.setActionCommand("start"); |
| 70 | + close = new JButton("Shutdown OpenFin"); |
| 71 | + close.setActionCommand("close"); |
| 72 | + topPanel.add(launch, "1,0,1,0"); |
| 73 | + topPanel.add(close, "3,0,3,0"); |
| 74 | + |
| 75 | + close.addActionListener(this); |
| 76 | + launch.addActionListener(this); |
| 77 | + |
| 78 | + buttonPanel.add(topPanel, "0,0"); |
| 79 | + return buttonPanel; |
| 80 | + } |
| 81 | + |
| 82 | + protected JPanel layoutEmbedPanel() { |
| 83 | + JPanel panel = new JPanel(); |
| 84 | + panel.setLayout(new BorderLayout()); |
| 85 | + panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(2), "HTML5 app")); |
| 86 | + |
| 87 | + embedCanvas = new java.awt.Canvas(); |
| 88 | + panel.add(embedCanvas, BorderLayout.CENTER); |
| 89 | + |
| 90 | + |
| 91 | + return panel; |
| 92 | + } |
| 93 | + |
| 94 | + private void setMainButtonsEnabled(boolean enabled) { |
| 95 | + launch.setEnabled(!enabled); |
| 96 | + close.setEnabled(enabled); |
| 97 | + |
| 98 | + if (enabled) { |
| 99 | + this.desktopConnection.getInterApplicationBus().addSubscribeListener(new SubscriptionListener() { |
| 100 | + public void subscribed(String uuid, String topic) { |
| 101 | + System.out.println("subscribed " + uuid + " on topic " + topic); |
| 102 | + } |
| 103 | + |
| 104 | + public void unsubscribed(String uuid, String topic) { |
| 105 | + System.out.println("unsubscribed " + uuid + " on topic " + topic); |
| 106 | + |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static void createAndShowGUI() { |
| 113 | + //Create and set up the window. |
| 114 | + jFrame = new JFrame("Java Embedding Demo"); |
| 115 | + jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); |
| 116 | + //Create and set up the content pane. |
| 117 | + TVBEmbedDemo newContentPane = new TVBEmbedDemo(); |
| 118 | + newContentPane.setOpaque(true); //content panes must be opaque |
| 119 | + jFrame.setContentPane(newContentPane); |
| 120 | + jFrame.addWindowListener(newContentPane); |
| 121 | + //Display the window. |
| 122 | + jFrame.pack(); |
| 123 | + jFrame.setSize(appWidth+80, appHeigth+160); |
| 124 | + jFrame.setLocationRelativeTo(null); |
| 125 | + jFrame.setResizable(true); |
| 126 | + jFrame.setVisible(true); |
| 127 | + } |
| 128 | + |
| 129 | + private void closeDesktop() { |
| 130 | + if (desktopConnection != null && desktopConnection.isConnected()) { |
| 131 | + try { |
| 132 | + if (startupHtml5app != null) { |
| 133 | + startupHtml5app.close(); |
| 134 | + } |
| 135 | + desktopConnection.disconnect(); |
| 136 | +// OpenFinRuntime runtime = new OpenFinRuntime(desktopConnection); |
| 137 | +// runtime.exit(); |
| 138 | + } catch (Exception e) { |
| 139 | + e.printStackTrace(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + //closeWebSocket(); |
| 144 | + SwingUtilities.invokeLater(new Runnable() { |
| 145 | + @Override |
| 146 | + public void run() { |
| 147 | + jFrame.dispose(); |
| 148 | + } |
| 149 | + }); |
| 150 | + try { |
| 151 | + Thread.sleep(1000); |
| 152 | + java.lang.System.exit(0); |
| 153 | + } catch (InterruptedException e) { |
| 154 | + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void windowOpened(WindowEvent e) { |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void windowClosing(WindowEvent e) { |
| 164 | + closeDesktop(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void windowClosed(WindowEvent e) { |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void windowIconified(WindowEvent e) { |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void windowDeiconified(WindowEvent e) { |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void windowActivated(WindowEvent e) { |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void windowDeactivated(WindowEvent e) { |
| 185 | + } |
| 186 | + |
| 187 | + public void actionPerformed(java.awt.event.ActionEvent e) { |
| 188 | + if ("start".equals(e.getActionCommand())) { |
| 189 | + runStartAction(); |
| 190 | + } else if ("close".equals(e.getActionCommand())) { |
| 191 | + closeDesktop(); |
| 192 | + } else if ("embed-window".equals(e.getActionCommand())) { |
| 193 | + embedStartupApp(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private void runStartAction() { |
| 198 | + try { |
| 199 | + DesktopStateListener listener = new DesktopStateListener() { |
| 200 | + @Override |
| 201 | + public void onReady() { |
| 202 | + setMainButtonsEnabled(true); |
| 203 | + |
| 204 | + long embedWaitTime = 3000L; |
| 205 | + String s = java.lang.System.getProperty("com.openfin.demo.embedWaitTime"); |
| 206 | + if (s != null) { |
| 207 | + embedWaitTime = Long.parseLong(s); |
| 208 | + } |
| 209 | + System.out.println(String.format("Embed wait time %d", embedWaitTime)); |
| 210 | + timer.schedule(new TimerTask() { |
| 211 | + @Override |
| 212 | + public void run() { |
| 213 | + embedStartupApp(); |
| 214 | + } |
| 215 | + }, embedWaitTime); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public void onClose() { |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void onError(String reason) { |
| 224 | + |
| 225 | + } |
| 226 | + @Override |
| 227 | + public void onMessage(String message) { |
| 228 | + } |
| 229 | + @Override |
| 230 | + public void onOutgoingMessage(String message) { |
| 231 | + } |
| 232 | + }; |
| 233 | + |
| 234 | + RuntimeConfiguration configuration = new RuntimeConfiguration(); |
| 235 | +// configuration.setRuntimeVersion("6.49.8.14"); |
| 236 | + |
| 237 | + String runtimeVersion = java.lang.System.getProperty("com.openfin.demo.version"); |
| 238 | + if (runtimeVersion == null) { |
| 239 | + runtimeVersion = "stable"; |
| 240 | + } |
| 241 | + configuration.setRuntimeVersion(runtimeVersion); |
| 242 | + configuration.setDevToolsPort(9090); |
| 243 | + configuration.setAdditionalRuntimeArguments("--v=1 --noerrdialogs --disable-web-security --enable-aggressive-domstorage-flushing --load-extension=%LOCALAPPDATA%/OpenFin/apps/1578057152/assets/gktvbext-8266cea06bade1e88033/1.0.0"); |
| 244 | + |
| 245 | + JSONObject asset = new JSONObject(); |
| 246 | + asset.put("src", "https://test.web.tradervoicebox.com/openfin/gkt-vb-chrome-extension-8266cea06bade1e88033.zip"); |
| 247 | + asset.put("alias", "gktvbext-8266cea06bade1e88033"); |
| 248 | + asset.put("version", "1.0.0"); |
| 249 | + configuration.addAppAsset(asset); |
| 250 | + |
| 251 | + JSONObject startupApp = new JSONObject(); |
| 252 | + startupApp.put("name", "TVBWebTest"); |
| 253 | + startupApp.put("uuid", startupUuid); |
| 254 | + startupApp.put("url", "https://test.web.tradervoicebox.com"); |
| 255 | + startupApp.put("applicationIcon", "https://test.web.tradervoicebox.com/img/logo-large.jpg"); |
| 256 | + startupApp.put("autoShow", false); |
| 257 | + startupApp.put("defaultWidth", appWidth); |
| 258 | + startupApp.put("defaultHeight", appHeigth); |
| 259 | + |
| 260 | + JSONArray permissions = new JSONArray(); |
| 261 | + permissions.put("audioCapture"); |
| 262 | + permissions.put("videoCapture"); |
| 263 | + permissions.put("midi"); |
| 264 | + startupApp.put("permissions",permissions); |
| 265 | + |
| 266 | + startupApp.put("delay_connection", true); |
| 267 | + startupApp.put("frame", false); |
| 268 | + startupApp.put("saveWindowState", false); |
| 269 | + configuration.setStartupApp(startupApp); |
| 270 | + |
| 271 | +// ApplicationOptions options = new ApplicationOptions("TVBWebTest", startupUuid, "https://test.web.tradervoicebox.com"); |
| 272 | +// options.setApplicationIcon("https://test.web.tradervoicebox.com/img/logo-large.jpg"); |
| 273 | +// WindowOptions mainWindowOptions = new WindowOptions(); |
| 274 | +// mainWindowOptions.setAutoShow(true); |
| 275 | +// mainWindowOptions.setDefaultHeight(appHeigth); |
| 276 | +// mainWindowOptions.setDefaultWidth(appWidth); |
| 277 | +// mainWindowOptions.setSaveWindowState(false); // set to false so all windows start at same initial positions for each run |
| 278 | +// mainWindowOptions.setFrame(false); |
| 279 | +// mainWindowOptions.toJsonObject().put("permissions","[audioCapture,videoCapture,midi]"); |
| 280 | +// mainWindowOptions.toJsonObject().put("delay_connection", true); |
| 281 | +// options.setMainWindowOptions(mainWindowOptions); |
| 282 | + |
| 283 | + |
| 284 | + desktopConnection.connect(configuration, listener, 60); |
| 285 | + |
| 286 | + } catch (Exception e) { |
| 287 | + e.printStackTrace(); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + private void launchHtmlApp() { |
| 292 | + try { |
| 293 | + ApplicationOptions options = new ApplicationOptions("TVBWebTest", startupUuid, "https://test.web.tradervoicebox.com"); |
| 294 | + options.setApplicationIcon("https://test.web.tradervoicebox.com/img/logo-large.jpg"); |
| 295 | + WindowOptions mainWindowOptions = new WindowOptions(); |
| 296 | + mainWindowOptions.setAutoShow(false); |
| 297 | + mainWindowOptions.setDefaultHeight(appHeigth); |
| 298 | + mainWindowOptions.setDefaultWidth(appWidth); |
| 299 | + mainWindowOptions.setSaveWindowState(false); // set to false so all windows start at same initial positions for each run |
| 300 | + mainWindowOptions.setFrame(false); |
| 301 | + mainWindowOptions.toJsonObject().put("permissions","[audioCapture,videoCapture,midi]"); |
| 302 | + mainWindowOptions.toJsonObject().put("delay_connection", true); |
| 303 | + options.setMainWindowOptions(mainWindowOptions); |
| 304 | + |
| 305 | + DemoUtils.runApplication(options, this.desktopConnection, new AckListener() { |
| 306 | + @Override |
| 307 | + public void onSuccess(Ack ack) { |
| 308 | + startupHtml5app = (Application) ack.getSource(); |
| 309 | +// embedStartupApp(); |
| 310 | + } |
| 311 | + @Override |
| 312 | + public void onError(Ack ack) { |
| 313 | + logger.error(String.format("Error launching %s %s", options.getUUID(), ack.getReason())); |
| 314 | + } |
| 315 | + }); |
| 316 | + } catch (Exception e) { |
| 317 | + logger.error("Error launching app", e); |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + private boolean trayIconAdded = false; |
| 322 | + private Application startupHtml5app; |
| 323 | + |
| 324 | + private void embedStartupApp() { |
| 325 | + try { |
| 326 | + if (startupHtml5app == null) { |
| 327 | + startupHtml5app = Application.wrap(this.startupUuid, this.desktopConnection); |
| 328 | + } |
| 329 | + |
| 330 | + com.openfin.desktop.Window html5Wnd = com.openfin.desktop.Window.wrap(startupUuid, startupUuid, desktopConnection); |
| 331 | + long parentHWndId = Native.getComponentID(this.embedCanvas); |
| 332 | + System.out.println("Canvas HWND " + Long.toHexString(parentHWndId)); |
| 333 | + WinMessageHelper.embedInto(parentHWndId, html5Wnd, appWidth, appHeigth, 0, 0, new AckListener() { |
| 334 | + @Override |
| 335 | + public void onSuccess(Ack ack) { |
| 336 | + if (ack.isSuccessful()) { |
| 337 | + previousPrarentHwndId = ack.getJsonObject().getLong("hWndPreviousParent"); |
| 338 | + try { |
| 339 | + html5Wnd.show(); |
| 340 | + } catch (Exception e) { |
| 341 | + e.printStackTrace(); |
| 342 | + } |
| 343 | + } else { |
| 344 | + java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString()); |
| 345 | + } |
| 346 | + } |
| 347 | + @Override |
| 348 | + public void onError(Ack ack) { |
| 349 | + java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString()); |
| 350 | + } |
| 351 | + }); |
| 352 | + } catch (Exception e) { |
| 353 | + e.printStackTrace(); |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + private void releaseStartupApp() { |
| 358 | + com.openfin.desktop.Window html5Wnd = com.openfin.desktop.Window.wrap(startupUuid, startupUuid, desktopConnection); |
| 359 | + if (this.previousPrarentHwndId != null) { |
| 360 | + WinMessageHelper.embedInto(this.previousPrarentHwndId, html5Wnd, 395, 525, 0, 0, new AckListener() { |
| 361 | + @Override |
| 362 | + public void onSuccess(Ack ack) { |
| 363 | + java.lang.System.out.println("embedding result: " + ack.getJsonObject().toString()); |
| 364 | + } |
| 365 | + @Override |
| 366 | + public void onError(Ack ack) { |
| 367 | + java.lang.System.out.println("embedding failed: " + ack.getJsonObject().toString()); |
| 368 | + } |
| 369 | + }); |
| 370 | + this.previousPrarentHwndId = null; |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + /** |
| 375 | + * To start OpenFin Desktop and Connect, pass full path of OpenFin with* |
| 376 | + * -DOpenFinOption=--config=\"RemoteConfigUrl\" |
| 377 | + * |
| 378 | + * Set UUID of startup HTML5 app to dock to |
| 379 | + * -DStartupUUID="550e8400-e29b-41d4-a716-4466333333000" |
| 380 | + * |
| 381 | + * @param args |
| 382 | + */ |
| 383 | + public static void main(String[] args) { |
| 384 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 385 | + public void run() { |
| 386 | + createAndShowGUI(); |
| 387 | + } |
| 388 | + }); |
| 389 | + } |
| 390 | + |
| 391 | +} |
0 commit comments