Source for java.awt.Label

   1: /* Label.java -- Java label widget
   2:    Copyright (C) 1999, 2000, 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt;
  40: 
  41: import java.awt.peer.LabelPeer;
  42: 
  43: import javax.accessibility.Accessible;
  44: import javax.accessibility.AccessibleContext;
  45: import javax.accessibility.AccessibleRole;
  46: 
  47: /**
  48:   * This component is used for displaying simple text strings that cannot
  49:   * be edited by the user.
  50:   *
  51:   * @author Aaron M. Renn (arenn@urbanophile.com)
  52:   * @author Tom Tromey (tromey@cygnus.com)
  53:   * @author Andrew John Hughes  (gnu_andrew@member.fsf.org)
  54:   */
  55: public class Label extends Component implements Accessible
  56: {
  57: 
  58: /*
  59:  * Static Variables
  60:  */
  61: 
  62: /**
  63:   * Alignment constant aligning the text to the left of its window.
  64:   */
  65: public static final int LEFT = 0;
  66: 
  67: /**
  68:   * Alignment constant aligning the text in the center of its window.
  69:   */
  70: public static final int CENTER = 1;
  71: 
  72: /**
  73:   * Alignment constant aligning the text to the right of its window.
  74:   */
  75: public static final int RIGHT = 2;
  76: 
  77: // Serialization version constant:
  78: private static final long serialVersionUID = 3094126758329070636L;
  79: 
  80: /*************************************************************************/
  81: 
  82: /*
  83:  * Instance Variables
  84:  */
  85: 
  86: /**
  87:   * @serial Indicates the alignment of the text within this label's window.
  88:   * This is one of the constants in this class.  The default value is 
  89:   * <code>LEFT</code>.
  90:   */
  91: private int alignment;
  92: 
  93: /**
  94:   * @serial The text displayed in the label
  95:   */
  96: private String text;
  97: 
  98: /*************************************************************************/
  99: 
 100: /*
 101:  * Constructors
 102:  */
 103: 
 104: /**
 105:   * Initializes a new instance of <code>Label</code> with no text.
 106:   *
 107:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 108:   */
 109: public
 110: Label()
 111: {
 112:   this("", LEFT);
 113: }
 114: 
 115: /*************************************************************************/
 116: 
 117: /**
 118:   * Initializes a new instance of <code>Label</code> with the specified
 119:   * text that is aligned to the left.
 120:   *
 121:   * @param text The text of the label.
 122:   *
 123:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 124:   */
 125: public
 126: Label(String text)
 127: {
 128:   this(text, LEFT);
 129: }
 130: 
 131: /*************************************************************************/
 132: 
 133: /**
 134:   * Initializes a new instance of <code>Label</code> with the specified
 135:   * text and alignment.
 136:   *
 137:   * @param text The text of the label.
 138:   * @param alignment The desired alignment for the text in this label,
 139:   * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
 140:   * <code>RIGHT</code>.
 141:   *
 142:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
 143:   */
 144: public
 145: Label(String text, int alignment)
 146: {
 147:   setAlignment (alignment);
 148:   setText (text);
 149: 
 150:   if (GraphicsEnvironment.isHeadless())
 151:     throw new HeadlessException ();
 152: }
 153: 
 154: /*************************************************************************/
 155: 
 156: /*
 157:  * Instance Variables
 158:  */
 159: 
 160: /**
 161:   * Returns the constant indicating the alignment of the text in this
 162:   * label.  The value returned will be one of the alignment constants
 163:   * from this class.
 164:   *
 165:   * @return The alignment of the text in the label.
 166:   */
 167: public int
 168: getAlignment()
 169: {
 170:   return(alignment);
 171: }
 172: 
 173: /*************************************************************************/
 174: 
 175: /**
 176:   * Sets the text alignment of this label to the specified value.
 177:   *
 178:   * @param alignment The desired alignment for the text in this label,
 179:   * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
 180:   * <code>RIGHT</code>.
 181:   */
 182: public synchronized void
 183: setAlignment(int alignment)
 184: {
 185:   if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
 186:     throw new IllegalArgumentException ("invalid alignment: " + alignment);
 187:   this.alignment = alignment;
 188:   if (peer != null)
 189:     {
 190:       LabelPeer lp = (LabelPeer) peer;
 191:       lp.setAlignment (alignment);
 192:     }
 193: }
 194: 
 195: /*************************************************************************/
 196: 
 197: /**
 198:   * Returns the text displayed in this label.
 199:   *
 200:   * @return The text for this label.
 201:   */
 202: public String
 203: getText()
 204: {
 205:   return(text);
 206: }
 207: 
 208: /*************************************************************************/
 209: 
 210: /**
 211:   * Sets the text in this label to the specified value.
 212:   *
 213:   * @param text The new text for this label.
 214:   */
 215: public synchronized void
 216: setText(String text)
 217: {
 218:   if ((this.text == null && text != null)
 219:       || (this.text != null && ! this.text.equals(text)))
 220:     {
 221:       this.text = text;
 222: 
 223:       if (peer != null)
 224:     {
 225:       LabelPeer lp = (LabelPeer) peer;
 226:       lp.setText (text);
 227:     }
 228:       invalidate();
 229:     }
 230: }
 231: 
 232: /*************************************************************************/
 233: 
 234: /**
 235:   * Notifies this label that it has been added to a container, causing
 236:   * the peer to be created.  This method is called internally by the AWT
 237:   * system.
 238:   */
 239: public void
 240: addNotify()
 241: {
 242:   if (peer == null)
 243:     peer = getToolkit ().createLabel (this);
 244:   super.addNotify ();
 245: }
 246: 
 247: /*************************************************************************/
 248: 
 249: /**
 250:   * Returns a parameter string useful for debugging.
 251:   *
 252:   * @return A debugging string.
 253:   */
 254: protected String
 255: paramString()
 256: {
 257:   return ("text=" + getText() + ",alignment=" +
 258:       getAlignment() + "," + super.paramString());
 259: }
 260: 
 261: /**
 262:  * This class provides accessibility support for the label.
 263:  */
 264: protected class AccessibleAWTLabel
 265:   extends AccessibleAWTComponent
 266: {
 267:   /**
 268:    * For compatability with Sun's JDK 1.4.2 rev. 5
 269:    */
 270:   private static final long serialVersionUID = -3568967560160480438L;
 271: 
 272:   /**
 273:    * Constructor for the accessible label.
 274:    */
 275:   public AccessibleAWTLabel()
 276:   {
 277:   }
 278: 
 279:   /**
 280:    * Returns the accessible name for the label.  This is
 281:    * the text used in the label.
 282:    *
 283:    * @return a <code>String</code> containing the accessible
 284:    *         name for this label.
 285:    */
 286:   public String getAccessibleName()
 287:   {
 288:     return getText();
 289:   }
 290: 
 291:   /**
 292:    * Returns the accessible role for the label.
 293:    *
 294:    * @return an instance of <code>AccessibleRole</code>, describing
 295:    *         the role of the label.
 296:    */
 297:   public AccessibleRole getAccessibleRole()
 298:   {
 299:     return AccessibleRole.LABEL;
 300:   }
 301: 
 302: }
 303: 
 304: /**
 305:  * Gets the AccessibleContext associated with this <code>Label</code>.
 306:  * The context is created, if necessary.
 307:  *
 308:  * @return the associated context
 309:  */
 310: public AccessibleContext getAccessibleContext()
 311: {
 312:   /* Create the context if this is the first request */
 313:   if (accessibleContext == null)
 314:     accessibleContext = new AccessibleAWTLabel();
 315:   return accessibleContext;
 316: }
 317: 
 318: } // class Label