001: /*
002: * @(#)ButtonStateIcon.java 3.1 2005-12-08
003: *
004: * Copyright (c) 2003-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 with different visuals reflecting the state of the AbstractButton
025: * on which it draws on.
026: *
027: * @author Werner Randelshofer
028: * @version 3.1 2005-12-08 Draw pressed state if model is armed, instead of
029: * drawing pressed state if model is armed and pressed.
030: * <br>3.0 2005-10-17 Changed superclass to MultiIcon.
031: * <br>2.0.1 2005-10-02 Used Enabled image for Pressed-Unarmed state.
032: * <br>2.0.1 2005-09-11 generateMissing icons can now deal with only one
033: * provided icon image.
034: * <br>2.0 2005-03-19 Reworked.
035: * <br>1.0 October 5, 2003 Create..
036: */
037: public class ButtonStateIcon extends MultiIcon {
038: private final static int E = 0;
039: private final static int EP = 1;
040: private final static int ES = 2;
041: private final static int EPS = 3;
042: private final static int D = 4;
043: private final static int DS = 5;
044: private final static int I = 6;
045: private final static int IS = 7;
046: private final static int DI = 8;
047: private final static int DIS = 9;
048:
049: /**
050: * Creates a new instance.
051: * All icons must have the same dimensions.
052: * If an icon is null, an icon is derived for the state from the
053: * other icons.
054: */
055: public ButtonStateIcon(Icon e, Icon ep, Icon es, Icon eps, Icon d,
056: Icon ds) {
057: super (new Icon[] { e, ep, es, eps, d, ds });
058: }
059:
060: /**
061: * Creates a new instance.
062: * All icons must have the same dimensions.
063: *
064: * The array indices are used to represente the following states:
065: * [0] Enabled
066: * [1] Enabled Pressed
067: * [2] Enabled Selected
068: * [3] Enabled Pressed Selected
069: * [4] Disabled
070: * [5] Disabled Selected
071: * [6] Enabled Inactive
072: * [7] Enabled Inactive Selected
073: * [8] Disabled Inactive
074: * [9] Disabled Inactive Selected
075: *
076: * If an array element is null, an icon is derived for the state from the
077: * other icons.
078: */
079: public ButtonStateIcon(Image[] images) {
080: super (images);
081: }
082:
083: /**
084: * Creates a new instance.
085: * All icons must have the same dimensions.
086: * If an icon is null, nothing is drawn for this state.
087: */
088: public ButtonStateIcon(Icon[] icons) {
089: super (icons);
090: }
091:
092: /**
093: * Creates a new instance.
094: * The icon representations are created lazily from the image.
095: */
096: public ButtonStateIcon(Image tiledImage, int tileCount,
097: boolean isTiledHorizontally) {
098: super (tiledImage, tileCount, isTiledHorizontally);
099: }
100:
101: protected Icon getIcon(Component c) {
102: Icon icon;
103: boolean isActive = QuaquaUtilities.isOnActiveWindow(c);
104:
105: if (c instanceof AbstractButton) {
106: ButtonModel model = ((AbstractButton) c).getModel();
107: if (isActive) {
108: if (model.isEnabled()) {
109: if (/*model.isPressed() && */model.isArmed()) {
110: if (model.isSelected()) {
111: icon = icons[EPS];
112: } else {
113: icon = icons[EP];
114: }
115: } else if (model.isSelected()) {
116: icon = icons[ES];
117: } else {
118: icon = icons[E];
119: }
120: } else {
121: if (model.isSelected()) {
122: icon = icons[DS];
123: } else {
124: icon = icons[D];
125: }
126: }
127: } else {
128: if (model.isEnabled()) {
129: if (model.isSelected()) {
130: icon = icons[IS];
131: } else {
132: icon = icons[I];
133: }
134: } else {
135: if (model.isSelected()) {
136: icon = icons[DIS];
137: } else {
138: icon = icons[DI];
139: }
140: }
141: }
142: } else {
143: if (isActive) {
144: if (c.isEnabled()) {
145: icon = icons[E];
146: } else {
147: icon = icons[D];
148: }
149: } else {
150: if (c.isEnabled()) {
151: icon = icons[I];
152: } else {
153: icon = icons[DI];
154: }
155: }
156: }
157: return icon;
158: }
159:
160: protected void generateMissingIcons() {
161: if (icons.length != 10) {
162: Icon[] helper = icons;
163: icons = new Icon[10];
164: System.arraycopy(helper, 0, icons, 0, Math.min(
165: helper.length, icons.length));
166: }
167:
168: if (icons[EP] == null) {
169: icons[EP] = icons[E];
170: }
171: if (icons[ES] == null) {
172: icons[ES] = icons[EP];
173: }
174: if (icons[EPS] == null) {
175: icons[EPS] = icons[EP];
176: }
177: if (icons[D] == null) {
178: icons[D] = icons[E];
179: }
180: if (icons[DS] == null) {
181: icons[DS] = icons[ES];
182: }
183: if (icons[I] == null) {
184: icons[I] = icons[E];
185: }
186: if (icons[IS] == null) {
187: icons[IS] = icons[ES];
188: }
189: if (icons[DI] == null) {
190: icons[DI] = icons[D];
191: }
192: if (icons[DIS] == null) {
193: icons[DIS] = icons[DS];
194: }
195: }
196: }
|