| 1 |
771 |
jeremybenn |
/* Label.java -- Java label widget
|
| 2 |
|
|
Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, Free Software
|
| 3 |
|
|
Foundation, Inc.
|
| 4 |
|
|
|
| 5 |
|
|
This file is part of GNU Classpath.
|
| 6 |
|
|
|
| 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)
|
| 10 |
|
|
any later version.
|
| 11 |
|
|
|
| 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.
|
| 16 |
|
|
|
| 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
|
| 20 |
|
|
02110-1301 USA.
|
| 21 |
|
|
|
| 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
|
| 25 |
|
|
combination.
|
| 26 |
|
|
|
| 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. */
|
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
|
|
package java.awt;
|
| 41 |
|
|
|
| 42 |
|
|
import java.awt.peer.LabelPeer;
|
| 43 |
|
|
|
| 44 |
|
|
import javax.accessibility.Accessible;
|
| 45 |
|
|
import javax.accessibility.AccessibleContext;
|
| 46 |
|
|
import javax.accessibility.AccessibleRole;
|
| 47 |
|
|
|
| 48 |
|
|
/**
|
| 49 |
|
|
* This component is used for displaying simple text strings that cannot
|
| 50 |
|
|
* be edited by the user.
|
| 51 |
|
|
*
|
| 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)
|
| 55 |
|
|
*/
|
| 56 |
|
|
public class Label extends Component implements Accessible
|
| 57 |
|
|
{
|
| 58 |
|
|
|
| 59 |
|
|
/**
|
| 60 |
|
|
* Alignment constant aligning the text to the left of its window.
|
| 61 |
|
|
*/
|
| 62 |
|
|
public static final int LEFT = 0;
|
| 63 |
|
|
|
| 64 |
|
|
/**
|
| 65 |
|
|
* Alignment constant aligning the text in the center of its window.
|
| 66 |
|
|
*/
|
| 67 |
|
|
public static final int CENTER = 1;
|
| 68 |
|
|
|
| 69 |
|
|
/**
|
| 70 |
|
|
* Alignment constant aligning the text to the right of its window.
|
| 71 |
|
|
*/
|
| 72 |
|
|
public static final int RIGHT = 2;
|
| 73 |
|
|
|
| 74 |
|
|
// Serialization version constant:
|
| 75 |
|
|
private static final long serialVersionUID = 3094126758329070636L;
|
| 76 |
|
|
|
| 77 |
|
|
/**
|
| 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
|
| 80 |
|
|
* <code>LEFT</code>.
|
| 81 |
|
|
*/
|
| 82 |
|
|
private int alignment;
|
| 83 |
|
|
|
| 84 |
|
|
/**
|
| 85 |
|
|
* @serial The text displayed in the label
|
| 86 |
|
|
*/
|
| 87 |
|
|
private String text;
|
| 88 |
|
|
|
| 89 |
|
|
/**
|
| 90 |
|
|
* Initializes a new instance of <code>Label</code> with no text.
|
| 91 |
|
|
*
|
| 92 |
|
|
* @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
| 93 |
|
|
*/
|
| 94 |
|
|
public Label()
|
| 95 |
|
|
{
|
| 96 |
|
|
this("", LEFT);
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
/**
|
| 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 |
|
|
}
|