home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 10.1 KB | 340 lines |
- package symantec.itools.awt.util;
-
- import java.awt.Canvas;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeEvent;
- import java.beans.VetoableChangeListener;
- import java.beans.PropertyChangeListener;
- import java.util.ResourceBundle;
-
- // 01/29/97 TWB Integrated changes from Macintosh
- // 07/24/97 LAB Updated version to 1.1. Added Space prperty. Added support
- // for ToolBarPanel's Orientation property. Removed unneeded
- // paint override.
- // 08/25/97 LAB Now we get the parent of the parent in getPreferredSize which allows us to
- // properly get the ToolBarPanel that contains us (Addresses Mac Bug #7543).
-
- /**
- * ToolBarPanelSpacer component.
- * This component is used to space items in a ToolBarPanel.
- * @see symantec.itools.awt.util.ToolBarPanel
- * @version 1.1, July 24, 1997
- * @author Symantec
- */
- public class ToolBarSpacer extends Canvas
- {
- /**
- * Create a ToolBarSpacer. It's default space value is 10 pixels.
- */
- public ToolBarSpacer()
- {
- space = 10;
- }
-
- /**
- * Sets the size of the space the spacer will occupy in pixels.
- * @param value the size of the space in pixels
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- * @see #getSpace
- */
- public void setSpace(int value) throws PropertyVetoException
- {
- if (space != value)
- {
- Integer oldValue = new Integer(space);
- Integer newValue = new Integer(value);
-
- vetos.fireVetoableChange("Space", oldValue, newValue);
-
- space = value;
- changes.firePropertyChange("Space", oldValue, newValue);
- }
- }
-
- /**
- * Gets the size of the space the spacer will occupy in pixels.
- * @return the size of the space in pixels
- * @see #setSpace
- */
- public int getSpace()
- {
- return space;
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @return The width is the Space value and the height is the tallest component
- * the ToolBarPanel, if the parent ToolBarPanel's Orientation is HORIZONTAL.
- * The height is the Space value and the width is the widest component
- * the ToolBarPanel, if the parent ToolBarPanel's Orientation is VERTICAL.
- *
- * NOTE: If the parent of this class is not an instance of ToolBarPanel, this will
- * return 10,10.
- *
- * @see #setSpace
- * @see #getSpace
- * @see #getMinimumSize
- */
- public synchronized Dimension getPreferredSize()
- {
- //We assume that we will be in a ToolBarPanel and since it has a java.awt.Panel
- //in it, we have to get the parent of the parent to actually get the ToolBarPanel.
- Component parent = getParent();
- if(parent != null)
- parent = parent.getParent();
-
- if (parent != null && parent instanceof ToolBarPanel)
- {
- Dimension s = new Dimension(0, 0);
-
- Component[] list = ((ToolBarPanel)parent).getComponents();
-
- switch(((ToolBarPanel)parent).getOrientation())
- {
- case ToolBarPanel.HORIZONTAL:
- s.width = space;
- for (int i = 0; i < list.length; ++i)
- {
- Component c = list[i];
- if (!(c instanceof ToolBarSpacer))
- s.height = Math.max(s.height, c.size().height);
- }
- break;
- case ToolBarPanel.VERTICAL:
- s.height = space;
- for (int i = 0; i < list.length; ++i)
- {
- Component c = list[i];
- if (!(c instanceof ToolBarSpacer))
- s.width = Math.max(s.width, c.size().width);
- }
- break;
- }
-
- return s;
- }
- else
- return new Dimension(10, 10);
- }
-
- /**
- * @deprecated
- * @see #getPreferredSize
- */
- public synchronized Dimension preferredSize()
- {
- return getPreferredSize();
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * In this case the minimum size is the same as the preferred size.
- *
- * @see #getPreferredSize
- */
- public synchronized Dimension getMinimumSize()
- {
- return preferredSize();
- }
-
- /**
- * @deprecated
- * @see #getMinimumSize
- */
- public synchronized Dimension minimumSize()
- {
- return getMinimumSize();
- }
-
- /**
- * Is the specified space size valid?
- * @param spaceSize the value to test
- * @return true if the parameter is greater or equal to zero.
- * @see #setSpace
- * @see #getSpace
- */
- public boolean isValidSpace(int spaceSize)
- {
- return (spaceSize >= 0);
- }
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- *
- * It has been overridden here to hook-up event listeners.
- *
- * @see #removeNotify
- */
- public synchronized void addNotify()
- {
- super.addNotify();
- errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
-
- //Hook up listeners
- if (veto == null)
- {
- veto = new Veto();
- addSpaceListener(veto);
- }
- }
-
- /**
- * Tells this component that it is being removed from a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is removed from a container. Typically, it is used to
- * destroy the peers of this component and all its subcomponents.
- *
- * It has been overridden here to unhook event listeners.
- *
- * @see #addNotify
- */
- public synchronized void removeNotify()
- {
- //Unhook listeners
- if (veto != null)
- {
- removeSpaceListener(veto);
- veto = null;
- }
-
- super.removeNotify();
- }
-
- /**
- * Adds a listener for all event changes.
- * @param listener the listener to add.
- * @see #removePropertyChangeListener
- */
- public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
- {
- //super.addPropertyChangeListener(listener);
- changes.addPropertyChangeListener(listener);
- }
-
- /**
- * Removes a listener for all event changes.
- * @param listener the listener to remove.
- * @see #addPropertyChangeListener
- */
- public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
- {
- //super.removePropertyChangeListener(listener);
- changes.removePropertyChangeListener(listener);
- }
-
- /**
- * Adds a vetoable listener for all event changes.
- * @param listener the listener to add.
- * @see #removeVetoableChangeListener
- */
- public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.addVetoableChangeListener(listener);
- vetos.addVetoableChangeListener(listener);
- }
-
- /**
- * Removes a vetoable listener for all event changes.
- * @param listener the listener to remove.
- * @see #addVetoableChangeListener
- */
- public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.removeVetoableChangeListener(listener);
- vetos.removeVetoableChangeListener(listener);
- }
-
- /**
- * Adds a listener for Space changes.
- * @param listener the listener to add.
- * @see #removeSpaceListener
- */
- public synchronized void addSpaceListener(PropertyChangeListener listener)
- {
- changes.addPropertyChangeListener("Space", listener);
- }
-
- /**
- * Removes a listener for Space changes.
- * @param listener the listener to remove.
- * @see #addSpaceListener
- */
- public synchronized void removeSpaceListener(PropertyChangeListener listener)
- {
- changes.removePropertyChangeListener("Space", listener);
- }
-
- /**
- * Adds a vetoable listener for Space changes.
- * @param listener the listener to add.
- * @see #removeSpaceListener
- */
- public synchronized void addSpaceListener(VetoableChangeListener listener)
- {
- vetos.addVetoableChangeListener("Space", listener);
- }
-
- /**
- * Removes a vetoable listener for Space changes.
- * @param listener the listener to remove.
- * @see #addSpaceListener
- */
- public synchronized void removeSpaceListener(VetoableChangeListener listener)
- {
- vetos.removeVetoableChangeListener("Space", listener);
- }
-
- /**
- * This is the PropertyChangeEvent handling inner class for the constrained Space property.
- * Handles vetoing Spaces that are not valid.
- */
- class Veto implements VetoableChangeListener
- {
- /**
- * This method gets called when an attempt to change the constrained Space property is made.
- * Ensures the given Space is valid.
- *
- * @param e a <code>PropertyChangeEvent</code> object describing the
- * event source and the property that has changed.
- * @exception PropertyVetoException if the recipient wishes the property
- * change to be rolled back.
- */
- public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
- {
- int i = ((Integer)e.getNewValue()).intValue();
- if (!isValidSpace(i))
- {
- throw new PropertyVetoException(errors.getString("InvalidSpace") + i, e);
- }
- }
- }
-
- /**
- * The size of the space the spacer will occupy in pixels..
- */
- protected int space;
-
- /**
- * Error strings.
- */
- transient protected ResourceBundle errors;
-
- private Veto veto = null;
- private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
- private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
- }
-
-