1 /* 2 * @(#)ButtonStateIcon.java 1.0 2006-02-13 3 * 4 * Copyright (c) 2006 Werner Randelshofer 5 * Staldenmattweg 2, Immensee, CH-6405, Switzerland. 6 * All rights reserved. 7 * 8 * The copyright of this software is owned by Werner Randelshofer. 9 * You may not use, copy or modify this software, except in 10 * accordance with the license agreement you entered into with 11 * Werner Randelshofer. For details see accompanying license terms. 12 */ 13 14 package ch.randelshofer.quaqua.icon; 15 16 import ch.randelshofer.quaqua.util.*; 17 import ch.randelshofer.quaqua.*; 18 import java.awt.*; 19 import javax.swing.*; 20 import javax.swing.plaf.*; 21 /** 22 * An Icon with different visuals reflecting the state of the AbstractButton 23 * on which it draws on. 24 * 25 * @author Werner Randelshofer 26 * @version 1.0 2006-02-13 Created. 27 */ 28 public class FrameButtonStateIcon extends MultiIcon { 29 private final static int E = 0; 30 private final static int EP = 1; 31 private final static int ES = 2; 32 private final static int EPS = 3; 33 private final static int D = 4; 34 private final static int DS = 5; 35 private final static int I = 6; 36 private final static int IS = 7; 37 private final static int DI = 8; 38 private final static int DIS = 9; 39 private final static int R = 10; 40 private final static int RS = 11; 41 42 /** 43 * Creates a new instance. 44 * All icons must have the same dimensions. 45 * 46 * The array indices are used to represente the following states: 47 * [0] Enabled 48 * [1] Armed 49 * [2] Pressed 50 * [3] Disabled 51 * [4] Enabled Selected 52 * [5] Armed Selected 53 * [6] Pressend Selected 54 * [7] Disabled Selected 55 * 56 * If an array element is null, an icon is derived for the state from the 57 * other icons. 58 */ FrameButtonStateIcon(Image[] images)59 public FrameButtonStateIcon(Image[] images) { 60 super(images); 61 } 62 /** 63 * Creates a new instance. 64 * All icons must have the same dimensions. 65 * If an icon is null, nothing is drawn for this state. 66 */ FrameButtonStateIcon(Icon[] icons)67 public FrameButtonStateIcon(Icon[] icons) { 68 super(icons); 69 } 70 71 /** 72 * Creates a new instance. 73 * The icon representations are created lazily from the image. 74 */ FrameButtonStateIcon(Image tiledImage, int tileCount, boolean isTiledHorizontally)75 public FrameButtonStateIcon(Image tiledImage, int tileCount, boolean isTiledHorizontally) { 76 super(tiledImage, tileCount, isTiledHorizontally); 77 } 78 isRollover(Component c)79 private boolean isRollover(Component c) { 80 if (c instanceof JComponent) { 81 return ((JComponent) c).getClientProperty("paintRollover") == Boolean.TRUE; 82 } 83 return false; 84 } 85 86 getIcon(Component c)87 protected Icon getIcon(Component c) { 88 Icon icon; 89 boolean isRollover = isRollover(c); 90 boolean isActive = QuaquaUtilities.isOnActiveWindow(c); 91 92 if (c instanceof AbstractButton) { 93 ButtonModel model = ((AbstractButton) c).getModel(); 94 if (isActive) { 95 if (model.isEnabled()) { 96 if (/*model.isPressed() && */model.isArmed()) { 97 if (model.isSelected()) { 98 icon = icons[EPS]; 99 } else { 100 icon = icons[EP]; 101 } 102 } else if (model.isSelected()) { 103 icon = (isRollover) ? icons[RS] : icons[ES]; 104 } else { 105 icon = (isRollover) ? icons[R] : icons[E]; 106 } 107 } else { 108 if (model.isSelected()) { 109 icon = icons[DS]; 110 } else { 111 icon = icons[D]; 112 } 113 } 114 } else { 115 if (model.isEnabled()) { 116 if (model.isSelected()) { 117 icon = (isRollover) ? icons[RS] : icons[IS]; 118 } else { 119 icon = (isRollover) ? icons[R] : icons[I]; 120 } 121 } else { 122 if (model.isSelected()) { 123 icon = icons[DIS]; 124 } else { 125 icon = icons[DI]; 126 } 127 } 128 } 129 } else { 130 if (isActive) { 131 if (c.isEnabled()) { 132 icon = (isRollover) ? icons[R] : icons[E]; 133 } else { 134 icon = icons[D]; 135 } 136 } else { 137 if (c.isEnabled()) { 138 icon = icons[I]; 139 } else { 140 icon = icons[DI]; 141 } 142 } 143 } 144 return icon; 145 } 146 generateMissingIcons()147 protected void generateMissingIcons() { 148 if (icons.length != 12) { 149 Icon[] helper = icons; 150 icons = new Icon[12]; 151 System.arraycopy(helper, 0, icons, 0, Math.min(helper.length, icons.length)); 152 } 153 154 if (icons[EP] == null) { 155 icons[EP] = icons[E]; 156 } 157 if (icons[ES] == null) { 158 icons[ES] = icons[EP]; 159 } 160 if (icons[EPS] == null) { 161 icons[EPS] = icons[EP]; 162 } 163 if (icons[D] == null) { 164 icons[D] = icons[E]; 165 } 166 if (icons[DS] == null) { 167 icons[DS] = icons[ES]; 168 } 169 if (icons[I] == null) { 170 icons[I] = icons[E]; 171 } 172 if (icons[IS] == null) { 173 icons[IS] = icons[ES]; 174 } 175 if (icons[DI] == null) { 176 icons[DI] = icons[D]; 177 } 178 if (icons[DIS] == null) { 179 icons[DIS] = icons[DS]; 180 } 181 if (icons[R] == null) { 182 icons[R] = icons[E]; 183 } 184 if (icons[RS] == null) { 185 icons[RS] = icons[ES]; 186 } 187 } 188 } 189