001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.plaf.LabelCG;
016:
017: /**
018: * Display area for a short text string or an image, or both.
019: * <p/>
020: * You can specify where in the label's display area the label's contents
021: * are aligned by setting the vertical and horizontal alignment.
022: * You can also specify the position of the text relative to the image.
023: *
024: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
025: */
026: public class SLabel extends SComponent {
027:
028: protected String text;
029: protected SIcon icon = null;
030: protected SIcon disabledIcon = null;
031: protected int verticalTextPosition = SConstants.CENTER;
032: protected int horizontalTextPosition = SConstants.RIGHT;
033: protected int iconTextGap = 4;
034: protected boolean wordWrap;
035:
036: /**
037: * Creates a new <code>SLabel</code> instance with the specified text
038: * (left alligned) and no icon.
039: *
040: * @param text The text to be displayed by the label.
041: */
042: public SLabel(String text) {
043: this (text, null, SConstants.LEFT);
044: }
045:
046: /**
047: * Creates a new <code>SLabel</code> instance with no text and no icon.
048: */
049: public SLabel() {
050: this ((String) null);
051: }
052:
053: /**
054: * Creates a new <code>SLabel</code> instance with the specified icon
055: * (left alligned) and no text.
056: *
057: * @param icon The image to be displayed by the label.
058: */
059: public SLabel(SIcon icon) {
060: this (icon, SConstants.LEFT);
061: }
062:
063: /**
064: * Creates a new <code>SLabel</code> instance with the specified icon
065: * (alligned as specified) and no text.
066: *
067: * @param icon The image to be displayed by the label.
068: * @param horizontalAlignment One of the following constants defined in
069: * <code>SConstants</code>:
070: * <code>LEFT</code>, <code>CENTER</code>, <code>RIGHT</code>.
071: * @see SConstants
072: */
073: public SLabel(SIcon icon, int horizontalAlignment) {
074: this (null, icon, horizontalAlignment);
075: }
076:
077: /**
078: * Creates a new <code>SLabel</code> instance with the specified icon
079: * and the specified text (left alligned).
080: *
081: * @param text The text to be displayed by the label.
082: * @param icon The image to be displayed by the label.
083: */
084: public SLabel(String text, SIcon icon) {
085: this (text, icon, SConstants.LEFT);
086: }
087:
088: /**
089: * Creates a new <code>SLabel</code> instance with the specified icon
090: * and the specified text (alligned as specified).
091: *
092: * @param text The text to be displayed by the label.
093: * @param icon The image to be displayed by the label.
094: * @param horizontalAlignment One of the following constants defined in
095: * <code>SConstants</code>:
096: * <code>LEFT</code>, <code>CENTER</code>, <code>RIGHT</code>.
097: * @see SConstants
098: */
099: public SLabel(String text, SIcon icon, int horizontalAlignment) {
100: setText(text);
101: setIcon(icon);
102: setHorizontalAlignment(horizontalAlignment);
103: }
104:
105: /**
106: * Creates a new <code>SLabel</code> instance with the specified text
107: * (alligned as specified) and no icon.
108: *
109: * @param text The text to be displayed by the label.
110: * @param horizontalAlignment One of the following constants defined in
111: * <code>SConstants</code>:
112: * <code>LEFT</code>, <code>CENTER</code>, <code>RIGHT</code>.
113: * @see SConstants
114: */
115: public SLabel(String text, int horizontalAlignment) {
116: this (text, null, horizontalAlignment);
117: }
118:
119: /**
120: * Returns the horizontal position of the lable's text
121: *
122: * @return the position
123: * @see SConstants
124: * @see #setHorizontalTextPosition
125: */
126: public int getHorizontalTextPosition() {
127: return horizontalTextPosition;
128: }
129:
130: /**
131: * Sets the horizontal position of the lable's text, relative to its icon.
132: * <p/>
133: * The default value of this property is CENTER.
134: *
135: * @param textPosition One of the following constants defined in
136: * <code>SConstants</code>:
137: * <code>LEFT</code>, <code>CENTER</code>, <code>RIGHT</code>.
138: */
139: public void setHorizontalTextPosition(int textPosition) {
140: reloadIfChange(horizontalTextPosition, textPosition);
141: horizontalTextPosition = textPosition;
142: }
143:
144: /**
145: * Sets the vertical position of the lable's text, relative to its icon.
146: * <p/>
147: * The default value of this property is CENTER.
148: *
149: * @param textPosition One of the following constants defined in
150: * <code>SConstants</code>:
151: * <code>TOP</code>, <code>CENTER</code>, <code>BOTTOM</code>.
152: */
153: public void setVerticalTextPosition(int textPosition) {
154: reloadIfChange(verticalTextPosition, textPosition);
155: verticalTextPosition = textPosition;
156: }
157:
158: /**
159: * Returns the vertical position of the label's text
160: *
161: * @return the position
162: * @see SConstants
163: * @see #setVerticalTextPosition
164: */
165: public int getVerticalTextPosition() {
166: return verticalTextPosition;
167: }
168:
169: /**
170: * Defines the icon the component will display.
171: * @param i
172: */
173: public void setIcon(SIcon i) {
174: if (isDifferent(icon, i)) {
175: // do reload if previous text was null
176: if (isUpdatePossible() && icon != null
177: && SLabel.class.isAssignableFrom(getClass()))
178: update(((LabelCG) getCG()).getIconUpdate(this , i));
179: else
180: reload();
181: icon = i;
182: }
183: }
184:
185: /**
186: * Returns the icon the label displays.
187: * @return the icon
188: */
189: public SIcon getIcon() {
190: return icon;
191: }
192:
193: /**
194: * Set the icon that will be displayed if the label is disabled.
195: * @param i
196: */
197: public void setDisabledIcon(SIcon i) {
198: reloadIfChange(disabledIcon, i);
199: disabledIcon = i;
200: }
201:
202: /**
203: * Returns the icon that is displayed when the label is disabled.
204: * @return the diabledIcon
205: */
206: public SIcon getDisabledIcon() {
207: return disabledIcon;
208: }
209:
210: /**
211: * Returns the text of the label
212: * @return the text
213: */
214: public String getText() {
215: return text;
216: }
217:
218: /**
219: * Sets the text of the label. Nothing will be displayed if the text is an empty string or null.
220: *
221: * @param t The new text
222: */
223: public void setText(String t) {
224: if (isDifferent(text, t)) {
225: // do reload if previous text was null
226: if (isUpdatePossible() && text != null
227: && SLabel.class.isAssignableFrom(getClass()))
228: update(((LabelCG) getCG()).getTextUpdate(this , t));
229: else
230: reload();
231: text = t;
232: }
233: }
234:
235: /**
236: * Determiens if the label text word wrap inside the browser. Defaults to <code>false</code> (Swing).
237: * @return <code>false</code> if the label should not word wrap an be in line as in Swing.
238: */
239: public boolean isWordWrap() {
240: return wordWrap;
241: }
242:
243: /**
244: * Defines if the label is allowed to wrap.
245: * @param wordWrap Set to <code>true</code> if you want labels to allow to break into more lines than passed.
246: */
247: public void setWordWrap(boolean wordWrap) {
248: if (this .wordWrap != wordWrap) {
249: this .wordWrap = wordWrap;
250: reload();
251: }
252: }
253:
254: /**
255: * Returns the amount of space between the text and the icon
256: * displayed in this label.
257: *
258: * @return an int equal to the number of pixels between the text
259: * and the icon.
260: * @see #setIconTextGap
261: */
262: public int getIconTextGap() {
263: return iconTextGap;
264: }
265:
266: /**
267: * If both the icon and text properties are set, this property
268: * defines the space between them.
269: * <p>
270: * The default value of this property is 4 pixels.
271: * <p>
272: *
273: * @see #getIconTextGap
274: */
275: public void setIconTextGap(int iconTextGap) {
276: reloadIfChange(this .iconTextGap, iconTextGap);
277: this .iconTextGap = iconTextGap;
278: }
279:
280: public void setCG(LabelCG cg) {
281: super.setCG(cg);
282: }
283: }
|