home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / Source.bin / Shape.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  9.6 KB  |  332 lines

  1. package symantec.itools.awt.shape;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.PropertyChangeListener;
  9. import java.beans.VetoableChangeListener;
  10. import symantec.itools.awt.BevelStyle;
  11.  
  12. //    06/02/97    LAB    Updated to support Java 1.1
  13. //  07/31/97    LAB    Made lightweight.  Added an override to setBackground to calculate bevel
  14. //                    colors.  Updated preferredSize to getPreferredSize.
  15. //  08/13/97    LAB    Added getMinimumSize which returns a 10 by 10 dimension.  Addresses Mac
  16. //                    Bug #7138.  Updated the color calculations to use new methods in ColorUtils.
  17. //    08/15/97    LAB    Reworked the way colors were calculated to avoid NullPointerExceptions,
  18. //                    and potential redraw problems.  Now colors are recalculated in paint,
  19. //                    if needed.
  20. //    09/16/97    RKM    Defaulted fillColor to Color.black
  21.  
  22. /**
  23.  * Abstract class for shape components.
  24.  * This is the parent Shape class for the various
  25.  * shape components.
  26.  * @see symantec.itools.awt.shape.Ellipse
  27.  * @see symantec.itools.awt.shape.Circle
  28.  * @see symantec.itools.awt.shape.Rectangle
  29.  * @see symantec.itools.awt.shape.Square
  30.  * @see symantec.itools.awt.shape.VericalLine
  31.  * @see symantec.itools.awt.shape.HorizontalLine
  32.  * @version 1.1, June 2, 1997
  33.  * @author Symantec
  34.  */
  35. public abstract class Shape extends Component implements BevelStyle
  36. {
  37.     /**
  38.      * Construct default Shape with BEVEL_LINE style.
  39.      */
  40.     protected Shape()
  41.     {
  42.         style = BEVEL_LINE;
  43.         cachedBackground = getBackground();
  44.     }
  45.  
  46.     /**
  47.      * Sets the border style of the shape.
  48.      * @see #getBevelStyle
  49.      *
  50.      * @exception PropertyVetoException
  51.      * if the specified property value is unacceptable
  52.      */
  53.     public void setBevelStyle(int s) throws PropertyVetoException
  54.     {
  55.         if(style != s)
  56.         {
  57.             Integer oldValue = new Integer(style);
  58.             Integer newValue = new Integer(s);
  59.  
  60.             vetos.fireVetoableChange("BevelStyle", oldValue, newValue);
  61.  
  62.             style = s;
  63.             repaint();
  64.  
  65.             changes.firePropertyChange("BevelStyle", oldValue, newValue);
  66.         }
  67.     }
  68.  
  69.     /**
  70.      * Returns the current style of the shape.
  71.      * @see #setBevelStyle
  72.      */
  73.     public int getBevelStyle()
  74.     {
  75.         return style;
  76.     }
  77.  
  78.     /**
  79.      * Sets the fill mode of the shape.
  80.      * @see #isFillMode
  81.      *
  82.      * @exception PropertyVetoException
  83.      * if the specified property value is unacceptable
  84.      */
  85.     public void setFillMode(boolean f) throws PropertyVetoException
  86.     {
  87.         if(fill != f)
  88.         {
  89.  
  90.             Boolean oldValue = new Boolean(fill);
  91.             Boolean newValue = new Boolean(f);
  92.  
  93.             vetos.fireVetoableChange("FillMode", oldValue, newValue);
  94.  
  95.             fill = f;
  96.             repaint();
  97.  
  98.             changes.firePropertyChange("FillMode", oldValue, newValue);
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Returns the current fill mode of the shape.
  104.      * @see #setFillMode
  105.      */
  106.     public boolean isFillMode()
  107.     {
  108.         return fill;
  109.     }
  110.  
  111.     /**
  112.      * @deprecated
  113.      * @see #isFillMode
  114.      */
  115.     public boolean getFillMode()
  116.     {
  117.         return isFillMode();
  118.     }
  119.  
  120.     /**
  121.      * Sets the fill color of the shape.
  122.      * @see #getFillColor
  123.      *
  124.      * @exception PropertyVetoException
  125.      * if the specified property value is unacceptable
  126.      */
  127.     public void setFillColor(Color color) throws PropertyVetoException
  128.     {
  129.         if(!symantec.itools.util.GeneralUtils.objectsEqual(fillColor, color))
  130.         {
  131.             Boolean oldValue = new Boolean(fill);
  132.  
  133.             vetos.fireVetoableChange("FillColor", oldValue, color);
  134.  
  135.             fillColor = color;
  136.             repaint();
  137.  
  138.             changes.firePropertyChange("FillColor", oldValue, color);
  139.         }
  140.     }
  141.  
  142.     /**
  143.      * Returns the current fill color of the shape.
  144.      * @see #setFillColor
  145.      */
  146.     public Color getFillColor()
  147.     {
  148.         return fillColor;
  149.     }
  150.  
  151.     /**
  152.      * Moves and/or resizes this component.
  153.      * This is a standard Java AWT method which gets called to move and/or
  154.      * resize this component. Components that are in containers with layout
  155.      * managers should not call this method, but rely on the layout manager
  156.      * instead.
  157.      *
  158.      * @param x horizontal position in the parent's coordinate space
  159.      * @param y vertical position in the parent's coordinate space
  160.      * @param width the new width
  161.      * @param height the new height
  162.      */
  163.     public void reshape(int x, int y, int width, int height)
  164.     {
  165.         this.width  = width;
  166.         this.height = height;
  167.  
  168.         super.reshape(x, y, width, height);
  169.     }
  170.  
  171.     /**
  172.      * Returns the minimum dimensions to properly display this component.
  173.      * This is a standard Java AWT method which gets called to determine
  174.      * the minimum size of this component.
  175.      * Returns the results of a call to getPreferredSize.
  176.      * @see #getPreferredSize
  177.      */
  178.     public Dimension getMinimumSize()
  179.     {
  180.         return new Dimension(10, 10);
  181.     }
  182.  
  183.     /**
  184.      * Returns the recommended dimensions to properly display this component.
  185.      * This is a standard Java AWT method which gets called to determine
  186.      * the recommended size of this component.
  187.      *
  188.      * @return horiziontal and vertical dimensions of 50, 50
  189.      *
  190.      * @see java.awt.Component#minimumSize
  191.      */
  192.     public Dimension getPreferredSize()
  193.     {
  194.         Dimension dim = size();
  195.         Dimension min = getMinimumSize();
  196.         return new Dimension(Math.max(dim.width, min.width), Math.max(dim.height, min.height));
  197.     }
  198.  
  199.     /**
  200.      * @deprecated
  201.      * @see getPreferredSize
  202.      */
  203.     public Dimension preferredSize()
  204.     {
  205.         return getPreferredSize();
  206.     }
  207.  
  208.     /**
  209.      * Paints this component using the given graphics context.
  210.      * This is a standard Java AWT method which typically gets called
  211.      * by the AWT to handle painting this component. It paints this component
  212.      * using the given graphics context. The graphics context clipping region
  213.      * is set to the bounding rectangle of this component and its [0,0]
  214.      * coordinate is this component's top-left corner.
  215.      *
  216.      * @param g the graphics context used for painting
  217.      * @see java.awt.Component#repaint
  218.      * @see java.awt.Component#update
  219.      */
  220.     public synchronized void paint(Graphics g)
  221.     {
  222.         //Make sure cached colors are correct.
  223.         Color curBackground = getBackground();
  224.         if (!symantec.itools.util.GeneralUtils.objectsEqual(curBackground, cachedBackground))
  225.         {
  226.             cachedBackground = curBackground;
  227.             updateBevelColors(curBackground);
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Adds a listener for all property change events.
  233.      * @param listener the listener to add
  234.      * @see #removePropertyChangeListener
  235.      */
  236.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  237.     {
  238.         changes.addPropertyChangeListener(listener);
  239.     }
  240.  
  241.     /**
  242.      * Removes a listener for all property change events.
  243.      * @param listener the listener to remove
  244.      * @see #addPropertyChangeListener
  245.      */
  246.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  247.     {
  248.         changes.removePropertyChangeListener(listener);
  249.     }
  250.  
  251.     /**
  252.      * Adds a listener for all vetoable property change events.
  253.      * @param listener the listener to add
  254.      * @see #removeVetoableChangeListener
  255.      */
  256.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  257.     {
  258.         vetos.addVetoableChangeListener(listener);
  259.     }
  260.  
  261.     /**
  262.      * Removes a listener for all vetoable property change events.
  263.      * @param listener the listener to remove
  264.      * @see #addVetoableChangeListener
  265.      */
  266.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  267.     {
  268.         vetos.removeVetoableChangeListener(listener);
  269.     }
  270.  
  271.     /**
  272.      * Used to calculate the colors for the different bevel styles.
  273.      * @param c The color of the background to make bevel colors from.
  274.      * @see java.awt.Component#addNotify
  275.      * @see java.awt.Component#setBackground
  276.      */
  277.     protected void updateBevelColors(Color c)
  278.     {
  279.         bevelDarkerColor    = symantec.itools.awt.util.ColorUtils.calculateShadowColor(c);
  280.         bevelLighterColor    = symantec.itools.awt.util.ColorUtils.calculateHilightColor(c);
  281.     }
  282.  
  283.     /**
  284.      * Width of this shape.
  285.      */
  286.     protected int width;
  287.  
  288.     /**
  289.      * Height of this shape.
  290.      */
  291.     protected int height;
  292.  
  293.     /**
  294.      * Border style of this shape.
  295.      */
  296.     protected int style;
  297.  
  298.     /**
  299.      * Shape is filled if true.
  300.      */
  301.     protected boolean fill;
  302.  
  303.     /**
  304.      * Color to fill shape with if fill is true.
  305.      */
  306.     protected Color fillColor = Color.black;
  307.  
  308.     /**
  309.      * The color to use as a hilight when BevelStyle is BEVEL_RAISED or BEVEL_LOWERED.
  310.      */
  311.     protected Color bevelLighterColor;
  312.     /**
  313.      * The color to use as a hilight when BevelStyle is BEVEL_RAISED or BEVEL_LOWERED.
  314.      */
  315.     protected Color bevelDarkerColor;
  316.     /**
  317.      * Cached value of the background color.  Used to determine if calculated colors need to be updated.
  318.      */
  319.     protected Color cachedBackground = null;
  320.  
  321.     /**
  322.      * Handles tracking vetoable change listeners and notifying them of each change
  323.      * to this component's properties.
  324.      */
  325.     protected symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  326.     /**
  327.      * Handles tracking non-vetoable change listeners and notifying them of each change
  328.      * to this component's properties.
  329.      */
  330.     protected symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  331. }
  332.