home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 22.4 KB | 766 lines |
- package symantec.itools.awt;
-
- import java.awt.Component;
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.FontMetrics;
- import java.awt.Font;
- import java.awt.Rectangle;
- import java.awt.Graphics;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.beans.PropertyChangeEvent;
- import symantec.itools.awt.util.ColorUtils;
- import symantec.itools.beans.VetoableChangeSupport;
- import symantec.itools.beans.PropertyChangeSupport;
-
- // 01/29/97 TWB Integrated changes from Windows
- // 01/29/97 TWB Integrated changes from Macintosh
- // 05/11/97 CAR Added getBorderedColor
- // 06/01/97 RKM Updated to support Java 1.1
- // Changed invalidate to repaint
- // 07/29/97 CAR marked fields transient as needed
- // 08/05/97 LAB Updated version number to 1.1. Now uses ColorUtils.calculateHilightColor
- // and ColorUtils.calculateShadowColor to calculate the bevel colors more
- // intelligently. Deprecated preferredSize and minimumSize in favor of
- // getPreferredSize and getMinimumSize. Made lightweight. Moved color
- // determination code into paint(). Removed color1 and color2 data members.
- // Made some protected data private and some private data protected.
- // Addressed Mac Bug #2771.
- // 08/12/97 LAB Removed overriden reshape. Made paint set the font of the graphics
- // to the container's font. This addresses Mac Bug #7237.
- // 08/15/97 LAB Reworked the way colors were calculated to avoid NullPointerExceptions,
- // and potential redraw problems. Now colors are recalculated in paint,
- // if needed.
-
- /**
- * Creates a text string in a rectangle that has a three-dimensional visual
- * effect. It is usually attached to an option, box, or button.
- * <p>
- * An application or applet can change the label text string, but a user
- * cannot edit it.
- * <p>
- * Note: For most components, labels are usually created by specifying text
- * for the Label property of that component.
- * <p>
- * @version 1.1, August 5, 1997
- * @author Symantec
- */
- public class Label3D extends Component implements AlignStyle, BevelStyle
- {
- /**
- * Constant indicating a drawing margin of 0 pixels around the outside of
- * the border.
- */
- public static final int INDENT_ZERO = 0;
-
- /**
- * Constant indicating a drawing margin of 1 pixel around the outside of
- * the border.
- */
- public static final int INDENT_ONE = 1;
-
- /**
- * Constant indicating a drawing margin of 2 pixels around the outside of
- * the border.
- */
- public static final int INDENT_TWO = 2;
-
- //
- // Constructors
- //
-
- /**
- * Constructs an empty Label3D with black text that is center aligned
- * and a raised border with zero border indent.
- */
- public Label3D()
- {
- this("", ALIGN_CENTERED, BEVEL_RAISED, Color.black, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with black text, zero border indent, and the
- * specified text alignment and bevel styles.
- * @param sText the label text
- * @param alignStyle the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle the bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- */
- public Label3D(String sText, int alignStyle, int bevelStyle)
- {
- this(sText, alignStyle, bevelStyle, Color.black, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with a zero border indent and the specified attributes.
- * @param sText the label text
- * @param alignStyle the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle the bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the text color
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, Color color)
- {
- this(sText, alignStyle, bevelStyle, color, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with black text and the specified attributes.
- * @param sText the label text
- * @param alignStyle the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle the bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param indent the amount to indent the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- * @see #INDENT_ZERO
- * @see #INDENT_ONE
- * @see #INDENT_TWO
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, int indent)
- {
- this(sText, alignStyle, bevelStyle, Color.black, indent);
- }
-
- /**
- * Constructs a Label3D with the specified attributes.
- * @param sText the label text
- * @param alignStyle the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle the bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the text color
- * @param indent the amount to indent the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- * @see #INDENT_ZERO
- * @see #INDENT_ONE
- * @see #INDENT_TWO
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, Color color, int indent)
- {
- String sOs = System.getProperty("os.name");
-
- if (sOs.startsWith("S") || // SunOS, Solaris
- sOs.startsWith("OSF") ) // OSF
- {
- bOsFlag = true;
- setFont(new Font("Dialog", Font.PLAIN, 10));
- }
- else
- {
- bOsFlag = false;
- }
-
- sLabel3D = sText;
- textColor = color;
- borderedColor = Color.black;
- cachedBackground = getBackground();
-
- try
- {
- setBorderIndent(indent);
- setAlignStyle(alignStyle);
- setBevelStyle(bevelStyle);
- }
- catch(PropertyVetoException veto) {};
- }
-
- //
- // Properties
- //
-
- /**
- * Sets the text alignment style.
- * @param style the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #getAlignStyle
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setAlignStyle(int newAlignStyle) throws PropertyVetoException
- {
- if (alignStyle != newAlignStyle)
- {
- Integer oldAlignStyleInt = new Integer(alignStyle);
- Integer newAlignStyleInt = new Integer(newAlignStyle);
-
- vetos.fireVetoableChange("AlignStyle",oldAlignStyleInt,newAlignStyleInt);
-
- alignStyle = newAlignStyle;
- repaint();
-
- changes.firePropertyChange("AlignStyle",oldAlignStyleInt,newAlignStyleInt);
- }
- }
-
- /**
- * Gets the current alignment style.
- * @return the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #setAlignStyle
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- */
- public int getAlignStyle()
- {
- return alignStyle;
- }
-
- /**
- * Sets the border style.
- * @param style the border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #getBevelStyle
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setBevelStyle(int newBevelStyle) throws PropertyVetoException
- {
- if (bevelStyle != newBevelStyle)
- {
- Integer oldValue = new Integer(bevelStyle);
- Integer newValue = new Integer(newBevelStyle);
-
- vetos.fireVetoableChange("BevelStyle", oldValue, newValue);
-
- bevelStyle = newBevelStyle;
-
- repaint();
-
- changes.firePropertyChange("BevelStyle", oldValue, newValue);
-
- }
- }
-
- /**
- * Gets the current border style.
- * @return the border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #setBevelStyle
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- */
- public int getBevelStyle()
- {
- return bevelStyle;
- }
-
- /**
- * Sets the border indent amount.
- * @param newBorderIndent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #getBorderIndent
- * @see #INDENT_ZERO
- * @see #INDENT_ONE
- * @see #INDENT_TWO
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setBorderIndent(int newBorderIndent) throws PropertyVetoException
- {
- //Constrain newBorderIndent
- if (newBorderIndent < INDENT_ZERO)
- newBorderIndent = INDENT_ZERO;
- else if (newBorderIndent > INDENT_TWO)
- newBorderIndent = INDENT_TWO;
-
- if (indent != newBorderIndent)
- {
- Integer oldBorderIndentInt = new Integer(indent);
- Integer newBorderIndentInt = new Integer(newBorderIndent);
-
- vetos.fireVetoableChange("BorderIndent",oldBorderIndentInt,newBorderIndentInt);
-
- indent = newBorderIndent;
- repaint();
-
- changes.firePropertyChange("BorderIndent",oldBorderIndentInt,newBorderIndentInt);
- }
- }
-
- /**
- * Gets the current border indent amount.
- * @return the amount the border is currently indented: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #setBorderIndent
- * @see #INDENT_ZERO
- * @see #INDENT_ONE
- * @see #INDENT_TWO
- */
- public int getBorderIndent()
- {
- return indent;
- }
-
- /**
- * Sets the border color.
- * @param newBorderColor the color to use for the border
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setBorderedColor(Color newBorderColor) throws PropertyVetoException
- {
- if (!symantec.itools.util.GeneralUtils.objectsEqual(borderedColor,newBorderColor))
- {
- Color oldBorderColor = borderedColor;
-
- vetos.fireVetoableChange("BorderedColor",oldBorderColor,newBorderColor);
-
- borderedColor = newBorderColor;
-
- repaint();
-
- changes.firePropertyChange("BorderedColor",oldBorderColor,newBorderColor);
-
- }
- }
-
- /**
- * Gets the border color.
- * @return the border color
- * @see #setBorderedColor
- */
- public Color getBorderedColor()
- {
- return borderedColor;
- }
-
- /**
- * Sets the label text.
- * @param newText the new label text
- * @see #getText
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setText(String newText) throws PropertyVetoException
- {
- if (!symantec.itools.util.GeneralUtils.objectsEqual(sLabel3D,newText))
- {
- String oldText = sLabel3D;
-
- vetos.fireVetoableChange("Text",oldText,newText);
-
- sLabel3D = newText;
- repaint();
-
- changes.firePropertyChange("Text",oldText,newText);
- }
- }
-
- /**
- * Gets the current label text.
- * @return the current label text
- * @see #setText
- */
- public String getText()
- {
- return sLabel3D;
- }
-
- /**
- * Sets the label text color.
- * @param newTextColor the new color for the label text
- * @see #getTextColor
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setTextColor(Color newTextColor) throws PropertyVetoException
- {
- if (!symantec.itools.util.GeneralUtils.objectsEqual(textColor,newTextColor))
- {
- Color oldValue = textColor;
-
- vetos.fireVetoableChange("TextColor", oldValue, newTextColor);
-
- textColor = newTextColor;
- repaint();
-
- changes.firePropertyChange("TextColor", oldValue, newTextColor);
- }
- }
-
- /**
- * Gets the current label text color.
- * @return the current color of the label text
- * @see #setTextColor
- */
- public Color getTextColor()
- {
- return textColor;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its [0,0]
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- Rectangle r;
- Color c, color1, color2;
-
- Color curBackground = getBackground();
- if (!symantec.itools.util.GeneralUtils.objectsEqual(curBackground, cachedBackground))
- {
- cachedBackground = curBackground;
- calculateHilightColors(curBackground);
- }
-
- r = bounds();
- c = g.getColor();
-
- g.setFont(getFont());
-
- switch (bevelStyle)
- {
- case BEVEL_LOWERED :
- {
- color1 = shadowColor;
- color2 = hilightColor;
- break;
- }
-
- case BEVEL_RAISED :
- {
- color1 = hilightColor;
- color2 = shadowColor;
- break;
- }
-
- case BEVEL_LINE :
- {
- color1 = borderedColor;
- color2 = borderedColor;
- break;
- }
-
- default: // catches any bogus type, paint(...) relies on color1 being null
- {
- color1 = color2 = null;
- break;
- }
- }
-
- g.setColor(getBackground());
- g.fillRect(0, 0, r.width - 1, r.height - 1);
-
- if (color1 != null)
- {
- // top
- g.setColor(color1);
- g.drawLine(1+indent, indent, r.width-3-indent, indent);
-
- // bottom
- g.setColor(color2);
- g.drawLine(1+indent, r.height-1-indent, r.width-3-indent, r.height-1-indent);
-
- // left
- g.setColor(color1);
- g.drawLine(indent, indent, indent, r.height-1-indent);
-
- // right
- g.setColor(color2);
- g.drawLine(r.width-2-indent, indent, r.width-2-indent, r.height-1-indent);
-
- g.clipRect(2 + indent, 1+indent, r.width-9-indent, r.height-4-indent);
- yTemp = 1 + indent;
- }
- else
- {
- g.drawRect(indent, indent, r.width-2-indent, r.height-1-indent);
- g.clipRect(2, 1, r.width-7, r.height-2);
- yTemp = 1;
- }
-
- // text
- g.setColor(textColor);
-
- fm = getFontMetrics(g.getFont());
- yTemp = (r.height - yTemp + fm.getAscent()) / 2;
-
- switch (alignStyle)
- {
- case ALIGN_LEFT:
- {
- if (bevelStyle == BEVEL_LINE)
- {
- g.drawString(sLabel3D, 4, yTemp);
- }
- else
- {
- g.drawString(sLabel3D, 8, yTemp);
- }
-
- break;
- }
-
- case ALIGN_RIGHT:
- {
- xTemp = r.width - fm.stringWidth(sLabel3D);
-
- if (bevelStyle == BEVEL_LINE)
- {
- g.drawString(sLabel3D, xTemp - 6, yTemp);
- }
- else
- {
- g.drawString(sLabel3D, xTemp - 10, yTemp);
- }
-
- break;
- }
-
- case ALIGN_CENTERED:
- {
- xTemp = (r.width - fm.stringWidth(sLabel3D)) / 2;
- g.drawString(sLabel3D, xTemp, yTemp);
-
- break;
- }
- }
-
- // reset color
- g.setColor(c);
- }
-
- /**
- * 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.
- *
- * For each axis, the preferred size is the maximum of the current size
- * and the minimum size needed to display the entire label text.
- *
- * @see #getMinimumSize
- */
- public Dimension getPreferredSize()
- {
- Dimension s = size();
- Dimension m = minimumSize();
-
- return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
- }
-
- /**
- * @deprecated
- * @see #getPreferredSize
- */
- public 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.
- *
- * The minimum size is that size needed to display the entire label text.
- *
- * @see #getPreferredSize
- */
- public Dimension getMinimumSize()
- {
- Dimension min;
- Font f;
-
- min = new Dimension(18, 10);
- f = getFont();
-
- if (f == null)
- {
- if (bOsFlag)
- {
- min.height = 29;
- }
- }
- else
- {
- fm = getFontMetrics(f);
- min.width = fm.stringWidth(sLabel3D) + 18;
- min.height = fm.getHeight() + 10;
-
- if (bOsFlag && min.height < 29)
- {
- min.height = 29;
- }
- }
-
- return min;
- }
-
- /**
- * @deprecated
- * @see #getMinimumSize
- */
- public Dimension minimumSize()
- {
- return getMinimumSize();
- }
-
- /**
- * 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);
- }
-
- /**
- * Used to calculate the hilight colors from the background color.
- * @see #paint
- */
- protected void calculateHilightColors(Color c)
- {
- hilightColor = ColorUtils.calculateHilightColor(c);
- shadowColor = ColorUtils.calculateShadowColor(c);
- }
-
- //Protected members
- /**
- * The label text.
- */
- protected String sLabel3D;
-
- /**
- * The current text alignment style.
- * The value is one of: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT.
- * @see #getAlignStyle
- * @see #setAlignStyle
- * @see AlignStyle#ALIGN_LEFT
- * @see AlignStyle#ALIGN_CENTERED
- * @see AlignStyle#ALIGN_RIGHT
- */
- protected int alignStyle;
- /**
- * The current border bevel style.
- * The value is one of: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE.
- * @see #getBevelStyle
- * @see #setBevelStyle
- * @see BevelStyle#BEVEL_RAISED
- * @see BevelStyle#BEVEL_LOWERED
- * @see BevelStyle#BEVEL_LINE
- * @see BevelStyle#BEVEL_NONE
- */
- protected int bevelStyle;
- /**
- * The amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO.
- * @see #getBorderIndent
- * @see #setBorderIndent
- * @see #INDENT_ZERO
- * @see #INDENT_ONE
- * @see #INDENT_TWO
- */
- protected int indent = INDENT_ZERO;
- /**
- * The label text color.
- * @see #getTextColor
- * @see #setTextColor
- */
- protected Color textColor;
- /**
- * The border color.
- * @see #getBorderedColor
- * @see #setBorderedColor
- */
- protected Color borderedColor;
- /**
- * The color of border bevel hilights.
- * This color is automatically determined.
- */
- protected Color hilightColor;
- /**
- * The color of border bevel shawdows.
- * This color is automatically determined.
- */
- protected Color shadowColor;
- /**
- * Cached value of the background color. Used to determine if calculated colors need to be updated.
- */
- protected Color cachedBackground = null;
-
- private VetoableChangeSupport vetos = new VetoableChangeSupport(this);
- private PropertyChangeSupport changes = new PropertyChangeSupport(this);
-
- transient private FontMetrics fm;
- transient private int xTemp;
- transient private int yTemp;
- transient private boolean bOsFlag = false;
- }
-
-