|
| 1 | +/** |
| 2 | + * Demo for launching OpenFin app and send messages via Inter application bus |
| 3 | + * |
| 4 | + * javascript side is in release/busdemo.html, which needs to be hosted in localhost:8888 |
| 5 | + */ |
| 6 | + |
| 7 | +package com.openfin.desktop.demo; |
| 8 | + |
| 9 | +import com.openfin.desktop.*; |
| 10 | +import javafx.application.Application; |
| 11 | +import javafx.collections.ObservableList; |
| 12 | +import javafx.event.ActionEvent; |
| 13 | +import javafx.event.EventHandler; |
| 14 | +import javafx.scene.Group; |
| 15 | +import javafx.scene.Scene; |
| 16 | +import javafx.scene.control.Button; |
| 17 | +import javafx.scene.layout.AnchorPane; |
| 18 | +import javafx.stage.Stage; |
| 19 | +import org.json.JSONObject; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +public class LauncherBusDemo extends Application { |
| 24 | + private final static Logger logger = LoggerFactory.getLogger(LauncherBusDemo.class.getName()); |
| 25 | + private final static String WINDOW_TITLE = "Launcher and InterAppBus Demo"; |
| 26 | + |
| 27 | + private DesktopConnection desktopConnection; |
| 28 | + private InterApplicationBus interApplicationBus; |
| 29 | + private Button btnOFApp1, btnOFApp2; |
| 30 | + private Button btnOFSendApp1, btnOFSendApp2; // send messages to OpenFin app via Inter App Bus |
| 31 | + private final String app1Uuid = "OpenFin app1"; |
| 32 | + private final String app2Uuid = "OpenFin app2"; |
| 33 | + private final String appUrl = "http://localhost:8888/busdemo.html"; |
| 34 | + com.openfin.desktop.Application app1, app2; // OpenFin apps |
| 35 | + |
| 36 | + @Override |
| 37 | + public void start(Stage stage) { |
| 38 | + btnOFApp1 = new Button(); |
| 39 | + btnOFApp1.setDisable(true); |
| 40 | + btnOFApp1.setText("Launch OpenFin app1"); |
| 41 | + btnOFApp1.setLayoutX(10); |
| 42 | + btnOFApp1.setLayoutY(10); |
| 43 | + btnOFApp1.setOnAction(new EventHandler<ActionEvent>() { |
| 44 | + @Override |
| 45 | + public void handle(ActionEvent event) { |
| 46 | + launchOFApp1(app1Uuid, appUrl, 500, 100); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + btnOFApp2 = new Button(); |
| 51 | + btnOFApp2.setDisable(true); |
| 52 | + btnOFApp2.setText("Launch OpenFin App2"); |
| 53 | + btnOFApp2.setLayoutX(10); |
| 54 | + btnOFApp2.setLayoutY(50); |
| 55 | + btnOFApp2.setOnAction(new EventHandler<ActionEvent>() { |
| 56 | + @Override |
| 57 | + public void handle(ActionEvent event) { |
| 58 | + launchOFApp2(app2Uuid, appUrl, 500, 500); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + btnOFSendApp1 = new Button(); |
| 63 | + btnOFSendApp1.setDisable(true); |
| 64 | + btnOFSendApp1.setText("Send messages OpenFin app1"); |
| 65 | + btnOFSendApp1.setLayoutX(10); |
| 66 | + btnOFSendApp1.setLayoutY(90); |
| 67 | + btnOFSendApp1.setOnAction(new EventHandler<ActionEvent>() { |
| 68 | + @Override |
| 69 | + public void handle(ActionEvent event) { |
| 70 | + sendOFApp(app1); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + btnOFSendApp2 = new Button(); |
| 75 | + btnOFSendApp2.setDisable(true); |
| 76 | + btnOFSendApp2.setText("Send messages OpenFin app2"); |
| 77 | + btnOFSendApp2.setLayoutX(10); |
| 78 | + btnOFSendApp2.setLayoutY(130); |
| 79 | + btnOFSendApp2.setOnAction(new EventHandler<ActionEvent>() { |
| 80 | + @Override |
| 81 | + public void handle(ActionEvent event) { |
| 82 | + sendOFApp(app2); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + AnchorPane anchorPane = new AnchorPane(); |
| 87 | + anchorPane.getChildren().add(btnOFApp1); |
| 88 | + anchorPane.getChildren().add(btnOFApp2); |
| 89 | + anchorPane.getChildren().add(btnOFSendApp1); |
| 90 | + anchorPane.getChildren().add(btnOFSendApp2); |
| 91 | + |
| 92 | + //Creating a Group object |
| 93 | + Group root = new Group(); |
| 94 | + |
| 95 | + //Retrieving the observable list object |
| 96 | + ObservableList list = root.getChildren(); |
| 97 | + |
| 98 | + //Creating a scene object |
| 99 | + Scene scene = new Scene(anchorPane, 800, 800); |
| 100 | + |
| 101 | + //Setting title to the Stage |
| 102 | + stage.setTitle(WINDOW_TITLE); |
| 103 | + |
| 104 | + //Adding scene to the stage |
| 105 | + stage.setScene(scene); |
| 106 | + |
| 107 | + //Displaying the contents of the stage |
| 108 | + stage.show(); |
| 109 | + |
| 110 | + launchRuntime(); |
| 111 | + } |
| 112 | + |
| 113 | + private void launchRuntime() { |
| 114 | + if (desktopConnection == null) { |
| 115 | + RuntimeConfiguration cfg = new RuntimeConfiguration(); |
| 116 | + cfg.setRuntimeVersion("stable"); |
| 117 | + cfg.setAdditionalRuntimeArguments("--v=1"); // enable verbose logging by Runtime |
| 118 | + try { |
| 119 | + desktopConnection = new DesktopConnection("Java app"); |
| 120 | + desktopConnection.connect(cfg, new DesktopStateListener() { |
| 121 | + @Override |
| 122 | + public void onReady() { |
| 123 | + logger.info("Connected to OpenFin Runtime"); |
| 124 | + interApplicationBus = new InterApplicationBus(desktopConnection); |
| 125 | + btnOFApp1.setDisable(false); |
| 126 | + btnOFApp2.setDisable(false); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onClose(String error) { |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onError(String reason) { |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void onMessage(String message) { |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onOutgoingMessage(String message) { |
| 143 | + } |
| 144 | + }, 60); |
| 145 | + } catch (Exception e) { |
| 146 | + e.printStackTrace(); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private void launchOFApp1(String uuid, String url, int left, int top) { |
| 152 | + ApplicationOptions options = createAppOptions(uuid, url, left, top); |
| 153 | + app1 = new com.openfin.desktop.Application(options, desktopConnection, new AckListener() { |
| 154 | + @Override |
| 155 | + public void onSuccess(Ack ack) { |
| 156 | + app1.run(new AckListener() { |
| 157 | + @Override |
| 158 | + public void onSuccess(Ack ack) { |
| 159 | + btnOFSendApp1.setDisable(false); |
| 160 | + } |
| 161 | + @Override |
| 162 | + public void onError(Ack ack) { |
| 163 | + logger.error(String.format("Error running %s", uuid)); |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | + @Override |
| 168 | + public void onError(Ack ack) { |
| 169 | + logger.error(String.format("Error creating %s", uuid)); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + private void launchOFApp2(String uuid, String url, int left, int width) { |
| 175 | + ApplicationOptions options = createAppOptions(uuid, url, left, width); |
| 176 | + app2 = new com.openfin.desktop.Application(options, desktopConnection, new AckListener() { |
| 177 | + @Override |
| 178 | + public void onSuccess(Ack ack) { |
| 179 | + app2.run(new AckListener() { |
| 180 | + @Override |
| 181 | + public void onSuccess(Ack ack) { |
| 182 | + btnOFSendApp2.setDisable(false); |
| 183 | + } |
| 184 | + @Override |
| 185 | + public void onError(Ack ack) { |
| 186 | + logger.error(String.format("Error running %s", uuid)); |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
| 190 | + @Override |
| 191 | + public void onError(Ack ack) { |
| 192 | + logger.error(String.format("Error creating %s", uuid)); |
| 193 | + } |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + private ApplicationOptions createAppOptions(String uuid, String url, int left, int top) { |
| 198 | + ApplicationOptions options = new ApplicationOptions(uuid, uuid, url); |
| 199 | + WindowOptions windowOptions = new WindowOptions(); |
| 200 | + windowOptions.setDefaultHeight(400); |
| 201 | + windowOptions.setDefaultWidth(400); |
| 202 | + windowOptions.setDefaultLeft(left); |
| 203 | + windowOptions.setDefaultTop(top); |
| 204 | + windowOptions.setSaveWindowState(false); // so last position is not saved |
| 205 | + windowOptions.setContextMenu(true); // enable Javascript Devtools |
| 206 | + windowOptions.setAutoShow(false); // hide the window initially, show it when a message is sent to it |
| 207 | + options.setMainWindowOptions(windowOptions); |
| 208 | + return options; |
| 209 | + } |
| 210 | + |
| 211 | + private void sendOFApp(com.openfin.desktop.Application app) { |
| 212 | + JSONObject msg = new JSONObject(); |
| 213 | + msg.put("ticker", "AAPL"); |
| 214 | + msg.put("price", Math.random() * 100); |
| 215 | + try { |
| 216 | + interApplicationBus.send(app.getOptions().getUUID(), "messageFromJavaTopic", msg); |
| 217 | + app.getWindow().show(); |
| 218 | + } catch (DesktopException e) { |
| 219 | + e.printStackTrace(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + public static void main(String args[]) { |
| 224 | + launch(args); |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +} |
0 commit comments