1. /*
  2. * @(#)Label.java 1.57 04/03/16
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.peer.LabelPeer;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import javax.accessibility.*;
  12. /**
  13. * A <code>Label</code> object is a component for placing text in a
  14. * container. A label displays a single line of read-only text.
  15. * The text can be changed by the application, but a user cannot edit it
  16. * directly.
  17. * <p>
  18. * For example, the code . . .
  19. * <p>
  20. * <hr><blockquote><pre>
  21. * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
  22. * add(new Label("Hi There!"));
  23. * add(new Label("Another Label"));
  24. * </pre></blockquote><hr>
  25. * <p>
  26. * produces the following labels:
  27. * <p>
  28. * <img src="doc-files/Label-1.gif" alt="Two labels: 'Hi There!' and 'Another label'"
  29. * ALIGN=center HSPACE=10 VSPACE=7>
  30. *
  31. * @version 1.57, 03/16/04
  32. * @author Sami Shaio
  33. * @since JDK1.0
  34. */
  35. public class Label extends Component implements Accessible {
  36. static {
  37. /* ensure that the necessary native libraries are loaded */
  38. Toolkit.loadLibraries();
  39. if (!GraphicsEnvironment.isHeadless()) {
  40. initIDs();
  41. }
  42. }
  43. /**
  44. * Indicates that the label should be left justified.
  45. */
  46. public static final int LEFT = 0;
  47. /**
  48. * Indicates that the label should be centered.
  49. */
  50. public static final int CENTER = 1;
  51. /**
  52. * Indicates that the label should be right justified.
  53. * @since JDK1.0t.
  54. */
  55. public static final int RIGHT = 2;
  56. /**
  57. * The text of this label.
  58. * This text can be modified by the program
  59. * but never by the user.
  60. *
  61. * @serial
  62. * @see #getText()
  63. * @see #setText(String)
  64. */
  65. String text;
  66. /**
  67. * The label's alignment. The default alignment is set
  68. * to be left justified.
  69. *
  70. * @serial
  71. * @see #getAlignment()
  72. * @see #setAlignment(int)
  73. */
  74. int alignment = LEFT;
  75. private static final String base = "label";
  76. private static int nameCounter = 0;
  77. /*
  78. * JDK 1.1 serialVersionUID
  79. */
  80. private static final long serialVersionUID = 3094126758329070636L;
  81. /**
  82. * Constructs an empty label.
  83. * The text of the label is the empty string <code>""</code>.
  84. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  85. * returns true.
  86. * @see java.awt.GraphicsEnvironment#isHeadless
  87. */
  88. public Label() throws HeadlessException {
  89. this("", LEFT);
  90. }
  91. /**
  92. * Constructs a new label with the specified string of text,
  93. * left justified.
  94. * @param text the string that the label presents.
  95. * A <code>null</code> value
  96. * will be accepted without causing a NullPointerException
  97. * to be thrown.
  98. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  99. * returns true.
  100. * @see java.awt.GraphicsEnvironment#isHeadless
  101. */
  102. public Label(String text) throws HeadlessException {
  103. this(text, LEFT);
  104. }
  105. /**
  106. * Constructs a new label that presents the specified string of
  107. * text with the specified alignment.
  108. * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
  109. * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  110. * @param text the string that the label presents.
  111. * A <code>null</code> value
  112. * will be accepted without causing a NullPointerException
  113. * to be thrown.
  114. * @param alignment the alignment value.
  115. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  116. * returns true.
  117. * @see java.awt.GraphicsEnvironment#isHeadless
  118. */
  119. public Label(String text, int alignment) throws HeadlessException {
  120. GraphicsEnvironment.checkHeadless();
  121. this.text = text;
  122. setAlignment(alignment);
  123. }
  124. /**
  125. * Read a label from an object input stream.
  126. * @exception HeadlessException if
  127. * <code>GraphicsEnvironment.isHeadless()</code> returns
  128. * <code>true</code>
  129. * @serial
  130. * @since 1.4
  131. * @see java.awt.GraphicsEnvironment#isHeadless
  132. */
  133. private void readObject(ObjectInputStream s)
  134. throws ClassNotFoundException, IOException, HeadlessException {
  135. GraphicsEnvironment.checkHeadless();
  136. s.defaultReadObject();
  137. }
  138. /**
  139. * Construct a name for this component. Called by getName() when the
  140. * name is <code>null</code>.
  141. */
  142. String constructComponentName() {
  143. synchronized (getClass()) {
  144. return base + nameCounter++;
  145. }
  146. }
  147. /**
  148. * Creates the peer for this label. The peer allows us to
  149. * modify the appearance of the label without changing its
  150. * functionality.
  151. */
  152. public void addNotify() {
  153. synchronized (getTreeLock()) {
  154. if (peer == null)
  155. peer = getToolkit().createLabel(this);
  156. super.addNotify();
  157. }
  158. }
  159. /**
  160. * Gets the current alignment of this label. Possible values are
  161. * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
  162. * <code>Label.CENTER</code>.
  163. * @see java.awt.Label#setAlignment
  164. */
  165. public int getAlignment() {
  166. return alignment;
  167. }
  168. /**
  169. * Sets the alignment for this label to the specified alignment.
  170. * Possible values are <code>Label.LEFT</code>,
  171. * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  172. * @param alignment the alignment to be set.
  173. * @exception IllegalArgumentException if an improper value for
  174. * <code>alignment</code> is given.
  175. * @see java.awt.Label#getAlignment
  176. */
  177. public synchronized void setAlignment(int alignment) {
  178. switch (alignment) {
  179. case LEFT:
  180. case CENTER:
  181. case RIGHT:
  182. this.alignment = alignment;
  183. LabelPeer peer = (LabelPeer)this.peer;
  184. if (peer != null) {
  185. peer.setAlignment(alignment);
  186. }
  187. return;
  188. }
  189. throw new IllegalArgumentException("improper alignment: " + alignment);
  190. }
  191. /**
  192. * Gets the text of this label.
  193. * @return the text of this label, or <code>null</code> if
  194. * the text has been set to <code>null</code>.
  195. * @see java.awt.Label#setText
  196. */
  197. public String getText() {
  198. return text;
  199. }
  200. /**
  201. * Sets the text for this label to the specified text.
  202. * @param text the text that this label displays. If
  203. * <code>text</code> is <code>null</code>, it is
  204. * treated for display purposes like an empty
  205. * string <code>""</code>.
  206. * @see java.awt.Label#getText
  207. */
  208. public void setText(String text) {
  209. boolean testvalid = false;
  210. synchronized (this) {
  211. if (text != this.text && (this.text == null ||
  212. !this.text.equals(text))) {
  213. this.text = text;
  214. LabelPeer peer = (LabelPeer)this.peer;
  215. if (peer != null) {
  216. peer.setText(text);
  217. }
  218. testvalid = true;
  219. }
  220. }
  221. // This could change the preferred size of the Component.
  222. if (testvalid && valid) {
  223. invalidate();
  224. }
  225. }
  226. /**
  227. * Returns a string representing the state of this <code>Label</code>.
  228. * This method is intended to be used only for debugging purposes, and the
  229. * content and format of the returned string may vary between
  230. * implementations. The returned string may be empty but may not be
  231. * <code>null</code>.
  232. *
  233. * @return the parameter string of this label
  234. */
  235. protected String paramString() {
  236. String str = ",align=";
  237. switch (alignment) {
  238. case LEFT: str += "left"; break;
  239. case CENTER: str += "center"; break;
  240. case RIGHT: str += "right"; break;
  241. }
  242. return super.paramString() + str + ",text=" + text;
  243. }
  244. /**
  245. * Initialize JNI field and method IDs
  246. */
  247. private static native void initIDs();
  248. /////////////////
  249. // Accessibility support
  250. ////////////////
  251. /**
  252. * Gets the AccessibleContext associated with this Label.
  253. * For labels, the AccessibleContext takes the form of an
  254. * AccessibleAWTLabel.
  255. * A new AccessibleAWTLabel instance is created if necessary.
  256. *
  257. * @return an AccessibleAWTLabel that serves as the
  258. * AccessibleContext of this Label
  259. */
  260. public AccessibleContext getAccessibleContext() {
  261. if (accessibleContext == null) {
  262. accessibleContext = new AccessibleAWTLabel();
  263. }
  264. return accessibleContext;
  265. }
  266. /**
  267. * This class implements accessibility support for the
  268. * <code>Label</code> class. It provides an implementation of the
  269. * Java Accessibility API appropriate to label user-interface elements.
  270. */
  271. protected class AccessibleAWTLabel extends AccessibleAWTComponent
  272. {
  273. /*
  274. * JDK 1.3 serialVersionUID
  275. */
  276. private static final long serialVersionUID = -3568967560160480438L;
  277. public AccessibleAWTLabel() {
  278. super();
  279. }
  280. /**
  281. * Get the accessible name of this object.
  282. *
  283. * @return the localized name of the object -- can be null if this
  284. * object does not have a name
  285. * @see AccessibleContext#setAccessibleName
  286. */
  287. public String getAccessibleName() {
  288. if (accessibleName != null) {
  289. return accessibleName;
  290. } else {
  291. if (getText() == null) {
  292. return super.getAccessibleName();
  293. } else {
  294. return getText();
  295. }
  296. }
  297. }
  298. /**
  299. * Get the role of this object.
  300. *
  301. * @return an instance of AccessibleRole describing the role of the object
  302. * @see AccessibleRole
  303. */
  304. public AccessibleRole getAccessibleRole() {
  305. return AccessibleRole.LABEL;
  306. }
  307. } // inner class AccessibleAWTLabel
  308. }