66import com .openfin .desktop .animation .AnimationTransitions ;
77import com .openfin .desktop .animation .OpacityTransition ;
88import com .openfin .desktop .animation .PositionTransition ;
9+ import com .openfin .desktop .channel .ChannelAction ;
10+ import com .openfin .desktop .channel .ChannelProvider ;
11+
912import info .clearthought .layout .TableLayout ;
1013
1114import javax .swing .*;
1619import java .awt .event .ActionListener ;
1720import java .awt .event .WindowEvent ;
1821import java .awt .event .WindowListener ;
22+ import java .lang .reflect .InvocationTargetException ;
1923import java .util .ArrayList ;
2024import java .util .HashMap ;
25+ import java .util .concurrent .atomic .AtomicInteger ;
2126
2227import org .json .JSONObject ;
2328import 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