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.item;
022:
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Image;
026: import java.awt.Insets;
027: import java.awt.Rectangle;
028: import com.javujavu.javux.wings.Style;
029: import com.javujavu.javux.wings.WingComponent;
030: import com.javujavu.javux.wings.WingImage;
031: import com.javujavu.javux.wings.WingToolkit;
032:
033: /**
034: * This class is a self-rendering label implementing <code>ItemRederer</code>,
035: * that can display multiline text and image icon with specified horizontal
036: * alignment and text position.<br>
037: * Object of this class can be used instead of String labels
038: * in labels, buttons, checkboxes, menus and so on, and as an item
039: * in lists, combo boxes, tables and so on.
040: * <br>
041: * <b>This class is thread safe.</b>
042: **/
043: public class LabelItem implements ItemRenderer {
044: public WingImage icon;
045: public String text;
046: public Object userData;
047: public int alignment;
048: public int textPosition;
049:
050: /**
051: * Creates a new label displaying specified text and icon
052: * @param text text of the label, null is allowed
053: * @param icon icon image of the label, null is allowed
054: */
055: public LabelItem(String text, WingImage icon) {
056: this (text, icon, null);
057: }
058:
059: /**
060: * Creates a new label displaying specified text and icon
061: * @param text text of the label, null is allowed
062: * @param icon icon image of the label, null is allowed
063: * @param userData custom user data object
064: */
065: public LabelItem(String text, WingImage icon, Object userData) {
066: this (text, icon, -1, -1, userData);
067: }
068:
069: /**
070: * Creates a new label displaying specified text and icon
071: * @param text text of the label, null is allowed
072: * @param icon icon image of the label, null is allowed
073: * @param alignment label alignment <code>WingConst.LEFT, RIGHT or CENTER</code>
074: * @param textPosition text position <code>WingConst.LEFT or RIGHT</code>
075: */
076: public LabelItem(String text, WingImage icon, int alignment,
077: int textPosition) {
078: this (text, icon, alignment, textPosition, null);
079: }
080:
081: /**
082: * Creates a new label displaying specified text and icon
083: * @param text text of the label, null is allowed
084: * @param icon icon image of the label, null is allowed
085: * @param alignment label alignment <code>WingConst.LEFT, RIGHT or CENTER</code>
086: * @param textPosition text position <code>WingConst.LEFT or RIGHT</code>
087: * @param userData custom user data object
088: */
089: public LabelItem(String text, WingImage icon, int alignment,
090: int textPosition, Object userData) {
091: this .text = text;
092: this .icon = icon;
093: this .userData = userData;
094: this .alignment = alignment;
095: this .textPosition = textPosition;
096: }
097:
098: public String toString() {
099: return text;
100: }
101:
102: /**
103: * @see com.javujavu.javux.wings.item.ItemRenderer#getItemSize(java.lang.Object, com.javujavu.javux.wings.WingComponent, com.javujavu.javux.wings.Style, java.lang.Object)
104: */
105: public Dimension getItemSize(Object item, WingComponent owner,
106: Style style, Object context) {
107: return WingToolkit.calcLabelSize(icon, text, style
108: .getFont(owner.getWingFont()), style.margin, style.gap,
109: owner);
110: }
111:
112: /**
113: * @see com.javujavu.javux.wings.item.ItemRenderer#drawItem(java.awt.Graphics, int, int, int, int, java.lang.Object, com.javujavu.javux.wings.WingComponent, com.javujavu.javux.wings.Style, java.awt.Insets, int, int, java.lang.Object)
114: */
115: public void drawItem(Graphics g, int x, int y, int width,
116: int height, Object item, WingComponent owner, Style style,
117: Insets margin, int alignment, int textPosition,
118: Object context) {
119: WingToolkit
120: .drawBackground(g, x, y, width, height, style, owner);
121: WingImage icon = this .icon;
122: if (icon == null)
123: icon = style.icon;
124:
125: int a = this .alignment;
126: if (a == -1)
127: a = alignment;
128: int tp = this .textPosition;
129: if (tp == -1)
130: tp = textPosition;
131: WingToolkit.drawLabel(g, x, y, width, height, icon, text, style
132: .getFont(owner.getWingFont()), style
133: .getForeground(owner.getForeground()), style.focus,
134: margin, style.gap, a, tp, owner);
135: }
136:
137: /**
138: * @see com.javujavu.javux.wings.item.ItemRenderer#imageUpdate(java.awt.Image, int, int, int, int, int, java.lang.Object, com.javujavu.javux.wings.WingComponent, com.javujavu.javux.wings.Style, java.awt.Rectangle)
139: */
140: public boolean imageUpdate(Image img, int infoflags, int x, int y,
141: int width, int height, Object item, WingComponent owner,
142: Style style, Rectangle itemBounds) {
143: WingImage icon = this .icon;
144: if ((icon != null && icon.getImage().equals(img))
145: || (style.image != null && style.image.getImage()
146: .equals(img))
147: || (style.icon != null && style.icon.getImage().equals(
148: img))) {
149: return owner.repaintVisible(itemBounds.x, itemBounds.y,
150: itemBounds.width, itemBounds.height)
151: && ((infoflags & (WingComponent.ALLBITS | WingComponent.ABORT)) == 0);
152: }
153: return false;
154: }
155: }
|