aboutsummaryrefslogtreecommitdiff
path: root/src/TestIvySwing.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/TestIvySwing.java')
-rw-r--r--src/TestIvySwing.java173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/TestIvySwing.java b/src/TestIvySwing.java
new file mode 100644
index 0000000..a6d3777
--- /dev/null
+++ b/src/TestIvySwing.java
@@ -0,0 +1,173 @@
+package fr.dgac.ivy ;
+
+import java.awt.BorderLayout;
+import java.awt.event.*;
+import javax.swing.* ;
+
+class TestIvySwing extends JPanel implements IvyApplicationListener {
+
+ private static int index;
+ private static int nbTIS=0;
+ String localname;
+
+ Ivy bus ;
+ String regexp = "^AIRCRAFT:";;
+ JLabel laRegex;
+ JTextArea ta ;
+ JTextField tfRegex, tfSend ;
+ JButton buApply, buSend, buClear ;
+ JComboBox ports;
+ int regexp_id;
+ REGCB reg;
+
+ public TestIvySwing() throws IvyException {
+ super(new BorderLayout());
+ nbTIS++;
+ // un textarea au centre
+ ta = new JTextArea(25,40);
+ ta.setEditable(false);
+ add(new JScrollPane(ta),BorderLayout.CENTER);
+ // une zone regex au nord
+ JPanel p = new JPanel(new BorderLayout());
+ p.add(new JLabel("Regex:"),BorderLayout.WEST);
+ tfRegex = new JTextField();
+ tfRegex.addActionListener(reg=new REGCB());
+ p.add(tfRegex,BorderLayout.CENTER);
+ p.add(laRegex=new JLabel(regexp),BorderLayout.EAST);
+ add(p,BorderLayout.NORTH);
+ // une zone messages au sud du nord, mais au nord du centre
+ JPanel p2 = new JPanel(new BorderLayout());
+ p2.add(new JLabel("Msg:"),BorderLayout.WEST);
+ tfSend = new JTextField("");
+ tfSend.addActionListener(new SENDCB());
+ p2.add(tfSend,BorderLayout.CENTER);
+ p.add(p2,BorderLayout.SOUTH);
+ // au sud, je rajoute un bouton pour spawner un autre testeur
+ JButton tmpb ;
+ (p = new JPanel()).add(tmpb=new JButton("spawn"));
+ tmpb.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ try { newTestIvy();
+ } catch (IvyException ie) {}
+ }
+ });
+ p.add(tmpb=new JButton("clear"));
+ tmpb.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ ta.setText("");
+ }
+ });
+
+ ports=new JComboBox();
+ ports.addItem("127.255.255.255:2010");
+ ports.addItem("143.196.53.255:2010");
+ ports.addItem("143.196.53.255:2020");
+ ports.addItem("143.196.53.255:3110");
+ ports.addActionListener(new ComboCB());
+ p.add(ports);
+ add(p,BorderLayout.SOUTH);
+ // gestion du fovus clavier
+ tfRegex.setNextFocusableComponent(tfSend);
+ tfSend.setNextFocusableComponent(tfRegex);
+ tfSend.setRequestFocusEnabled(true);
+ localname = "TestIvySwing ("+index+")";
+ index++;
+ bus = new Ivy(localname,localname+" prêt",this);
+ regexp_id = bus.bindMsg(regexp,reg);
+ bus.start(null);
+ append( "Ivy Domain: "+ bus.getDomain(null) );
+ }
+
+ public String getLocalname(){ return localname;}
+
+ public void stop() { bus.stop(); }
+
+ public void connect(IvyClient client) {
+ append(client.getApplicationName() + " connected " );
+ }
+
+ public void disconnect(IvyClient client) {
+ append(client.getApplicationName() + " disconnected " );
+ }
+
+ public void die(IvyClient client, int id) {
+ append(client.getApplicationName() + " die "+ id );
+ }
+
+ public void directMessage(IvyClient client, int id, String arg) {
+ append(client.getApplicationName() + " direct Message "+ id + arg );
+ }
+
+ /* inner */ class ComboCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ String newDomain=(String)ports.getSelectedItem();
+ bus.stop();
+ try {
+ bus.start(newDomain);
+ } catch (IvyException ie ) {
+ System.err.println("auuuugh "+newDomain);
+ }
+ }
+ } // ComboCB
+
+ /* inner */ class REGCB implements ActionListener, IvyMessageListener {
+ public void actionPerformed(ActionEvent e) {
+ // enlever l'ancienne regex
+ bus.unBindMsg(regexp_id);
+ // ajoute la nouvelle regex
+ regexp=tfRegex.getText();
+ regexp.trim();
+ regexp_id = bus.bindMsg(regexp,this);
+ tfRegex.setText("");
+ laRegex.setText(regexp);
+ }
+ public void receive(IvyClient client, String[] args) {
+ String out="client " + client.getApplicationName() + " envoie: [ ";
+ for (int i=0;i<args.length;i++) {
+ out+=args[i]+ ((i<args.length-1)?" , ":"");
+ }
+ out+=" ]";
+ append(out);
+ }
+ }
+
+ /* inner */ class SENDCB implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ int count;
+ String tosend = tfSend.getText();
+ tfSend.setText("");
+ if ( (count = bus.sendMsg(tosend)) != 0 )
+ append("Sending '" + tosend + "' count " + count );
+ else
+ append("not Sending '" + tosend + "' nobody cares");
+ }
+ }
+
+ private void append(String s) { ta.append(s + "\n"); }
+
+
+ public static void newTestIvy() throws IvyException {
+ TestIvySwing tb = new TestIvySwing();
+ JFrame f = new JFrame(tb.getLocalname());
+ f.addWindowListener( tb.new WCCB(f,tb)) ;
+ f.getContentPane().add(tb, BorderLayout.CENTER);
+ f.pack();
+ f.setVisible(true);
+ }
+
+ /* inner */ class WCCB extends WindowAdapter {
+ private JFrame f;
+ public WCCB(JFrame f, TestIvySwing b) { this.f=f; }
+ public void windowClosing(WindowEvent e) {
+ bus.stop();
+ f.dispose();
+ if (--nbTIS == 0) System.exit(0);
+ // je quitte l'appli au départ du dernier TIS
+ }
+ public void windowActivated(WindowEvent e) { tfSend.grabFocus(); }
+ } // WCCB
+
+ public static void main(String[] args) throws IvyException { newTestIvy(); }
+}
+
+// EOF