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.extra;
022:
023: import java.awt.Color;
024: import java.awt.Dimension;
025: import java.awt.Graphics;
026: import java.awt.Image;
027: import java.awt.Insets;
028: import java.awt.Rectangle;
029: import com.javujavu.javux.wings.Style;
030: import com.javujavu.javux.wings.WingComponent;
031: import com.javujavu.javux.wings.WingConst;
032: import com.javujavu.javux.wings.WingToolkit;
033: import com.javujavu.javux.wings.item.ItemRenderer;
034:
035: /**
036: * This class is a self-rendering label implementing <code>ItemRederer</code>,
037: * that can display multiline text using specified color.<br>
038: * Object of this class can be used instead of String labels
039: * in labels, buttons, checkboxes, menus and so on, and as an item
040: * in lists, combo boxes, tables and so on.
041: * <br>
042: * <b>This in an extra class, NOT a part of base WingS component set.</b>
043: * <br>
044: * <br>
045: * <b>This class is thread safe.</b>
046: **/
047: public class ColorItemBox implements ItemRenderer {
048: public final Color color;
049: private static final int SIZE = 16;
050:
051: public ColorItemBox(Color color) {
052: this .color = color;
053: }
054:
055: public Dimension getItemSize(Object item, WingComponent owner,
056: Style style, Object context) {
057: int width = SIZE, height = SIZE;
058: if (style.icon != null) {
059: width += style.icon.getWidth() + style.gap;
060: if (height < style.icon.getHeight())
061: height = style.icon.getHeight();
062: }
063: width += style.margin.left + style.margin.right;
064: height += style.margin.top + style.margin.bottom;
065: return new Dimension(width, height);
066: }
067:
068: public void drawItem(Graphics g, int x, int y, int width,
069: int height, Object item, WingComponent owner, Style style,
070: Insets margin, int alignment, int textPosition,
071: Object context) {
072: if ((style.state & (WingConst.SELECTED | WingConst.PRESSED)) == 0) {
073: WingToolkit.drawBackground(g, x, y, width, height, style,
074: owner);
075: } else {
076: g.setColor(color);
077: g.fillRect(x, y, width, height);
078: if ((style.state & (WingConst.FOCUSED | WingConst.PRESSED)) != 0) {
079: g.setColor(Color.black);
080: g.drawRect(x, y, width - 1, height - 1);
081: }
082: }
083: x += margin.left;
084: y += margin.top;
085: width -= margin.left + margin.right;
086: height -= margin.top + margin.bottom;
087:
088: if (style.icon != null) {
089: int dy = (height - style.icon.getHeight()) / 2;
090: style.icon.drawImage(g, x, y + dy, owner);
091: x += style.icon.getWidth();
092: }
093: g.setColor(color);
094: g.fillRect(x, y, width, height);
095: }
096:
097: public boolean imageUpdate(Image img, int infoflags, int x, int y,
098: int width, int height, Object item, WingComponent owner,
099: Style style, Rectangle itemBounds) {
100: return false;
101: }
102: }
|