| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.dishevelled.identify; |
| 25 | |
|
| 26 | |
import java.awt.ComponentOrientation; |
| 27 | |
|
| 28 | |
import javax.swing.Action; |
| 29 | |
import javax.swing.ImageIcon; |
| 30 | |
import javax.swing.JButton; |
| 31 | |
import javax.swing.UIManager; |
| 32 | |
|
| 33 | |
import org.dishevelled.iconbundle.IconBundle; |
| 34 | |
import org.dishevelled.iconbundle.IconSize; |
| 35 | |
import org.dishevelled.iconbundle.IconState; |
| 36 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public final class IdButton |
| 48 | |
extends JButton |
| 49 | |
{ |
| 50 | |
|
| 51 | 2 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 52 | |
|
| 53 | |
|
| 54 | 2 | private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 55 | |
|
| 56 | |
|
| 57 | 62 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 58 | |
|
| 59 | |
|
| 60 | 62 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
public IdButton(final IdentifiableAction action) |
| 69 | |
{ |
| 70 | 42 | super(); |
| 71 | |
|
| 72 | 42 | UIManager.put("Button.gradient", null); |
| 73 | 42 | UIManager.getDefaults().put("Button.gradient", null); |
| 74 | 42 | UIManager.getLookAndFeelDefaults().put("Button.gradient", null); |
| 75 | |
|
| 76 | 42 | if (action == null) |
| 77 | |
{ |
| 78 | 4 | throw new IllegalArgumentException("action must not be null"); |
| 79 | |
} |
| 80 | 38 | setAction(action); |
| 81 | 38 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
public IdButton(final IdentifiableAction action, final IconSize iconSize) |
| 91 | |
{ |
| 92 | 20 | super(); |
| 93 | |
|
| 94 | 20 | UIManager.put("Button.gradient", null); |
| 95 | 20 | UIManager.getDefaults().put("Button.gradient", null); |
| 96 | 20 | UIManager.getLookAndFeelDefaults().put("Button.gradient", null); |
| 97 | |
|
| 98 | 20 | if (action == null) |
| 99 | |
{ |
| 100 | 2 | throw new IllegalArgumentException("action must not be null"); |
| 101 | |
} |
| 102 | 18 | setIconSize(iconSize); |
| 103 | 16 | setAction(action); |
| 104 | 16 | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public IconSize getIconSize() |
| 113 | |
{ |
| 114 | 8 | return iconSize; |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public void setIconSize(final IconSize iconSize) |
| 125 | |
{ |
| 126 | 24 | if (iconSize == null) |
| 127 | |
{ |
| 128 | 4 | throw new IllegalArgumentException("iconSize must not be null"); |
| 129 | |
} |
| 130 | 20 | IconSize oldIconSize = this.iconSize; |
| 131 | 20 | this.iconSize = iconSize; |
| 132 | |
|
| 133 | 20 | if (!this.iconSize.equals(oldIconSize)) |
| 134 | |
{ |
| 135 | 20 | firePropertyChange("iconSize", oldIconSize, this.iconSize); |
| 136 | 20 | rebuild(); |
| 137 | |
} |
| 138 | 20 | } |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
IconTextDirection getIconTextDirection() |
| 146 | |
{ |
| 147 | 10 | return iconTextDirection; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public void displayIcon() |
| 154 | |
{ |
| 155 | |
|
| 156 | 4 | setText(null); |
| 157 | 4 | rebuild(); |
| 158 | 4 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public void displayText() |
| 164 | |
{ |
| 165 | |
|
| 166 | 4 | Action action = getAction(); |
| 167 | 4 | if ((action != null) && (action instanceof IdentifiableAction)) |
| 168 | |
{ |
| 169 | 4 | IdentifiableAction identifiableAction = (IdentifiableAction) action; |
| 170 | 4 | setText(identifiableAction.getName()); |
| 171 | |
} |
| 172 | 4 | setIcon(null); |
| 173 | 4 | setPressedIcon(null); |
| 174 | 4 | setSelectedIcon(null); |
| 175 | 4 | setRolloverIcon(null); |
| 176 | 4 | setRolloverSelectedIcon(null); |
| 177 | 4 | setDisabledIcon(null); |
| 178 | 4 | } |
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
public void displayIconAndText() |
| 184 | |
{ |
| 185 | |
|
| 186 | 6 | Action action = getAction(); |
| 187 | 6 | if ((action != null) && (action instanceof IdentifiableAction)) |
| 188 | |
{ |
| 189 | 6 | IdentifiableAction identifiableAction = (IdentifiableAction) action; |
| 190 | 6 | setText(identifiableAction.getName()); |
| 191 | |
} |
| 192 | 6 | rebuild(); |
| 193 | 6 | } |
| 194 | |
|
| 195 | |
@Override |
| 196 | |
public void setComponentOrientation(final ComponentOrientation orientation) |
| 197 | |
{ |
| 198 | 10 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 199 | |
|
| 200 | 10 | if (!oldOrientation.equals(orientation)) |
| 201 | |
{ |
| 202 | 10 | if (orientation != null) |
| 203 | |
{ |
| 204 | 8 | iconTextDirection = orientation.isLeftToRight() |
| 205 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 206 | |
|
| 207 | 8 | rebuild(); |
| 208 | |
} |
| 209 | |
} |
| 210 | |
|
| 211 | 10 | super.setComponentOrientation(orientation); |
| 212 | 10 | } |
| 213 | |
|
| 214 | |
@Override |
| 215 | |
public void applyComponentOrientation(final ComponentOrientation orientation) |
| 216 | |
{ |
| 217 | 6 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 218 | |
|
| 219 | 6 | if (!oldOrientation.equals(orientation)) |
| 220 | |
{ |
| 221 | 6 | if (orientation != null) |
| 222 | |
{ |
| 223 | 4 | iconTextDirection = orientation.isLeftToRight() |
| 224 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 225 | |
|
| 226 | 4 | rebuild(); |
| 227 | |
} |
| 228 | |
} |
| 229 | |
|
| 230 | 6 | super.applyComponentOrientation(orientation); |
| 231 | 4 | } |
| 232 | |
|
| 233 | |
@Override |
| 234 | |
protected void configurePropertiesFromAction(final Action action) |
| 235 | |
{ |
| 236 | 54 | super.configurePropertiesFromAction(action); |
| 237 | 54 | rebuild(); |
| 238 | 54 | } |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
private void rebuild() |
| 245 | |
{ |
| 246 | 96 | Action action = getAction(); |
| 247 | 96 | if ((action != null) && (action instanceof IdentifiableAction)) |
| 248 | |
{ |
| 249 | 80 | IdentifiableAction identifiableAction = (IdentifiableAction) action; |
| 250 | 80 | IconBundle bndl = identifiableAction.getIconBundle(); |
| 251 | 80 | setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize))); |
| 252 | 80 | setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize))); |
| 253 | 80 | setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 254 | 80 | setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize))); |
| 255 | 80 | setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 256 | |
|
| 257 | |
} |
| 258 | 96 | } |
| 259 | |
} |