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

  1.  
  2. package sun.beanbox;
  3.  
  4. /**
  5.  * The ToolBox is a panel that shows icons and ID strings
  6.  * for the available JavaBeans in the current BeanBoxFrame.
  7.  */
  8.  
  9. import java.beans.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.io.*;
  13. import java.util.Vector;
  14.  
  15. // We currently keep all the contents of the ToolBox in a separate
  16. // ToolBoxPanel object, to workaround win32 bug #4028421   KGH 1/28/97
  17.  
  18. class ToolBox extends Frame {
  19.     private ToolBoxPanel panel;
  20.  
  21.     ToolBox(int x, int y) {
  22.     super("ToolBox");
  23.  
  24.     setLayout(null);
  25.     setBackground(Color.lightGray);
  26.     setFont(new Font("Dialog", Font.PLAIN, 10));
  27.  
  28.     panel = new ToolBoxPanel(this);
  29.     add(panel);
  30.     panel.setLocation(0,0);
  31.  
  32.     setLocation(x, y);
  33.     show();
  34.     }
  35.  
  36.     Vector getLoadedJarInfo() {
  37.     return panel.getLoadedJarInfo();
  38.     }
  39.  
  40.     void addBeansInJar(String jarFile) {
  41.     panel.addBeansInJar(jarFile);
  42.     }
  43. }
  44.  
  45. class ToolBoxPanel extends Panel implements Runnable, MouseListener {
  46.  
  47.     ToolBoxPanel(Frame frame) {
  48.     this.frame = frame;
  49.  
  50.  
  51.     setLayout(null);
  52.     setBackground(Color.lightGray);
  53.     setFont(new Font("Dialog", Font.PLAIN, 10));
  54.     addMouseListener(this);
  55.  
  56.     // addBeanClass("sun.beanbox.BeanBox"); -- buggy and not fully implemented
  57.  
  58.     if (! BeanBoxFrame.getQuickStart()) {
  59.         Vector jarNames = getJarNames();    
  60.         for (int i = 0; i < jarNames.size(); i++) {
  61.             String name = (String)jarNames.elementAt(i);
  62.             addBeansInJar(name);
  63.         }
  64.     }
  65.  
  66.     insertThread = new Thread(this);
  67.     insertThread.start();
  68.     }
  69.  
  70.     // keep it with package scope for the inner class' sake
  71.     void addWithUniqueName(Vector v, String s) {
  72.     if (! v.contains(s)) {
  73.         debug("adding "+s);
  74.         v.addElement(s);
  75.         return;
  76.     }
  77.     int count = 2;
  78.     while (v.contains(s+"<"+count+">")) {
  79.         count += 1;
  80.     }
  81.     debug("adding "+s+"<"+count+">");
  82.     v.addElement(s+"<"+count+">");
  83.     return;
  84.     }
  85.  
  86.     private class Helper implements DoOnBean {
  87.     public void action(JarInfo ji, BeanInfo bi, String beanName) {
  88.         String label;
  89.         Image img = null;
  90.  
  91.         BeanDescriptor bd = bi.getBeanDescriptor();
  92.         debug("Helper.action; beanName: "+beanName);
  93.         debug("  beanName:  "+beanName);
  94.         debug("  getName(): "+bd.getName());
  95.         debug("  getBeanClass(): "+bd.getBeanClass());
  96.  
  97.         Class beanClass = bd.getBeanClass();
  98.         if (beanName.equals(beanClass.getName())) {
  99.         label = bi.getBeanDescriptor().getDisplayName();
  100.         img = bi.getIcon(BeanInfo.ICON_COLOR_16x16);
  101.         } else {
  102.             label = beanName;
  103.         int ix = beanName.lastIndexOf('.');
  104.         if (ix >= 0) {
  105.             label = beanName.substring(ix+1);
  106.         }
  107.         img = null;
  108.         }
  109.         addWithUniqueName(beanLabels, label);
  110.         beanNames.addElement(beanName);
  111.         beanIcons.addElement(img);
  112.         beanJars.addElement(ji);
  113.     }
  114.     public void error(String msg) {
  115.         ToolBoxPanel.this.error(msg);
  116.     }
  117.     public void error(String msg, Exception ex) {
  118.         ToolBoxPanel.this.error(msg, ex);
  119.     }
  120.     }
  121.  
  122.     private Helper helper = new Helper();
  123.  
  124.     /**
  125.      * Add all the beans in a Jar file into the ToolBox
  126.      */
  127.     synchronized void addBeansInJar(String jarFile) {
  128.     JarLoader.loadJarDoOnBean(jarFile, helper);
  129.     doLayout();
  130.     }
  131.  
  132.     /**
  133.      * Special action.
  134.      *
  135.      * Currently only used to register the BeanBox
  136.      */
  137.     synchronized void addBeanClass(String beanClassName) {
  138.     Class beanClass;
  139.     try {
  140.         beanClass = Class.forName(beanClassName);
  141.     } catch (Exception ex) {
  142.         System.err.println("Toolbox: couldn't instantiate " +
  143.                                  beanClassName);
  144.         return;
  145.     }
  146.     BeanInfo bi;
  147.     try {
  148.         bi = Introspector.getBeanInfo(beanClass);
  149.     } catch (Exception ex) {
  150.         System.err.println("Toolbox: couldn't find BeanInfo for " + 
  151.                                 beanClassName);
  152.         ex.printStackTrace();
  153.         return;
  154.     }
  155.     String label = bi.getBeanDescriptor().getDisplayName();
  156.     Image img = bi.getIcon(BeanInfo.ICON_COLOR_16x16);
  157.     addWithUniqueName(beanLabels, label);
  158.     beanNames.addElement(beanClassName);
  159.     beanIcons.addElement(img);
  160.     beanJars.addElement(null);
  161.     doLayout();
  162.     }
  163.  
  164.     public void doLayout() {
  165.     topPad = frame.getInsets().top;
  166.     sidePad = frame.getInsets().left;
  167.  
  168.     int height = topPad + (rowHeight * beanLabels.size()) 
  169.             + 6 + frame.getInsets().bottom;
  170.         if (height != getSize().height) {
  171.         setSize(145, height);
  172.         frame.setSize(145, height);
  173.     }
  174.     }
  175.  
  176.     public void paint(Graphics g) {
  177.     for (int i = 0; i < beanLabels.size(); i++) {
  178.         String name = (String)beanLabels.elementAt(i);
  179.         g.drawString(name, sidePad + 21, topPad + (rowHeight*i) + rowHeight-4);
  180.         Image img = (Image)beanIcons.elementAt(i);
  181.         if (img != null) {
  182.             boolean status =
  183.             g.drawImage(img, sidePad+2, topPad + (rowHeight*i) + 2, 16, 16, this);
  184.         }
  185.     }
  186.     }
  187.  
  188.     public void run() {
  189.     // We run an internal thread to handle insertions, one at a time.
  190.     for (;;) {
  191.         Object bean;
  192.         String beanLabel;
  193.         String beanName;
  194.         synchronized (this) {
  195.         // Wait for a bean to be ready for insertion.
  196.         while (pendingBean == null) {
  197.             try {
  198.             wait();
  199.             } catch (InterruptedException ix) {
  200.             System.err.println("ToolBox.run: unexpected interrupt");
  201.             }
  202.         }
  203.         bean = pendingBean;
  204.         beanLabel = pendingBeanLabel;
  205.         beanName = pendingBeanName;
  206.         }
  207.  
  208.         // Figure our the current BeanBox.
  209.         BeanBox box = BeanBoxFrame.getCurrentBeanBox();
  210.  
  211.             // Change the cursor to indicate an "insert".
  212.         frame.setCursor(crosshairCursor);
  213.  
  214.         // do the insertion.
  215.         box.doInsert(bean, beanLabel, beanName, false);
  216.  
  217.         // Clear any pending bean.  This will normally be the bean we
  218.         // just inserted, but we also want to clear any bogus insert
  219.         // that has been queued since we started.
  220.         pendingBean = null;
  221.  
  222.         frame.setCursor(defaultCursor);
  223.     }
  224.     }
  225.  
  226.     /**
  227.      * Provide information on all the loaded JAR files, regardless of origin.
  228.      */
  229.     public Vector getLoadedJarInfo() {
  230.     return beanJars;
  231.     }
  232.  
  233.     private static Vector getJarNames() {
  234.     File cwd = new File(System.getProperty("user.dir"));
  235.     File pwd = new File(cwd.getParent());
  236.     File jars = new File(pwd, "jars");
  237.  
  238.     if (! jars.isDirectory()) {
  239.         System.err.println(jars+" is not a directory!!");
  240.     }
  241.  
  242.     Vector result = new Vector();
  243.     String names[];
  244.     names = jars.list(new FileExtension(".jar"));
  245.     for (int i=0; i<names.length; i++) {
  246.         result.addElement(jars.getPath() + File.separatorChar + names[i]);
  247.     }
  248.     names = jars.list(new FileExtension(".JAR"));
  249.     for (int i=0; i<names.length; i++) {
  250.         result.addElement(jars.getPath() + File.separatorChar + names[i]);
  251.     }
  252.  
  253.     return result;
  254.     }
  255.  
  256.  
  257.     //----------------------------------------------------------------------
  258.  
  259.     // Mouse listener methods for this panel.
  260.  
  261.     public void mouseClicked(MouseEvent evt) {
  262.     }
  263.  
  264.     public void mousePressed(MouseEvent evt) {
  265.     int row = (evt.getY() - topPad)/rowHeight;
  266.  
  267.     Object bean;
  268.  
  269.     String beanName = (String) beanNames.elementAt(row);
  270.     String beanLabel = (String) beanLabels.elementAt(row);
  271.     if (beanName.equals("sun.beanbox.BeanBox")) {
  272.         bean = new BeanBox();
  273.     } else {
  274.         JarInfo ji = (JarInfo) beanJars.elementAt(row);
  275.         try {
  276.         bean = ji.getInstance(beanName);
  277.         } catch (Exception ex) {
  278.         error("instantion of a new bean failed", ex);
  279.         return;
  280.         }
  281.     }
  282.  
  283.     // Wakeup the insert thread to insert the new bean.
  284.     synchronized (this) {
  285.         pendingBean = bean;
  286.         pendingBeanLabel = beanLabel;
  287.         pendingBeanName = beanName;
  288.         notifyAll();
  289.     }
  290.     }
  291.  
  292.     public void mouseReleased(MouseEvent evt) {
  293.     }
  294.  
  295.     public void mouseEntered(MouseEvent evt) {
  296.     }
  297.  
  298.     public void mouseExited(MouseEvent evt) {
  299.     }
  300.  
  301.     public void mouseDragged(MouseEvent evt) {
  302.     }
  303.  
  304.     public void mouseMoved(MouseEvent evt) {
  305.     }
  306.  
  307.     //----------------------------------------------------------------------
  308.  
  309.     public void error(String message, Exception ex) {
  310.     String mess = message + ":\n" + ex;
  311.     System.err.println(message);
  312.     ex.printStackTrace();
  313.     // Popup an ErrorDialog with the given error message.
  314.     new ErrorDialog(frame, mess);
  315.  
  316.     }
  317.  
  318.     public void error(String message) {
  319.     String mess = message;
  320.     System.err.println(message);
  321.     // Popup an ErrorDialog with the given error message.
  322.     new ErrorDialog(frame, mess);
  323.  
  324.     }
  325.  
  326.     static  void debug(String msg) {
  327.     if (debug) {
  328.         System.err.println("ToolBox:: "+msg);
  329.     }
  330.     }
  331.  
  332.     Vector beanLabels = new Vector();
  333.     Vector beanNames = new Vector();
  334.     Vector beanIcons = new Vector();
  335.     Vector beanJars = new Vector();
  336.     private int topPad = 0;
  337.     private int sidePad = 0;
  338.     private final static int rowHeight = 20;
  339.     private Thread insertThread = null;
  340.     private static boolean debug = false;
  341.     private Object pendingBean;
  342.     private String pendingBeanLabel;
  343.     private String pendingBeanName;
  344.     private Frame frame;
  345.  
  346.     private static Cursor crosshairCursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
  347.     private static Cursor defaultCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  348. }
  349.