1. /*
  2. * @(#)Label.java 1.42 01/11/29
  3. *
  4. * Copyright 2002 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. /**
  10. * A <code>Label</code> object is a component for placing text in a
  11. * container. A label displays a single line of read-only text.
  12. * The text can be changed by the application, but a user cannot edit it
  13. * directly.
  14. * <p>
  15. * For example, the code . . .
  16. * <p>
  17. * <hr><blockquote><pre>
  18. * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
  19. * add(new Label("Hi There!"));
  20. * add(new Label("Another Label"));
  21. * </pre></blockquote><hr>
  22. * <p>
  23. * produces the following label:
  24. * <p>
  25. * <img src="doc-files/Label-1.gif"
  26. * ALIGN=center HSPACE=10 VSPACE=7>
  27. *
  28. * @version 1.42, 11/29/01
  29. * @author Sami Shaio
  30. * @since JDK1.0
  31. */
  32. public class Label extends Component {
  33. static {
  34. /* ensure that the necessary native libraries are loaded */
  35. Toolkit.loadLibraries();
  36. initIDs();
  37. }
  38. /**
  39. * Indicates that the label should be left justified.
  40. */
  41. public static final int LEFT = 0;
  42. /**
  43. * Indicates that the label should be centered.
  44. */
  45. public static final int CENTER = 1;
  46. /**
  47. * Indicates that the label should be right justified.
  48. * @since JDK1.0t.
  49. */
  50. public static final int RIGHT = 2;
  51. /**
  52. * The text of this label.
  53. * This text can be modified by the program
  54. * but never by the user.
  55. *
  56. * @serial
  57. * @see getText()
  58. * @see setText()
  59. */
  60. String text;
  61. /**
  62. * The label's alignment. The default alignment is set
  63. * to be left justified.
  64. *
  65. * @serial
  66. * @see getAlignment()
  67. * @see setAlignment()
  68. */
  69. int alignment = LEFT;
  70. private static final String base = "label";
  71. private static int nameCounter = 0;
  72. /*
  73. * JDK 1.1 serialVersionUID
  74. */
  75. private static final long serialVersionUID = 3094126758329070636L;
  76. /**
  77. * Constructs an empty label.
  78. * The text of the label is the empty string <code>""</code>.
  79. */
  80. public Label() {
  81. this("", LEFT);
  82. }
  83. /**
  84. * Constructs a new label with the specified string of text,
  85. * left justified.
  86. * @param text the string that the label presents.
  87. * A <code>null</code> value
  88. * will be accepted without causing a NullPointerException
  89. * to be thrown.
  90. */
  91. public Label(String text) {
  92. this(text, LEFT);
  93. }
  94. /**
  95. * Constructs a new label that presents the specified string of
  96. * text with the specified alignment.
  97. * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
  98. * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  99. * @param text the string that the label presents.
  100. * A <code>null</code> value
  101. * will be accepted without causing a NullPointerException
  102. * to be thrown.
  103. * @param alignment the alignment value.
  104. */
  105. public Label(String text, int alignment) {
  106. this.text = text;
  107. setAlignment(alignment);
  108. }
  109. /**
  110. * Construct a name for this component. Called by getName() when the
  111. * name is <code>null</code>.
  112. */
  113. String constructComponentName() {
  114. synchronized (getClass()) {
  115. return base + nameCounter++;
  116. }
  117. }
  118. /**
  119. * Creates the peer for this label. The peer allows us to
  120. * modify the appearance of the label without changing its
  121. * functionality.
  122. */
  123. public void addNotify() {
  124. synchronized (getTreeLock()) {
  125. if (peer == null)
  126. peer = getToolkit().createLabel(this);
  127. super.addNotify();
  128. }
  129. }
  130. /**
  131. * Gets the current alignment of this label. Possible values are
  132. * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
  133. * <code>Label.CENTER</code>.
  134. * @see java.awt.Label#setAlignment
  135. */
  136. public int getAlignment() {
  137. return alignment;
  138. }
  139. /**
  140. * Sets the alignment for this label to the specified alignment.
  141. * Possible values are <code>Label.LEFT</code>,
  142. * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
  143. * @param alignment the alignment to be set.
  144. * @exception IllegalArgumentException if an improper value for
  145. * <code>alignment</code> is given.
  146. * @see java.awt.Label#getAlignment
  147. */
  148. public synchronized void setAlignment(int alignment) {
  149. switch (alignment) {
  150. case LEFT:
  151. case CENTER:
  152. case RIGHT:
  153. this.alignment = alignment;
  154. LabelPeer peer = (LabelPeer)this.peer;
  155. if (peer != null) {
  156. peer.setAlignment(alignment);
  157. }
  158. return;
  159. }
  160. throw new IllegalArgumentException("improper alignment: " + alignment);
  161. }
  162. /**
  163. * Gets the text of this label.
  164. * @return the text of this label, or <code>null</code> if
  165. * the text has been set to <code>null</code>.
  166. * @see java.awt.Label#setText
  167. */
  168. public String getText() {
  169. return text;
  170. }
  171. /**
  172. * Sets the text for this label to the specified text.
  173. * @param text the text that this label displays. If
  174. * <code>text</code> is <code>null</code>, it is
  175. * treated for display purposes like an empty
  176. * string <code>""</code>.
  177. * @see java.awt.Label#getText
  178. */
  179. public void setText(String text) {
  180. boolean testvalid = false;
  181. synchronized (this) {
  182. if (text != this.text && (this.text == null ||
  183. !this.text.equals(text))) {
  184. this.text = text;
  185. LabelPeer peer = (LabelPeer)this.peer;
  186. if (peer != null) {
  187. peer.setText(text);
  188. }
  189. testvalid = true;
  190. }
  191. }
  192. // This could change the preferred size of the Component.
  193. if (testvalid && valid) {
  194. invalidate();
  195. }
  196. }
  197. /**
  198. * Returns the parameter string representing the state of this
  199. * label. This string is useful for debugging.
  200. * @return the parameter string of this label.
  201. */
  202. protected String paramString() {
  203. String str = ",align=";
  204. switch (alignment) {
  205. case LEFT: str += "left"; break;
  206. case CENTER: str += "center"; break;
  207. case RIGHT: str += "right"; break;
  208. }
  209. return super.paramString() + str + ",text=" + text;
  210. }
  211. /**
  212. * Initialize JNI field and method IDs
  213. */
  214. private static native void initIDs();
  215. }