001/* Label.java -- Java label widget
002   Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006,  Free Software 
003   Foundation, Inc.
004
005This file is part of GNU Classpath.
006
007GNU Classpath is free software; you can redistribute it and/or modify
008it under the terms of the GNU General Public License as published by
009the Free Software Foundation; either version 2, or (at your option)
010any later version.
011
012GNU Classpath is distributed in the hope that it will be useful, but
013WITHOUT ANY WARRANTY; without even the implied warranty of
014MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015General Public License for more details.
016
017You should have received a copy of the GNU General Public License
018along with GNU Classpath; see the file COPYING.  If not, write to the
019Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02002110-1301 USA.
021
022Linking this library statically or dynamically with other modules is
023making a combined work based on this library.  Thus, the terms and
024conditions of the GNU General Public License cover the whole
025combination.
026
027As a special exception, the copyright holders of this library give you
028permission to link this library with independent modules to produce an
029executable, regardless of the license terms of these independent
030modules, and to copy and distribute the resulting executable under
031terms of your choice, provided that you also meet, for each linked
032independent module, the terms and conditions of the license of that
033module.  An independent module is a module which is not derived from
034or based on this library.  If you modify this library, you may extend
035this exception to your version of the library, but you are not
036obligated to do so.  If you do not wish to do so, delete this
037exception statement from your version. */
038
039
040package java.awt;
041
042import java.awt.peer.LabelPeer;
043
044import javax.accessibility.Accessible;
045import javax.accessibility.AccessibleContext;
046import javax.accessibility.AccessibleRole;
047
048/**
049 * This component is used for displaying simple text strings that cannot
050 * be edited by the user.
051 *
052 * @author Aaron M. Renn (arenn@urbanophile.com)
053 * @author Tom Tromey (tromey@cygnus.com)
054 * @author Andrew John Hughes  (gnu_andrew@member.fsf.org)
055 */
056public class Label extends Component implements Accessible
057{
058
059  /**
060   * Alignment constant aligning the text to the left of its window.
061   */
062  public static final int LEFT = 0;
063
064  /**
065   * Alignment constant aligning the text in the center of its window.
066   */
067  public static final int CENTER = 1;
068
069  /**
070   * Alignment constant aligning the text to the right of its window.
071   */
072  public static final int RIGHT = 2;
073
074  // Serialization version constant:
075  private static final long serialVersionUID = 3094126758329070636L;
076
077  /**
078   * @serial Indicates the alignment of the text within this label's window.
079   * This is one of the constants in this class.  The default value is 
080   * <code>LEFT</code>.
081   */
082  private int alignment;
083
084  /**
085   * @serial The text displayed in the label
086   */
087  private String text;
088
089  /**
090   * Initializes a new instance of <code>Label</code> with no text.
091   *
092   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
093   */
094  public Label()
095  {
096    this("", LEFT);
097  }
098
099  /**
100   * Initializes a new instance of <code>Label</code> with the specified
101   * text that is aligned to the left.
102   *
103   * @param text The text of the label.
104   *
105   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
106   */
107  public Label(String text)
108  {
109    this(text, LEFT);
110  }
111
112  /**
113   * Initializes a new instance of <code>Label</code> with the specified
114   * text and alignment.
115   *
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>.
120   *
121   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
122   */
123  public Label(String text, int alignment)
124  {
125    setAlignment(alignment);
126    setText(text);
127
128    if (GraphicsEnvironment.isHeadless())
129      throw new HeadlessException();
130  }
131
132  /**
133   * Returns the constant indicating the alignment of the text in this
134   * label.  The value returned will be one of the alignment constants
135   * from this class.
136   *
137   * @return The alignment of the text in the label.
138   */
139  public int getAlignment()
140  {
141    return(alignment);
142  }
143
144  /**
145   * Sets the text alignment of this label to the specified value.
146   *
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>.
150   */
151  public synchronized void setAlignment(int alignment)
152  {
153    if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
154      throw new IllegalArgumentException("invalid alignment: " + alignment);
155    this.alignment = alignment;
156    if (peer != null)
157      {
158        LabelPeer lp = (LabelPeer) peer;
159        lp.setAlignment(alignment);
160      }
161  }
162
163  /**
164   * Returns the text displayed in this label.
165   *
166   * @return The text for this label.
167   */
168  public String getText()
169  {
170    return text;
171  }
172
173  /**
174   * Sets the text in this label to the specified value.
175   *
176   * @param text The new text for this label.
177   */
178  public synchronized void setText(String text)
179  {
180    if ((this.text == null && text != null)
181        || (this.text != null && ! this.text.equals(text)))
182      {
183        this.text = text;
184
185        if (peer != null)
186         {
187           LabelPeer lp = (LabelPeer) peer;
188           lp.setText(text);
189         }
190       invalidate();
191      }
192  }
193
194  /**
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
197   * system.
198   */
199  public void addNotify()
200  {
201    if (peer == null)
202      peer = getToolkit().createLabel(this);
203    super.addNotify();
204  }
205
206  /**
207   * Returns a parameter string useful for debugging.
208   *
209   * @return A debugging string.
210   */
211  protected String paramString()
212  {
213    return ("text=" + getText() + ",alignment=" +
214          getAlignment() + "," + super.paramString());
215  }
216
217  /**
218   * This class provides accessibility support for the label.
219   */
220  protected class AccessibleAWTLabel
221    extends AccessibleAWTComponent
222  {
223    /**
224     * For compatability with Sun's JDK 1.4.2 rev. 5
225     */
226    private static final long serialVersionUID = -3568967560160480438L;
227
228    /**
229     * Constructor for the accessible label.
230     */
231    public AccessibleAWTLabel()
232    {
233    }
234
235    /**
236     * Returns the accessible name for the label.  This is
237     * the text used in the label.
238     *
239     * @return a <code>String</code> containing the accessible
240     *         name for this label.
241     */
242    public String getAccessibleName()
243    {
244      return getText();
245    }
246
247    /**
248     * Returns the accessible role for the label.
249     *
250     * @return an instance of <code>AccessibleRole</code>, describing
251     *         the role of the label.
252     */
253    public AccessibleRole getAccessibleRole()
254    {
255      return AccessibleRole.LABEL;
256    }
257
258  }
259
260  /**
261   * Gets the AccessibleContext associated with this <code>Label</code>.
262   * The context is created, if necessary.
263   *
264   * @return the associated context
265   */
266  public AccessibleContext getAccessibleContext()
267  {
268    /* Create the context if this is the first request */
269    if (accessibleContext == null)
270      accessibleContext = new AccessibleAWTLabel();
271    return accessibleContext;
272  }
273
274  /**
275   * Generate a unique name for this button.
276   *
277   * @return A unique name for this button.
278   */
279  String generateName()
280  {
281    return "label" + getUniqueLong();
282  }
283  
284  /**
285   * The number used to generate the name returned by getName.
286   */
287  private static transient long nextLabelNumber;
288
289  private static synchronized long getUniqueLong()
290  {
291    return nextLabelNumber++;
292  }
293
294}
295