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.WingSkin;
032: import com.javujavu.javux.wings.WingToolkit;
033:
034: /**
035: * This class implements item renderer used by WingS components by default.
036: * This renderer treat item objects in the following way:<br>
037: * <ul>
038: * <li>if the item object implements <code>ItemRenderer</code> uses the item as a renderer
039: * <li>if the item object is a <code>WingImage</code> renders it as an image
040: * <li>it the item object is a <code>Boolean</code> renders it as an image using checkbox images
041: * <li>if the item object is not null renders it as a <code>String</code> using <code>item.toString()</code>
042: * </ul>
043: * <br>
044: * <b>This is one of the core WingS classes required by all the components</b><br>
045: * <br>
046: * <b>This class is thread safe.</b>
047: **/
048: public class DefaultItemRenderer implements ItemRenderer {
049: /**
050: * @see ItemRenderer#getItemSize(java.lang.Object, com.javujavu.javux.wings.WingComponent, com.javujavu.javux.wings.Style, java.lang.Object)
051: */
052: public Dimension getItemSize(Object item, WingComponent owner,
053: Style style, Object context) {
054: if (item instanceof ItemRenderer) {
055: return ((ItemRenderer) item).getItemSize(item, owner,
056: style, context);
057: } else {
058: WingImage icon = null;
059: String text = null;
060: if (item instanceof WingImage) {
061: icon = (WingImage) item;
062: } else if (item instanceof Boolean) {
063: icon = WingSkin.getImage(((Boolean) item)
064: .booleanValue() ? "checkbox.on.normal.icon"
065: : "checkbox.normal.icon", null);
066: }
067:
068: if (icon == null && item != null) {
069: text = item.toString();
070: }
071: if (icon == null)
072: icon = style.icon;
073: return WingToolkit.calcLabelSize(icon, text, style
074: .getFont(owner.getWingFont()), style.margin,
075: style.gap, owner);
076: }
077: }
078:
079: /**
080: * @see 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)
081: */
082: public void drawItem(Graphics g, int x, int y, int width,
083: int height, Object item, WingComponent owner, Style style,
084: Insets margin, int alignment, int textPosition,
085: Object context) {
086: if (item instanceof ItemRenderer) {
087: ((ItemRenderer) item).drawItem(g, x, y, width, height,
088: item, owner, style, margin, alignment,
089: textPosition, context);
090: } else {
091: WingToolkit.drawBackground(g, x, y, width, height, style,
092: owner);
093: WingImage icon = null;
094: String text = null;
095: if (item instanceof WingImage) {
096: icon = (WingImage) item;
097: } else if (item instanceof Boolean) {
098: icon = WingSkin.getImage(((Boolean) item)
099: .booleanValue() ? "checkbox.on.normal.icon"
100: : "checkbox.normal.icon", null);
101: }
102: if (icon == null && item != null) {
103: text = item.toString();
104: }
105: if (icon == null)
106: icon = style.icon;
107: WingToolkit.drawLabel(g, x, y, width, height, icon, text,
108: style.getFont(owner.getWingFont()), style
109: .getForeground(owner.getForeground()),
110: style.focus, margin, style.gap, alignment,
111: textPosition, owner);
112: }
113: }
114:
115: /**
116: * @see 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)
117: */
118: public boolean imageUpdate(Image img, int infoflags, int x, int y,
119: int width, int height, Object item, WingComponent owner,
120: Style style, Rectangle itemBounds) {
121: if (item instanceof ItemRenderer) {
122: return ((ItemRenderer) item).imageUpdate(img, infoflags, x,
123: y, width, height, item, owner, style, itemBounds);
124: }
125: if ((item instanceof WingImage && ((WingImage) item).getImage()
126: .equals(img))
127: || (style.image != null && style.image.getImage()
128: .equals(img))
129: || (style.icon != null && style.icon.getImage().equals(
130: img))) {
131: return owner.repaintVisible(itemBounds.x, itemBounds.y,
132: itemBounds.width, itemBounds.height)
133: && (infoflags & (WingComponent.ALLBITS | WingComponent.ABORT)) == 0;
134: }
135: return false;
136: }
137: }
|