001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app;
031:
032: /**
033: * A component which displays a text string, an icon, or both.
034: */
035: public class Label extends Component {
036:
037: public static final String PROPERTY_ICON = "icon";
038: public static final String PROPERTY_ICON_TEXT_MARGIN = "iconTextMargin";
039: public static final String PROPERTY_LINE_WRAP = "lineWrap";
040: public static final String PROPERTY_TEXT = "text";
041: public static final String PROPERTY_TEXT_ALIGNMENT = "textAlignment";
042: public static final String PROPERTY_TEXT_POSITION = "textPosition";
043: public static final String PROPERTY_TOOL_TIP_TEXT = "toolTipText";
044:
045: /**
046: * Creates a label with no text or icon.
047: */
048: public Label() {
049: this (null, null);
050: }
051:
052: /**
053: * Creates a label with text.
054: *
055: * @param text the text to be displayed
056: */
057: public Label(String text) {
058: this (text, null);
059: }
060:
061: /**
062: * Creates a label with an icon.
063: *
064: * @param icon the icon to be displayed
065: */
066: public Label(ImageReference icon) {
067: this (null, icon);
068: }
069:
070: /**
071: * Creates a label with text and an icon.
072: *
073: * @param text the text to be displayed
074: * @param icon the icon to be displayed
075: */
076: public Label(String text, ImageReference icon) {
077: super ();
078:
079: setIcon(icon);
080: setText(text);
081: }
082:
083: /**
084: * Returns the icon of the label.
085: *
086: * @return the icon
087: */
088: public ImageReference getIcon() {
089: return (ImageReference) getProperty(PROPERTY_ICON);
090: }
091:
092: /**
093: * Returns the margin size between the icon and the text.
094: * The margin will only be displayed if the label has both
095: * icon and text properties set.
096: *
097: * @return the margin size
098: */
099: public Extent getIconTextMargin() {
100: return (Extent) getProperty(PROPERTY_ICON_TEXT_MARGIN);
101: }
102:
103: /**
104: * Returns the text of the label.
105: *
106: * @return the text
107: */
108: public String getText() {
109: return (String) getProperty(PROPERTY_TEXT);
110: }
111:
112: /**
113: * Returns the alignment of the text relative to the icon.
114: *
115: * @return the text alignment
116: */
117: public Alignment getTextAlignment() {
118: return (Alignment) getProperty(PROPERTY_TEXT_ALIGNMENT);
119: }
120:
121: /**
122: * Returns the position of the text relative to the icon.
123: *
124: * @return the text position
125: */
126: public Alignment getTextPosition() {
127: return (Alignment) getProperty(PROPERTY_TEXT_POSITION);
128: }
129:
130: /**
131: * Returns the tool tip text (displayed when the mouse cursor is hovered
132: * over the component).
133: *
134: * @return the tool tip text
135: */
136: public String getToolTipText() {
137: return (String) getProperty(PROPERTY_TOOL_TIP_TEXT);
138: }
139:
140: /**
141: * Determines if the text of the label should wrap in the event that
142: * horizontal space is limited. Default value is true.
143: *
144: * @return the line wrap state
145: */
146: public boolean isLineWrap() {
147: Boolean value = (Boolean) getProperty(PROPERTY_LINE_WRAP);
148: return value == null ? true : value.booleanValue();
149: }
150:
151: /**
152: * This component does not support children.
153: *
154: * @see nextapp.echo2.app.Component#isValidChild(nextapp.echo2.app.Component)
155: */
156: public boolean isValidChild(Component component) {
157: return false;
158: }
159:
160: /**
161: * Sets the icon to be displayed.
162: *
163: * @param newValue the icon to be displayed
164: */
165: public void setIcon(ImageReference newValue) {
166: setProperty(PROPERTY_ICON, newValue);
167: }
168:
169: /**
170: * Sets the margin size between the icon and the text.
171: * The margin will only be displayed if the label has both
172: * icon and text properties set.
173: *
174: * @param newValue the margin size
175: */
176: public void setIconTextMargin(Extent newValue) {
177: setProperty(PROPERTY_ICON_TEXT_MARGIN, newValue);
178: }
179:
180: /**
181: * Sets whether the text of the label should wrap in the event that
182: * horizontal space is limited. Default value is true.
183: *
184: * @param newValue the new line wrap state
185: */
186: public void setLineWrap(boolean newValue) {
187: setProperty(PROPERTY_LINE_WRAP, new Boolean(newValue));
188: }
189:
190: /**
191: * Sets the text to be displayed.
192: *
193: * @param newValue the text to be displayed
194: */
195: public void setText(String newValue) {
196: setProperty(PROPERTY_TEXT, newValue);
197: }
198:
199: /**
200: * Sets the alignment of the text relative to the icon.
201: * Note that only one of the provided <code>Alignment</code>'s
202: * settings should be non-default.
203: *
204: * @param newValue the new text position
205: */
206: public void setTextAlignment(Alignment newValue) {
207: setProperty(PROPERTY_TEXT_ALIGNMENT, newValue);
208: }
209:
210: /**
211: * Sets the position of the text relative to the icon.
212: * Note that only one of the provided <code>Alignment</code>'s
213: * settings should be non-default.
214: *
215: * @param newValue the new text position
216: */
217: public void setTextPosition(Alignment newValue) {
218: setProperty(PROPERTY_TEXT_POSITION, newValue);
219: }
220:
221: /**
222: * Sets the tool tip text (displayed when the mouse cursor is hovered
223: * over the component).
224: *
225: * @param newValue the new tool tip text
226: */
227: public void setToolTipText(String newValue) {
228: setProperty(PROPERTY_TOOL_TIP_TEXT, newValue);
229: }
230: }
|