| 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.JCheckBoxMenuItem; |
| 31 | |
|
| 32 | |
import org.dishevelled.iconbundle.IconBundle; |
| 33 | |
import org.dishevelled.iconbundle.IconSize; |
| 34 | |
import org.dishevelled.iconbundle.IconState; |
| 35 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public final class IdCheckBoxMenuItem |
| 47 | |
extends JCheckBoxMenuItem |
| 48 | |
{ |
| 49 | |
|
| 50 | 0 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 51 | |
|
| 52 | |
|
| 53 | 0 | private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 54 | |
|
| 55 | |
|
| 56 | 0 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 57 | |
|
| 58 | |
|
| 59 | 0 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 60 | |
|
| 61 | |
|
| 62 | 0 | private transient boolean dirty = true; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public IdCheckBoxMenuItem(final IdentifiableAction action) |
| 71 | |
{ |
| 72 | 0 | super(); |
| 73 | |
|
| 74 | 0 | if (action == null) |
| 75 | |
{ |
| 76 | 0 | throw new IllegalArgumentException("action must not be null"); |
| 77 | |
} |
| 78 | 0 | setAction(action); |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public IdCheckBoxMenuItem(final IdentifiableAction action, final boolean state) |
| 88 | |
{ |
| 89 | 0 | super(); |
| 90 | |
|
| 91 | 0 | if (action == null) |
| 92 | |
{ |
| 93 | 0 | throw new IllegalArgumentException("action must not be null"); |
| 94 | |
} |
| 95 | 0 | setAction(action); |
| 96 | 0 | setState(state); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize) |
| 107 | |
{ |
| 108 | 0 | super(); |
| 109 | |
|
| 110 | 0 | if (action == null) |
| 111 | |
{ |
| 112 | 0 | throw new IllegalArgumentException("action must not be null"); |
| 113 | |
} |
| 114 | 0 | setIconSize(iconSize); |
| 115 | 0 | setAction(action); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize, final boolean state) |
| 127 | |
{ |
| 128 | 0 | super(); |
| 129 | |
|
| 130 | 0 | if (action == null) |
| 131 | |
{ |
| 132 | 0 | throw new IllegalArgumentException("action must not be null"); |
| 133 | |
} |
| 134 | 0 | setIconSize(iconSize); |
| 135 | 0 | setAction(action); |
| 136 | 0 | setState(state); |
| 137 | 0 | } |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public IconSize getIconSize() |
| 146 | |
{ |
| 147 | 0 | return iconSize; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
public void setIconSize(final IconSize iconSize) |
| 158 | |
{ |
| 159 | 0 | if (iconSize == null) |
| 160 | |
{ |
| 161 | 0 | throw new IllegalArgumentException("iconSize must not be null"); |
| 162 | |
} |
| 163 | 0 | IconSize oldIconSize = this.iconSize; |
| 164 | 0 | this.iconSize = iconSize; |
| 165 | |
|
| 166 | 0 | if (!this.iconSize.equals(oldIconSize)) |
| 167 | |
{ |
| 168 | 0 | firePropertyChange("iconSize", oldIconSize, this.iconSize); |
| 169 | 0 | setDirty(true); |
| 170 | |
} |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public IconTextDirection getIconTextDirection() |
| 179 | |
{ |
| 180 | 0 | return iconTextDirection; |
| 181 | |
} |
| 182 | |
|
| 183 | |
@Override |
| 184 | |
public void setComponentOrientation(final ComponentOrientation orientation) |
| 185 | |
{ |
| 186 | 0 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 187 | |
|
| 188 | 0 | if (!oldOrientation.equals(orientation)) |
| 189 | |
{ |
| 190 | 0 | if (orientation != null) |
| 191 | |
{ |
| 192 | 0 | iconTextDirection = orientation.isLeftToRight() |
| 193 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 194 | |
|
| 195 | 0 | setDirty(true); |
| 196 | |
} |
| 197 | |
} |
| 198 | |
|
| 199 | 0 | super.setComponentOrientation(orientation); |
| 200 | 0 | } |
| 201 | |
|
| 202 | |
@Override |
| 203 | |
public void applyComponentOrientation(final ComponentOrientation orientation) |
| 204 | |
{ |
| 205 | 0 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 206 | |
|
| 207 | 0 | if (!oldOrientation.equals(orientation)) |
| 208 | |
{ |
| 209 | 0 | if (orientation != null) |
| 210 | |
{ |
| 211 | 0 | iconTextDirection = orientation.isLeftToRight() |
| 212 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 213 | |
|
| 214 | 0 | setDirty(true); |
| 215 | |
} |
| 216 | |
} |
| 217 | |
|
| 218 | 0 | super.applyComponentOrientation(orientation); |
| 219 | 0 | } |
| 220 | |
|
| 221 | |
@Override |
| 222 | |
protected void configurePropertiesFromAction(final Action action) |
| 223 | |
{ |
| 224 | 0 | super.configurePropertiesFromAction(action); |
| 225 | |
|
| 226 | 0 | if (isDirty()) |
| 227 | |
{ |
| 228 | 0 | rebuild(); |
| 229 | |
} |
| 230 | 0 | } |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
private void setDirty(final boolean dirty) |
| 239 | |
{ |
| 240 | 0 | this.dirty = (this.dirty || dirty); |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
private boolean isDirty() |
| 249 | |
{ |
| 250 | 0 | return dirty; |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
private void rebuild() |
| 258 | |
{ |
| 259 | 0 | Action action = getAction(); |
| 260 | 0 | if ((action != null) && (action instanceof IdentifiableAction)) |
| 261 | |
{ |
| 262 | 0 | IdentifiableAction identifiableAction = (IdentifiableAction) action; |
| 263 | 0 | IconBundle bndl = identifiableAction.getIconBundle(); |
| 264 | 0 | setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize))); |
| 265 | 0 | setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize))); |
| 266 | 0 | setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 267 | 0 | setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize))); |
| 268 | 0 | setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 269 | |
|
| 270 | |
} |
| 271 | 0 | dirty = false; |
| 272 | 0 | } |
| 273 | |
} |