|
7 | 7 | import com.chartiq.finsemble.Finsemble; |
8 | 8 | import com.chartiq.finsemble.interfaces.ConnectionEventGenerator; |
9 | 9 | import com.chartiq.finsemble.interfaces.ConnectionListener; |
| 10 | +import org.json.JSONArray; |
10 | 11 | import org.json.JSONObject; |
11 | 12 |
|
12 | 13 | import javax.swing.*; |
|
19 | 20 | import java.io.*; |
20 | 21 | import java.util.ArrayList; |
21 | 22 | import java.util.Arrays; |
| 23 | +import java.util.HashMap; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.logging.*; |
24 | 26 | import java.util.stream.Collectors; |
@@ -46,6 +48,8 @@ public class JavaSwingExample extends JFrame implements WindowListener { |
46 | 48 | private JButton group5Button; |
47 | 49 | private JButton group6Button; |
48 | 50 |
|
| 51 | + private HashMap<String, JButton> linkerButtons = new HashMap<>(); |
| 52 | + |
49 | 53 | private Finsemble fsbl; |
50 | 54 |
|
51 | 55 | /** |
@@ -89,82 +93,88 @@ private void createForm() { |
89 | 93 | contentPane = getContentPane(); |
90 | 94 | contentPane.setLayout(new GridBagLayout()); |
91 | 95 | final GridBagConstraints constraints = new GridBagConstraints(); |
92 | | - constraints.insets= new Insets(10,10,0,10); |
| 96 | + constraints.insets = new Insets(10, 10, 0, 10); |
93 | 97 |
|
94 | 98 | // region Linker buttons |
95 | 99 | group1Button = new JButton(); |
96 | | - group1Button.setBackground(new Color(135,129,189)); |
97 | | - group1Button.setForeground(new Color(187,187,187)); |
| 100 | + group1Button.setBackground(new Color(135, 129, 189)); |
| 101 | + group1Button.setForeground(new Color(187, 187, 187)); |
98 | 102 | group1Button.setName("group1"); |
99 | | - constraints.insets= new Insets(10,10,0,0); |
| 103 | + constraints.insets = new Insets(10, 10, 0, 0); |
100 | 104 | constraints.ipady = 40; |
101 | 105 | constraints.weightx = 0.5; |
102 | 106 | constraints.fill = GridBagConstraints.HORIZONTAL; |
103 | 107 | constraints.gridx = 0; |
104 | 108 | constraints.gridy = 0; |
105 | 109 | contentPane.add(group1Button, constraints); |
| 110 | + linkerButtons.put("group1", group1Button); |
106 | 111 |
|
107 | 112 | group2Button = new JButton(); |
108 | | - group2Button.setBackground(new Color(255,224,53)); |
109 | | - group2Button.setForeground(new Color(187,187,187)); |
| 113 | + group2Button.setBackground(new Color(255, 224, 53)); |
| 114 | + group2Button.setForeground(new Color(187, 187, 187)); |
110 | 115 | group2Button.setName("group2"); |
111 | | - constraints.insets= new Insets(10,0,0,0); |
| 116 | + constraints.insets = new Insets(10, 0, 0, 0); |
112 | 117 | constraints.weightx = 0.5; |
113 | 118 | constraints.fill = GridBagConstraints.HORIZONTAL; |
114 | 119 | constraints.gridx = 1; |
115 | 120 | constraints.gridy = 0; |
116 | 121 | contentPane.add(group2Button, constraints); |
| 122 | + linkerButtons.put("group2", group2Button); |
117 | 123 |
|
118 | 124 | group3Button = new JButton(); |
119 | | - group3Button.setBackground(new Color(137,216,3)); |
120 | | - group3Button.setForeground(new Color(187,187,187)); |
| 125 | + group3Button.setBackground(new Color(137, 216, 3)); |
| 126 | + group3Button.setForeground(new Color(187, 187, 187)); |
121 | 127 | group3Button.setName("group3"); |
122 | | - constraints.insets= new Insets(10,0,0,0); |
| 128 | + constraints.insets = new Insets(10, 0, 0, 0); |
123 | 129 | constraints.weightx = 0.5; |
124 | 130 | constraints.fill = GridBagConstraints.HORIZONTAL; |
125 | 131 | constraints.gridx = 2; |
126 | 132 | constraints.gridy = 0; |
127 | 133 | contentPane.add(group3Button, constraints); |
| 134 | + linkerButtons.put("group3", group3Button); |
128 | 135 |
|
129 | 136 | group4Button = new JButton(); |
130 | | - group4Button.setBackground(new Color(254,98,98)); |
131 | | - group4Button.setForeground(new Color(187,187,187)); |
| 137 | + group4Button.setBackground(new Color(254, 98, 98)); |
| 138 | + group4Button.setForeground(new Color(187, 187, 187)); |
132 | 139 | group4Button.setName("group4"); |
133 | | - constraints.insets= new Insets(10,0,0,0); |
| 140 | + constraints.insets = new Insets(10, 0, 0, 0); |
134 | 141 | constraints.weightx = 0.5; |
135 | 142 | constraints.fill = GridBagConstraints.HORIZONTAL; |
136 | 143 | constraints.gridx = 3; |
137 | 144 | constraints.gridy = 0; |
138 | 145 | contentPane.add(group4Button, constraints); |
| 146 | + linkerButtons.put("group4", group4Button); |
139 | 147 |
|
140 | 148 | group5Button = new JButton(); |
141 | | - group5Button.setBackground(new Color(45,172,255)); |
142 | | - group5Button.setForeground(new Color(187,187,187)); |
| 149 | + group5Button.setBackground(new Color(45, 172, 255)); |
| 150 | + group5Button.setForeground(new Color(187, 187, 187)); |
143 | 151 | group5Button.setName("group5"); |
144 | | - constraints.insets= new Insets(10,0,0,0); |
| 152 | + constraints.insets = new Insets(10, 0, 0, 0); |
145 | 153 | constraints.weightx = 0.5; |
146 | 154 | constraints.fill = GridBagConstraints.HORIZONTAL; |
147 | 155 | constraints.gridx = 4; |
148 | 156 | constraints.gridy = 0; |
149 | 157 | contentPane.add(group5Button, constraints); |
| 158 | + linkerButtons.put("group5", group5Button); |
150 | 159 |
|
151 | 160 | group6Button = new JButton(); |
152 | | - group6Button.setBackground(new Color(255,162,0)); |
153 | | - group6Button.setForeground(new Color(187,187,187)); |
| 161 | + group6Button.setBackground(new Color(255, 162, 0)); |
| 162 | + group6Button.setForeground(new Color(187, 187, 187)); |
154 | 163 | group6Button.setName("group6"); |
155 | | - constraints.insets= new Insets(10,0,0,10); |
| 164 | + constraints.insets = new Insets(10, 0, 0, 10); |
156 | 165 | constraints.weightx = 0.5; |
157 | 166 | constraints.fill = GridBagConstraints.HORIZONTAL; |
158 | 167 | constraints.gridx = 5; |
159 | 168 | constraints.gridy = 0; |
160 | 169 | contentPane.add(group6Button, constraints); |
| 170 | + linkerButtons.put("group6", group6Button); |
161 | 171 | // endregion |
162 | 172 |
|
163 | 173 | // region Symbol |
164 | 174 | symbolTextField = new JTextField("MSFT"); |
165 | 175 | symbolTextField.setEditable(true); |
166 | 176 | symbolTextField.setEnabled(false); |
167 | | - constraints.insets= new Insets(10,10,0,10); |
| 177 | + constraints.insets = new Insets(10, 10, 0, 10); |
168 | 178 | constraints.fill = GridBagConstraints.HORIZONTAL; |
169 | 179 | constraints.ipady = 10; |
170 | 180 | constraints.gridx = 0; |
@@ -219,7 +229,7 @@ private void createForm() { |
219 | 229 |
|
220 | 230 | messages = new JTextArea(); |
221 | 231 | messages.setVisible(false); |
222 | | - constraints.insets= new Insets(10,10,10,10); |
| 232 | + constraints.insets = new Insets(10, 10, 10, 10); |
223 | 233 | constraints.fill = GridBagConstraints.BOTH; |
224 | 234 | constraints.weighty = 1; |
225 | 235 | constraints.gridx = 0; |
@@ -331,6 +341,55 @@ private void initForm() { |
331 | 341 | group4Button.addActionListener(this::toggleLinker); |
332 | 342 | group5Button.addActionListener(this::toggleLinker); |
333 | 343 | group6Button.addActionListener(this::toggleLinker); |
| 344 | + |
| 345 | + //GetComponentState |
| 346 | + final JSONArray componentStateFields = new JSONArray() {{ |
| 347 | + put("Finsemble_Linker"); |
| 348 | + put("symbol"); |
| 349 | + }}; |
| 350 | + final JSONObject getComponentStateParam = new JSONObject() {{ |
| 351 | + put("fields", componentStateFields); |
| 352 | + }}; |
| 353 | + fsbl.getClients().getWindowClient().getComponentState(getComponentStateParam, this::handleGetComponentStateCb); |
| 354 | + } |
| 355 | + |
| 356 | + private void handleGetComponentStateCb(JSONObject err, JSONObject res) { |
| 357 | + if (err != null) { |
| 358 | + LOGGER.log(Level.SEVERE, "Error in handleGetComponentStateCb", err); |
| 359 | + } else { |
| 360 | + //Set subscribe linker channel |
| 361 | + if (res.has("Finsemble_Linker")) { |
| 362 | + final JSONArray channelToLink = res.getJSONArray("Finsemble_Linker"); |
| 363 | + final JSONObject windowIdentifier = fsbl.getClients().getWindowClient().getWindowIdentifier(); |
| 364 | + for (int i = 0; i < channelToLink.length(); i++) { |
| 365 | + try { |
| 366 | + String currentChannel = channelToLink.getString(i); |
| 367 | + JButton lkrBtn = linkerButtons.get(currentChannel); |
| 368 | + fsbl.getClients().getLinkerClient().linkToChannel(currentChannel, windowIdentifier, (error, response) -> { |
| 369 | + if (error != null) { |
| 370 | + LOGGER.log(Level.SEVERE, String.format("Error linking to channel: %s", currentChannel), err); |
| 371 | + } else { |
| 372 | + LOGGER.info((String.format("Linked to channel: %s", currentChannel))); |
| 373 | + } |
| 374 | + }); |
| 375 | + lkrBtn.setText("X"); |
| 376 | + lkrBtn.updateUI(); |
| 377 | + } catch (Exception ex) { |
| 378 | + String errorMsg = String.format("Error linking channel %s to group", channelToLink); |
| 379 | + LOGGER.log(Level.SEVERE, errorMsg, ex.getMessage()); |
| 380 | + fsbl.getClients().getLoggerClient().system().error(errorMsg + " cause: " + ex.getMessage()); |
| 381 | + } |
| 382 | + } |
| 383 | + } |
| 384 | + |
| 385 | + //Set symbol value |
| 386 | + if (res.has("symbol")) { |
| 387 | + final String symbol = res.getString("symbol"); |
| 388 | + if (!symbol.equals("")) { |
| 389 | + symbolLabel.setText(symbol); |
| 390 | + } |
| 391 | + } |
| 392 | + } |
334 | 393 | } |
335 | 394 |
|
336 | 395 | private void handleSymbol(JSONObject err, JSONObject res) { |
|
0 commit comments