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