001: /*
002: * @(#)Label.java 1.42 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: import sun.awt.peer.LabelPeer;
030: import sun.awt.PeerBasedToolkit;
031:
032: /**
033: * A <code>Label</code> object is a component for placing text in a
034: * container. A label displays a single line of read-only text.
035: * The text can be changed by the application, but a user cannot edit it
036: * directly.
037: * <p>
038: * For example, the code . . .
039: * <p>
040: * <hr><blockquote><pre>
041: * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
042: * add(new Label("Hi There!"));
043: * add(new Label("Another Label"));
044: * </pre></blockquote><hr>
045: * <p>
046: * produces the following label:
047: * <p>
048: * <img src="images-awt/Label-1.gif"
049: * ALIGN=center HSPACE=10 VSPACE=7>
050: *
051: * @version 1.31, 07/01/98
052: * @author Sami Shaio
053: * @since JDK1.0
054: */
055: public class Label extends Component {
056: /**
057: * Indicates that the label should be left justified.
058: * @since JDK1.0
059: */
060: public static final int LEFT = 0;
061: /**
062: * Indicates that the label should be centered.
063: * @since JDK1.0
064: */
065: public static final int CENTER = 1;
066: /**
067: * Indicates that the label should be right justified.
068: * @since JDK1.0t.
069: */
070: public static final int RIGHT = 2;
071: /**
072: * The text of this label.
073: */
074: String text;
075: /**
076: * The label's alignment. The default alignment is set
077: * to be left justified.
078: */
079: int alignment = LEFT;
080: private static final String base = "label";
081: private static int nameCounter = 0;
082: /*
083: * JDK 1.1 serialVersionUID
084: */
085: private static final long serialVersionUID = 3094126758329070636L;
086:
087: /**
088: * Constructs an empty label.
089: * @since JDK1.0
090: */
091: public Label() {
092: this ("", LEFT);
093: }
094:
095: /**
096: * Constructs a new label with the specified string of text,
097: * left justified.
098: * @param text the string that the label presents.
099: * @since JDK1.0
100: */
101: public Label(String text) {
102: this (text, LEFT);
103: }
104:
105: /**
106: * Constructs a new label that presents the specified string of
107: * text with the specified alignment.
108: * <p>
109: * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
110: * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
111: * @param text the string that the label presents.
112: * @param alignment the alignment value.
113: * @since JDK1.0
114: */
115: public Label(String text, int alignment) {
116: this .text = text;
117: setAlignment(alignment);
118: }
119:
120: /**
121: * Construct a name for this component. Called by getName() when the
122: * name is null.
123: */
124: String constructComponentName() {
125: return base + nameCounter++;
126: }
127:
128: /**
129: * Creates the peer for this label. The peer allows us to
130: * modify the appearance of the label without changing its
131: * functionality.
132: */
133: public void addNotify() {
134: synchronized (getTreeLock()) {
135: if (peer == null)
136: peer = ((PeerBasedToolkit) getToolkit())
137: .createLabel(this );
138: super .addNotify();
139: }
140: }
141:
142: /**
143: * Gets the current alignment of this label. Possible values are
144: * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
145: * <code>Label.CENTER</code>.
146: * @see java.awt.Label#setAlignment
147: * @since JDK1.0
148: */
149: public int getAlignment() {
150: return alignment;
151: }
152:
153: /**
154: * Sets the alignment for this label to the specified alignment.
155: * Possible values are <code>Label.LEFT</code>,
156: * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
157: * @param alignment the alignment to be set.
158: * @exception IllegalArgumentException if an improper value for
159: * <code>alignment</code> is given.
160: * @see java.awt.Label#getAlignment
161: * @since JDK1.0
162: */
163: public synchronized void setAlignment(int alignment) {
164: switch (alignment) {
165: case LEFT:
166: case CENTER:
167: case RIGHT:
168: this .alignment = alignment;
169: LabelPeer peer = (LabelPeer) this .peer;
170: if (peer != null) {
171: peer.setAlignment(alignment);
172: }
173: return;
174: }
175: throw new IllegalArgumentException("improper alignment: "
176: + alignment);
177: }
178:
179: /**
180: * Gets the text of this label.
181: * @return the text of this label.
182: * @see java.awt.Label#setText
183: * @since JDK1.0
184: */
185: public String getText() {
186: return text;
187: }
188:
189: /**
190: * Sets the text for this label to the specified text.
191: * @param text the text that this label presents.
192: * @see java.awt.Label#getText
193: * @since JDK1.0
194: */
195: public synchronized void setText(String text) {
196: if (text != this .text
197: && (this .text == null || !this .text.equals(text))) {
198: this .text = text;
199: LabelPeer peer = (LabelPeer) this .peer;
200: if (peer != null) {
201: peer.setText(text);
202: }
203: }
204: }
205:
206: /**
207: * Returns the parameter string representing the state of this
208: * label. This string is useful for debugging.
209: * @return the parameter string of this label.
210: * @since JDK1.0
211: */
212: protected String paramString() {
213: String str = ",align=";
214: switch (alignment) {
215: case LEFT:
216: str += "left";
217: break;
218:
219: case CENTER:
220: str += "center";
221: break;
222:
223: case RIGHT:
224: str += "right";
225: break;
226: }
227: return super .paramString() + str + ",text=" + text;
228: }
229: }
|