home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / os2 / edm2 / 0506 / grinding2.java < prev    next >
Encoding:
Java Source  |  1997-06-01  |  6.5 KB  |  178 lines

  1. package GUITools;
  2.  
  3. import java.AWT.*;
  4. import java.AWT.event.*;
  5. import java.util.Vector;
  6.  
  7. /**
  8.  * Docable toolbar class written by Shai Almog 1996.
  9.  * revised for JDK 1.1 in 4/97.
  10.  * The source is in the public domain and may be modified and used freely.
  11.  * It is not a requirment but I would consider it good manners if you credit me
  12.  * and EDM/2 (www.edm2.com) in applications in which this code was used ;-)
  13.  * The Dockable toolbar class creats a toolbar container that can be torn
  14.  * off the Frame holding it and left to hover above it.
  15.  * It's based on the panel and Window classes and it must be put
  16.  * on a frame.
  17.  * A problem with the Dockable toolbar is the way different implementations
  18.  * of the JDK treat the Window class. This creates a highly unportable
  19.  * implementation, the solution was to abstract everything and hope that
  20.  * a future implementation of the JDK solves this problem.
  21. **/
  22.  
  23. public class DockableToolbar extends
  24. java.AWT.Panel
  25.     implements java.AWT.event.ActionListener
  26. {
  27.     public DockableToolbar(Frame Owner) // Owner is the component who owns the Toolbar.
  28.     {
  29.           this.Owner = Owner; // This is an easy trick to avoid confusion with parameters
  30.               // This way you don't have to invent a name for every parameter.
  31.  
  32.           setLayout(new FlowLayout()); // The flow layout just puts the Components
  33.                 // one after the other as best as it can. No better is needed here.
  34.  
  35.           addToolbarToOwner();  // This method will add the panel and the frame to
  36.     }
  37.  
  38.     public void actionPerformed(ActionEvent E) // This method get calls by the action listener.
  39.      {
  40.           if(E.getSource() instanceof Button) // If the event source is a button
  41.           sendEvent(whichButtonGotPressed((Button)E.getSource())); // Send an event with the
  42.              // serial numeber of the button.This enables us to create the buttons in the
  43.              // toolbar and not in the program, to further simplify the programmers life ....
  44.      }
  45.  
  46.     /**
  47.      * This method allows an object to listen to events dispatched by the toolbar,
  48.      * in a similar way to listening to events dispatched by buttons.
  49.     **/
  50.     public void addActionListener(ActionListener listener)
  51.     {
  52.          listOfActionListeners.addElement(listener);
  53.     }
  54.  
  55.     public void addButton(Button B) // This method add's a button object to the toolbar.
  56.     {
  57.          add(B);     // Add the button to the Panel.
  58.          listOfButtons.addElement((Object)B);
  59.          B.addActionListener(this); // Listen to button related events.
  60.     }
  61.  
  62.     public void addButton(String caption) // This method creats a new button and adds it.
  63.      {
  64.           addButton(new Button(caption));
  65.      }
  66.  
  67.      private void addToolbarToOwner() // This method add's the panel to the Frame.
  68.      {
  69.           Owner.add(this);
  70.      }
  71.  
  72.     /**
  73.      * This method, removes the toolbar from the docking position and makes it
  74.      * hover in the air above the toolbar.
  75.     **/
  76.     public void detachToolbar()
  77.     {
  78.           detachedToolbar = new Window(Owner); // Here we create a window object
  79.                    // above the Frame.
  80.  
  81.           detachedToolbar.setLayout(new BorderLayout()); // We will only have one
  82.  
  83.                   // component in the frame (the Panel), but it's best to do it this way.
  84.  
  85.           detachedToolbar.add("South", this); // We add this (the Panel) to the frame yet keep
  86.                   // it hidden.
  87.  
  88.           Owner.remove(this); // We remove the Panel from the Frame.
  89.  
  90.           systemSpecificToolbarCalibration(); // This part was moved to a separate
  91.                  // function do to problems with VA-Java.
  92.  
  93.           detachedToolbar.show(); // Show the toolbar.
  94.           Owner.pack(); // Sort the items in the window where the toolbar was removed from.
  95.      }
  96.  
  97.      /**
  98.       * This method takes a Toolbar that is hovering and
  99.       * dock's it into place.
  100.      **/
  101.      public void dockToolbar()
  102.      {
  103.           Owner.add(this); // Return the Panel into the frame.
  104.           Owner.pack();    // Sort the items in the Frame.
  105.           detachedToolbar.removeAll(); // Remove the panel from the toolbar.
  106.           detachedToolbar.dispose();   // Destroy the toolbar.
  107.      }
  108.  
  109.     /**
  110.      * This method sends button events to all of the listeners.
  111.     **/
  112.      private void sendEvent(int idOfTheButtonPressed)
  113.     {
  114.          ActionListener currentListener;
  115.          ActionEvent E = new
  116.            ActionEvent(listOfButtons.elementAt(idOfTheButtonPressed),
  117.            ActionEvent.ACTION_PERFORMED,
  118.            Integer.toString(idOfTheButtonPressed));
  119.  
  120.          for (int counter = 0; counter < listOfActionListeners.size() ; counter++)
  121.          {
  122.  
  123.             currentListener = (ActionListener)listOfActionListeners.elementAt(counter);
  124.             currentListener.actionPerformed(E);
  125.          }
  126.      }
  127.  
  128.      private int whichButtonGotPressed(Button B)
  129.      { // This method returns the Buttons offset in the vector.
  130.           for (int counter = 0; counter < listOfButtons.size() ; counter++)
  131.              if(listOfButtons.elementAt(counter).equals(B)) return(counter);
  132.          return(-1);
  133.      }
  134.  
  135.      private void systemSpecificToolbarCalibration()
  136.      {
  137.           Dimension windowSize = detachedToolbar.getSize();
  138.           buttonSize = ((Component)listOfButtons.elementAt(0)).getSize();
  139.           detachedToolbarTitleBar = new ToolbarTitleBar(windowSize.width,3);
  140.  
  141.           detachedToolbar.add("North", detachedToolbarTitleBar);
  142.           detachedToolbar.pack();
  143.      }
  144.  
  145.      private  Window detachedToolbar;
  146.      private  ToolbarTitleBar detachedToolbarTitleBar;
  147.      private  Button exitButton = new Button("Exit");
  148.      private  Label helloAWTLabel = new Label("Hello AWT");
  149.      private  Vector listOfActionListeners = new Vector();
  150.      private  Vector listOfButtons = new Vector();
  151.      private  Frame Owner;
  152. }
  153.  
  154.  
  155. /**
  156.  * This class is for internal use only. It paints the title on top of
  157.  * the detached window.
  158. **/
  159. class ToolbarTitleBar extends Canvas
  160. {
  161.     ToolbarTitleBar(int width, int height)
  162.     {
  163.         super();
  164.         setSize(width,height); // Canvas on the window top in 3 pixel height.
  165.         this.height = height;
  166.         this.width  = width;
  167.     }
  168.  
  169.     public void paint(Graphics g)
  170.     {
  171.         g.setColor(SystemColor.activeCaption); // The SystemColor class
  172.                    // contains colors specific to this system.
  173.         g.fillRect(0,0,width,i);
  174.     }
  175.  
  176.     private int height,width;
  177. }
  178.