This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gui][PATCH] Reimplement JLabel


Hi,

This patch re-implements JLabel to match Sun's spec. Committed to the
gui branch.

Cheers,

Kim

2004-02-16  Kim Ho  <kho@redhat.com>
	* javax/swing/JLabel.java: Re-implement.
	* javax/swing/plaf/basic/BasicLabelUI.java
	Re-implement.
	* javax/swing/plaf/basic/BasicLookAndFeel.java:
	Added constant.
Index: javax/swing/JLabel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JLabel.java,v
retrieving revision 1.4
diff -u -r1.4 JLabel.java
--- javax/swing/JLabel.java	12 Feb 2004 00:17:23 -0000	1.4
+++ javax/swing/JLabel.java	16 Feb 2004 16:47:28 -0000
@@ -1,5 +1,5 @@
-/* JLabel.java -- 
-   Copyright (C) 2002 Free Software Foundation, Inc.
+/* JLabel.java --
+   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -35,205 +35,655 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
-
 package javax.swing;
 
 import java.awt.Component;
 import java.awt.Image;
+import java.awt.Font;
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
+import javax.swing.Icon;
 import javax.swing.plaf.LabelUI;
 
+
+/**
+ * <p>
+ * A swing widget that displays a text message and/or an icon. 
+ * </p>
+ */
 public class JLabel extends JComponent implements Accessible, SwingConstants
 {
+  /** DOCUMENT ME! */
   private static final long serialVersionUID = 5496508283662221534L;
+
+  /**
+   * The Component the label will give focus to when its mnemonic is
+   * activated.
+   */
+  protected Component labelFor;
+
+  /** The label's text. */
+  private transient String labelText;
+
+  /** Where the label will be positioned horizontally. */
+  private transient int horizontalAlignment = CENTER;
+
+  /** Where the label text will be placed horizontally relative to the icon. */
+  private transient int horizontalTextPosition = TRAILING;
+
+  /** Where the label will be positioned vertically. */
+  private transient int verticalAlignment = CENTER;
+
+  /** Where the label text will be place vertically relative to the icon. */
+  private transient int verticalTextPosition = CENTER;
+
+  /** The icon painted when the label is enabled. */
+  private transient Icon activeIcon;
+
+  /** The icon painted when the label is disabled. */
+  private transient Icon disabledIcon;
+
+  /** The label's mnemnonic key. */
+  private transient char mnemonicKey;
+
+  /** The index of the menemonic character in the text. */
+  private transient int underlinedChar = -1;
+
+  /** The gap between the icon and the text. */
+  private transient int iconTextGap = 4;
+
+  /**
+   * Fired in a PropertyChangeEvent when the "disabledIcon" property changes.
+   */
+  public static String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
+
+  /**
+   * Fired in a PropertyChangeEvent when the "displayedMnemonic" property
+   * changes.
+   */
+  public static String DISPLAYED_MNEMONIC_CHANGED_PROPERTY = "displayedMnemonic";
+
+  /**
+   * Fired in a PropertyChangeEvent when the "horizontalAlignment" property
+   * changes.
+   */
+  public static String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
+
+  /**
+   * Fired in a PropertyChangeEvent when the "horizontalTextPosition" property
+   * changes.
+   */
+  public static String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
+
+  /** Fired in a PropertyChangeEvent when the "icon" property changes. */
+  public static String ICON_CHANGED_PROPERTY = "icon";
+
+  /** Fired in a PropertyChangeEvent when the "iconTextGap" property changes. */
+  public static String ICON_TEXT_GAP_CHANGED_PROPERTY = "iconTextGap";
+
+  /** Fired in a PropertyChangeEvent when the "labelFor" property changes. */
+  public static String LABEL_FOR_CHANGED_PROPERTY = "labelFor";
+
+  /** Fired in a PropertyChangeEvent when the "text" property changes. */
+  public static String TEXT_CHANGED_PROPERTY = "text";
+
+  /**
+   * Fired in a PropertyChangeEvent when the "verticalAlignment" property
+   * changes.
+   */
+  public static String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
+
+  /**
+   * Fired in a PropertyChangeEvent when the "verticalTextPosition" property
+   * changes.
+   */
+  public static String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
+
+  /**
+   * Creates a new horizontally and vertically centered JLabel object with no text and no
+   * icon.
+   */
+  public JLabel()
+  {
+    this(null, null, CENTER);
+  }
+
+  /**
+   * Creates a new horizontally and vertically centered JLabel object with no text and the
+   * given icon.
+   *
+   * @param image The icon to use with the label.
+   */
+  public JLabel(Icon image)
+  {
+    this(null, image, CENTER);
+  }
+
+  /**
+   * Creates a new vertically centered JLabel object with no text and the given icon and
+   * horizontal alignment. By default, the text is TRAILING the image.
+   *
+   * @param image The icon to use with the label.
+   * @param horizontalAlignment The horizontal alignment of the label.
+   */
+  public JLabel(Icon image, int horizontalAlignment)
+  {
+    this(null, image, horizontalAlignment);
+  }
+
+  /**
+   * Creates a new horizontally and vertically centered JLabel object with no icon and the
+   * given text.
+   *
+   * @param text The text to use with the label.
+   */
+  public JLabel(String text)
+  {
+    this(text, null, CENTER);
+  }
+
+  /**
+   * Creates a new vertically centered JLabel object with no icon and the given text and
+   * horizontal alignment.
+   *
+   * @param text The text to use with the label.
+   * @param horizontalAlignment The horizontal alignment of the label.
+   */
+  public JLabel(String text, int horizontalAlignment)
+  {
+    this(text, null, horizontalAlignment);
+  }
+
+  /**
+   * Creates a new vertically centered JLabel object with the given text, icon, and horizontal
+   * alignment.
+   *
+   * @param text The text to use with the label.
+   * @param icon The icon to use with the label.
+   * @param horizontalAlignment The horizontal alignment of the label.
+   */
+  public JLabel(String text, Icon icon, int horizontalAlignment)
+  {
+    labelText = text;
+    activeIcon = icon;
+    this.horizontalAlignment = horizontalAlignment;
+    updateUI();
+  }
+
+  /**
+   * This method returns the label's UI delegate.
+   *
+   * @return The label's UI delegate.
+   */
+  public LabelUI getUI()
+  {
+    return (LabelUI) ui;
+  }
+
+  /**
+   * This method sets the label's UI delegate.
+   *
+   * @param ui The label's UI delegate.
+   */
+  public void setUI(LabelUI ui)
+  {
+    super.setUI(ui);
+  }
+
+  /**
+   * This method resets the label's UI delegate to the default UI for the
+   * current look and feel.
+   */
+  public void updateUI()
+  {
+    setUI((LabelUI) UIManager.getUI(this));
+  }
+
+  /**
+   * This method returns a name to identify which look and feel class will be
+   * the UI delegate for this label.
+   *
+   * @return The UIClass identifier. "LabelUI"
+   */
+  public String getUIClassID()
+  {
+    return "LabelUI";
+  }
+
+  /**
+   * This method is used primarily for debugging purposes and returns a string
+   * that can be used to represent this label.
+   *
+   * @return A string to represent this label.
+   */
+  protected String paramString()
+  {
+    return "JLabel";
+  }
+
+  /**
+   * This method returns the label text.
+   *
+   * @return The label text.
+   */
+  public String getText()
+  {
+    return labelText;
+  }
+
+  /**
+   * This method changes the "text" property. The given text will be painted
+   * in the label.
+   *
+   * @param text The label's text.
+   */
+  public void setText(String text)
+  {
+    if (text != labelText)
+      {
+	String oldText = labelText;
+	labelText = text;
+	firePropertyChange(TEXT_CHANGED_PROPERTY, oldText, labelText);
+      }
+  }
+
+  /**
+   * This method returns the active icon. The active icon is painted when the
+   * label is enabled.
+   *
+   * @return The active icon.
+   */
+  public Icon getIcon()
+  {
+    return activeIcon;
+  }
+
+  /**
+   * This method changes the "icon" property. This icon (the active icon) will
+   * be the one displayed when the label is enabled.
+   *
+   * @param icon The active icon.
+   */
+  public void setIcon(Icon icon)
+  {
+    if (icon != activeIcon)
+      {
+	Icon oldIcon = activeIcon;
+	activeIcon = icon;
+	firePropertyChange(ICON_CHANGED_PROPERTY, oldIcon, activeIcon);
+      }
+  }
+
+  /**
+   * This method returns the disabled icon. The disabled icon is painted when
+   * the label is disabled. If the disabled icon is null and the active icon is
+   * an ImageIcon, this method returns a grayed version of the icon. The grayed 
+   * version of the icon becomes the disabledIcon.
+   *
+   * @return The disabled icon.
+   */
+  public Icon getDisabledIcon()
+  {
+    //FIXME: We should be gray-scaling the active icon and then returning it
+    if (disabledIcon == null && activeIcon instanceof ImageIcon)
+      setDisabledIcon(activeIcon);
+    return disabledIcon;
+  }
+
+  /**
+   * This method changes the "disabledIcon" property. This icon (the disabled
+   * icon) will be the one displayed when the label is disabled.
+   *
+   * @param disabledIcon The disabled icon.
+   */
+  public void setDisabledIcon(Icon disabledIcon)
+  {
+    if (disabledIcon != this.disabledIcon)
+      {
+	Icon oldDisabledIcon = this.disabledIcon;
+	this.disabledIcon = disabledIcon;
+	firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldDisabledIcon,
+	                   this.disabledIcon);
+      }
+  }
+
+  /**
+   * This method sets the keycode that will be the label's mnemonic. If the
+   * label is used as a label for another component, the label will give
+   * focus to that component when the mnemonic is activated.
+   *
+   * @param key The keycode to use for the mnemonic.
+   */
+  public void setDisplayedMnemonic(int key)
+  {
+    setDisplayedMnemonic((char) key);
+  }
+
+  /**
+   * This method sets the character that will be the mnemonic used. If the
+   * label is used as a label for another component, the label will give
+   * focus to that component when the mnemonic is activated.
+   *
+   * @param aChar The character to use for the mnemonic.
+   */
+  public void setDisplayedMnemonic(char aChar)
+  {
+    if (aChar != mnemonicKey)
+      {
+	char oldKey = mnemonicKey;
+	mnemonicKey = aChar;
+	firePropertyChange(DISPLAYED_MNEMONIC_CHANGED_PROPERTY, oldKey,
+	                   mnemonicKey);
+	if (labelText != null)
+	  setDisplayedMnemonicIndex(labelText.indexOf(mnemonicKey));
+      }
+  }
+
+  /**
+   * This method returns the keycode that is used for the label's mnemonic.
+   *
+   * @return The keycode that is used for the label's mnemonic.
+   */
+  public int getDisplayedMnemonic()
+  {
+    return (int) mnemonicKey;
+  }
+
+  /**
+   * This method sets which character in the text will be  the underlined
+   * character. If the given index is -1, then this indicates  that there is
+   * no mnemonic. If the index is less than -1 or if the index is equal to
+   * the length, this method will throw an IllegalArgumentException.
+   *
+   * @param index The index of the character to underline.
+   *
+   * @throws IllegalArgumentException If index less than -1 or index equals
+   *         length.
+   */
+  public void setDisplayedMnemonicIndex(int index)
+                                 throws IllegalArgumentException
+  {
+    if (labelText == null)
+      {
+	underlinedChar = -1;
+	return;
+      }
+    if (index < -1 || index >= labelText.length())
+      throw new IllegalArgumentException();
+
+    if (index == -1 || labelText.charAt(index) != mnemonicKey)
+      underlinedChar = -1;
+    else
+      underlinedChar = index;
+  }
+
+  /**
+   * This method returns which character in the text will be  the underlined
+   * character.
+   *
+   * @return The index of the character that will be underlined.
+   */
+  public int getDisplayedMnemonicIndex()
+  {
+    if (labelText.length() - 1 < underlinedChar)
+      underlinedChar = labelText.length() - 1;
+    return underlinedChar;
+  }
+
+  /**
+   * This method ensures that the key is valid as a horizontal alignment.
+   * Valid keys are: LEFT, CENTER, RIGHT, LEADING, TRAILING
+   *
+   * @param key The key to check.
+   * @param message The message of the exception to be thrown if the key is
+   *        invalid.
+   *
+   * @return The key if it's valid.
+   *
+   * @throws IllegalArgumentException If the key is invalid.
+   */
+  protected int checkHorizontalKey(int key, String message)
+  {
+    if (key != LEFT && key != CENTER && key != RIGHT && key != LEADING
+        && key != TRAILING)
+      throw new IllegalArgumentException(message);
+    else
+      return key;
+  }
+
+  /**
+   * This method ensures that the key is valid as a  vertical alignment. Valid
+   * keys are: TOP, CENTER, and BOTTOM.
+   *
+   * @param key The key to check.
+   * @param message The message of the exception to be thrown if the key is
+   *        invalid.
+   *
+   * @return The key if it's valid.
+   *
+   * @throws IllegalArgumentException If the key is invalid.
+   */
+  protected int checkVerticalKey(int key, String message)
+  {
+    if (key != TOP && key != BOTTOM && key != CENTER)
+      throw new IllegalArgumentException(message);
+    else
+      return key;
+  }
+
+  /**
+   * This method returns the gap between the icon and the text.
+   *
+   * @return The gap between the icon and the text.
+   */
+  public int getIconTextGap()
+  {
+    return iconTextGap;
+  }
+
+  /**
+   * This method changes the "iconTextGap" property. The iconTextGap
+   * determines how much space there is between the icon and the text.
+   *
+   * @param iconTextGap The gap between the icon and the text.
+   */
+  public void setIconTextGap(int iconTextGap)
+  {
+    if (iconTextGap != this.iconTextGap)
+      {
+	int oldIconTextGap = this.iconTextGap;
+	this.iconTextGap = iconTextGap;
+	firePropertyChange(ICON_TEXT_GAP_CHANGED_PROPERTY, oldIconTextGap,
+	                   iconTextGap);
+      }
+  }
+
+  /**
+   * This method returns the vertical alignment of the label.
+   *
+   * @return The vertical alignment of the label.
+   */
+  public int getVerticalAlignment()
+  {
+    return verticalAlignment;
+  }
+
+  /**
+   * This method changes the "verticalAlignment" property of the label. The
+   * vertical alignment determines how where the label will be placed
+   * vertically. If the alignment is not valid, it will default to the
+   * center.
+   *
+   * @param alignment The vertical alignment of the label.
+   */
+  public void setVerticalAlignment(int alignment)
+  {
+    if (alignment != verticalAlignment)
+      {
+	int oldAlignment = verticalAlignment;
+	verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
+	firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldAlignment,
+	                   verticalAlignment);
+      }
+  }
+
+  /**
+   * This method returns the horziontal alignment of the label.
+   *
+   * @return The horizontal alignment of the label.
+   */
+  public int getHorizontalAlignment()
+  {
+    return horizontalAlignment;
+  }
+
+  /**
+   * This method changes the "horizontalAlignment" property. The horizontal
+   * alignment determines where the label will be placed horizontally.
+   *
+   * @param alignment The horizontal alignment of the label.
+   */
+  public void setHorizontalAlignment(int alignment)
+  {
+    int oldAlignment = horizontalAlignment;
+    horizontalAlignment = checkHorizontalKey(alignment, "horizontalAlignment");
+    firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, oldAlignment,
+                       horizontalAlignment);
+  }
+
+  /**
+   * This method returns the vertical text position of the label.
+   *
+   * @return The vertical text position of the label.
+   */
+  public int getVerticalTextPosition()
+  {
+    return verticalTextPosition;
+  }
+
+  /**
+   * This method changes the "verticalTextPosition" property of the label. The
+   * vertical text position determines where the text will be placed
+   * vertically relative to the icon.
+   *
+   * @param textPosition The vertical text position.
+   */
+  public void setVerticalTextPosition(int textPosition)
+  {
+    if (textPosition != verticalTextPosition)
+      {
+	int oldPos = verticalTextPosition;
+	verticalTextPosition = checkVerticalKey(textPosition,
+	                                        "verticalTextPosition");
+	firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldPos,
+	                   verticalTextPosition);
+      }
+  }
+
+  /**
+   * This method returns the horizontal text position of the label.
+   *
+   * @return The horizontal text position.
+   */
+  public int getHorizontalTextPosition()
+  {
+    return horizontalTextPosition;
+  }
+
+  /**
+   * This method changes the "horizontalTextPosition" property of the label.
+   * The horizontal text position determines where the text will be placed
+   * horizontally relative to the icon.
+   *
+   * @param textPosition The horizontal text position.
+   */
+  public void setHorizontalTextPosition(int textPosition)
+  {
+    if (textPosition != horizontalTextPosition)
+      {
+	int oldPos = horizontalTextPosition;
+	horizontalTextPosition = checkHorizontalKey(textPosition,
+	                                            "horizontalTextPosition");
+	firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, oldPos,
+	                   horizontalTextPosition);
+      }
+  }
+
+  /**
+   * This method simply returns false if the current icon image (current  icon
+   * will depend on whether the label is enabled) is not equal to the passed
+   * in image.
+   *
+   * @param img The image to check.
+   * @param infoflags The bitwise inclusive OR of ABORT, ALLBITS, ERROR,
+   *        FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
+   * @param x The x position
+   * @param y The y position
+   * @param w The width
+   * @param h The height
+   *
+   * @return Whether the current icon image is equal to the image given.
+   */
+  public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
+                             int h)
+  {
+    Icon currIcon = (isEnabled()) ? activeIcon : disabledIcon;
+
+    //Is this the correct way to check for image equality?
+    if (currIcon != null && currIcon instanceof ImageIcon)
+      return (((ImageIcon) currIcon).getImage() == img);
+    return false;
+  }
+
+  /**
+   * This method returns the component that the label gives focus to  when the
+   * mnemonic is activated.
+   *
+   * @return The component that gets focus when the label's mnemonic is
+   *         activated.
+   */
+  public Component getLabelFor()
+  {
+    return labelFor;
+  }
+
+  /**
+   * This method changes the "labelFor" property. The component that the label
+   * is acting as a label for will request focus when the label's  mnemonic
+   * is activated.
+   *
+   * @param c The component that gets focus when the label's mnemonic is
+   *        activated.
+   */
+  public void setLabelFor(Component c)
+  {
+    if (c != labelFor)
+      {
+	Component oldLabelFor = labelFor;
+	labelFor = c;
+	firePropertyChange(LABEL_FOR_CHANGED_PROPERTY, oldLabelFor, labelFor);
+      }
+  }
   
-    String text;
-    Icon icon;
-    int gap;
-    int align;
-
-    int hor_align;
-    int hor_text_pos;
-
-    int vert_align;
-    int vert_text_pos;
-
-    public JLabel()
-    {
-	this("", null, 0);
-    }
-
-    public JLabel(Icon image)
-    {
-	this("", image, 0);
-    }
-
-    public JLabel(Icon image, int horizontalAlignment)
-    {
-	this("", image, horizontalAlignment);
-    }
-
-    public JLabel(String text)
-    {
-	this(text, null, 0);
-    }
-
-    public JLabel(String text, int horizontalAlignment)
-    {
-	this(text, null, horizontalAlignment);
-    }
-
-    public JLabel(String text, Icon icon, int horizontalAlignment)
-    {
-	// do the work.....
-	this.text = text;
-	setIcon(icon);
-	this.align     = horizontalAlignment;
-
-	updateUI(); // get a proper ui
-    } 
-
-
-    protected  int checkHorizontalKey(int key, String message)
-    {
-	//    Verify that key is a legal value for the horizontalAlignment properties. 
-	return 0;
-    }
-    protected  int checkVerticalKey(int key, String message)
-    {
-	//      Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.  
-	return 0;
-    }
-    public AccessibleContext getAccessibleContext()
-    {
-	//          Get the AccessibleContext of this object 
-	return null;
-    }
-    public Icon getDisabledIcon()
-    {
-	//          Returns the value of the disabledIcon property if it's been set, If it hasn't been set and the value of the icon property is an ImageIcon, we compute a "grayed out" version of the icon and update the disabledIcon property with that.  
-	return null;
-    }
-    public int getDisplayedMnemonic()
-    {
-	//          Return the keycode that indicates a mnemonic key.   
-	return 0;
-    }
-    public int getHorizontalAlignment()
-    {
-	//          Returns the alignment of the label's contents along the X axis.   
-	return hor_align;
-    }
-    public int getHorizontalTextPosition()
-    {
-	//          Returns the horizontal position of the label's text, relative to its image.    
-	return hor_text_pos;
-    }
-
-    public Icon getIcon()
-    {	return icon;    }
-
-    public int getIconTextGap()
-    {
-	//          Returns the amount of space between the text and the icon displayed in this label.   
-	return 0;
-    }
-    public Component getLabelFor()
-    {
-	//          Get the component this is labelling.  
-	return null;
-    }
-    public String getText()
-    {	return text;    }
-
-    public String getUIClassID()
-    {	return "LabelUI";    }
-
-    public int getVerticalAlignment()
-    {
-	//          Returns the alignment of the label's contents along the Y axis. 
-	return vert_align;
-    }
-    public int getVerticalTextPosition()
-    {
-	//          Returns the vertical position of the label's text, relative to its image. 
-	return vert_text_pos;
-    }
-
-    public boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
-    {
-	//          This is overriden to return false if the current Icon's Image is not equal to the passed in Image img. 
-	return (img == icon);
-    }
-    protected  String paramString()
-    {
-	//          Returns a string representation of this JLabel.  
-	return "JLabel";
-    }
-    public void setDisabledIcon(Icon disabledIcon)
-    {
-	//          Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)).  
-    }
-    public void setDisplayedMnemonic(char aChar)
-    {
-	//          Specifies the displayedMnemonic as a char value.  
-    }
-    public void setDisplayedMnemonic(int key)
-    {
-	//          Specify a keycode that indicates a mnemonic key.  
-    }
-    public void setHorizontalAlignment(int alignment)
-    {
-	//          Sets the alignment of the label's contents along the X axis.  
-	hor_align = alignment;
-    }
-    public void setHorizontalTextPosition(int textPosition)
-    {
-	//          Sets the horizontal position of the label's text, relative to its image.  
-	hor_text_pos = textPosition;
-    }
-    public void setIcon(Icon icon)
-    {
-	this.icon = icon;
-	if (icon != null)
-	    {
-                  // XXX FIXME - icons do not know their parent
-//  		icon.setParent(this);
-	    }
-	revalidate();
-	repaint();
-    }
-
-    public void setIconTextGap(int iconTextGap)
-    {
-	gap = iconTextGap;
-    }
-  
-    public void setLabelFor(Component c)
-    {
-	//          Set the component this is labelling.  
-    }
-    public void setText(String text)
-    {
-	this.text = text;
-	revalidate();
-	repaint();
-    }
-  
-    public void setVerticalAlignment(int alignment)
-    {
-	//          Sets the alignment of the label's contents along the Y axis.  
-	vert_align = alignment;
-    }
-    public void setVerticalTextPosition(int textPosition)
-    {
-	//          Sets the vertical position of the label's text, relative to its image.  
-	vert_text_pos = textPosition;
-    }
-    public void updateUI()
-    {	
-	LabelUI b = (LabelUI)UIManager.getUI(this);
-	setUI(b);
-    }
+  /**
+   * This method overrides setFont so that we can call for a repaint
+   * after the font is changed.
+   *
+   * @param f The font for this label.
+   */
+  public void setFont(Font f)
+  {
+    super.setFont(f);
+    repaint();
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @return
+   */
+  public AccessibleContext getAccessibleContext()
+  {
+    return null;
+  }
 }
Index: javax/swing/plaf/basic/BasicLabelUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicLabelUI.java,v
retrieving revision 1.4
diff -u -r1.4 BasicLabelUI.java
--- javax/swing/plaf/basic/BasicLabelUI.java	13 Jul 2003 15:29:11 -0000	1.4
+++ javax/swing/plaf/basic/BasicLabelUI.java	16 Feb 2004 16:47:29 -0000
@@ -1,5 +1,5 @@
 /* BasicLabelUI.java
-   Copyright (C) 2002 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -35,7 +35,6 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
-
 package javax.swing.plaf.basic;
 
 import java.awt.Color;
@@ -47,156 +46,377 @@
 import java.awt.Rectangle;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import javax.swing.Icon;
 import javax.swing.JComponent;
 import javax.swing.JLabel;
+import javax.swing.SwingConstants;
 import javax.swing.SwingUtilities;
+import javax.swing.UIDefaults;
+import javax.swing.UIManager;
 import javax.swing.plaf.ComponentUI;
 import javax.swing.plaf.LabelUI;
 
-public class BasicLabelUI extends LabelUI
-  implements PropertyChangeListener
+
+/**
+ * This is the Basic Look and Feel class for the JLabel.  One BasicLabelUI
+ * object is used to paint all JLabels that utilize the Basic L&F.
+ */
+public class BasicLabelUI extends LabelUI implements PropertyChangeListener
 {
-    int gap = 3;
-    Color foreground;
+  /** The labelUI that is shared by all labels. */
+  protected static BasicLabelUI labelUI;
 
-    
-    public static ComponentUI createUI(final JComponent c)  {
-	return new BasicLabelUI();
-    }
-    
-    
-    public void installUI(final JComponent c)  {
-	super.installUI(c);
-	
-	foreground = new Color(0,0,250);
-    }
-    
+  /**
+   * Creates a new BasicLabelUI object.
+   */
+  public BasicLabelUI()
+  {
+    super();
+  }
 
-    public Dimension getPreferredSize(JComponent c) 
-    {
-	JLabel b = (JLabel)c;
-        /*
-          We cannot use this method because it is not part of the
-          official Swing API.
-
-	Dimension d = BasicGraphicsUtils.getPreferredSize(b, 
-							  gap,
-							  b.getText(),
-							  b.getIcon(),
-							  b.getVerticalAlignment(),
-							  b.getHorizontalAlignment(),
-							  b.getHorizontalTextPosition(),
-							  b.getVerticalTextPosition());
-	System.out.println("JLABEL->^^^^^^^^^^^^^^^^^^^^^^   BASIC-PREF="+d + ",T="+b.getText());
-        */
-        return new Dimension(100, 30);
-    }
-    
+  /**
+   * Creates and returns a UI for the label. Since one UI is shared by  all
+   * labels, this means creating only if necessary and returning the  shared
+   * UI.
+   *
+   * @param c The {@link JComponent} that a UI is being created for.
+   *
+   * @return A label UI for the Basic L&F.
+   */
+  public static ComponentUI createUI(JComponent c)
+  {
+    if (labelUI == null)
+      labelUI = new BasicLabelUI();
+    return labelUI;
+  }
+
+  /**
+   * This method returns the preferred size of the {@link JComponent} given.
+   * If this method returns null (which it does by default), then it is up to
+   * the Layout Manager to give this component a size.
+   *
+   * @param c This {@link JComponent} to get a preferred size for.
+   *
+   * @return The preferred size.
+   */
+  public Dimension getPreferredSize(JComponent c)
+  {
+    return null;
+  }
 
-    public void paint(Graphics g, JComponent c)
-    {      
-	JLabel b = (JLabel) c;
-
-	Rectangle tr = new Rectangle();
-	Rectangle ir = new Rectangle();
-	Rectangle vr = new Rectangle();
-
-        Font f = c.getFont();
-
-        g.setFont(f);
-
-        FontMetrics fm = g.getFontMetrics(f);
-
-        Insets i = c.getInsets();
-
-	Rectangle bound = c.getBounds();
-	
-	System.out.println("BOUND=" + bound + ", insets = " + i + ", " + b.getText());
-	
-	if (bound == null)
-	    {
-		vr.x      = i.left;
-		vr.y      = i.top;
-		vr.width  = b.getWidth() - (i.right  + i.left);
-		vr.height = b.getHeight() - (i.bottom + i.top);
-	    }
-	else
-	    {
-		vr.x      = bound.x + i.left;
-		vr.y      = bound.y + i.top;
-		vr.width  = bound.width - (i.right  + i.left);
-		vr.height = bound.height - (i.bottom + i.top);
-	    }
-
-	System.out.println("             VIEW-RECT-JLABEL="+vr+", insets="+i+", FONTM="+fm);
-
-	String text = SwingUtilities.layoutCompoundLabel(c,
-							 fm, 
-							 b.getText(),
-							 b.getIcon(),
-							 b.getVerticalAlignment(), 
-							 b.getHorizontalAlignment(),
-							 b.getVerticalTextPosition(), 
-							 b.getHorizontalTextPosition(),
-							 vr,
-							 ir,
-							 tr,
-							 gap);
-
-	paintIcon(g, c, ir);
-	paintText(g, c, tr, b.getText());
-	paintFocus(g, c, vr, tr, ir);
-    }
+  /**
+   * This method returns the minimum size of the {@link JComponent} given. If
+   * this method returns null, then it is up to the Layout Manager to give
+   * this component a minimum size.
+   *
+   * @param c The {@link JComponent} to get a minimum size for.
+   *
+   * @return The minimum size.
+   */
+  public Dimension getMinimumSize(JComponent c)
+  {
+    return getPreferredSize(c);
+  }
 
+  /**
+   * This method returns the maximum size of the {@link JComponent} given. If
+   * this method returns null, then it is up to the Layout Manager to give
+   * this component a maximum size.
+   *
+   * @param c The {@link JComponent} to get a maximum size for.
+   *
+   * @return The maximum size.
+   */
+  public Dimension getMaximumSize(JComponent c)
+  {
+    return getPreferredSize(c);
+  }
 
-    protected void paintFocus(Graphics g, 
-			      JComponent c,
-			      Rectangle vr,
-			      Rectangle tr,
-			      Rectangle ir)
-    {
-    }
+  /**
+   * The method that paints the label according to its current state.
+   *
+   * @param g The {@link Graphics} object to paint with.
+   * @param c The {@link JComponent} to paint.
+   */
+  public void paint(Graphics g, JComponent c)
+  {
+    JLabel b = (JLabel) c;
 
-    protected void paintIcon(Graphics g, 
-			     JComponent c, 
-			     Rectangle iconRect)
-    {
-	JLabel b = (JLabel) c;
-	if (b.getIcon() != null)
-	    {
-		int x = iconRect.x;
-		int y = iconRect.y;
-
-		System.out.println("WE HAVE AN ICON: " + b.getIcon());
- 
-		b.getIcon().paintIcon(c, g, x, y);
-	    }
-	else
-	    {
-		//System.out.println("NO ICON FOR BUTTON:" + b.text);
-	    }
-    }
+    Font saved_font = g.getFont();
+
+    Rectangle tr = new Rectangle();
+    Rectangle ir = new Rectangle();
+    Rectangle vr = new Rectangle();
+
+    Font f = c.getFont();
+
+    g.setFont(f);
+    FontMetrics fm = g.getFontMetrics(f);
+
+    vr = SwingUtilities.calculateInnerArea(c, vr);
 
+    if (vr.width < 0)
+      vr.width = 0;
+    if (vr.height < 0)
+      vr.height = 0;
+
+    Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon();
+
+    String text = layoutCL(b, fm, b.getText(), icon, vr, ir, tr);
     
-    protected void paintText(Graphics g,
-			     JComponent c,
-			     Rectangle textRect,
-			     String text) 
+    if (b.isOpaque())
     {
-	//        AbstractLabel b = (AbstractLabel) c;
-	
-	System.out.println("JLabel: drawing string: " + text + ", at:" + textRect);
-	
-	g.setColor(foreground);
-	//g.setBackColor(new Color(190,190,190));
-
-	g.drawLine(0,0,100,100);
-	
-	BasicGraphicsUtils.drawString(g, text, 0, 0 /*textRect.x*/, 0 /*textRect.y*/);
+      g.setColor(b.getBackground());
+      g.fillRect(vr.x, vr.y, vr.width, vr.height);
     }
 
-  public void propertyChange (PropertyChangeEvent event)
+    if (icon != null)
+      icon.paintIcon(b, g, ir.x, ir.y);
+    if (b.isEnabled())
+      paintEnabledText(b, g, text, tr.x, tr.y + fm.getAscent());
+    else
+      paintDisabledText(b, g, text, tr.x, tr.y + fm.getAscent());
+    g.setFont(saved_font);
+  }
+
+  /**
+   * This method is simply calls SwingUtilities's layoutCompoundLabel.
+   *
+   * @param label The label to lay out.
+   * @param fontMetrics The FontMetrics for the font used.
+   * @param text The text to paint.
+   * @param icon The icon to draw.
+   * @param viewR The entire viewable rectangle.
+   * @param iconR The icon bounds rectangle.
+   * @param textR The text bounds rectangle.
+   *
+   * @return A possibly clipped version of the text.
+   */
+  protected String layoutCL(JLabel label, FontMetrics fontMetrics,
+                            String text, Icon icon, Rectangle viewR,
+                            Rectangle iconR, Rectangle textR)
+  {
+    return SwingUtilities.layoutCompoundLabel(label, fontMetrics, text, icon,
+                                              label.getVerticalAlignment(),
+                                              label.getHorizontalAlignment(),
+                                              label.getVerticalTextPosition(),
+                                              label.getHorizontalTextPosition(),
+                                              viewR, iconR, textR,
+                                              label.getIconTextGap());
+  }
+
+  /**
+   * Paints the text if the label is disabled. By default, this paints the
+   * clipped text returned by layoutCompoundLabel using the
+   * background.brighter() color. It also paints the same text using the
+   * background.darker() color one pixel to the right and one pixel down.
+   *
+   * @param l The {@link JLabel} being painted.
+   * @param g The {@link Graphics} object to paint with.
+   * @param s The String to paint.
+   * @param textX The x coordinate of the start of the baseline.
+   * @param textY The y coordinate of the start of the baseline.
+   */
+  protected void paintDisabledText(JLabel l, Graphics g, String s, int textX,
+                                   int textY)
+  {
+    Color saved_color = g.getColor();
+
+    g.setColor(l.getBackground().brighter());
+
+    int mnemIndex = l.getDisplayedMnemonicIndex();
+
+    if (mnemIndex != -1)
+      BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
+                                                   textY);
+    else
+      g.drawString(s, textX, textY);
+
+    g.setColor(l.getBackground().darker());
+    if (mnemIndex != -1)
+      BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX + 1,
+                                                   textY + 1);
+    else
+      g.drawString(s, textX + 1, textY + 1);
+
+    g.setColor(saved_color);
+  }
+
+  /**
+   * Paints the text if the label is enabled. The text is painted using the
+   * foreground color.
+   *
+   * @param l The {@link JLabel} being painted.
+   * @param g The {@link Graphics} object to paint with.
+   * @param s The String to paint.
+   * @param textX The x coordinate of the start of the baseline.
+   * @param textY The y coordinate of the start of the baseline.
+   */
+  protected void paintEnabledText(JLabel l, Graphics g, String s, int textX,
+                                  int textY)
+  {
+    Color saved_color = g.getColor();
+    if (l.getForeground() == null)
+      System.out.println("CRAP");
+    g.setColor(l.getForeground());
+
+    int mnemIndex = l.getDisplayedMnemonicIndex();
+
+    if (mnemIndex != -1)
+      BasicGraphicsUtils.drawStringUnderlineCharAt(g, s, mnemIndex, textX,
+                                                   textY);
+    else
+      g.drawString(s, textX, textY);
+
+    g.setColor(saved_color);
+  }
+
+  /**
+   * This method installs the UI for the given {@link JComponent}.  This
+   * method will install the component, defaults, listeners,  and keyboard
+   * actions.
+   *
+   * @param c The {@link JComponent} that this UI is being installed on.
+   */
+  public void installUI(JComponent c)
+  {
+    super.installUI(c);
+    if (c instanceof JLabel)
+      {
+	JLabel l = (JLabel) c;
+
+	installComponents(l);
+	installDefaults(l);
+	installListeners(l);
+	installKeyboardActions(l);
+      }
+  }
+
+  /**
+   * This method uninstalls the UI for the given {@link JComponent}. This
+   * method will uninstall the component, defaults, listeners,  and keyboard
+   * actions.
+   *
+   * @param c The {@link JComponent} that this UI is being installed on.
+   */
+  public void uninstallUI(JComponent c)
+  {
+    super.uninstallUI(c);
+    if (c instanceof JLabel)
+      {
+	JLabel l = (JLabel) c;
+
+	uninstallKeyboardActions(l);
+	uninstallListeners(l);
+	uninstallDefaults(l);
+	uninstallComponents(l);
+      }
+  }
+
+  /**
+   * This method installs the components for this {@link JLabel}.
+   *
+   * @param c The {@link JLabel} to install components for.
+   */
+  protected void installComponents(JLabel c)
+  {
+    //FIXME: fix javadoc + implement.
+  }
+
+  /**
+   * This method uninstalls the components for this {@link JLabel}.
+   *
+   * @param c The {@link JLabel} to uninstall components for.
+   */
+  protected void uninstallComponents(JLabel c)
+  {
+    //FIXME: fix javadoc + implement.
+  }
+
+  /**
+   * This method installs the defaults that are defined in  the Basic look and
+   * feel for this {@link JLabel}.
+   *
+   * @param c The {@link JLabel} to install defaults for.
+   */
+  protected void installDefaults(JLabel c)
+  {
+    UIDefaults defaults = UIManager.getLookAndFeelDefaults();
+
+    c.setForeground(defaults.getColor("Label.foreground"));
+    c.setBackground(defaults.getColor("Label.background"));
+    c.setFont(defaults.getFont("Label.font"));
+    c.setBorder(defaults.getBorder("Label.border"));
+    //XXX: There are properties we don't use called disabledForeground
+    //and disabledShadow.
+  }
+
+  /**
+   * This method uninstalls the defaults that are defined in the Basic look
+   * and feel for this {@link JLabel}.
+   *
+   * @param c The {@link JLabel} to uninstall defaults for.
+   */
+  protected void uninstallDefaults(JLabel c)
+  {
+    c.setForeground(null);
+    c.setBackground(null);
+    c.setFont(null);
+    c.setBorder(null);
+  }
+
+  /**
+   * This method installs the keyboard actions for the given {@link JLabel}.
+   *
+   * @param l The {@link JLabel} to install keyboard actions for.
+   */
+  protected void installKeyboardActions(JLabel l)
+  {
+    //FIXME: implement.
+  }
+
+  /**
+   * This method uninstalls the keyboard actions for the given {@link JLabel}.
+   *
+   * @param l The {@link JLabel} to uninstall keyboard actions for.
+   */
+  protected void uninstallKeyboardActions(JLabel l)
+  {
+    //FIXME: implement.
+  }
+
+  /**
+   * This method installs the listeners for the  given {@link JLabel}. The UI
+   * delegate only listens to  the label.
+   *
+   * @param c The {@link JLabel} to install listeners for.
+   */
+  protected void installListeners(JLabel c)
+  {
+    c.addPropertyChangeListener(this);
+  }
+
+  /**
+   * This method uninstalls the listeners for the given {@link JLabel}. The UI
+   * delegate only listens to the label.
+   *
+   * @param c The {@link JLabel} to uninstall listeners for.
+   */
+  protected void uninstallListeners(JLabel c)
+  {
+    c.removePropertyChangeListener(this);
+  }
+
+  /**
+   * This method is called whenever any JLabel's that use this UI has one of
+   * their properties change.
+   *
+   * @param e The {@link PropertyChangeEvent} that describes the change.
+   */
+  public void propertyChange(PropertyChangeEvent e)
   {
-    throw new Error ("Not implemented");
+    JLabel c = (JLabel) e.getSource();
+    c.revalidate();
+    c.repaint();
   }
 }
Index: javax/swing/plaf/basic/BasicLookAndFeel.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicLookAndFeel.java,v
retrieving revision 1.4.2.1
diff -u -r1.4.2.1 BasicLookAndFeel.java
--- javax/swing/plaf/basic/BasicLookAndFeel.java	13 Feb 2004 16:21:46 -0000	1.4.2.1
+++ javax/swing/plaf/basic/BasicLookAndFeel.java	16 Feb 2004 16:47:29 -0000
@@ -434,6 +434,7 @@
       "Label.disabledForeground", new ColorUIResource(Color.white),
       "Label.disabledShadow", new ColorUIResource(Color.gray),
       "Label.font", new FontUIResource("Dialog", Font.PLAIN, 12),
+      "Label.foreground", new ColorUIResource(Color.black),
       "List.focusInputMap", new UIDefaults.LazyInputMap(new Object[] {
         "PAGE_UP", "scrollUp",
         "ctrl \\", "clearSelection",

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]