0N/A/*
2362N/A * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.awt;
0N/A
0N/Aimport java.awt.peer.LabelPeer;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport javax.accessibility.*;
0N/A
0N/A/**
0N/A * A <code>Label</code> object is a component for placing text in a
0N/A * container. A label displays a single line of read-only text.
0N/A * The text can be changed by the application, but a user cannot edit it
0N/A * directly.
0N/A * <p>
0N/A * For example, the code&nbsp;.&nbsp;.&nbsp;.
0N/A * <p>
0N/A * <hr><blockquote><pre>
0N/A * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
0N/A * add(new Label("Hi There!"));
0N/A * add(new Label("Another Label"));
0N/A * </pre></blockquote><hr>
0N/A * <p>
0N/A * produces the following labels:
0N/A * <p>
0N/A * <img src="doc-files/Label-1.gif" alt="Two labels: 'Hi There!' and 'Another label'"
0N/A * ALIGN=center HSPACE=10 VSPACE=7>
0N/A *
0N/A * @author Sami Shaio
0N/A * @since JDK1.0
0N/A */
0N/Apublic class Label extends Component implements Accessible {
0N/A
0N/A static {
0N/A /* ensure that the necessary native libraries are loaded */
0N/A Toolkit.loadLibraries();
0N/A if (!GraphicsEnvironment.isHeadless()) {
0N/A initIDs();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Indicates that the label should be left justified.
0N/A */
0N/A public static final int LEFT = 0;
0N/A
0N/A /**
0N/A * Indicates that the label should be centered.
0N/A */
0N/A public static final int CENTER = 1;
0N/A
0N/A /**
0N/A * Indicates that the label should be right justified.
0N/A * @since JDK1.0t.
0N/A */
0N/A public static final int RIGHT = 2;
0N/A
0N/A /**
0N/A * The text of this label.
0N/A * This text can be modified by the program
0N/A * but never by the user.
0N/A *
0N/A * @serial
0N/A * @see #getText()
0N/A * @see #setText(String)
0N/A */
0N/A String text;
0N/A
0N/A /**
0N/A * The label's alignment. The default alignment is set
0N/A * to be left justified.
0N/A *
0N/A * @serial
0N/A * @see #getAlignment()
0N/A * @see #setAlignment(int)
0N/A */
0N/A int alignment = LEFT;
0N/A
0N/A private static final String base = "label";
0N/A private static int nameCounter = 0;
0N/A
0N/A /*
0N/A * JDK 1.1 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = 3094126758329070636L;
0N/A
0N/A /**
0N/A * Constructs an empty label.
0N/A * The text of the label is the empty string <code>""</code>.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public Label() throws HeadlessException {
0N/A this("", LEFT);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new label with the specified string of text,
0N/A * left justified.
0N/A * @param text the string that the label presents.
0N/A * A <code>null</code> value
0N/A * will be accepted without causing a NullPointerException
0N/A * to be thrown.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public Label(String text) throws HeadlessException {
0N/A this(text, LEFT);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new label that presents the specified string of
0N/A * text with the specified alignment.
0N/A * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
0N/A * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
0N/A * @param text the string that the label presents.
0N/A * A <code>null</code> value
0N/A * will be accepted without causing a NullPointerException
0N/A * to be thrown.
0N/A * @param alignment the alignment value.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A public Label(String text, int alignment) throws HeadlessException {
0N/A GraphicsEnvironment.checkHeadless();
0N/A this.text = text;
0N/A setAlignment(alignment);
0N/A }
0N/A
0N/A /**
0N/A * Read a label from an object input stream.
0N/A * @exception HeadlessException if
0N/A * <code>GraphicsEnvironment.isHeadless()</code> returns
0N/A * <code>true</code>
0N/A * @serial
0N/A * @since 1.4
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws ClassNotFoundException, IOException, HeadlessException {
0N/A GraphicsEnvironment.checkHeadless();
0N/A s.defaultReadObject();
0N/A }
0N/A
0N/A /**
0N/A * Construct a name for this component. Called by getName() when the
0N/A * name is <code>null</code>.
0N/A */
0N/A String constructComponentName() {
0N/A synchronized (Label.class) {
0N/A return base + nameCounter++;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates the peer for this label. The peer allows us to
0N/A * modify the appearance of the label without changing its
0N/A * functionality.
0N/A */
0N/A public void addNotify() {
0N/A synchronized (getTreeLock()) {
0N/A if (peer == null)
0N/A peer = getToolkit().createLabel(this);
0N/A super.addNotify();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the current alignment of this label. Possible values are
0N/A * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
0N/A * <code>Label.CENTER</code>.
0N/A * @see java.awt.Label#setAlignment
0N/A */
0N/A public int getAlignment() {
0N/A return alignment;
0N/A }
0N/A
0N/A /**
0N/A * Sets the alignment for this label to the specified alignment.
0N/A * Possible values are <code>Label.LEFT</code>,
0N/A * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
0N/A * @param alignment the alignment to be set.
0N/A * @exception IllegalArgumentException if an improper value for
0N/A * <code>alignment</code> is given.
0N/A * @see java.awt.Label#getAlignment
0N/A */
0N/A public synchronized void setAlignment(int alignment) {
0N/A switch (alignment) {
0N/A case LEFT:
0N/A case CENTER:
0N/A case RIGHT:
0N/A this.alignment = alignment;
0N/A LabelPeer peer = (LabelPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setAlignment(alignment);
0N/A }
0N/A return;
0N/A }
0N/A throw new IllegalArgumentException("improper alignment: " + alignment);
0N/A }
0N/A
0N/A /**
0N/A * Gets the text of this label.
0N/A * @return the text of this label, or <code>null</code> if
0N/A * the text has been set to <code>null</code>.
0N/A * @see java.awt.Label#setText
0N/A */
0N/A public String getText() {
0N/A return text;
0N/A }
0N/A
0N/A /**
0N/A * Sets the text for this label to the specified text.
0N/A * @param text the text that this label displays. If
0N/A * <code>text</code> is <code>null</code>, it is
0N/A * treated for display purposes like an empty
0N/A * string <code>""</code>.
0N/A * @see java.awt.Label#getText
0N/A */
0N/A public void setText(String text) {
0N/A boolean testvalid = false;
0N/A synchronized (this) {
0N/A if (text != this.text && (this.text == null ||
0N/A !this.text.equals(text))) {
0N/A this.text = text;
0N/A LabelPeer peer = (LabelPeer)this.peer;
0N/A if (peer != null) {
0N/A peer.setText(text);
0N/A }
0N/A testvalid = true;
0N/A }
0N/A }
0N/A
0N/A // This could change the preferred size of the Component.
555N/A if (testvalid) {
555N/A invalidateIfValid();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representing the state of this <code>Label</code>.
0N/A * This method is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not be
0N/A * <code>null</code>.
0N/A *
0N/A * @return the parameter string of this label
0N/A */
0N/A protected String paramString() {
0N/A String str = ",align=";
0N/A switch (alignment) {
0N/A case LEFT: str += "left"; break;
0N/A case CENTER: str += "center"; break;
0N/A case RIGHT: str += "right"; break;
0N/A }
0N/A return super.paramString() + str + ",text=" + text;
0N/A }
0N/A
0N/A /**
0N/A * Initialize JNI field and method IDs
0N/A */
0N/A private static native void initIDs();
0N/A
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this Label.
0N/A * For labels, the AccessibleContext takes the form of an
0N/A * AccessibleAWTLabel.
0N/A * A new AccessibleAWTLabel instance is created if necessary.
0N/A *
0N/A * @return an AccessibleAWTLabel that serves as the
0N/A * AccessibleContext of this Label
0N/A * @since 1.3
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleAWTLabel();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>Label</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to label user-interface elements.
0N/A * @since 1.3
0N/A */
0N/A protected class AccessibleAWTLabel extends AccessibleAWTComponent
0N/A {
0N/A /*
0N/A * JDK 1.3 serialVersionUID
0N/A */
0N/A private static final long serialVersionUID = -3568967560160480438L;
0N/A
0N/A public AccessibleAWTLabel() {
0N/A super();
0N/A }
0N/A
0N/A /**
0N/A * Get the accessible name of this object.
0N/A *
0N/A * @return the localized name of the object -- can be null if this
0N/A * object does not have a name
0N/A * @see AccessibleContext#setAccessibleName
0N/A */
0N/A public String getAccessibleName() {
0N/A if (accessibleName != null) {
0N/A return accessibleName;
0N/A } else {
0N/A if (getText() == null) {
0N/A return super.getAccessibleName();
0N/A } else {
0N/A return getText();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.LABEL;
0N/A }
0N/A
0N/A } // inner class AccessibleAWTLabel
0N/A
0N/A}