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:   this.text = text;
 219: 
 220:   if (peer != null)
 221:     {
 222:       LabelPeer lp = (LabelPeer) peer;
 223:       lp.setText (text);
 224:     }
 225: }
 226: 
 227: /*************************************************************************/
 228: 
 229: /**
 230:   * Notifies this label that it has been added to a container, causing
 231:   * the peer to be created.  This method is called internally by the AWT
 232:   * system.
 233:   */
 234: public void
 235: addNotify()
 236: {
 237:   if (peer == null)
 238:     peer = getToolkit ().createLabel (this);
 239:   super.addNotify ();
 240: }
 241: 
 242: /*************************************************************************/
 243: 
 244: /**
 245:   * Returns a parameter string useful for debugging.
 246:   *
 247:   * @return A debugging string.
 248:   */
 249: protected String
 250: paramString()
 251: {
 252:   return ("text=" + getText() + ",alignment=" +
 253:       getAlignment() + "," + super.paramString());
 254: }
 255: 
 256: /**
 257:  * This class provides accessibility support for the label.
 258:  */
 259: protected class AccessibleAWTLabel
 260:   extends AccessibleAWTComponent
 261: {
 262:   /**
 263:    * For compatability with Sun's JDK 1.4.2 rev. 5
 264:    */
 265:   private static final long serialVersionUID = -3568967560160480438L;
 266: 
 267:   /**
 268:    * Constructor for the accessible label.
 269:    */
 270:   public AccessibleAWTLabel()
 271:   {
 272:   }
 273: 
 274:   /**
 275:    * Returns the accessible name for the label.  This is
 276:    * the text used in the label.
 277:    *
 278:    * @return a <code>String</code> containing the accessible
 279:    *         name for this label.
 280:    */
 281:   public String getAccessibleName()
 282:   {
 283:     return getText();
 284:   }
 285: 
 286:   /**
 287:    * Returns the accessible role for the label.
 288:    *
 289:    * @return an instance of <code>AccessibleRole</code>, describing
 290:    *         the role of the label.
 291:    */
 292:   public AccessibleRole getAccessibleRole()
 293:   {
 294:     return AccessibleRole.LABEL;
 295:   }
 296: 
 297: }
 298: 
 299: /**
 300:  * Gets the AccessibleContext associated with this <code>Label</code>.
 301:  * The context is created, if necessary.
 302:  *
 303:  * @return the associated context
 304:  */
 305: public AccessibleContext getAccessibleContext()
 306: {
 307:   /* Create the context if this is the first request */
 308:   if (accessibleContext == null)
 309:     accessibleContext = new AccessibleAWTLabel();
 310:   return accessibleContext;
 311: }
 312: 
 313: } // class Label