1 /* Label.java -- Java label widget
2 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, Free Software
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import java.awt.peer.LabelPeer;
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
49 * This component is used for displaying simple text strings that cannot
50 * be edited by the user.
52 * @author Aaron M. Renn (arenn@urbanophile.com)
53 * @author Tom Tromey (tromey@cygnus.com)
54 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
56 public class Label extends Component implements Accessible
60 * Alignment constant aligning the text to the left of its window.
62 public static final int LEFT = 0;
65 * Alignment constant aligning the text in the center of its window.
67 public static final int CENTER = 1;
70 * Alignment constant aligning the text to the right of its window.
72 public static final int RIGHT = 2;
74 // Serialization version constant:
75 private static final long serialVersionUID = 3094126758329070636L;
78 * @serial Indicates the alignment of the text within this label's window.
79 * This is one of the constants in this class. The default value is
82 private int alignment;
85 * @serial The text displayed in the label
90 * Initializes a new instance of <code>Label</code> with no text.
92 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
100 * Initializes a new instance of <code>Label</code> with the specified
101 * text that is aligned to the left.
103 * @param text The text of the label.
105 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
107 public Label(String text)
113 * Initializes a new instance of <code>Label</code> with the specified
114 * text and alignment.
116 * @param text The text of the label.
117 * @param alignment The desired alignment for the text in this label,
118 * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
119 * <code>RIGHT</code>.
121 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
123 public Label(String text, int alignment)
125 setAlignment(alignment);
128 if (GraphicsEnvironment.isHeadless())
129 throw new HeadlessException();
133 * Returns the constant indicating the alignment of the text in this
134 * label. The value returned will be one of the alignment constants
137 * @return The alignment of the text in the label.
139 public int getAlignment()
145 * Sets the text alignment of this label to the specified value.
147 * @param alignment The desired alignment for the text in this label,
148 * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
149 * <code>RIGHT</code>.
151 public synchronized void setAlignment(int alignment)
153 if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
154 throw new IllegalArgumentException("invalid alignment: " + alignment);
155 this.alignment = alignment;
158 LabelPeer lp = (LabelPeer) peer;
159 lp.setAlignment(alignment);
164 * Returns the text displayed in this label.
166 * @return The text for this label.
168 public String getText()
174 * Sets the text in this label to the specified value.
176 * @param text The new text for this label.
178 public synchronized void setText(String text)
180 if ((this.text == null && text != null)
181 || (this.text != null && ! this.text.equals(text)))
187 LabelPeer lp = (LabelPeer) peer;
195 * Notifies this label that it has been added to a container, causing
196 * the peer to be created. This method is called internally by the AWT
199 public void addNotify()
202 peer = getToolkit().createLabel(this);
207 * Returns a parameter string useful for debugging.
209 * @return A debugging string.
211 protected String paramString()
213 return ("text=" + getText() + ",alignment=" +
214 getAlignment() + "," + super.paramString());
218 * This class provides accessibility support for the label.
220 protected class AccessibleAWTLabel
221 extends AccessibleAWTComponent
224 * For compatability with Sun's JDK 1.4.2 rev. 5
226 private static final long serialVersionUID = -3568967560160480438L;
229 * Constructor for the accessible label.
231 public AccessibleAWTLabel()
236 * Returns the accessible name for the label. This is
237 * the text used in the label.
239 * @return a <code>String</code> containing the accessible
240 * name for this label.
242 public String getAccessibleName()
248 * Returns the accessible role for the label.
250 * @return an instance of <code>AccessibleRole</code>, describing
251 * the role of the label.
253 public AccessibleRole getAccessibleRole()
255 return AccessibleRole.LABEL;
261 * Gets the AccessibleContext associated with this <code>Label</code>.
262 * The context is created, if necessary.
264 * @return the associated context
266 public AccessibleContext getAccessibleContext()
268 /* Create the context if this is the first request */
269 if (accessibleContext == null)
270 accessibleContext = new AccessibleAWTLabel();
271 return accessibleContext;
275 * Generate a unique name for this button.
277 * @return A unique name for this button.
279 String generateName()
281 return "label" + getUniqueLong();
285 * The number used to generate the name returned by getName.
287 private static transient long nextLabelNumber;
289 private static synchronized long getUniqueLong()
291 return nextLabelNumber++;