Skip to content

Commit 584761b

Browse files
committed
added channel api counter example
1 parent ea8fe05 commit 584761b

2 files changed

Lines changed: 222 additions & 7 deletions

File tree

release/counter_client.html

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<html>
2+
3+
<head>
4+
<script>
5+
let channelClient;
6+
7+
async function makeClient() {
8+
const connectPayload = { payload: 'place connection payload here' };
9+
const client = await fin.InterApplicationBus.Channel.connect('CounterJavaProvider', connectPayload);
10+
11+
return {
12+
getValue: () => client.dispatch('getValue'),
13+
increment: () => client.dispatch('increment'),
14+
incrementBy: (x) => client.dispatch('incrementBy', { delta: x }),
15+
}
16+
}
17+
18+
function getCounterValue() {
19+
channelClient.getValue().then(v => { document.getElementById("count").textContent = v.value; });
20+
}
21+
22+
function incrementCounterValue() {
23+
channelClient.increment().then(v => { document.getElementById("count").textContent = v.value; });
24+
}
25+
26+
function incrementCounterValueBy(delta) {
27+
channelClient.incrementBy(delta).then(v => { document.getElementById("count").textContent = v.value; });
28+
}
29+
30+
document.addEventListener('DOMContentLoaded', () => {
31+
fin.desktop.main(() => {
32+
makeClient().then(async (clientBus) => {
33+
channelClient = clientBus;
34+
});
35+
});
36+
});
37+
38+
</script>
39+
</head>
40+
41+
<body>
42+
count: <span id="count">N/A</span>
43+
<button onclick="getCounterValue()">Get</button>
44+
<br>
45+
<br>
46+
<button onclick="incrementCounterValue()">Increment</button>
47+
<br>
48+
<br>
49+
<input id="deltaValue"></input>
50+
<button onclick="incrementCounterValueBy(document.getElementById('deltaValue').value)">IncrementBy</button>
51+
</body>
52+
53+
</html>

src/main/java/com/openfin/desktop/demo/OpenFinDesktopDemo.java

Lines changed: 169 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.openfin.desktop.animation.AnimationTransitions;
77
import com.openfin.desktop.animation.OpacityTransition;
88
import com.openfin.desktop.animation.PositionTransition;
9+
import com.openfin.desktop.channel.ChannelAction;
10+
import com.openfin.desktop.channel.ChannelProvider;
11+
912
import info.clearthought.layout.TableLayout;
1013

1114
import javax.swing.*;
@@ -16,8 +19,10 @@
1619
import java.awt.event.ActionListener;
1720
import java.awt.event.WindowEvent;
1821
import java.awt.event.WindowListener;
22+
import java.lang.reflect.InvocationTargetException;
1923
import java.util.ArrayList;
2024
import java.util.HashMap;
25+
import java.util.concurrent.atomic.AtomicInteger;
2126

2227
import org.json.JSONObject;
2328
import org.slf4j.Logger;
@@ -213,6 +218,7 @@ private JPanel layoutCenterPanel() {
213218
JTabbedPane tabbedPane = new JTabbedPane();
214219
tabbedPane.addTab("Window", layoutWindowControlPanel());
215220
tabbedPane.addTab("IAB", layoutIABControlPanel());
221+
tabbedPane.addTab("Channel", layoutChannelControlPanel());
216222

217223
panel.add(tabbedPane, "0,1,0,1");
218224
panel.add(layoutStatusPanel(), "0,2,0,2");
@@ -239,13 +245,13 @@ private JPanel layoutAppDescriptionPanel() {
239245
}
240246

241247
private JPanel layoutIABControlPanel() {
242-
JPanel pnl = new JPanel(new BorderLayout());
248+
JPanel pnl = new JPanel(new GridLayout(2, 1));
243249

244250
JPanel pnlPublish = new JPanel(new GridBagLayout());
245251
pnlPublish.setBorder(BorderFactory.createTitledBorder("Publish"));
246-
252+
247253
GridBagConstraints gbConstraints = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
248-
GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);
254+
GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
249255
pnlPublish.add(new JLabel("Topic"), gbConstraints);
250256

251257
gbConstraints.gridx = 1;
@@ -284,13 +290,13 @@ public void actionPerformed(ActionEvent actionEvent) {
284290

285291
pnlPublish.add(btnPublish, gbConstraints);
286292

287-
pnl.add(pnlPublish, BorderLayout.NORTH);
293+
pnl.add(pnlPublish);
288294

289295
JPanel pnlSubscribe = new JPanel(new FlowLayout(FlowLayout.LEFT));
290296
pnlSubscribe.setBorder(BorderFactory.createTitledBorder("Subscribe"));
291297

292298
pnlSubscribe.add(new JLabel("Topic"));
293-
299+
294300
JTextField tfSubTopic = new JTextField();
295301
tfSubTopic.setPreferredSize(new Dimension(150, tfSubTopic.getPreferredSize().height));
296302
pnlSubscribe.add(tfSubTopic);
@@ -323,13 +329,169 @@ public void actionPerformed(ActionEvent actionEvent) {
323329
}
324330
catch (DesktopException e) {
325331
e.printStackTrace();
326-
};
332+
}
333+
;
327334
}
328335
}
329336
});
330337

331338
pnlSubscribe.add(btnSubscribe);
332-
pnl.add(pnlSubscribe, BorderLayout.SOUTH);
339+
pnl.add(pnlSubscribe);
340+
341+
return pnl;
342+
}
343+
344+
private int getCounterValue(JTextField textField) {
345+
if (SwingUtilities.isEventDispatchThread()) {
346+
return Integer.parseInt(textField.getText());
347+
}
348+
else {
349+
AtomicInteger value = new AtomicInteger(0);
350+
try {
351+
SwingUtilities.invokeAndWait(new Runnable() {
352+
353+
@Override
354+
public void run() {
355+
value.set(Integer.parseInt(textField.getText()));
356+
}
357+
});
358+
}
359+
catch (InvocationTargetException | InterruptedException e) {
360+
e.printStackTrace();
361+
}
362+
363+
return value.get();
364+
}
365+
}
366+
367+
private void setCounterValue(JTextField textField, int value) {
368+
if (SwingUtilities.isEventDispatchThread()) {
369+
textField.setText(Integer.toString(value));
370+
}
371+
else {
372+
try {
373+
SwingUtilities.invokeAndWait(new Runnable() {
374+
375+
@Override
376+
public void run() {
377+
textField.setText(Integer.toString(value));
378+
}
379+
});
380+
}
381+
catch (InvocationTargetException | InterruptedException e) {
382+
e.printStackTrace();
383+
}
384+
}
385+
}
386+
387+
private JPanel layoutChannelProviderControlPanel() {
388+
JPanel pnlProvider = new JPanel(new GridBagLayout());
389+
pnlProvider.setBorder(BorderFactory.createTitledBorder("Provider"));
390+
GridBagConstraints gbConstraints = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST,
391+
GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
392+
393+
pnlProvider.add(new JLabel("Provider Name"), gbConstraints);
394+
395+
gbConstraints.gridx = 1;
396+
gbConstraints.weightx = 0.5;
397+
JTextField tfProviderName = new JTextField("CounterJavaProvider");
398+
tfProviderName.setPreferredSize(new Dimension(150, tfProviderName.getPreferredSize().height));
399+
pnlProvider.add(tfProviderName, gbConstraints);
400+
401+
gbConstraints.gridx = 3;
402+
gbConstraints.weightx = 0;
403+
pnlProvider.add(new JLabel("Count"), gbConstraints);
404+
405+
gbConstraints.gridx = 4;
406+
gbConstraints.weightx = 0.5;
407+
JTextField tfCount = new JTextField("0");
408+
tfCount.setEditable(false);
409+
tfCount.setPreferredSize(new Dimension(150, tfProviderName.getPreferredSize().height));
410+
pnlProvider.add(tfCount, gbConstraints);
411+
412+
gbConstraints.gridx = 2;
413+
gbConstraints.weightx = 0;
414+
JToggleButton tbProvider = new JToggleButton("Enable");
415+
tbProvider.addActionListener(new ActionListener() {
416+
417+
@Override
418+
public void actionPerformed(ActionEvent e) {
419+
tfProviderName.setEditable(!tbProvider.isSelected());
420+
String channelName = tfProviderName.getText();
421+
if (tbProvider.isSelected()) {
422+
// see below
423+
tbProvider.setEnabled(false);
424+
425+
desktopConnection.getChannel(channelName).create(new AsyncCallback<ChannelProvider>() {
426+
@Override
427+
public void onSuccess(ChannelProvider provider) {
428+
// provider created, register actions.
429+
430+
provider.register("getValue", new ChannelAction() {
431+
@Override
432+
public JSONObject invoke(String action, JSONObject payload) {
433+
logger.info(String.format("provider processing action %s, payload=%s", action,
434+
payload.toString()));
435+
JSONObject obj = new JSONObject();
436+
obj.put("value", getCounterValue(tfCount));
437+
return obj;
438+
}
439+
});
440+
provider.register("increment", new ChannelAction() {
441+
@Override
442+
public JSONObject invoke(String action, JSONObject payload) {
443+
logger.info(String.format("provider processing action %s, payload=%s", action,
444+
payload.toString()));
445+
JSONObject obj = new JSONObject();
446+
int currentValue = getCounterValue(tfCount);
447+
int newValue = currentValue + 1;
448+
setCounterValue(tfCount, newValue);
449+
obj.put("value", newValue);
450+
return obj;
451+
}
452+
});
453+
provider.register("incrementBy", new ChannelAction() {
454+
@Override
455+
public JSONObject invoke(String action, JSONObject payload) {
456+
logger.info(String.format("provider processing action %s, payload=%s", action,
457+
payload.toString()));
458+
int delta = payload.getInt("delta");
459+
JSONObject obj = new JSONObject();
460+
int currentValue = getCounterValue(tfCount);
461+
int newValue = currentValue + delta;
462+
setCounterValue(tfCount, newValue);
463+
obj.put("value", newValue);
464+
return obj;
465+
}
466+
});
467+
468+
}
469+
});
470+
}
471+
else {
472+
// currently, provider doesn't have the "destroy" method, otherwise should
473+
// destroy the provider and re-enable the toggle button.
474+
}
475+
}
476+
});
477+
pnlProvider.add(tbProvider, gbConstraints);
478+
479+
return pnlProvider;
480+
}
481+
482+
private JPanel layoutChannelClientControlPanel() {
483+
JPanel pnlClient = new JPanel();
484+
pnlClient.setBorder(BorderFactory.createTitledBorder("Client"));
485+
486+
return pnlClient;
487+
}
488+
489+
private JPanel layoutChannelControlPanel() {
490+
491+
JPanel pnl = new JPanel(new GridLayout(2, 1));
492+
pnl.setBorder(BorderFactory.createTitledBorder("Counter Demo with Channel API"));
493+
pnl.add(layoutChannelProviderControlPanel());
494+
//pnl.add(layoutChannelClientControlPanel());
333495

334496
return pnl;
335497
}

0 commit comments

Comments
 (0)