|
1 | 1 | package com.openfin.desktop.demo; |
2 | 2 |
|
3 | 3 | import com.openfin.desktop.*; |
| 4 | +import org.json.JSONObject; |
4 | 5 | import org.slf4j.Logger; |
5 | 6 | import org.slf4j.LoggerFactory; |
6 | 7 |
|
| 8 | +import java.util.Objects; |
7 | 9 | import java.util.concurrent.CountDownLatch; |
| 10 | +import java.util.concurrent.ExecutionException; |
8 | 11 | import java.util.concurrent.TimeUnit; |
9 | 12 | import java.util.concurrent.atomic.AtomicReference; |
10 | 13 |
|
|
14 | 17 | public class DemoUtils { |
15 | 18 | private static Logger logger = LoggerFactory.getLogger(DemoUtils.class.getName()); |
16 | 19 |
|
17 | | - public static void addEventListener(Window window, String evenType, EventListener eventListener) throws Exception { |
| 20 | + public static void addEventListener(Application application, String evenType, EventListener eventListener, AckListener ackListener) throws Exception { |
18 | 21 | logger.debug("addEventListener " + evenType); |
19 | | - CountDownLatch latch = new CountDownLatch(1); |
20 | | - window.addEventListener(evenType, eventListener, null); |
21 | | -// window.addEventListener(evenType, eventListener, new AckListener() { |
22 | | -// @Override |
23 | | -// public void onSuccess(Ack ack) { |
24 | | -// latch.countDown(); |
25 | | -// logger.debug("addEventListener ack " + ack.isSuccessful()); |
26 | | -// } |
27 | | -// @Override |
28 | | -// public void onError(Ack ack) { |
29 | | -// logger.error(String.format("Error adding event listener %s %s", evenType, ack.getReason())); |
30 | | -// } |
31 | | -// }); |
32 | | -// latch.await(5, TimeUnit.SECONDS); |
| 22 | + application.addEventListener(evenType, eventListener, new AckListener() { |
| 23 | + @Override |
| 24 | + public void onSuccess(Ack ack) { |
| 25 | + logger.debug("addEventListener ack " + ack.isSuccessful()); |
| 26 | + ackSuccess(ackListener, application); |
| 27 | + } |
| 28 | + @Override |
| 29 | + public void onError(Ack ack) { |
| 30 | + logger.error(String.format("Error adding event listener %s %s", evenType, ack.getReason())); |
| 31 | + ackError(ackListener, ack.getReason()); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public static void runApplication(ApplicationOptions options, DesktopConnection desktopConnection, AckListener ackListener) throws Exception { |
| 37 | + AtomicReference<Application> appRef = new AtomicReference<>(); |
| 38 | + Application application = createApplication(options, desktopConnection, new AckListener() { |
| 39 | + @Override |
| 40 | + public void onSuccess(Ack ack) { |
| 41 | + try { |
| 42 | + runApplication(appRef.get(), true, ackListener); |
| 43 | + } catch (Exception e) { |
| 44 | + logger.error("Error running application", e); |
| 45 | + ackError(ackListener, e.getMessage()); |
| 46 | + } |
| 47 | + } |
| 48 | + @Override |
| 49 | + public void onError(Ack ack) { |
| 50 | + logger.error(String.format("onError %s", ack.getReason())); |
| 51 | + ackError(ackListener, ack.getReason()); |
| 52 | + } |
| 53 | + }); |
| 54 | + appRef.set(application); |
| 55 | + } |
| 56 | + |
| 57 | + public static void runApplication(Application application, boolean checkAppConnected, AckListener ackListener) throws Exception { |
| 58 | + EventListener listener = new EventListener() { |
| 59 | + @Override |
| 60 | + public void eventReceived(ActionEvent actionEvent) { |
| 61 | + if (actionEvent.getType().equals("started")) { |
| 62 | + if (!checkAppConnected) { |
| 63 | + ackSuccess(ackListener, application); |
| 64 | + } |
| 65 | + } |
| 66 | + else if (actionEvent.getType().equals("app-connected")) { |
| 67 | + if (checkAppConnected) { |
| 68 | + ackSuccess(ackListener, application); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + AckListener eventAddAck = new AckListener() { |
| 75 | + @Override |
| 76 | + public void onSuccess(Ack ack) { |
| 77 | + application.run(new AckListener() { |
| 78 | + @Override |
| 79 | + public void onSuccess(Ack ack) { |
| 80 | + logger.debug(String.format("Successful run %s", application.getOptions().getUUID())); |
| 81 | + } |
| 82 | + @Override |
| 83 | + public void onError(Ack ack) { |
| 84 | + ackError(ackListener, ack.getReason()); |
| 85 | + logger.error("Error running application", ack.getReason()); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + @Override |
| 90 | + public void onError(Ack ack) { |
| 91 | + logger.error(String.format("Error adding event listener %s", ack.getReason())); |
| 92 | + } |
| 93 | + }; |
| 94 | + if (checkAppConnected) { |
| 95 | + addEventListener(application.getWindow(), "app-connected", listener, eventAddAck); |
| 96 | + } else { |
| 97 | + addEventListener(application, "started", listener, eventAddAck); |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + public static Application createApplication(ApplicationOptions options, DesktopConnection desktopConnection, AckListener ackListener) throws Exception { |
| 103 | + Application application = new Application(options, desktopConnection, new AckListener() { |
| 104 | + @Override |
| 105 | + public void onSuccess(Ack ack) { |
| 106 | + ackSuccess(ackListener, options); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onError(Ack ack) { |
| 111 | + logger.error("Error creating application", ack.getReason()); |
| 112 | + ackError(ackListener, ack.getReason()); |
| 113 | + } |
| 114 | + }); |
| 115 | + return application; |
| 116 | + } |
| 117 | + |
| 118 | + public static void addEventListener(Window window, String evenType, EventListener eventListener, AckListener ackListener) throws Exception { |
| 119 | + logger.debug("addEventListener " + evenType); |
| 120 | + window.addEventListener(evenType, eventListener, new AckListener() { |
| 121 | + @Override |
| 122 | + public void onSuccess(Ack ack) { |
| 123 | + logger.debug("addEventListener ack " + ack.isSuccessful()); |
| 124 | + ackSuccess(ackListener, window); |
| 125 | + } |
| 126 | + @Override |
| 127 | + public void onError(Ack ack) { |
| 128 | + logger.error(String.format("Error adding event listener %s %s", evenType, ack.getReason())); |
| 129 | + ackError(ackListener, ack.getReason()); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + public static void ackSuccess(AckListener ackListener, Object source) { |
| 135 | + if (ackListener != null) { |
| 136 | + ackListener.onSuccess(new Ack(new JSONObject(), source)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static void ackError(AckListener ackListener, String reason) { |
| 141 | + if (ackListener != null) { |
| 142 | + JSONObject obj = new JSONObject(); |
| 143 | + obj.put("reason", reason); |
| 144 | + ackListener.onError(new Ack(obj, null)); |
| 145 | + } |
33 | 146 | } |
34 | 147 |
|
35 | 148 | } |
0 commit comments