1 7 package java.awt; 8 9 import java.awt.peer.LabelPeer; 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import javax.accessibility.*; 13 14 37 public class Label extends Component implements Accessible { 38 39 static { 40 41 Toolkit.loadLibraries(); 42 if (!GraphicsEnvironment.isHeadless()) { 43 initIDs(); 44 } 45 } 46 47 50 public static final int LEFT = 0; 51 52 55 public static final int CENTER = 1; 56 57 61 public static final int RIGHT = 2; 62 63 72 String text; 73 74 82 int alignment = LEFT; 83 84 private static final String base = "label"; 85 private static int nameCounter = 0; 86 87 90 private static final long serialVersionUID = 3094126758329070636L; 91 92 99 public Label() throws HeadlessException { 100 this("", LEFT); 101 } 102 103 114 public Label(String text) throws HeadlessException { 115 this(text, LEFT); 116 } 117 118 132 public Label(String text, int alignment) throws HeadlessException { 133 GraphicsEnvironment.checkHeadless(); 134 this.text = text; 135 setAlignment(alignment); 136 } 137 138 147 private void readObject(ObjectInputStream s) 148 throws ClassNotFoundException , IOException , HeadlessException { 149 GraphicsEnvironment.checkHeadless(); 150 s.defaultReadObject(); 151 } 152 153 157 String constructComponentName() { 158 synchronized (getClass()) { 159 return base + nameCounter++; 160 } 161 } 162 163 168 public void addNotify() { 169 synchronized (getTreeLock()) { 170 if (peer == null) 171 peer = getToolkit().createLabel(this); 172 super.addNotify(); 173 } 174 } 175 176 182 public int getAlignment() { 183 return alignment; 184 } 185 186 195 public synchronized void setAlignment(int alignment) { 196 switch (alignment) { 197 case LEFT: 198 case CENTER: 199 case RIGHT: 200 this.alignment = alignment; 201 LabelPeer peer = (LabelPeer)this.peer; 202 if (peer != null) { 203 peer.setAlignment(alignment); 204 } 205 return; 206 } 207 throw new IllegalArgumentException ("improper alignment: " + alignment); 208 } 209 210 216 public String getText() { 217 return text; 218 } 219 220 228 public void setText(String text) { 229 boolean testvalid = false; 230 synchronized (this) { 231 if (text != this.text && (this.text == null || 232 !this.text.equals(text))) { 233 this.text = text; 234 LabelPeer peer = (LabelPeer)this.peer; 235 if (peer != null) { 236 peer.setText(text); 237 } 238 testvalid = true; 239 } 240 } 241 242 if (testvalid && valid) { 244 invalidate(); 245 } 246 } 247 248 257 protected String paramString() { 258 String str = ",align="; 259 switch (alignment) { 260 case LEFT: str += "left"; break; 261 case CENTER: str += "center"; break; 262 case RIGHT: str += "right"; break; 263 } 264 return super.paramString() + str + ",text=" + text; 265 } 266 267 270 private static native void initIDs(); 271 272 273 277 278 287 public AccessibleContext getAccessibleContext() { 288 if (accessibleContext == null) { 289 accessibleContext = new AccessibleAWTLabel(); 290 } 291 return accessibleContext; 292 } 293 294 299 protected class AccessibleAWTLabel extends AccessibleAWTComponent 300 { 301 304 private static final long serialVersionUID = -3568967560160480438L; 305 306 public AccessibleAWTLabel() { 307 super(); 308 } 309 310 317 public String getAccessibleName() { 318 if (accessibleName != null) { 319 return accessibleName; 320 } else { 321 if (getText() == null) { 322 return super.getAccessibleName(); 323 } else { 324 return getText(); 325 } 326 } 327 } 328 329 335 public AccessibleRole getAccessibleRole() { 336 return AccessibleRole.LABEL; 337 } 338 339 } 341 } 342 | Popular Tags |