Merge from the pain train
[official-gcc.git] / libjava / java / awt / Label.java
blobe1a43c8d189410665207e67a1a2aa00beba6653b
1 /* Label.java -- Java label widget
2 Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package java.awt;
41 import java.awt.peer.LabelPeer;
43 import javax.accessibility.Accessible;
44 import javax.accessibility.AccessibleContext;
45 import javax.accessibility.AccessibleRole;
47 /**
48 * This component is used for displaying simple text strings that cannot
49 * be edited by the user.
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)
55 public class Label extends Component implements Accessible
59 * Static Variables
62 /**
63 * Alignment constant aligning the text to the left of its window.
65 public static final int LEFT = 0;
67 /**
68 * Alignment constant aligning the text in the center of its window.
70 public static final int CENTER = 1;
72 /**
73 * Alignment constant aligning the text to the right of its window.
75 public static final int RIGHT = 2;
77 // Serialization version constant:
78 private static final long serialVersionUID = 3094126758329070636L;
80 /*************************************************************************/
83 * Instance Variables
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>.
91 private int alignment;
93 /**
94 * @serial The text displayed in the label
96 private String text;
98 /*************************************************************************/
101 * Constructors
105 * Initializes a new instance of <code>Label</code> with no text.
107 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
109 public
110 Label()
112 this("", LEFT);
115 /*************************************************************************/
118 * Initializes a new instance of <code>Label</code> with the specified
119 * text that is aligned to the left.
121 * @param text The text of the label.
123 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
125 public
126 Label(String text)
128 this(text, LEFT);
131 /*************************************************************************/
134 * Initializes a new instance of <code>Label</code> with the specified
135 * text and alignment.
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>.
142 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
144 public
145 Label(String text, int alignment)
147 setAlignment (alignment);
148 setText (text);
150 if (GraphicsEnvironment.isHeadless())
151 throw new HeadlessException ();
154 /*************************************************************************/
157 * Instance Variables
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.
165 * @return The alignment of the text in the label.
167 public int
168 getAlignment()
170 return(alignment);
173 /*************************************************************************/
176 * Sets the text alignment of this label to the specified value.
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>.
182 public synchronized void
183 setAlignment(int alignment)
185 if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
186 throw new IllegalArgumentException ("invalid alignment: " + alignment);
187 this.alignment = alignment;
188 if (peer != null)
190 LabelPeer lp = (LabelPeer) peer;
191 lp.setAlignment (alignment);
195 /*************************************************************************/
198 * Returns the text displayed in this label.
200 * @return The text for this label.
202 public String
203 getText()
205 return(text);
208 /*************************************************************************/
211 * Sets the text in this label to the specified value.
213 * @param text The new text for this label.
215 public synchronized void
216 setText(String text)
218 this.text = text;
220 if (peer != null)
222 LabelPeer lp = (LabelPeer) peer;
223 lp.setText (text);
227 /*************************************************************************/
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.
234 public void
235 addNotify()
237 if (peer == null)
238 peer = getToolkit ().createLabel (this);
239 super.addNotify ();
242 /*************************************************************************/
245 * Returns a parameter string useful for debugging.
247 * @return A debugging string.
249 protected String
250 paramString()
252 return ("text=" + getText() + ",alignment=" +
253 getAlignment() + "," + super.paramString());
257 * This class provides accessibility support for the label.
259 protected class AccessibleAWTLabel
260 extends AccessibleAWTComponent
263 * For compatability with Sun's JDK 1.4.2 rev. 5
265 private static final long serialVersionUID = -3568967560160480438L;
268 * Constructor for the accessible label.
270 public AccessibleAWTLabel()
275 * Returns the accessible name for the label. This is
276 * the text used in the label.
278 * @return a <code>String</code> containing the accessible
279 * name for this label.
281 public String getAccessibleName()
283 return getText();
287 * Returns the accessible role for the label.
289 * @return an instance of <code>AccessibleRole</code>, describing
290 * the role of the label.
292 public AccessibleRole getAccessibleRole()
294 return AccessibleRole.LABEL;
300 * Gets the AccessibleContext associated with this <code>Label</code>.
301 * The context is created, if necessary.
303 * @return the associated context
305 public AccessibleContext getAccessibleContext()
307 /* Create the context if this is the first request */
308 if (accessibleContext == null)
309 accessibleContext = new AccessibleAWTLabel();
310 return accessibleContext;
313 } // class Label