| 1 | /* Label.java -- Java label widget
|
|---|
| 2 | Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.LabelPeer;
|
|---|
| 42 | import java.awt.peer.ComponentPeer;
|
|---|
| 43 | import java.io.Serializable;
|
|---|
| 44 | import javax.accessibility.Accessible;
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * This component is used for displaying simple text strings that cannot
|
|---|
| 48 | * be edited.
|
|---|
| 49 | *
|
|---|
| 50 | * @author Aaron M. Renn ([email protected])
|
|---|
| 51 | * @author Tom Tromey <[email protected]>
|
|---|
| 52 | */
|
|---|
| 53 | public class Label extends Component implements Serializable, Accessible
|
|---|
| 54 | {
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Static Variables
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Alignment constant aligning the text to the left of its window.
|
|---|
| 62 | */
|
|---|
| 63 | public static final int LEFT = 0;
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Alignment constant aligning the text in the center of its window.
|
|---|
| 67 | */
|
|---|
| 68 | public static final int CENTER = 1;
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Alignment constant aligning the text to the right of its window.
|
|---|
| 72 | */
|
|---|
| 73 | public static final int RIGHT = 2;
|
|---|
| 74 |
|
|---|
| 75 | // Serialization version constant:
|
|---|
| 76 | private static final long serialVersionUID = 3094126758329070636L;
|
|---|
| 77 |
|
|---|
| 78 | /*************************************************************************/
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * Instance Variables
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * @serial Indicates the alignment of the text within this label's window.
|
|---|
| 86 | * This is one of the constants in this class. The default value is
|
|---|
| 87 | * <code>LEFT</code>.
|
|---|
| 88 | */
|
|---|
| 89 | private int alignment;
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * @serial The text displayed in the label
|
|---|
| 93 | */
|
|---|
| 94 | private String text;
|
|---|
| 95 |
|
|---|
| 96 | /*************************************************************************/
|
|---|
| 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | * Constructors
|
|---|
| 100 | */
|
|---|
| 101 |
|
|---|
| 102 | /**
|
|---|
| 103 | * Initializes a new instance of <code>Label</code> with no text.
|
|---|
| 104 | *
|
|---|
| 105 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 106 | */
|
|---|
| 107 | public
|
|---|
| 108 | Label()
|
|---|
| 109 | {
|
|---|
| 110 | this("", LEFT);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /*************************************************************************/
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Initializes a new instance of <code>Label</code> with the specified
|
|---|
| 117 | * text that is aligned to the left.
|
|---|
| 118 | *
|
|---|
| 119 | * @param text The text of the label.
|
|---|
| 120 | *
|
|---|
| 121 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 122 | */
|
|---|
| 123 | public
|
|---|
| 124 | Label(String text)
|
|---|
| 125 | {
|
|---|
| 126 | this(text, LEFT);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /*************************************************************************/
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Initializes a new instance of <code>Label</code> with the specified
|
|---|
| 133 | * text and alignment.
|
|---|
| 134 | *
|
|---|
| 135 | * @param text The text of the label.
|
|---|
| 136 | * @param alignment The desired alignment for the text in this label,
|
|---|
| 137 | * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
|
|---|
| 138 | * <code>RIGHT</code>.
|
|---|
| 139 | *
|
|---|
| 140 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 141 | */
|
|---|
| 142 | public
|
|---|
| 143 | Label(String text, int alignment)
|
|---|
| 144 | {
|
|---|
| 145 | setAlignment (alignment);
|
|---|
| 146 | setText (text);
|
|---|
| 147 |
|
|---|
| 148 | if (GraphicsEnvironment.isHeadless())
|
|---|
| 149 | throw new HeadlessException ();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /*************************************************************************/
|
|---|
| 153 |
|
|---|
| 154 | /*
|
|---|
| 155 | * Instance Variables
|
|---|
| 156 | */
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Returns the constant indicating the alignment of the text in this
|
|---|
| 160 | * label. The value returned will be one of the alignment constants
|
|---|
| 161 | * from this class.
|
|---|
| 162 | *
|
|---|
| 163 | * @return The alignment of the text in the label.
|
|---|
| 164 | */
|
|---|
| 165 | public int
|
|---|
| 166 | getAlignment()
|
|---|
| 167 | {
|
|---|
| 168 | return(alignment);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /*************************************************************************/
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Sets the text alignment of this label to the specified value.
|
|---|
| 175 | *
|
|---|
| 176 | * @param alignment The desired alignment for the text in this label,
|
|---|
| 177 | * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
|
|---|
| 178 | * <code>RIGHT</code>.
|
|---|
| 179 | */
|
|---|
| 180 | public synchronized void
|
|---|
| 181 | setAlignment(int alignment)
|
|---|
| 182 | {
|
|---|
| 183 | if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
|
|---|
| 184 | throw new IllegalArgumentException ("invalid alignment: " + alignment);
|
|---|
| 185 | this.alignment = alignment;
|
|---|
| 186 | if (peer != null)
|
|---|
| 187 | {
|
|---|
| 188 | LabelPeer lp = (LabelPeer) peer;
|
|---|
| 189 | lp.setAlignment (alignment);
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /*************************************************************************/
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Returns the text displayed in this label.
|
|---|
| 197 | *
|
|---|
| 198 | * @return The text for this label.
|
|---|
| 199 | */
|
|---|
| 200 | public String
|
|---|
| 201 | getText()
|
|---|
| 202 | {
|
|---|
| 203 | return(text);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | /*************************************************************************/
|
|---|
| 207 |
|
|---|
| 208 | /**
|
|---|
| 209 | * Sets the text in this label to the specified value.
|
|---|
| 210 | *
|
|---|
| 211 | * @param text The new text for this label.
|
|---|
| 212 | */
|
|---|
| 213 | public synchronized void
|
|---|
| 214 | setText(String text)
|
|---|
| 215 | {
|
|---|
| 216 | this.text = text;
|
|---|
| 217 |
|
|---|
| 218 | if (peer != null)
|
|---|
| 219 | {
|
|---|
| 220 | LabelPeer lp = (LabelPeer) peer;
|
|---|
| 221 | lp.setText (text);
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /*************************************************************************/
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Notifies this lable that it has been added to a container, causing
|
|---|
| 229 | * the peer to be created. This method is called internally by the AWT
|
|---|
| 230 | * system.
|
|---|
| 231 | */
|
|---|
| 232 | public void
|
|---|
| 233 | addNotify()
|
|---|
| 234 | {
|
|---|
| 235 | if (peer == null)
|
|---|
| 236 | peer = getToolkit ().createLabel (this);
|
|---|
| 237 | super.addNotify ();
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /*************************************************************************/
|
|---|
| 241 |
|
|---|
| 242 | /**
|
|---|
| 243 | * Returns a parameter string useful for debugging.
|
|---|
| 244 | *
|
|---|
| 245 | * @param A debugging string.
|
|---|
| 246 | */
|
|---|
| 247 | protected String
|
|---|
| 248 | paramString()
|
|---|
| 249 | {
|
|---|
| 250 | return ("text=" + getText() + ",alignment=" +
|
|---|
| 251 | getAlignment() + "," + super.paramString());
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | } // class Label
|
|---|
| 255 |
|
|---|