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

  1.  
  2. /**
  3.  * A BeanBoxFrame acts as the top-level "frame" that contains a BeanBox.
  4.  * <p>
  5.  * The BeanBoxFrame manages the frame's menubar and keeps track of which
  6.  * bean currently has the focus.
  7.  * <p>
  8.  * Note that there is an asumption that there is only one BeanBoxFrame
  9.  * per application, so various method/fields are static.
  10.  */
  11.  
  12. package sun.beanbox;
  13.  
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import java.util.*;
  17. import java.beans.*;
  18.  
  19. class BeanBoxFrame extends Frame implements LayoutManager, Runnable, ActionListener {
  20.  
  21.     private static String tmpDir = "tmp";
  22.     private static boolean doShowTimes = false;
  23.     private static Thread focusThread;
  24.     private static Component nextFocus;
  25.     private static BeanBoxFrame instance;
  26.     private static String clipDir = "tmp";
  27.     private static String clipFile = "beanBoxClip.ser";
  28.     private static String clipResource = "beanBoxClip";
  29.     private static String versionID = "BDK1.0 - September 1997";
  30.     private static String clipLabel;
  31.     private static String clipName;
  32.     private static boolean quickStart = false;
  33.     private static boolean defineOnDemand = true;
  34.  
  35.     public static void main(String argv[]) {
  36.         Timer tim = new Timer();
  37.  
  38.     if (argv.length > 1) {
  39.         throw new Error("Bad args");
  40.     }
  41.     for (int i=0; i<argv.length; i++) {
  42.         if (argv[i].equals("-version")) {
  43.         System.out.println("BeanBox version: "+versionID);
  44.         System.exit(0);
  45.         } else if (argv[i].equals("-quick")) {
  46. System.err.println("quick starting...");
  47.         quickStart = true;
  48.         } else { // undocumented? 
  49.         tmpDir = argv[0];
  50.         }
  51.     }
  52.     Beans.setDesignTime(true);
  53.  
  54.     // Popup a "we're starting" frame.
  55.         Timer tim2 = new Timer();
  56.         Frame starter = new StartFrame();
  57.  
  58.     if (showTimes()) {
  59.         System.err.println("awt startup => " + tim2.elapsed());
  60.     }
  61.  
  62.     new BeanBoxFrame();
  63.  
  64.     if (showTimes()) {
  65.         System.err.println("total startup time => " + tim.elapsed());
  66.     }
  67.  
  68.     starter.dispose();
  69.  
  70.     }
  71.  
  72.     /**
  73.      * Create a new BeanBoxFrame at a default screen location.
  74.      */
  75.  
  76.     public BeanBoxFrame() {
  77.  
  78.     super("BeanBox");
  79.  
  80.     // there should only be one instance of this class.    
  81.     if (instance != null) {
  82.         throw new Error("Attempt to create multiple instances of BeanBoxFrame");
  83.     }
  84.     instance = this;
  85.  
  86.     setLayout(null);
  87.     setBackground(Color.lightGray);
  88.     setMenuBar(new MenuBar());
  89.  
  90.     // Timing note: the setFont causes AWT to initialiie itself,
  91.     // so a large chunk of time happens here.
  92.     setFont(new Font("Dialog", Font.PLAIN, 10));
  93.  
  94.         Timer tim = new Timer();
  95.     toolBox = new ToolBox(20, 20);
  96.     new WindowCloser(toolBox, true);
  97.  
  98.     if (showTimes()) {
  99.         System.err.println("new Toolbox => " + tim.elapsed());
  100.         tim.reset();
  101.     }
  102.  
  103.     topBox = new BeanBox();
  104.     topBox.setBackground(Color.lightGray);
  105.     topWrapper = new Wrapper(topBox, "BeanBox", "sun.beanbox.BeanBox");
  106.     topWrapper.setBackground(Color.lightGray);
  107.     add(topWrapper);
  108.  
  109.     if (showTimes()) {
  110.         System.err.println("new BeanBox => " + tim.elapsed());
  111.         tim.reset();
  112.     }
  113.  
  114.     doSetCurrentFocus(topWrapper);
  115.     topBox.setSize(100,100);
  116.     setLayout(this);
  117.  
  118.     setBounds(170, 20, 400, 550);
  119.     new WindowCloser(this, true);
  120.     show();
  121.  
  122.         propertySheet = new PropertySheet(topWrapper, 575, 20);
  123.     new WindowCloser(propertySheet, true);
  124.  
  125.     toolBox.show();
  126.  
  127.     // Create a thread to handle focus changes.
  128.     focusThread = new Thread(this);
  129.     focusThread.start();
  130.     }
  131.  
  132.     private boolean inBeanBox(Component c) {
  133.     while (c != null) {
  134.         if (c instanceof BeanBox) {
  135.         return true;
  136.         }
  137.             c = c.getParent();
  138.     }
  139.     return false;
  140.     }
  141.  
  142.     public void actionPerformed(ActionEvent evt) {
  143.     // Menu actions come here,
  144.     // We forward them to the current BeanBox.
  145.         getCurrentBeanBox().queueMenuItem(evt);
  146.     }
  147.  
  148.     static Object getCurrentBean() {
  149.     return currentFocus.getBean();
  150.     }
  151.  
  152.     static Component getCurrentComponent() {
  153.     return currentFocus.getChild();
  154.     }
  155.  
  156.  
  157.     static Wrapper getCurrentWrapper() {
  158.     return currentFocus;
  159.     }
  160.  
  161.     static BeanBox getCurrentBeanBox() {
  162.     Component c = getCurrentComponent();
  163.     for (;;) {
  164.         if (c == null) {
  165.             System.err.println("No BeanBox in focus???");
  166.         return null;
  167.         }
  168.         if (c instanceof BeanBox) {
  169.         return (BeanBox) c;
  170.         }
  171.         c = c.getParent();
  172.     }
  173.     }
  174.  
  175.  
  176.    static void setCurrentComponent(Component focus) {
  177.     // Null means focus on the top-level beanbox.
  178.     if (focus == null) {
  179.         focus = topWrapper;
  180.     }
  181.     // Wakeup our internal thread to do the focus change.
  182.     synchronized (instance) {
  183.         nextFocus = focus;
  184.         instance.notifyAll();
  185.     }    
  186.     }
  187.  
  188.     // Internal thread to handle focus changes.  We use a separate
  189.     // thread for this to offload work from the event thread in order
  190.     // to avoid deadlocks.
  191.     public void run() {
  192.     for (;;) {
  193.         Component bean;
  194.         synchronized (this) {
  195.         while (nextFocus == null) {
  196.             try {
  197.                 wait();
  198.             } catch (Exception ex) {
  199.             System.err.println("Unexpected interrupt in focus thread");
  200.             }
  201.         }
  202.         bean = nextFocus;
  203.         nextFocus = null;
  204.         }
  205.         doSetCurrentFocus(bean);
  206.     }
  207.     }
  208.  
  209.     private void doSetCurrentFocus(Component focus) {
  210.     Wrapper target = Wrapper.findWrapper(focus);
  211.  
  212.     if (target == currentFocus) {
  213.         return;
  214.     }
  215.     
  216.     // Deactivate the old Wrapper.
  217.     if (currentFocus != null) {
  218.         currentFocus.deactivate();
  219.     }
  220.  
  221.     currentFocus = target;
  222.  
  223.         // Activate the new Wrapper.
  224.     currentFocus.activate();
  225.  
  226.     // Find the nearest surrounding BeanBox and use its menubar.
  227.     for (Component c = target.getChild(); c != null; c = c.getParent()) {
  228.         if (c instanceof BeanBox) {
  229.         BeanBox hdr = (BeanBox) c;
  230.         hdr.updateMenuBar(getMenuBar());
  231.         break;
  232.         }
  233.     }
  234.  
  235.     // Point the property sheet at the new focus.
  236.     if (propertySheet != null) {
  237.         propertySheet.setTarget(getCurrentWrapper());
  238.     }
  239.     }
  240.  
  241.     public void setCustomizer(Customizer c) {
  242.     if (propertySheet != null) {
  243.         propertySheet.setCustomizer(c);
  244.     }
  245.     }
  246.  
  247.     public void dispose() {
  248.     System.exit(0);
  249.     }
  250.  
  251.     public static String getClipDir() {
  252.     return clipDir;
  253.     }
  254.  
  255.     public static String getClipFileName() {
  256.     if (clipDir != null) {
  257.         return clipDir+java.io.File.separator+clipFile;
  258.     } else {
  259.         return clipFile;
  260.     }
  261.     }
  262.  
  263.     // This method is currently unused
  264.     public static String getClipResource() {
  265.     return clipResource;
  266.     }
  267.  
  268.     public static String getTmpDir() {
  269.     return tmpDir;
  270.     }
  271.  
  272.     public static void setClipName(String name) {
  273.         clipName = name;
  274.     }
  275.  
  276.     public static String getClipName() {
  277.         return clipName;
  278.     }
  279.  
  280.     public static void setClipLabel(String label) {
  281.         clipLabel = label;
  282.     }
  283.  
  284.     public static String getClipLabel() {
  285.         return clipLabel;
  286.     }
  287.  
  288.     public static boolean getQuickStart() {
  289.     return quickStart;
  290.     }
  291.  
  292.     public static boolean getDefineOnDemand() {
  293.     return defineOnDemand;
  294.     }
  295.  
  296.     //----------------------------------------------------------------------
  297.     // Layout related stuff.
  298.     //----------------------------------------------------------------------
  299.  
  300.     public void addLayoutComponent(String name, Component comp) {
  301.     }
  302.  
  303.     public void removeLayoutComponent(Component comp) {
  304.     }
  305.  
  306.     public Dimension preferredLayoutSize(Container parent) {
  307.     return new Dimension(400,300);
  308.     }
  309.  
  310.     public Dimension minimumLayoutSize(Container parent) {
  311.     return new Dimension(100,100);
  312.     }
  313.  
  314.     public void layoutContainer(Container parent) {
  315.     Dimension d = parent.getSize();
  316.     Insets ins = parent.getInsets();
  317.     int width = d.width - (ins.left + ins.right);
  318.     int height = d.height - (ins.top + ins.bottom);
  319.     topWrapper.setBounds(ins.left, ins.top, width, height);
  320.     topWrapper.invalidate();
  321.     topWrapper.doLayout();
  322.     }
  323.  
  324.     public static BeanBox getTopBox() {
  325.     return topBox;
  326.     }   
  327.  
  328.     public static Wrapper getTopWrapper() {
  329.     return topWrapper;
  330.     }   
  331.  
  332.     public static ToolBox getToolBox() {
  333.     return toolBox;
  334.     }
  335.  
  336.     public static boolean showTimes() {
  337.     return doShowTimes;
  338.     }
  339.  
  340.     public static boolean getHideInvisibleBeans() {
  341.     return hideInvisibleBeans;
  342.     }
  343.  
  344.     public static void setHideInvisibleBeans(boolean hide) {
  345.     hideInvisibleBeans = hide;
  346.     Wrapper.showInvisibleBeans(!hide);
  347.     }
  348.  
  349.     public static void setDesignTime(boolean designMode) {
  350.     Beans.setDesignTime(designMode);
  351.     if (designMode) {
  352.         toolBox.setVisible(true);
  353.         propertySheet.setVisible(true);
  354.     } else {
  355.         toolBox.setVisible(false);
  356.         propertySheet.setVisible(false);
  357.     }
  358.     }
  359.  
  360.     public static String getVersionID() {
  361.     return versionID;
  362.     }
  363.  
  364.     //----------------------------------------------------------------------
  365.  
  366.     private static BeanBox topBox;
  367.     private static Wrapper topWrapper;
  368.  
  369.     private static Wrapper currentFocus;
  370.  
  371.     private static PropertySheet propertySheet;
  372.     private static ToolBox toolBox;
  373.  
  374.     private static boolean hideInvisibleBeans;    
  375. }
  376.