home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / BridgeTester.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  17.6 KB  |  624 lines

  1. /**
  2.  * This is the bridge tester bean.
  3.  *
  4.  * Simply define all property types, fire evemts and define a customizer
  5.  */
  6.  
  7. package sunw.demo.test;
  8.  
  9. import java.awt.*;
  10. import java.beans.*;
  11. import java.awt.event.*;
  12.  
  13. public class BridgeTester extends Panel
  14.                 implements ActionListener, KeyListener, MouseListener {
  15.  
  16.     public BridgeTester() {
  17.  
  18.     setLayout(new GridLayout(6,4,10,10));
  19.     event1Button = new Button("Fire Event 1 ");
  20.     event1Button.addActionListener(this);
  21.     add(event1Button);
  22.     event2Button = new Button("Fire Event 2 ");
  23.     event2Button.addActionListener(this);
  24.     add(event2Button);
  25.     event3Button = new Button("Fire Event 3 ");
  26.     event3Button.addActionListener(this);
  27.     add(event3Button);
  28.     event4Button = new Button("Fire Event 4 ");
  29.     event4Button.addActionListener(this);
  30.     add(event4Button);
  31.  
  32.     add(new Label("Int Value : "));
  33.     intField = new TextField();
  34.     intField.addKeyListener(this);
  35.     add(intField);
  36.  
  37.     doubleField = new TextField();
  38.     add(new Label("Double Value : "));
  39.     doubleField.addKeyListener(this);
  40.     add(doubleField);
  41.  
  42.     add(new Label("Short Value :"));
  43.     shortField = new TextField();
  44.     shortField.addKeyListener(this);
  45.     add(shortField);
  46.  
  47.     add(new Label("Float Value :"));
  48.     floatField = new TextField();
  49.     floatField.addKeyListener(this);
  50.     add(floatField);
  51.  
  52.     add(new Label("Char Value :"));
  53.     charField = new TextField();
  54.     charField.addKeyListener(this);
  55.     add(charField);
  56.  
  57.     add(new Label("Byte Value :"));
  58.     byteField = new TextField();
  59.     byteField.addKeyListener(this);
  60.     add(byteField);
  61.  
  62.     add(new Label("String value :"));
  63.     stringField = new TextField();
  64.     stringField.addKeyListener(this);
  65.     add(stringField);
  66.  
  67.     add(new Label("Boolean Value :"));
  68.     booleanField = new Checkbox();
  69.         booleanField.addMouseListener(this);
  70.     add(booleanField);
  71.  
  72.     array1 = new TextField();
  73.     add(array1);
  74.     array2 = new TextField();
  75.     add(array2);
  76.     array3 = new TextField();
  77.     add(array3);
  78.     array4 = new TextField();
  79.     add(array4);    
  80.  
  81.     int[] myIntArray = { 1 , 2, 3, 4, 5 };
  82.     setIntArray(myIntArray);
  83.     double[] myDoubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
  84.     setDoubleArray(myDoubleArray);
  85.            String[] myStringArray = {"TOTO", "s'en", "va", "en", "guerre"};
  86.         setStringArray(myStringArray);
  87.         float[] myFloatArray = { (float) 1.01,(float)  2.02,(float)  3.03,(float)  4.04,(float)  5.05 };
  88.     setFloatArray(myFloatArray);
  89.     char[] myCharArray = { 'A', 'B', 'c', 'd', 'E' };
  90.     setCharArray(myCharArray);
  91.     byte[] myByteArray = {1, 2, 3, 4, 5};
  92.     setByteArray(myByteArray);
  93.     short[] myShortArray = {5, 4, 3, 2, 1};
  94.     setShortArray(myShortArray);
  95.     boolean[] myBooleanArray = {true, false, false, true, false};
  96.     setBooleanArray(myBooleanArray);
  97.     }
  98.  
  99.     public java.awt.Dimension getPreferredSize() {
  100.     return new java.awt.Dimension(400,200);
  101.     }
  102.  
  103.     /**
  104.      * @deprecated provided for backward compatibility with old layout managers.
  105.      */
  106.     public Dimension preferredSize() {
  107.     return getPreferredSize();
  108.     }
  109.  
  110.     public void setIntValue(int newValue)  throws PropertyVetoException {
  111.  
  112.     // Fires first the Vetoable event, if it's vetoed the following code
  113.     // won't happen
  114.     int oldValue = intValue;
  115.     vetos.fireVetoableChange("intValue", new Integer(oldValue), new Integer(newValue));
  116.     intValue = newValue;
  117.     intField.setText(String.valueOf(intValue));
  118.     changes.firePropertyChange("intValue", new Integer(oldValue), new Integer(newValue));
  119.     }
  120.  
  121.     public int getIntValue() {
  122.     return intValue;
  123.     }
  124.  
  125.     public void setDoubleValue(double newValue) {    
  126.     double oldValue = doubleValue;
  127.     doubleValue = newValue;
  128.     doubleField.setText(String.valueOf(doubleValue));
  129.     changes.firePropertyChange("doubleValue", new Double(oldValue), new Double(newValue));
  130.     }
  131.  
  132.     public double getDoubleValue() {
  133.     return doubleValue;
  134.     }
  135.   
  136.     public void setShortValue(short newValue) {
  137.     short oldValue = shortValue;    
  138.     shortValue = newValue;
  139.     shortField.setText(String.valueOf(shortValue));
  140.     changes.firePropertyChange("shortValue", new Short(oldValue), new Short(newValue));
  141.     }
  142.  
  143.     public short getShortValue() {
  144.     return shortValue;
  145.     }
  146.  
  147.     public void setFloatValue(float newValue) {
  148.     float oldValue = floatValue;
  149.     floatValue = newValue;
  150.     floatField.setText(String.valueOf(floatValue));
  151.     changes.firePropertyChange("floatValue", new Float(oldValue), new Float(newValue));
  152.     }
  153.  
  154.     public float getFloatValue() {
  155.     return floatValue;
  156.     }
  157.  
  158.     public void setCharValue(char newValue) {
  159.     char oldValue = charValue;
  160.     charValue = newValue;
  161.     charField.setText(String.valueOf(charValue));
  162.     changes.firePropertyChange("charValue", new Character(oldValue), new Character(newValue));
  163.     }
  164.  
  165.     public char getCharValue() {
  166.     return charValue;
  167.     }
  168.  
  169.     public void setByteValue(byte  newValue) {
  170.     byte oldValue = byteValue;
  171.     byteValue = newValue;
  172.     byteField.setText(String.valueOf(byteValue));
  173.     changes.firePropertyChange("byteValue", new Byte(oldValue), new Byte(newValue));
  174.     }
  175.  
  176.     public void setByteValue(int  newValue) {
  177.     setByteValue((byte) newValue);
  178.     }
  179.  
  180.     public byte getByteValue() {
  181.     return byteValue;
  182.     }
  183.  
  184.     public void setLongValue(long  newValue) {
  185.     longValue = newValue;
  186.     }
  187.  
  188.     public long getLongValue() {
  189.     return longValue;
  190.     }
  191.     public boolean isBooleanValue() {
  192.     return booleanValue;
  193.     } 
  194.  
  195.     public void setBooleanValue(boolean newValue) {
  196.     boolean oldValue = booleanValue;
  197.     booleanValue = newValue;
  198.     booleanField.setState(booleanValue);
  199.     changes.firePropertyChange("booleanValue", new Boolean(oldValue), new Boolean(newValue));
  200.     }
  201.  
  202.     public String getStringValue() {
  203.       return stringValue;
  204.     }
  205.   
  206.     public void setStringValue(String newValue) {
  207.     String oldValue = stringValue;
  208.     stringValue = newValue;
  209.     stringField.setText(stringValue);
  210.     changes.firePropertyChange("stringValue", oldValue, newValue);
  211.     }
  212.  
  213.  
  214.  
  215.     public int[] getIntArray() {
  216.         return intArray;
  217.     }
  218.  
  219.     public void setIntArray(int[] newIntArray) {
  220.          intArray = newIntArray;
  221.      array1.setText(String.valueOf(intArray[0]));
  222.      array2.setText(String.valueOf(intArray[1]));
  223.      array3.setText(String.valueOf(intArray[2]));
  224.      array4.setText(String.valueOf(intArray[3]));
  225.     }
  226.  
  227.     public void setIntArrayWithIndex(int index, int value) {
  228.      intArray[index] = value;
  229.     }
  230.  
  231.     public int getIntArrayWithIndex(int index) {       
  232.     return intArray[index];
  233.     }
  234.  
  235.     public double[] getDoubleArray() {
  236.         return doubleArray;
  237.     }
  238.  
  239.     public void setDoubleArray(double[] newDoubleArray) {
  240.          doubleArray = newDoubleArray;
  241.      array1.setText(String.valueOf(doubleArray[0]));
  242.      array2.setText(String.valueOf(doubleArray[1]));
  243.      array3.setText(String.valueOf(doubleArray[2]));
  244.      array4.setText(String.valueOf(doubleArray[3]));
  245.     }
  246.  
  247.     public void setDoubleArrayWithIndex(int index, double value) {
  248.      doubleArray[index] = value;
  249.     }
  250.  
  251.     public double getDoubleArrayWithIndex(int index) {       
  252.     return doubleArray[index];
  253.     }
  254.  
  255.     public String[] getStringArray() {
  256.         return stringArray;
  257.     }
  258.  
  259.     public void setStringArray(String[] newStringArray) {
  260.          stringArray = newStringArray;
  261.      array1.setText(stringArray[0]);
  262.      array2.setText(stringArray[1]);
  263.      array3.setText(stringArray[2]);
  264.      array4.setText(stringArray[3]);
  265.     }
  266.  
  267.     public void setStringArrayWithIndex(int index, String value) {
  268.      stringArray[index] = value;
  269.     }
  270.  
  271.     public String getStringArrayWithIndex(int index) {       
  272.     return stringArray[index];
  273.     }
  274.  
  275.     public byte[] getByteArray() {
  276.         return byteArray;
  277.     }
  278.  
  279.     public void setByteArray(byte[] newByteArray) {
  280.          byteArray = newByteArray;
  281.      array1.setText(String.valueOf(byteArray[0]));
  282.      array2.setText(String.valueOf(byteArray[1]));
  283.      array3.setText(String.valueOf(byteArray[2]));
  284.      array4.setText(String.valueOf(byteArray[3]));
  285.     }
  286.  
  287.     public void setByteArrayWithIndex(int index, byte value) {
  288.      byteArray[index] = value;
  289.     }
  290.  
  291.     public byte getByteArrayWithIndex(int index) {       
  292.     return byteArray[index];
  293.     }
  294.  
  295.     public float[] getFloatArray() {
  296.         return floatArray;
  297.     }
  298.  
  299.     public void setFloatArray(float[] newFloatArray) {
  300.          floatArray = newFloatArray;
  301.      array1.setText(String.valueOf(floatArray[0]));
  302.      array2.setText(String.valueOf(floatArray[1]));
  303.      array3.setText(String.valueOf(floatArray[2]));
  304.      array4.setText(String.valueOf(floatArray[3]));
  305.     }
  306.  
  307.     public void setFloatArrayWithIndex(int index, float value) {
  308.      floatArray[index] = value;
  309.     }
  310.  
  311.     public float getFloatArrayWithIndex(int index) {       
  312.     return floatArray[index];
  313.     }
  314.  
  315.     public char[] getCharArray() {
  316.         return charArray;
  317.     }
  318.  
  319.     public void setCharArray(char[] newCharArray) {
  320.          charArray = newCharArray;
  321.      array1.setText(String.valueOf(charArray[0]));
  322.      array2.setText(String.valueOf(charArray[1]));
  323.      array3.setText(String.valueOf(charArray[2]));
  324.      array4.setText(String.valueOf(charArray[3]));
  325.     }
  326.  
  327.     public void setCharArrayWithIndex(int index, char value) {
  328.      charArray[index] = value;
  329.     }
  330.  
  331.     public char getCharArrayWithIndex(int index) {       
  332.     return charArray[index];
  333.     }
  334.  
  335.     public boolean[] getBooleanArray() {
  336.         return booleanArray;
  337.     }
  338.  
  339.     public void setBooleanArray(boolean[] newBooleanArray) {
  340.          booleanArray = newBooleanArray;
  341.      array1.setText(String.valueOf(booleanArray[0]));
  342.      array2.setText(String.valueOf(booleanArray[1]));
  343.      array3.setText(String.valueOf(booleanArray[2]));
  344.      array4.setText(String.valueOf(booleanArray[3]));
  345.     }
  346.  
  347.     public void setBooleanArrayWithIndex(int index, boolean value) {
  348.      booleanArray[index] = value;
  349.     }
  350.  
  351.     public boolean getBooleanArrayWithIndex(int index) {       
  352.     return booleanArray[index];
  353.     }
  354.  
  355.     public short[] getShortArray() {
  356.         return shortArray;
  357.     }
  358.  
  359.     public void setShortArray(short[] newShortArray) {
  360.          shortArray = newShortArray;
  361.      array1.setText(String.valueOf(shortArray[0]));
  362.      array2.setText(String.valueOf(shortArray[1]));
  363.      array3.setText(String.valueOf(shortArray[2]));
  364.      array4.setText(String.valueOf(shortArray[3]));
  365.     }
  366.  
  367.     public void setShortArrayWithIndex(int index, short value) {
  368.      shortArray[index] = value;
  369.     }
  370.  
  371.     public short getShortArrayWithIndex(int index) {       
  372.     return shortArray[index];
  373.     }
  374.  
  375.   public void printObjectInField(Object o) {
  376.     if (o==null)
  377.       stringField.setText("NULL REF");
  378.     else
  379.       stringField.setText(o.toString());
  380.   }
  381.  
  382.     /**
  383.      * The specified PropertyChangeListeners <b>propertyChange</b> method will
  384.      * be called each time the value of any bound property is changed.
  385.      * The PropertyListener object is addded to a list of PropertyChangeListeners
  386.      * managed by this button, it can be removed with removePropertyChangeListener.
  387.      * Note: the JavaBeans specification does not require PropertyChangeListeners
  388.      * to run in any particular order.
  389.      *
  390.      * @see #removePropertyChangeListener
  391.      * @param l the PropertyChangeListener
  392.      */      
  393.     public void addPropertyChangeListener(PropertyChangeListener l) {
  394.     changes.addPropertyChangeListener(l);
  395.     }
  396.  
  397.     /** 
  398.      * Remove this PropertyChangeListener from the buttons internal list.  
  399.      * If the PropertyChangeListener isn't on the list, silently do nothing.
  400.      * 
  401.      * @see #addPropertyChangeListener
  402.      * @param l the PropertyChangeListener
  403.      */      
  404.     public void removePropertyChangeListener(PropertyChangeListener l) {
  405.     changes.removePropertyChangeListener(l);
  406.     }
  407.  
  408.     public void addVetoableChangeListener(VetoableChangeListener l) {
  409.     vetos.addVetoableChangeListener(l);
  410.     }
  411.  
  412.     public void removeVetoableChangeListener(VetoableChangeListener l) {
  413.     vetos.removeVetoableChangeListener(l);
  414.     }
  415.  
  416.     // Event Listeners handling
  417.     public synchronized void addBridgeTesterListener(BridgeTesterListener l) {
  418.     listeners.addElement(l);
  419.     }
  420.  
  421.     public synchronized void removeBridgeTesterListener(BridgeTesterListener l) {
  422.     listeners.removeElement(l);
  423.     }
  424.  
  425.     /*
  426.      * Small adaptor class that implement the java.awt.event.ActionListener
  427.      * @return ActionListener interface implementor
  428.      */
  429.     public java.awt.event.ActionListener getActionListener() {
  430.       return new java.awt.event.ActionListener() {
  431.     public void actionPerformed(java.awt.event.ActionEvent e) {
  432.       Dialog myDialog = new Dialog(new Frame(), "Event Received");
  433.       myDialog.setBounds(200,200,400,400);
  434.       myDialog.show();
  435.     }
  436.       };
  437.     }
  438.       
  439.  
  440.     private void fireEvent(int eventNumber) {
  441.  
  442.     java.util.Vector listenerObjects;
  443.     synchronized(this) {
  444.         listenerObjects = (java.util.Vector) listeners.clone();
  445.     }
  446.     
  447.     for (int i=0; i<listenerObjects.size();i++) {
  448.         BridgeTesterListener l = (BridgeTesterListener) listenerObjects.elementAt(i);
  449.         switch(eventNumber) {
  450.             case 1 : 
  451.           BridgeTesterEvent evt = new BridgeTesterEvent(this, stringValue, intValue);                  l.eventNumber1(evt);
  452.           break;
  453.               case 2 :
  454.           l.eventNumber2(stringValue);
  455.           break;
  456.                 case 3:
  457.           l.eventNumber3(shortValue);
  458.           break;
  459.         case 4:
  460.           l.eventNumber4(intArray);
  461.           break;
  462.         }
  463.     }
  464.     }
  465.  
  466.     // ActionListener implementation              
  467.     public void actionPerformed(ActionEvent e) {
  468.  
  469.          Object source = e.getSource();
  470.          if (source==event1Button) {
  471.                 fireEvent(1);
  472.         return;
  473.      }
  474.          if (source==event2Button) {
  475.                 fireEvent(2);
  476.         return;
  477.      }
  478.          if (source==event3Button) {
  479.                 fireEvent(3);
  480.         return;
  481.      }
  482.          if (source==event4Button) {
  483.                 fireEvent(4);
  484.         return;
  485.      }
  486.  
  487.     }
  488.     
  489.     
  490.     // KeyListener implementation
  491.     public void keyPressed(KeyEvent e) {}
  492.     public void keyTyped(KeyEvent e) {}
  493.     public void keyReleased(KeyEvent e) {
  494.     
  495.          Object source = e.getSource();
  496.      if (source==stringField) {
  497.             setStringValue(stringField.getText());
  498.         return;
  499.      }
  500.      if (source==doubleField) {
  501.         try {
  502.           double oldValue = doubleValue;
  503.           doubleValue = new Double(doubleField.getText()).doubleValue();
  504.           changes.firePropertyChange("doubleValue", new Double(oldValue), new Double(doubleValue));
  505.  
  506.         } catch (java.lang.NumberFormatException ex) {
  507.           doubleField.setText(String.valueOf(getDoubleValue()));
  508.         }
  509.         return;
  510.      } 
  511.      if (source==intField) {
  512.         int oldValue = intValue;
  513.         try {     
  514.           int newValue = new Integer(intField.getText()).intValue();
  515.           vetos.fireVetoableChange("intValue", new Integer(oldValue), new Integer(newValue));
  516.           intValue = newValue;
  517.           changes.firePropertyChange("intValue", new Integer(oldValue), new Integer(newValue));     
  518.         } catch (java.lang.NumberFormatException ex) {
  519.              intField.setText(String.valueOf(getIntValue()));
  520.         } catch (PropertyVetoException ex) {
  521.              intField.setText(String.valueOf(getIntValue()));
  522.         }  
  523.         return;
  524.      } 
  525.      if (source==shortField) {
  526.         try {
  527.           short oldValue = shortValue;
  528.           shortValue = new Short(shortField.getText()).shortValue();
  529.           changes.firePropertyChange("shortValue", new Short(oldValue), new Short(shortValue));
  530.         } catch (java.lang.NumberFormatException ex) {
  531.              shortField.setText(String.valueOf(getShortValue()));
  532.         }
  533.         return;
  534.      } 
  535.      if (source==floatField) {
  536.         try {
  537.           float oldValue = floatValue;
  538.           floatValue = new Float(floatField.getText()).floatValue();
  539.           changes.firePropertyChange("floatValue", new Float(oldValue), new Float(floatValue));
  540.         } catch (java.lang.NumberFormatException ex) {
  541.           floatField.setText(String.valueOf(getFloatValue()));
  542.         }
  543.         return;
  544.      } 
  545.      if (source==byteField) {
  546.         try {
  547.           byte oldValue = byteValue;
  548.           byteValue = new Byte(byteField.getText()).byteValue();
  549.           changes.firePropertyChange("byteValue", new Byte(oldValue), new Byte(byteValue));
  550.         } catch (java.lang.NumberFormatException ex) {
  551.              byteField.setText(String.valueOf(getByteValue()));
  552.         }
  553.         return;
  554.      } 
  555.      if (source==charField) {
  556.         try {
  557.           setCharValue(charField.getText().charAt(0));
  558.         } catch (java.lang.NumberFormatException ex) {
  559.           char[] value = {getCharValue()};
  560.              charField.setText(new String(value));
  561.         }
  562.         return;
  563.      } 
  564.     }
  565.  
  566.     // Mouse Listener implementation
  567.     public void mousePressed(MouseEvent e) {}
  568.     public void mouseClicked(MouseEvent e) {}
  569.     public void mouseEntered(MouseEvent e) {}
  570.     public void mouseExited(MouseEvent e) {}
  571.     public void mouseReleased(MouseEvent e) {
  572.       if (e.getSource()==booleanField) {
  573.     boolean oldValue = booleanValue;
  574.     boolean newValue;
  575.     if (booleanValue) 
  576.       newValue=false;
  577.     else 
  578.       newValue = true;
  579.     booleanValue = newValue;
  580.     changes.firePropertyChange("booleanValue", new Boolean(oldValue), new Boolean(newValue));
  581.       }
  582.  
  583.     }
  584.     
  585.     // All the properties we publish
  586.     private int intValue;
  587.     private short shortValue;
  588.     private double doubleValue;
  589.     private float floatValue;
  590.     private char charValue;
  591.     private byte byteValue;
  592.     private boolean booleanValue;
  593.     private String stringValue;
  594.     private long longValue;
  595.  
  596.     // Indexed Properties
  597.     private int[] intArray;
  598.     private double[] doubleArray;
  599.     private String[] stringArray;
  600.     private float[] floatArray;
  601.     private char[] charArray;
  602.     private short[] shortArray;
  603.     private byte[] byteArray;
  604.     private boolean[] booleanArray;
  605.  
  606.     // All the widgets to display the properties
  607.     private TextField intField;
  608.     private TextField doubleField;
  609.     private TextField shortField;
  610.     private TextField floatField;
  611.     private TextField charField;
  612.     private TextField byteField;
  613.     private Checkbox  booleanField;
  614.     private TextField stringField;
  615.     private Button event1Button, event2Button, event3Button, event4Button;
  616.     private TextField array1, array2, array3, array4;
  617.  
  618.     // Some utility objects
  619.     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  620.     private VetoableChangeSupport vetos = new VetoableChangeSupport(this);
  621.     private java.util.Vector listeners = new java.util.Vector();
  622.     private java.util.Vector actionListeners = new java.util.Vector();
  623. }
  624.