|
| 1 | +package com.openfin.desktop.demo; |
| 2 | + |
| 3 | +/** |
| 4 | + * Example of Java window that can be managed by OpenFin layout service for snap&dock |
| 5 | + */ |
| 6 | + |
| 7 | +import com.openfin.desktop.*; |
| 8 | +import com.openfin.desktop.Window; |
| 9 | +import com.openfin.desktop.channel.ChannelAction; |
| 10 | +import com.openfin.desktop.channel.ChannelClient; |
| 11 | +import com.openfin.desktop.win32.ExternalWindowObserver; |
| 12 | +import org.json.JSONObject; |
| 13 | + |
| 14 | +import javax.swing.*; |
| 15 | +import java.awt.*; |
| 16 | +import java.awt.event.ActionEvent; |
| 17 | +import java.awt.event.ActionListener; |
| 18 | +import java.awt.event.WindowAdapter; |
| 19 | +import java.awt.event.WindowEvent; |
| 20 | +import java.lang.System; |
| 21 | + |
| 22 | +public class LayoutFrame extends JFrame { |
| 23 | + private ExternalWindowObserver externalWindowObserver; |
| 24 | + private JButton btnUndock; |
| 25 | + private String windowName; |
| 26 | + |
| 27 | + public LayoutFrame(DesktopConnection desktopConnection, String appUuid, String windowName) throws DesktopException { |
| 28 | + super(); |
| 29 | + System.out.println(windowName + " being created "); |
| 30 | + this.windowName = windowName; |
| 31 | + this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 32 | + this.setPreferredSize(new Dimension(640, 480)); |
| 33 | + JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER)); |
| 34 | + this.btnUndock = new JButton("undock"); |
| 35 | + this.btnUndock.setEnabled(false); |
| 36 | + pnl.add(btnUndock); |
| 37 | + this.getContentPane().add(pnl); |
| 38 | + this.pack(); |
| 39 | + this.setLocationRelativeTo(null); |
| 40 | + this.setVisible(true); |
| 41 | + |
| 42 | + this.externalWindowObserver = new ExternalWindowObserver(desktopConnection.getPort(), appUuid, windowName, |
| 43 | + this, new AckListener() { |
| 44 | + @Override |
| 45 | + public void onSuccess(Ack ack) { |
| 46 | + ExternalWindowObserver observer = (ExternalWindowObserver) ack.getSource(); |
| 47 | + observer.getDesktopConnection().getChannel().connect("of-layouts-service-v1", |
| 48 | + new AsyncCallback<ChannelClient>() { |
| 49 | + @Override |
| 50 | + public void onSuccess(ChannelClient client) { |
| 51 | + btnUndock.addActionListener(new ActionListener() { |
| 52 | + @Override |
| 53 | + public void actionPerformed(ActionEvent e) { |
| 54 | + JSONObject payload = new JSONObject(); |
| 55 | + payload.put("uuid", appUuid); |
| 56 | + payload.put("name", windowName); |
| 57 | + client.dispatch("UNDOCK-WINDOW", payload, null); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + client.register("event", new ChannelAction() { |
| 62 | + @Override |
| 63 | + public JSONObject invoke(String action, JSONObject payload) { |
| 64 | + System.out.printf("channel event " + action); |
| 65 | + return null; |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + @Override |
| 72 | + public void onError(Ack ack) { |
| 73 | + System.out.println(windowName + ": unable to register external window, " + ack.getReason()); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + Window w = Window.wrap(appUuid, windowName, desktopConnection); |
| 78 | + w.addEventListener("group-changed", new EventListener() { |
| 79 | + @Override |
| 80 | + public void eventReceived(com.openfin.desktop.ActionEvent actionEvent) { |
| 81 | + JSONObject eventObj = actionEvent.getEventObject(); |
| 82 | + w.getGroup(new AsyncCallback<java.util.List<Window>>() { |
| 83 | + @Override |
| 84 | + public void onSuccess(java.util.List<Window> result) { |
| 85 | + if (result.size() > 0) { |
| 86 | + LayoutFrame.this.btnUndock.setEnabled(true); |
| 87 | + } else { |
| 88 | + LayoutFrame.this.btnUndock.setEnabled(false); |
| 89 | + } |
| 90 | + } |
| 91 | + }, null); |
| 92 | + } |
| 93 | + }, null); |
| 94 | + |
| 95 | + this.addWindowListener(new WindowAdapter() { |
| 96 | + public void windowClosing(WindowEvent e) { |
| 97 | + super.windowClosing(e); |
| 98 | + try { |
| 99 | + LayoutFrame.this.cleanup(); |
| 100 | + LayoutFrame.this.dispose(); |
| 101 | + } catch (Exception e1) { |
| 102 | + e1.printStackTrace(); |
| 103 | + } |
| 104 | + } |
| 105 | + public void windowClosed(WindowEvent e) { |
| 106 | + super.windowClosed(e); |
| 107 | + System.out.println(windowName + " closed "); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + public String getWindowName() { |
| 113 | + return windowName; |
| 114 | + } |
| 115 | + |
| 116 | + public void cleanup() { |
| 117 | + try { |
| 118 | + System.out.println(windowName + " cleaning up "); |
| 119 | + this.externalWindowObserver.dispose(); |
| 120 | + } catch (Exception e) { |
| 121 | + e.printStackTrace(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments