001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Image;
026: import java.awt.Rectangle;
027: import com.javujavu.javux.wings.item.DefaultItemRenderer;
028: import com.javujavu.javux.wings.item.ItemRenderer;
029:
030: /**
031: * A display area for a label. It can be text, image or any object
032: * that can be rendered with <code>{@link ItemRenderer}</code> set to this component.
033: *
034: * <br>
035: * <b>This is one of the core WingS classes required by all the components</b><br>
036: * <br>
037: * <b>This class is thread safe.</b>
038: * @see ItemRenderer
039: * @see DefaultItemRenderer
040: **/
041: public class WingLabel extends WingComponent {
042: protected Object label;
043: protected int alignment;
044: protected int textPosition;
045:
046: /**
047: * Constructs an empty label.
048: **/
049: public WingLabel() {
050: this (null, LEFT);
051: }
052:
053: /**
054: * Constructs a new label with the specified <code>label</code> item.
055: * @param label label item usually String
056: **/
057: public WingLabel(Object label) {
058: this (label, LEFT);
059: }
060:
061: /**
062: * Constructs a new label with the specified <code>label</code> item
063: * with the specified alignment
064: * @param label label item usually String
065: * @param alignment the alignment value.
066: **/
067: public WingLabel(Object label, int alignment) {
068: this .label = label;
069: this .alignment = alignment;
070: this .textPosition = RIGHT;
071: }
072:
073: /**
074: * Loads skin resources.<br>
075: * <pre>
076: * styles:
077: * [optional styleID.]label.normal
078: * [optional styleID.]label.disabled
079: * </pre>
080: * @see Style
081: * @see WingSkin
082: */
083: public void loadSkin() {
084: stNormal = WingSkin.getStyle(styleId, "label", NORMAL, null);
085: stDisabled = WingSkin.getStyle(styleId, "label", DISABLED,
086: stNormal).merge(stTop);
087: stNormal = stNormal.merge(stTop);
088: }
089:
090: /**
091: * Sets the label item for this label to the specified value.
092: * @param label the label item that this label displays.
093: */
094: public void setLabel(Object label) {
095: if ((this .label == null && label == null)
096: || this .label == label) {
097: return;
098: }
099: this .label = label;
100: revalidateAndRepaint();
101: }
102:
103: /**
104: * Returns the label item of this label.
105: * @return the label item of this label
106: */
107: public Object getLabel() {
108: return label;
109: }
110:
111: /**
112: * Sets the alignment of the label's contents along the X axis.
113: * @param alignment One of the following constants
114: * <code>WingConst.LEFT</code>,
115: * <code>WingConst.CENTER</code>
116: * <code>WingConst.RIGHT</code>
117: */
118: public void setHorizontalAlignment(int alignment) {
119: this .alignment = alignment;
120: repaintVisible();
121: }
122:
123: /**
124: * Sets the horizontal position of the label's text,
125: * relative to its image.
126: *
127: * @param textPosition One of the following constants
128: * <code>WingConst.LEFT</code>,
129: * <code>WingConst.RIGHT</code>
130: */
131: public void setTextPosition(int textPosition) {
132: this .textPosition = textPosition;
133: repaintVisible();
134: }
135:
136: public Dimension getPreferredSize() {
137: Dimension prefSize = wingPrefSize;
138: if (prefSize == null) {
139: wingPrefSize = prefSize = getRenderer().getItemSize(label,
140: this , getStyle(), null);
141: }
142: return prefSize;
143: }
144:
145: /**
146: * @see com.javujavu.javux.wings.WingComponent#paintBackground(java.awt.Graphics)
147: */
148: public void paintBackground(Graphics g) {
149: Style st = getStyle();
150: Dimension size = getSize();
151: getRenderer().drawItem(g, 0, 0, size.width, size.height, label,
152: this , st, st.margin, alignment, textPosition, null);
153: }
154:
155: /**
156: * @see com.javujavu.javux.wings.WingComponent#imageUpdate(java.awt.Image, int, int, int, int, int)
157: */
158: public boolean imageUpdate(Image img, int infoflags, int x, int y,
159: int width, int height) {
160: return isShowing()
161: && getRenderer().imageUpdate(img, infoflags, x, y,
162: width, height, label, this , getStyle(),
163: new Rectangle(getSize()));
164: }
165:
166: }
|