home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
Source.bin
/
LabelButton.java
< prev
next >
Wrap
Text File
|
1998-03-18
|
23KB
|
692 lines
package symantec.itools.awt;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Font;
import java.awt.Graphics;
import java.beans.PropertyVetoException;
import java.beans.PropertyChangeListener;
import java.beans.VetoableChangeListener;
import java.beans.PropertyChangeEvent;
import java.lang.IllegalArgumentException;
import symantec.itools.awt.util.ColorUtils;
import java.util.ResourceBundle;
// 01/29/97 TWB Integrated changes from Macintosh
// 02/27/97 RKM Merged in Scott's changes
// 05/31/97 LAB Updated to Java 1.1
// 07/09/97 LAB Changed the way the button is drawn. Now it uses the offscreen
// Image. Cleaned up the drawing code to be more universal.
// Added pressed and disabled colors for the text durring those states.
// Added a VerticalAlignStyle property to align the text vertically.
// Added a preferedSize() method that works.
// Removed BevelStyle, Show/HideLabel, and BorderIndent.
// No longer overrides setFont(), minimumSize(), or paint().
// 07/13/97 RKM Fixed misspelling of prefered
// 07/20/97 LAB Fixed a logic problem when testing for null strings.
// 07/23/97 CAR changed getPreferredSize
// 07/23/97 CAR marked field transient as needed, inner classes implement java.io.Serializable
// 08/05/97 LAB Added a call to clipRect in the updateButtonImage method to clip subsequent
// drawings to the internal button area (sans border and bevel). This should be
// removed when the VM interprets the base class' call to clipRect correctly.
// 08/06/97 LAB Removed the call to clipRect; now uses the base classes buttonImageGraphics
// Graphics to draw with, which inherits the clipping.
// 08/19/97 CAR commented out duplicate call to setVerticalAlignStyle in constructor
/**
* LabelButton is a 3D looking button that displays a string on it's face.
* Use an LabelButton to:
* <UL>
* <DT>╖ Display text in a 3D looking button.</DT>
* <DT>╖ Generate a train of action events while the user presses the button.</DT>
* </UL>
* <p>
* @version 1.1, July 8, 1997
* @author Symantec
*/
public class LabelButton extends ButtonBase implements AlignStyle
{
/**
* Defines the "top" label text alignment style.
*/
public static final int ALIGN_TOP = 0;
/**
* Defines the "bottom" label text alignment style.
*/
public static final int ALIGN_BOTTOM = 2;
/**
* Constructs an empty LabelButton with center aligned black text.
*/
public LabelButton()
{
this("", ALIGN_CENTERED, ALIGN_CENTERED, Color.black);
}
/**
* Constructs a LabelButton with the specified centered, black text.
* @param sText the label text
*/
public LabelButton(String sText)
{
this(sText, ALIGN_CENTERED, ALIGN_CENTERED, Color.black);
}
/**
* Constructs a LabelButton with the specified centered text,
* with the specified color.
* @param sText the label text
*/
public LabelButton(String sText, Color color)
{
this(sText, ALIGN_CENTERED, ALIGN_CENTERED, color);
}
/**
* Constructs a LabelButton with the specified vertically centered black text, and the
* specified horizontal text alignment style.
* @param sText the label text
* @param alignStyle the text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
*/
public LabelButton(String sText, int alignStyle)
{
this(sText, alignStyle, ALIGN_CENTERED, Color.black);
}
/**
* Constructs a LabelButton with the specified attributes.
* @param sText the label text
* @param hAlignStyle the text horizontal alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
* @param vAlignStyle the text vertical alignment style: ALIGN_TOP, ALIGN_CENTERED, or ALIGN_BOTTOM
* @param color the label text color
* @see AlignStyle#ALIGN_LEFT
* @see AlignStyle#ALIGN_CENTERED
* @see AlignStyle#ALIGN_RIGHT
* @see #ALIGN_TOP
* @see #ALIGN_BOTTOM
*/
public LabelButton(String sText, int hAlignStyle, int vAlignStyle, Color color)
{
try
{
setText(sText);
setTextColor(color);
setAlignStyle(hAlignStyle);
setVerticalAlignStyle(vAlignStyle);
//setVerticalAlignStyle(ALIGN_CENTERED);
}
catch(PropertyVetoException e){}
}
/**
* Sets the text horizontal alignment style.
* Causes the component to redraw.
* @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 style) throws PropertyVetoException
{
if(hAlignStyle != style)
{
Integer oldValue = new Integer(hAlignStyle);
Integer newValue = new Integer(style);
vetos.fireVetoableChange("AlignStyle", oldValue, newValue);
hAlignStyle = style;
repaint();
changes.firePropertyChange("AlignStyle", oldValue, newValue);
}
}
/**
* Gets the current text 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 hAlignStyle;
}
/**
* Sets the text vertical alignment style.
* Causes the component to redraw.
* @param style the text alignment style: ALIGN_TOP, ALIGN_CENTERED, or ALIGN_BOTTOM
* @see #getVerticalAlignStyle
* @see #ALIGN_TOP
* @see AlignStyle#ALIGN_CENTERED
* @see #ALIGN_BOTTOM
* @exception PropertyVetoException
* if the specified property value is unacceptable
*/
public void setVerticalAlignStyle(int style) throws PropertyVetoException
{
if(vAlignStyle != style)
{
Integer oldValue = new Integer(vAlignStyle);
Integer newValue = new Integer(style);
vetos.fireVetoableChange("VerticalAlignStyle", oldValue, newValue);
vAlignStyle = style;
repaint();
changes.firePropertyChange("VerticalAlignStyle", oldValue, newValue);
}
}
/**
* Gets the current text alignment style.
* @return the text alignment style: ALIGN_TOP, ALIGN_CENTERED, or ALIGN_BOTTOM
* @see #setVerticalAlignStyle
* @see #ALIGN_TOP
* @see AlignStyle#ALIGN_CENTERED
* @see #ALIGN_BOTTOM
*/
public int getVerticalAlignStyle()
{
return vAlignStyle;
}
/**
* Sets the label text.
* Causes the component to redraw.
* @param sText the new label text
* @see #getText
* @exception PropertyVetoException
* if the specified property value is unacceptable
*/
public void setText(String sText) throws PropertyVetoException
{
if(sLabelButton == null || !sLabelButton.equals(sText))
{
String oldValue = sLabelButton == null ? null : new String(sLabelButton);
vetos.fireVetoableChange("Text", oldValue, sText);
sLabelButton = sText;
repaint();
changes.firePropertyChange("Text", oldValue, sText);
}
}
/**
* Gets the current label text.
* @return the current label text
* @see #setText
*/
public String getText()
{
return sLabelButton;
}
/**
* Sets the label text color.
* Causes the component to redraw.
* @param color the new color for the label text
* @see #getTextColor
* @exception PropertyVetoException
* if the specified property value is unacceptable
*/
public void setTextColor(Color color) throws PropertyVetoException
{
if(! symantec.itools.util.GeneralUtils.objectsEqual(textColor, color))
{
Color oldValue = textColor;
vetos.fireVetoableChange("TextColor", oldValue, color);
textColor = color;
try
{
disabledTextColor = ColorUtils.lighten(textColor, 0.333);
pressedTextColor = ColorUtils.darken(textColor, 0.250);
}
catch (IllegalArgumentException exc) {}
repaint();
changes.firePropertyChange("TextColor", oldValue, color);
}
}
/**
* Gets the current label text color.
* @return the current color of the label text
* @see #setTextColor
*/
public Color getTextColor()
{
return textColor;
}
/**
* 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.
*
* The Dimension returned is large enough for the label and a one pixel
* space between the label and the button's bevel.
*/
public Dimension getPreferredSize()
{
if(isAdded)
{
Dimension s = size();
Graphics g = getGraphics();
FontMetrics fm = g.getFontMetrics();
int labelWidth = 0;
//Make sure we don't cause a NullPointerException when accessing the string width.
if(!(sLabelButton == null || sLabelButton.equals("")))
labelWidth = fm.stringWidth(sLabelButton);
if (g != null)
g.dispose();
return new Dimension(labelWidth + bevel + bevel + 4, fm.getAscent() + fm.getDescent() + bevel + bevel + 4);
}
return super.getPreferredSize();
}
/**
* 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 (horizontalVeto == null)
{
horizontalVeto = new HAVeto();
addAlignStyleListener(horizontalVeto);
}
if (verticalVeto == null)
{
verticalVeto = new VAVeto();
addVerticalAlignStyleListener(verticalVeto);
}
}
/**
* 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 (horizontalVeto != null)
{
removeAlignStyleListener(horizontalVeto);
horizontalVeto = null;
}
if (verticalVeto != null)
{
removeVerticalAlignStyleListener(verticalVeto);
verticalVeto = 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(java.beans.VetoableChangeListener)
*/
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(java.beans.VetoableChangeListener)
*/
public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
{
super.removeVetoableChangeListener(listener);
vetos.removeVetoableChangeListener(listener);
}
/**
* Adds a listener for the AlignStyle property changes.
* @param listener the listener to add.
* @see #removeAlignStyleListener(java.beans.PropertyChangeListener)
*/
public synchronized void addAlignStyleListener(PropertyChangeListener listener)
{
changes.addPropertyChangeListener("AlignStyle", listener);
}
/**
* Removes a listener for the AlignStyle property changes.
* @param listener the listener to remove.
* @see #addAlignStyleListener(java.beans.PropertyChangeListener)
*/
public synchronized void removeAlignStyleListener(PropertyChangeListener listener)
{
changes.removePropertyChangeListener("AlignStyle", listener);
}
/**
* Adds a vetoable listener for the AlignStyle property changes.
* @param listener the listener to add.
* @see #removeAlignStyleListener(java.beans.VetoableChangeListener)
*/
public synchronized void addAlignStyleListener(VetoableChangeListener listener)
{
vetos.addVetoableChangeListener("AlignStyle", listener);
}
/**
* Removes a vetoable listener for the AlignStyle property changes.
* @param listener the listener to remove.
* @see #addAlignStyleListener(java.beans.VetoableChangeListener)
*/
public synchronized void removeAlignStyleListener(VetoableChangeListener listener)
{
vetos.removeVetoableChangeListener("AlignStyle", listener);
}
/**
* Adds a listener for the VerticalAlignStyle property changes.
* @param listener the listener to add.
* @see #removeVerticalAlignStyleListener(java.beans.PropertyChangeListener)
*/
public synchronized void addVerticalAlignStyleListener(PropertyChangeListener listener)
{
changes.addPropertyChangeListener("VerticalAlignStyle", listener);
}
/**
* Removes a listener for the VerticalAlignStyle property changes.
* @param listener the listener to remove.
* @see #addVerticalAlignStyleListener(java.beans.PropertyChangeListener)
*/
public synchronized void removeVerticalAlignStyleListener(PropertyChangeListener listener)
{
changes.removePropertyChangeListener("VerticalAlignStyle", listener);
}
/**
* Adds a vetoable listener for the VerticalAlignStyle property changes.
* @param listener the listener to add.
* @see #removeVerticalAlignStyleListener(java.beans.VetoableChangeListener)
*/
public synchronized void addVerticalAlignStyleListener(VetoableChangeListener listener)
{
vetos.addVetoableChangeListener("VerticalAlignStyle", listener);
}
/**
* Removes a vetoable listener for the VerticalAlignStyle property changes.
* @param listener the listener to remove.
* @see #addVerticalAlignStyleListener(java.beans.VetoableChangeListener)
*/
public synchronized void removeVerticalAlignStyleListener(VetoableChangeListener listener)
{
vetos.removeVetoableChangeListener("VerticalAlignStyle", listener);
}
/**
* This is the PropertyChangeEvent handling inner class for the constrained AlignStyle property.
* Handles vetoing AlignStyles that are not valid.
*/
class HAVeto implements java.beans.VetoableChangeListener, java.io.Serializable
{
/**
* This method gets called when an attempt to change the constrained AlignStyle property is made.
* Ensures the given AlignStyle 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 (!isValidHorizontalAlignStyle(i))
{
throw new PropertyVetoException(errors.getString("InvalidAlignStyle") + i, e);
}
}
}
/**
* This is the PropertyChangeEvent handling inner class for the constrained VerticalAlignStyle property.
* Handles vetoing VerticalAlignStyles that are not valid.
*/
class VAVeto implements java.beans.VetoableChangeListener, java.io.Serializable
{
/**
* This method gets called when an attempt to change the constrained VerticalAlignStyle property is made.
* Ensures the given AlignStyle 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 (!isValidVerticalAlignStyle(i))
{
throw new PropertyVetoException(errors.getString("InvalidVerticalAlignStyle") + i, e);
}
}
}
/**
* Is the given Horizontal AlignStyle valid for this button.
* @param i the given Horizontal AlignStyle
* @return true if the given Horizontal AlignStyle is acceptable, false if not.
*/
protected boolean isValidHorizontalAlignStyle(int i)
{
switch(i)
{
case ALIGN_LEFT:
case ALIGN_CENTERED:
case ALIGN_RIGHT:
return true;
default:
return false;
}
}
/**
* Is the given Vertical AlignStyle valid for this button.
* @param i the given Vertical AlignStyle
* @return true if the given Vertical AlignStyle is acceptable, false if not.
*/
protected boolean isValidVerticalAlignStyle(int i)
{
switch(i)
{
case ALIGN_TOP:
case ALIGN_CENTERED:
case ALIGN_BOTTOM:
return true;
default:
return false;
}
}
/**
* Draws the button in the buttonImage offscreen image.
* This should only be called after the component is added to a container,
* to avoid a NullPointerException when accessing FontMetrics.
* @see symantec.itools.awt.ButtonBase#updateButtonImage
*/
protected void updateButtonImage()
{
super.updateButtonImage();
FontMetrics fm = buttonImageGraphics.getFontMetrics();
Color tempColor;
int xPos = 0;
int yPos = 0;
int labelWidth = 0;
Dimension s = size();
//Make sure we don't cause a NullPointerException when accessing the string width.
if(!(sLabelButton == null || sLabelButton.equals("")))
labelWidth = fm.stringWidth(sLabelButton);
if(isEnabled())
{
if(pressed)
tempColor = pressedTextColor;
else
tempColor = textColor;
}
else
{
tempColor = disabledTextColor;
}
switch(hAlignStyle)
{
case ALIGN_LEFT:
//Align with the left edge and add a one pixel margin.
xPos = bevel + 2;
break;
case ALIGN_RIGHT:
//Align with the right edge and add a one pixel margin.
xPos = s.width - 3 - bevel - labelWidth;
break;
case ALIGN_CENTERED:
xPos = (s.width - labelWidth) >> 1;
break;
}
switch(vAlignStyle)
{
case ALIGN_TOP:
//Align with the top edge and add a one pixel margin.
yPos = bevel + 2 + fm.getAscent();
break;
case ALIGN_BOTTOM:
//Align with the bottom edge and add a one pixel margin.
yPos = s.height - 3 - bevel - fm.getDescent();
break;
case ALIGN_CENTERED:
yPos = (s.height + fm.getAscent()) >> 1;
break;
}
buttonImageGraphics.setColor(tempColor);
buttonImageGraphics.drawString(sLabelButton, xPos + pressedAdjustment, yPos + pressedAdjustment);
}
/**
* The button label text.
*/
protected String sLabelButton;
/**
* The text vertical alignment style.
* The value is one of: ALIGN_TOP, ALIGN_CENTERED, or ALIGN_BOTTOM
* @see #getVerticalAlignStyle
* @see #setVerticalAlignStyle
* @see #ALIGN_TOP
* @see AlignStyle#ALIGN_CENTERED
* @see #ALIGN_BOTTOM
*/
protected int vAlignStyle;
/**
* The text horizontal 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 hAlignStyle;
/**
* The color used for text when this component is enabled.
* @see #getTextColor
* @see #setTextColor
*/
protected Color textColor = null;
/**
* The color used for text when this component is pressed.
* This color is automatically determined.
*/
protected Color pressedTextColor = null;
/**
* The color used for text when this component is disabled.
* This color is automatically determined.
*/
protected Color disabledTextColor = null;
/**
* Error strings.
*/
transient protected ResourceBundle errors;
private HAVeto horizontalVeto = null;
private VAVeto verticalVeto = 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);
}