| 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 |
|
|---|
| 44 | /**
|
|---|
| 45 | * This component is used for displaying simple text strings that cannot
|
|---|
| 46 | * be edited.
|
|---|
| 47 | *
|
|---|
| 48 | * @author Aaron M. Renn ([email protected])
|
|---|
| 49 | * @author Tom Tromey <[email protected]>
|
|---|
| 50 | */
|
|---|
| 51 | public class Label extends Component implements java.io.Serializable
|
|---|
| 52 | {
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | * Static Variables
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * Alignment constant aligning the text to the left of its window.
|
|---|
| 60 | */
|
|---|
| 61 | public static final int LEFT = 0;
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Alignment constant aligning the text in the center of its window.
|
|---|
| 65 | */
|
|---|
| 66 | public static final int CENTER = 1;
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Alignment constant aligning the text to the right of its window.
|
|---|
| 70 | */
|
|---|
| 71 | public static final int RIGHT = 2;
|
|---|
| 72 |
|
|---|
| 73 | // Serialization version constant:
|
|---|
| 74 | private static final long serialVersionUID = 3094126758329070636L;
|
|---|
| 75 |
|
|---|
| 76 | /*************************************************************************/
|
|---|
| 77 |
|
|---|
| 78 | /*
|
|---|
| 79 | * Instance Variables
|
|---|
| 80 | */
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * @serial Indicates the alignment of the text within this label's window.
|
|---|
| 84 | * This is one of the constants in this class. The default value is
|
|---|
| 85 | * <code>LEFT</code>.
|
|---|
| 86 | */
|
|---|
| 87 | private int alignment;
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * @serial The text displayed in the label
|
|---|
| 91 | */
|
|---|
| 92 | private String text;
|
|---|
| 93 |
|
|---|
| 94 | /*************************************************************************/
|
|---|
| 95 |
|
|---|
| 96 | /*
|
|---|
| 97 | * Constructors
|
|---|
| 98 | */
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Initializes a new instance of <code>Label</code> with no text.
|
|---|
| 102 | */
|
|---|
| 103 | public
|
|---|
| 104 | Label()
|
|---|
| 105 | {
|
|---|
| 106 | this("", LEFT);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /*************************************************************************/
|
|---|
| 110 |
|
|---|
| 111 | /**
|
|---|
| 112 | * Initializes a new instance of <code>Label</code> with the specified
|
|---|
| 113 | * text that is aligned to the left.
|
|---|
| 114 | *
|
|---|
| 115 | * @param text The text of the label.
|
|---|
| 116 | */
|
|---|
| 117 | public
|
|---|
| 118 | Label(String text)
|
|---|
| 119 | {
|
|---|
| 120 | this(text, LEFT);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /*************************************************************************/
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Initializes a new instance of <code>Label</code> with the specified
|
|---|
| 127 | * text and alignment.
|
|---|
| 128 | *
|
|---|
| 129 | * @param text The text of the label.
|
|---|
| 130 | * @param alignment The desired alignment for the text in this label,
|
|---|
| 131 | * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
|
|---|
| 132 | * <code>RIGHT</code>.
|
|---|
| 133 | */
|
|---|
| 134 | public
|
|---|
| 135 | Label(String text, int alignment)
|
|---|
| 136 | {
|
|---|
| 137 | setAlignment (alignment);
|
|---|
| 138 | setText (text);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /*************************************************************************/
|
|---|
| 142 |
|
|---|
| 143 | /*
|
|---|
| 144 | * Instance Variables
|
|---|
| 145 | */
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Returns the constant indicating the alignment of the text in this
|
|---|
| 149 | * label. The value returned will be one of the alignment constants
|
|---|
| 150 | * from this class.
|
|---|
| 151 | *
|
|---|
| 152 | * @return The alignment of the text in the label.
|
|---|
| 153 | */
|
|---|
| 154 | public int
|
|---|
| 155 | getAlignment()
|
|---|
| 156 | {
|
|---|
| 157 | return(alignment);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /*************************************************************************/
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Sets the text alignment of this label to the specified value.
|
|---|
| 164 | *
|
|---|
| 165 | * @param alignment The desired alignment for the text in this label,
|
|---|
| 166 | * which must be one of <code>LEFT</code>, <code>CENTER</code>, or
|
|---|
| 167 | * <code>RIGHT</code>.
|
|---|
| 168 | */
|
|---|
| 169 | public synchronized void
|
|---|
| 170 | setAlignment(int alignment)
|
|---|
| 171 | {
|
|---|
| 172 | if (alignment != CENTER && alignment != LEFT && alignment != RIGHT)
|
|---|
| 173 | throw new IllegalArgumentException ("invalid alignment: " + alignment);
|
|---|
| 174 | this.alignment = alignment;
|
|---|
| 175 | if (peer != null)
|
|---|
| 176 | {
|
|---|
| 177 | LabelPeer lp = (LabelPeer) peer;
|
|---|
| 178 | lp.setAlignment (alignment);
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /*************************************************************************/
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * Returns the text displayed in this label.
|
|---|
| 186 | *
|
|---|
| 187 | * @return The text for this label.
|
|---|
| 188 | */
|
|---|
| 189 | public String
|
|---|
| 190 | getText()
|
|---|
| 191 | {
|
|---|
| 192 | return(text);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /*************************************************************************/
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Sets the text in this label to the specified value.
|
|---|
| 199 | *
|
|---|
| 200 | * @param text The new text for this label.
|
|---|
| 201 | */
|
|---|
| 202 | public synchronized void
|
|---|
| 203 | setText(String text)
|
|---|
| 204 | {
|
|---|
| 205 | this.text = text;
|
|---|
| 206 |
|
|---|
| 207 | if (peer != null)
|
|---|
| 208 | {
|
|---|
| 209 | LabelPeer lp = (LabelPeer) peer;
|
|---|
| 210 | lp.setText (text);
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /*************************************************************************/
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * Notifies this lable that it has been added to a container, causing
|
|---|
| 218 | * the peer to be created. This method is called internally by the AWT
|
|---|
| 219 | * system.
|
|---|
| 220 | */
|
|---|
| 221 | public void
|
|---|
| 222 | addNotify()
|
|---|
| 223 | {
|
|---|
| 224 | if (peer == null)
|
|---|
| 225 | peer = getToolkit ().createLabel (this);
|
|---|
| 226 | super.addNotify ();
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /*************************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Returns a parameter string useful for debugging.
|
|---|
| 233 | *
|
|---|
| 234 | * @param A debugging string.
|
|---|
| 235 | */
|
|---|
| 236 | protected String
|
|---|
| 237 | paramString()
|
|---|
| 238 | {
|
|---|
| 239 | return ("text=" + getText() + ",alignment=" +
|
|---|
| 240 | getAlignment() + "," + super.paramString());
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | } // class Label
|
|---|
| 244 |
|
|---|