|
| 1 | +package com.openfin.desktop.demo; |
| 2 | + |
| 3 | +import com.openfin.desktop.*; |
| 4 | +import com.openfin.desktop.win32.ExternalWindowObserver; |
| 5 | +import info.clearthought.layout.TableLayout; |
| 6 | +import org.json.JSONObject; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.awt.event.ActionListener; |
| 13 | +import java.awt.event.WindowEvent; |
| 14 | +import java.awt.event.WindowListener; |
| 15 | + |
| 16 | +/** |
| 17 | + * This app works with OpenFinDockingDemo as another dockable Java window |
| 18 | + * |
| 19 | + * Created by wche on 3/9/2016. |
| 20 | + */ |
| 21 | +public class DockingDemo2 extends JPanel implements ActionListener, WindowListener { |
| 22 | + private final static Logger logger = LoggerFactory.getLogger(DockingDemo2.class.getName()); |
| 23 | + |
| 24 | + protected String javaParentAppUuid = "Java Parent App"; // Its value must match OpenFinDockingDemo. Please see OpenFinDockingDemo for comments |
| 25 | + |
| 26 | + protected String appUuid = "AnotherJavaDocking"; // UUID for desktopConnection |
| 27 | + protected String javaWindowName = "Another Java Dock Window"; |
| 28 | + |
| 29 | + protected DesktopConnection desktopConnection; |
| 30 | + protected ExternalWindowObserver externalWindowObserver; |
| 31 | + |
| 32 | + private static JFrame jFrame; |
| 33 | + protected JButton undockButton; |
| 34 | + protected JTextField dockStatus; // show Ready to dock message |
| 35 | + |
| 36 | + public DockingDemo2() { |
| 37 | + try { |
| 38 | + this.desktopConnection = new DesktopConnection(appUuid); |
| 39 | + startOpenFinRuntime(); |
| 40 | + } catch (DesktopException desktopError) { |
| 41 | + desktopError.printStackTrace(); |
| 42 | + } |
| 43 | + setLayout(new BorderLayout()); |
| 44 | + add(layoutCenterPanel(), BorderLayout.CENTER); |
| 45 | + add(layoutLeftPanel(), BorderLayout.WEST); |
| 46 | + setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); |
| 47 | + } |
| 48 | + |
| 49 | + private JPanel layoutLeftPanel() { |
| 50 | + JPanel panel = new JPanel(); |
| 51 | + double size[][] = {{410}, {120, 30, TableLayout.FILL}}; |
| 52 | + panel.setLayout(new TableLayout(size)); |
| 53 | + panel.add(layoutActionButtonPanel(), "0,0,0,0"); |
| 54 | + panel.add(layoutDockStatus(), "0,1,0,1"); |
| 55 | + return panel; |
| 56 | + } |
| 57 | + |
| 58 | + private JTextField layoutDockStatus() { |
| 59 | + this.dockStatus = new JTextField(); |
| 60 | + return this.dockStatus; |
| 61 | + } |
| 62 | + |
| 63 | + private JPanel layoutActionButtonPanel() { |
| 64 | + JPanel buttonPanel = new JPanel(); |
| 65 | + JPanel topPanel = new JPanel(); |
| 66 | + double size[][] = {{10, 190, 20, 190, 10}, {25, 10, 25, 10}}; |
| 67 | + topPanel.setLayout(new TableLayout(size)); |
| 68 | + topPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createBevelBorder(2), "Desktop")); |
| 69 | + undockButton = new JButton("Undock"); |
| 70 | + undockButton.setActionCommand("undock-window"); |
| 71 | + undockButton.setEnabled(false); |
| 72 | + topPanel.add(undockButton, "1,0,1,0"); |
| 73 | + undockButton.addActionListener(this); |
| 74 | + buttonPanel.add(topPanel, "0,0"); |
| 75 | + return buttonPanel; |
| 76 | + } |
| 77 | + |
| 78 | + private JPanel layoutCenterPanel() { |
| 79 | + JPanel panel = new JPanel(); |
| 80 | + double size[][] = {{TableLayout.FILL}, {150, 150, TableLayout.FILL}}; |
| 81 | + panel.setLayout(new TableLayout(size)); |
| 82 | + |
| 83 | + return panel; |
| 84 | + } |
| 85 | + |
| 86 | + private void undockFromOtherWindows() { |
| 87 | + try { |
| 88 | + // send message to Docking Manager to undock me |
| 89 | + JSONObject msg = new JSONObject(); |
| 90 | + msg.put("applicationUuid", javaParentAppUuid); |
| 91 | + msg.put("windowName", javaWindowName); |
| 92 | + desktopConnection.getInterApplicationBus().publish("undock-window", msg); |
| 93 | + |
| 94 | + } catch (Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } |
| 97 | + undockButton.setEnabled(false); |
| 98 | + } |
| 99 | + |
| 100 | + private void startOpenFinRuntime() { |
| 101 | + try { |
| 102 | + DesktopStateListener listener = new DesktopStateListener() { |
| 103 | + @Override |
| 104 | + public void onReady() { |
| 105 | + onRuntimeReady(); |
| 106 | + } |
| 107 | + @Override |
| 108 | + public void onError(String reason) { |
| 109 | + logger.error("Connection failed: " + reason); |
| 110 | + java.lang.System.exit(0); |
| 111 | + } |
| 112 | + @Override |
| 113 | + public void onMessage(String message) { |
| 114 | + } |
| 115 | + @Override |
| 116 | + public void onOutgoingMessage(String message) { |
| 117 | + } |
| 118 | + }; |
| 119 | + desktopConnection.setAdditionalRuntimeArguments(" --v=1"); // enable additional logging from Runtime |
| 120 | + desktopConnection.connectToVersion("5.44.10.26", listener, 60); // 5.44.10.26 has fix for cross-app docking, which is required for windowsInShameGroupMoveTogether |
| 121 | + |
| 122 | + } catch (Exception e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + } |
| 126 | + private void onRuntimeReady() { |
| 127 | + try { |
| 128 | + this.desktopConnection.getInterApplicationBus().subscribe("*", "window-docked", (uuid, topic, data) -> { |
| 129 | + JSONObject event = (JSONObject) data; |
| 130 | + String appUuid = event.getString("applicationUuid"); |
| 131 | + String windowName = event.getString("windowName"); |
| 132 | + if (javaParentAppUuid.equals(appUuid) && javaWindowName.equals(windowName)) { |
| 133 | + updateUndockButton(true); |
| 134 | + } |
| 135 | + }); |
| 136 | + this.desktopConnection.getInterApplicationBus().subscribe("*", "window-undocked", (uuid, topic, data) -> { |
| 137 | + JSONObject event = (JSONObject) data; |
| 138 | + String appUuid = event.getString("applicationUuid"); |
| 139 | + String windowName = event.getString("windowName"); |
| 140 | + if (javaParentAppUuid.equals(appUuid) && javaWindowName.equals(windowName)) { |
| 141 | + updateUndockButton(false); |
| 142 | + } |
| 143 | + }); |
| 144 | + registerJavaWindow(); |
| 145 | + } catch (Exception e) { |
| 146 | + logger.error("Error creating DockingManager", e); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void registerJavaWindow() { |
| 151 | + try { |
| 152 | + externalWindowObserver = new ExternalWindowObserver(desktopConnection.getPort(), javaParentAppUuid, javaWindowName, jFrame, |
| 153 | + new AckListener() { |
| 154 | + @Override |
| 155 | + public void onSuccess(Ack ack) { |
| 156 | + try { |
| 157 | + if (ack.isSuccessful()) { |
| 158 | + JSONObject request = new JSONObject(); |
| 159 | + request.put("applicationUuid", javaParentAppUuid); |
| 160 | + request.put("windowName", javaWindowName); |
| 161 | + desktopConnection.getInterApplicationBus().publish("register-docking-window", request); |
| 162 | + } |
| 163 | + } catch (Exception ex) { |
| 164 | + logger.error("Error registering window", ex); |
| 165 | + } |
| 166 | + } |
| 167 | + @Override |
| 168 | + public void onError(Ack ack) { |
| 169 | + logger.error("Error registering java window ", ack.getReason()); |
| 170 | + } |
| 171 | + }); |
| 172 | + } catch (Exception e) { |
| 173 | + logger.error("Error registering external window", e); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public void actionPerformed(java.awt.event.ActionEvent e) { |
| 178 | + if ("undock-window".equals(e.getActionCommand())) { |
| 179 | + undockFromOtherWindows(); |
| 180 | + } |
| 181 | + } |
| 182 | + private void updateUndockButton(boolean enabled) { |
| 183 | + this.undockButton.setEnabled(enabled); |
| 184 | + if (enabled) { |
| 185 | + this.dockStatus.setText("Docked. Click Unlock button to undock"); |
| 186 | + } else { |
| 187 | + this.dockStatus.setText(""); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private static void createAndShowGUI() { |
| 192 | + //Create and set up the window. |
| 193 | + jFrame = new JFrame("Another Java Docking Demo"); |
| 194 | + jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 195 | + |
| 196 | + //Create and set up the content pane. |
| 197 | + DockingDemo2 newContentPane = new DockingDemo2(); |
| 198 | + newContentPane.setOpaque(true); //content panes must be opaque |
| 199 | + jFrame.setContentPane(newContentPane); |
| 200 | + jFrame.addWindowListener(newContentPane); |
| 201 | + |
| 202 | + //Display the window. |
| 203 | + jFrame.pack(); |
| 204 | + jFrame.setSize(470, 300); |
| 205 | + jFrame.setLocation(600, 400); |
| 206 | +// jFrame.setLocationRelativeTo(null); |
| 207 | + jFrame.setResizable(false); |
| 208 | + jFrame.setVisible(true); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * |
| 213 | + * @param args |
| 214 | + */ |
| 215 | + public static void main(String[] args) { |
| 216 | + javax.swing.SwingUtilities.invokeLater(new Runnable() { |
| 217 | + public void run() { |
| 218 | + createAndShowGUI(); |
| 219 | + } |
| 220 | + }); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public void windowOpened(WindowEvent e) { |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public void windowClosing(WindowEvent e) { |
| 230 | + if (externalWindowObserver != null) { |
| 231 | + logger.debug("Closing"); |
| 232 | + try { |
| 233 | + JSONObject request = new JSONObject(); |
| 234 | + request.put("applicationUuid", javaParentAppUuid); |
| 235 | + request.put("windowName", javaWindowName); |
| 236 | + desktopConnection.getInterApplicationBus().publish("unregister-docking-window", request); |
| 237 | + Thread.sleep(2000); // give time to flush messages to Runtime |
| 238 | + externalWindowObserver.dispose(); |
| 239 | + desktopConnection.disconnect(); |
| 240 | + Thread.sleep(1000); // give time to flush messages to Runtime |
| 241 | + } catch (Exception ex) { |
| 242 | + logger.error("Error existing", ex); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public void windowClosed(WindowEvent e) { |
| 249 | + |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public void windowIconified(WindowEvent e) { |
| 254 | + |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public void windowDeiconified(WindowEvent e) { |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public void windowActivated(WindowEvent e) { |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + @Override |
| 268 | + public void windowDeactivated(WindowEvent e) { |
| 269 | + |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | + |
0 commit comments