001: /*
002: * @(#)MultiIcon.java 1.0.1 2006-02-14
003: *
004: * Copyright (c) 2005 Werner Randelshofer
005: * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information of
009: * Werner Randelshofer. ("Confidential Information"). You shall not
010: * disclose such Confidential Information and shall use it only in
011: * accordance with the terms of the license agreement you entered into
012: * with Werner Randelshofer.
013: */
014:
015: package contrib.ch.randelshofer.quaqua;
016:
017: import java.awt.*;
018: import javax.swing.*;
019: import javax.swing.plaf.*;
020:
021: import contrib.ch.randelshofer.quaqua.util.*;
022:
023: /**
024: * An icon which paints one out of multiple icons depending on the state
025: * of the component.
026: * MultiIcon can lazily create the icons from a tiled image.
027: *
028: * @author Werner Randelshofer
029: * @version 1.0.1 2006-02-14 Created tileCount icons from image.
030: * <br>1.0 October 17, 2005 Created.
031: */
032: public abstract class MultiIcon implements Icon {
033: /**
034: * The icons from which we choose from.
035: * This variable is null, if we are using a tiled image as our base.
036: */
037: protected Icon[] icons;
038:
039: /** Holds the icon pictures in a single image. This variable is used only
040: *until we create the icons array. Then it is set to null.
041: */
042: private Image tiledImage;
043: /**
044: * The number of icons in the tiledImage.
045: */
046: private int tileCount;
047: /**
048: * Whether the tiledImage needs to be tiled horizontaly or vertically
049: * to get the icons out of it.
050: */
051: private boolean isTiledHorizontaly;
052:
053: /**
054: * Creates a new instance from an array of icons.
055: * All icons must have the same dimensions.
056: * If an icon is null, an icon is derived for the state from the
057: * other icons.
058: */
059: public MultiIcon(Icon[] icons) {
060: this .icons = icons;
061: generateMissingIcons();
062: }
063:
064: /**
065: * Creates a new instance from an array of images.
066: * All icons must have the same dimensions.
067: * If an icon is null, an icon is derived for the state from the
068: * other icons.
069: */
070: public MultiIcon(Image[] images) {
071: this .icons = new Icon[images.length];
072: for (int i = 0, n = icons.length; i < n; i++) {
073: if (images[i] != null) {
074: icons[i] = new ImageIcon(images[i]);
075: }
076: }
077: generateMissingIcons();
078: }
079:
080: /**
081: * Creates a new instance.
082: * The icon representations are created lazily from the tiled image.
083: */
084: public MultiIcon(Image tiledImage, int tileCount,
085: boolean isTiledHorizontaly) {
086: this .tiledImage = tiledImage;
087: this .tileCount = tileCount;
088: this .isTiledHorizontaly = isTiledHorizontaly;
089: }
090:
091: public int getIconHeight() {
092: generateIconsFromTiledImage();
093: return icons[0].getIconHeight();
094: }
095:
096: public int getIconWidth() {
097: generateIconsFromTiledImage();
098: return icons[0].getIconWidth();
099: }
100:
101: public void paintIcon(java.awt.Component c, java.awt.Graphics g,
102: int x, int y) {
103: generateIconsFromTiledImage();
104: Icon icon = getIcon(c);
105: if (icon != null) {
106: icon.paintIcon(c, g, x, y);
107: }
108: }
109:
110: private void generateIconsFromTiledImage() {
111: if (icons == null) {
112: icons = new Icon[tileCount];
113: Image[] images = Images.split(tiledImage, tileCount,
114: isTiledHorizontaly);
115: for (int i = 0, n = Math.min(images.length, icons.length); i < n; i++) {
116: if (images[i] != null) {
117: icons[i] = new ImageIcon(images[i]);
118: }
119: }
120: generateMissingIcons();
121: tiledImage = null;
122: }
123: }
124:
125: protected abstract Icon getIcon(Component c);
126:
127: protected abstract void generateMissingIcons();
128: }
|