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

  1.  
  2. package sun.beanbox;
  3.  
  4. import java.beans.*;
  5. import java.lang.reflect.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import java.util.Hashtable;
  9. import java.util.Vector;
  10.  
  11. public class EventTargetDialog extends Dialog implements Runnable, ActionListener {
  12.  
  13.     EventTargetDialog(Frame frame, Wrapper sourceWrapper, Wrapper targetWrapper,
  14.             EventSetDescriptor esd, Method listenerMethod) {
  15.     super(frame, "EventTargetDialog", false);
  16.     new WindowCloser(this);
  17.     setLayout(null);
  18.     this.esd = esd;
  19.     this.listenerMethod = listenerMethod;
  20.  
  21.     Vector matchMethods = new Vector();
  22.     Vector zeroArgMethods = new Vector();
  23.  
  24.     this.sourceWrapper = sourceWrapper;
  25.     this.targetWrapper = targetWrapper;
  26.     Object target = targetWrapper.getBean();
  27.  
  28.     try {
  29.  
  30.         // Search for target methods that either match the event
  31.         // listener signature or which have zero args and no results.
  32.  
  33.         MethodDescriptor mds[] = Introspector.getBeanInfo(target.getClass()).getMethodDescriptors();
  34.         Class eargs[] = listenerMethod.getParameterTypes();
  35.  
  36.         for (int i = 0; i < mds.length; i++) {
  37.         MethodDescriptor md = mds[i];
  38.             Class margs[] = md.getMethod().getParameterTypes();
  39.      
  40.         if (margs.length == 0 && 
  41.             md.getMethod().getReturnType() == Void.TYPE) {
  42.             zeroArgMethods.addElement(md);
  43.             continue;
  44.         }
  45.             if (eargs.length != margs.length) {
  46.             continue;
  47.             }
  48.             boolean match = true;
  49.             for (int j = 0; j < eargs.length; j++) {
  50.             if (!isSubclass(eargs[j], margs[j])) {
  51.                 match = false;
  52.                 break;
  53.             }
  54.             }
  55.         if (match) {
  56.             matchMethods.addElement(md);
  57.         }
  58.         }
  59.     } catch (Exception ex) {
  60.         new ErrorDialog(frame, "EventTargetDialog: Unexpected exception: \n" + ex);
  61.         return;
  62.     }
  63.  
  64.     int width = 300;
  65.  
  66.     sortMethods(matchMethods);
  67.     sortMethods(zeroArgMethods);
  68.     int count = matchMethods.size() + zeroArgMethods.size();
  69.     methods = new MethodDescriptor[count];
  70.  
  71.     for (int i = 0; i < matchMethods.size(); i++) {
  72.         methods[i] = (MethodDescriptor) matchMethods.elementAt(i);
  73.     }
  74.     for (int i = 0; i < zeroArgMethods.size(); i++) {
  75.         methods[i + matchMethods.size()] = (MethodDescriptor) zeroArgMethods.elementAt(i);
  76.     }
  77.  
  78.     if (count == 0) {
  79.         new ErrorDialog(frame, "No suitable target method on\n" + 
  80.                     target.getClass().getName());
  81.         return;
  82.     }
  83.  
  84.     int height = 200;
  85.  
  86.     Label l = new Label("Please chose a target method:", Label.CENTER);
  87.     l.setBounds(2, 30, width-4, 25);
  88.     add(l);
  89.  
  90.     list = new List(8, false);
  91.     for (int i = 0; i < methods.length; i++) {
  92.         list.add(methods[i].getName());
  93.     }
  94.     list.select(0);
  95.     list.setBounds(10, 60, width-20, height-60);
  96.     add(list);
  97.  
  98.     // Now do the "Cancel" and "OK" buttons.
  99.     height += 10;
  100.     cancelButton = new Button("Cancel");
  101.     cancelButton.addActionListener(this);
  102.     add(cancelButton);
  103.     cancelButton.setBounds((width/2)-70, height-5, 60, 30);
  104.  
  105.     okButton = new Button("OK");
  106.     okButton.addActionListener(this);
  107.     add(okButton);
  108.     okButton.setBounds((width/2)+10, height-5, 60, 30);
  109.     height += 55;
  110.  
  111.     list.setBounds(10, 60, width-20, height-130);
  112.  
  113.     int x = frame.getLocation().x + 30;
  114.     int y = frame.getLocation().y + 50;
  115.     setBounds(x, y, width, height);
  116.     show();
  117.     }
  118.  
  119.  
  120.     // Run is called in a sepaarte thread to actually complete a
  121.     // requested event hookup.
  122.  
  123.     public void run() {
  124.     int index = list.getSelectedIndex();
  125.  
  126.     // Remove the current dialog, and put up a status line.    
  127.     removeAll();
  128.     Label status = new Label("Generating and compiling adaptor class");
  129.     add(status);
  130.     status.setBounds(20, getSize().height/2, getSize().width-30, 25);
  131.     repaint();
  132.  
  133.     HookupManager.hookup(esd, listenerMethod, sourceWrapper,
  134.             targetWrapper, methods[index].getMethod());
  135.  
  136.     dispose();
  137.     }
  138.  
  139.     public void actionPerformed(ActionEvent evt) {
  140.     if (evt.getSource() == okButton) {
  141.         Thread th = new Thread(this);
  142.         th.start();
  143.     } else if (evt.getSource() == cancelButton) {
  144.         dispose();
  145.     }
  146.     }
  147.  
  148.     /**
  149.       * Do a simple bubble sort on a Vector of MethodDescriptors.
  150.       */
  151.     private void sortMethods(Vector methods) {
  152.     for (int i = methods.size()-2; i >= 0; i--) {
  153.         for (int j = 0; j <= i; j++) {
  154.         String s1 = ((MethodDescriptor)methods.elementAt(j)).getName();
  155.         String s2 = ((MethodDescriptor)methods.elementAt(j+1)).getName();
  156.         if (s1.compareTo(s2) > 0) {
  157.            Object tmp = methods.elementAt(j);
  158.            methods.setElementAt(methods.elementAt(j+1), j);
  159.            methods.setElementAt(tmp, j+1);
  160.         }
  161.         }
  162.     }
  163.     }
  164.  
  165.     /**
  166.      * Return true if class a is either equivalent to class b, or
  167.      * if class a is a subclass of class b.
  168.      * Note tht either or both "Class" objects may represent interfaces.
  169.      */
  170.     static boolean isSubclass(Class a, Class b) {
  171.     // We rely on the fact that for any given java class or
  172.         // primtitive type there is a unqiue Class object, so
  173.     // we can use object equivalence in the comparisons.
  174.     if (a == b) {
  175.         return true;
  176.     }
  177.     if (a == null || b == null) {
  178.         return false;
  179.     }
  180.     for (Class x = a; x != null; x = x.getSuperclass()) {
  181.         if (x == b) {    
  182.         return true;
  183.         }
  184.         if (b.isInterface()) {
  185.         Class interfaces[] = x.getInterfaces();
  186.         for (int i = 0; i < interfaces.length; i++) {
  187.             if (interfaces[i] == b) {
  188.             return true;
  189.             }
  190.         }
  191.         }
  192.     }
  193.     return false;
  194.     }
  195.  
  196.     private Button okButton;
  197.     private Button cancelButton;
  198.     private List list;
  199.  
  200.     private EventSetDescriptor esd;
  201.     private Method listenerMethod;
  202.     private Wrapper sourceWrapper;
  203.     private Wrapper targetWrapper;
  204.     private MethodDescriptor methods[];
  205. }
  206.