aboutsummaryrefslogtreecommitdiff
path: root/tests/SwingIvy.java
blob: afe27f07ff74eb37ab8686113264b500fa977634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * Ivy java library API tester.
 *
 * @author  Yannick Jestin <mailto:yannick.jestin@enac.fr>
 *
 * (c) ENAC
 *
 */
import fr.dgac.ivy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingIvy implements Runnable {

  private Ivy bus;
  private final static int DODO = 2000;
  private final static String BLAH = "ivy blah blah blah";
  private final String[] data = {"one","two","three","four"};

  DefaultListModel model = new DefaultListModel();
  JFrame f = new JFrame("Test Ivy Swing");
  JSlider scale = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
  JTextArea text = new JTextArea("type anything inside this area");
  JToggleButton startstop = new JToggleButton("trigger ivy messages");
  JList list = new JList(model);
  volatile Thread runThread = null;
  boolean doSend = false;

  public SwingIvy(String domain)  {
    int index=0;
    for (String s : data) { model.add(index++,s); }
    f.getContentPane().add( scale, BorderLayout.PAGE_START );
    f.getContentPane().add( text, BorderLayout.CENTER );
    f.getContentPane().add( list, BorderLayout.LINE_END );
    f.getContentPane().add( startstop, BorderLayout.PAGE_END );
    f.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
	System.out.println("closing gracefully");
	f.dispose();
	bus.stop();
	Thread t = runThread;
	runThread = null;
	//if (t!=null) t.interrupt();
	t.interrupt();
      }
    });
    text.setRows(25);
    text.setColumns(40);
    startstop.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
	doSend = startstop.isSelected();
      }
    });
    f.pack();
    f.setVisible(true);
    runThread = new Thread(this);
    runThread.start();
    try {
      bus = new Ivy("SwingIvy",null, null);
      bus.bindAsyncMsg("^AddList (.*)", new IvyMessageListener() {
	public void receive(IvyClient c,String[] args) {
	  //System.out.println("SetText received");
	  model.add(model.getSize() , args[0]);
	}
      }, BindType.SWING);
      bus.bindAsyncMsg("^SetText (.*)", new IvyMessageListener() {
	public void receive(IvyClient c,String[] args) {
	  //System.out.println("SetText received");
	  text.append(args[0]+"\n");
	}
      }, BindType.SWING);
      bus.bindAsyncMsg("^SetRange ([0-9]+)", new IvyMessageListener() {
	public void receive(IvyClient c,String[] args) {
	  int i = Integer.parseInt(args[0]);
	  scale.setValue(i);
	  //System.out.println("SetRange received: "+i);
	}
      }, BindType.SWING);
      bus.sendToSelf(true);
      bus.start(domain);
    } catch (IvyException ie) {
      ie.printStackTrace();
    }
  }

  public void run() {
    int intRange=0;
    Thread thisThread=Thread.currentThread();
    while(runThread ==thisThread) {
      try {
	Thread.sleep(DODO);
	intRange++;
	if (doSend) {
	  if (intRange>99) intRange=0;
	  bus.sendMsg("SetRange "+intRange);
	  bus.sendMsg("SetText "+BLAH);
	  bus.sendMsg("AddList "+intRange);
	}
      } catch (IvyException e) {
	e.printStackTrace();
      } catch (InterruptedException e) {
	if (thisThread!=runThread) { break ;}
      }   
    }
  }

  public static void main(String[] args) throws IvyException {
    new SwingIvy(Ivy.getDomainArgs("SwingIvy",args));
  }

}