home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-01 | 6.5 KB | 178 lines |
- package GUITools;
-
- import java.AWT.*;
- import java.AWT.event.*;
- import java.util.Vector;
-
- /**
- * Docable toolbar class written by Shai Almog 1996.
- * revised for JDK 1.1 in 4/97.
- * The source is in the public domain and may be modified and used freely.
- * It is not a requirment but I would consider it good manners if you credit me
- * and EDM/2 (www.edm2.com) in applications in which this code was used ;-)
- * The Dockable toolbar class creats a toolbar container that can be torn
- * off the Frame holding it and left to hover above it.
- * It's based on the panel and Window classes and it must be put
- * on a frame.
- * A problem with the Dockable toolbar is the way different implementations
- * of the JDK treat the Window class. This creates a highly unportable
- * implementation, the solution was to abstract everything and hope that
- * a future implementation of the JDK solves this problem.
- **/
-
- public class DockableToolbar extends
- java.AWT.Panel
- implements java.AWT.event.ActionListener
- {
- public DockableToolbar(Frame Owner) // Owner is the component who owns the Toolbar.
- {
- this.Owner = Owner; // This is an easy trick to avoid confusion with parameters
- // This way you don't have to invent a name for every parameter.
-
- setLayout(new FlowLayout()); // The flow layout just puts the Components
- // one after the other as best as it can. No better is needed here.
-
- addToolbarToOwner(); // This method will add the panel and the frame to
- }
-
- public void actionPerformed(ActionEvent E) // This method get calls by the action listener.
- {
- if(E.getSource() instanceof Button) // If the event source is a button
- sendEvent(whichButtonGotPressed((Button)E.getSource())); // Send an event with the
- // serial numeber of the button.This enables us to create the buttons in the
- // toolbar and not in the program, to further simplify the programmers life ....
- }
-
- /**
- * This method allows an object to listen to events dispatched by the toolbar,
- * in a similar way to listening to events dispatched by buttons.
- **/
- public void addActionListener(ActionListener listener)
- {
- listOfActionListeners.addElement(listener);
- }
-
- public void addButton(Button B) // This method add's a button object to the toolbar.
- {
- add(B); // Add the button to the Panel.
- listOfButtons.addElement((Object)B);
- B.addActionListener(this); // Listen to button related events.
- }
-
- public void addButton(String caption) // This method creats a new button and adds it.
- {
- addButton(new Button(caption));
- }
-
- private void addToolbarToOwner() // This method add's the panel to the Frame.
- {
- Owner.add(this);
- }
-
- /**
- * This method, removes the toolbar from the docking position and makes it
- * hover in the air above the toolbar.
- **/
- public void detachToolbar()
- {
- detachedToolbar = new Window(Owner); // Here we create a window object
- // above the Frame.
-
- detachedToolbar.setLayout(new BorderLayout()); // We will only have one
-
- // component in the frame (the Panel), but it's best to do it this way.
-
- detachedToolbar.add("South", this); // We add this (the Panel) to the frame yet keep
- // it hidden.
-
- Owner.remove(this); // We remove the Panel from the Frame.
-
- systemSpecificToolbarCalibration(); // This part was moved to a separate
- // function do to problems with VA-Java.
-
- detachedToolbar.show(); // Show the toolbar.
- Owner.pack(); // Sort the items in the window where the toolbar was removed from.
- }
-
- /**
- * This method takes a Toolbar that is hovering and
- * dock's it into place.
- **/
- public void dockToolbar()
- {
- Owner.add(this); // Return the Panel into the frame.
- Owner.pack(); // Sort the items in the Frame.
- detachedToolbar.removeAll(); // Remove the panel from the toolbar.
- detachedToolbar.dispose(); // Destroy the toolbar.
- }
-
- /**
- * This method sends button events to all of the listeners.
- **/
- private void sendEvent(int idOfTheButtonPressed)
- {
- ActionListener currentListener;
- ActionEvent E = new
- ActionEvent(listOfButtons.elementAt(idOfTheButtonPressed),
- ActionEvent.ACTION_PERFORMED,
- Integer.toString(idOfTheButtonPressed));
-
- for (int counter = 0; counter < listOfActionListeners.size() ; counter++)
- {
-
- currentListener = (ActionListener)listOfActionListeners.elementAt(counter);
- currentListener.actionPerformed(E);
- }
- }
-
- private int whichButtonGotPressed(Button B)
- { // This method returns the Buttons offset in the vector.
- for (int counter = 0; counter < listOfButtons.size() ; counter++)
- if(listOfButtons.elementAt(counter).equals(B)) return(counter);
- return(-1);
- }
-
- private void systemSpecificToolbarCalibration()
- {
- Dimension windowSize = detachedToolbar.getSize();
- buttonSize = ((Component)listOfButtons.elementAt(0)).getSize();
- detachedToolbarTitleBar = new ToolbarTitleBar(windowSize.width,3);
-
- detachedToolbar.add("North", detachedToolbarTitleBar);
- detachedToolbar.pack();
- }
-
- private Window detachedToolbar;
- private ToolbarTitleBar detachedToolbarTitleBar;
- private Button exitButton = new Button("Exit");
- private Label helloAWTLabel = new Label("Hello AWT");
- private Vector listOfActionListeners = new Vector();
- private Vector listOfButtons = new Vector();
- private Frame Owner;
- }
-
-
- /**
- * This class is for internal use only. It paints the title on top of
- * the detached window.
- **/
- class ToolbarTitleBar extends Canvas
- {
- ToolbarTitleBar(int width, int height)
- {
- super();
- setSize(width,height); // Canvas on the window top in 3 pixel height.
- this.height = height;
- this.width = width;
- }
-
- public void paint(Graphics g)
- {
- g.setColor(SystemColor.activeCaption); // The SystemColor class
- // contains colors specific to this system.
- g.fillRect(0,0,width,i);
- }
-
- private int height,width;
- }
-