| 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.JMenuItem; |
| 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 IdMenuItem |
| 47 | |
extends JMenuItem |
| 48 | |
{ |
| 49 | |
|
| 50 | 2 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 51 | |
|
| 52 | |
|
| 53 | 2 | private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 54 | |
|
| 55 | |
|
| 56 | 54 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 57 | |
|
| 58 | |
|
| 59 | 54 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 60 | |
|
| 61 | |
|
| 62 | 54 | private transient boolean dirty = true; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public IdMenuItem(final IdentifiableAction action) |
| 71 | |
{ |
| 72 | 34 | super(); |
| 73 | |
|
| 74 | 34 | if (action == null) |
| 75 | |
{ |
| 76 | 2 | throw new IllegalArgumentException("action must not be null"); |
| 77 | |
} |
| 78 | 32 | setAction(action); |
| 79 | 32 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public IdMenuItem(final IdentifiableAction action, final IconSize iconSize) |
| 89 | |
{ |
| 90 | 20 | super(); |
| 91 | |
|
| 92 | 20 | if (action == null) |
| 93 | |
{ |
| 94 | 2 | throw new IllegalArgumentException("action must not be null"); |
| 95 | |
} |
| 96 | 18 | setIconSize(iconSize); |
| 97 | 16 | setAction(action); |
| 98 | 16 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
public IconSize getIconSize() |
| 107 | |
{ |
| 108 | 8 | return iconSize; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public void setIconSize(final IconSize iconSize) |
| 119 | |
{ |
| 120 | 24 | if (iconSize == null) |
| 121 | |
{ |
| 122 | 4 | throw new IllegalArgumentException("iconSize must not be null"); |
| 123 | |
} |
| 124 | 20 | IconSize oldIconSize = this.iconSize; |
| 125 | 20 | this.iconSize = iconSize; |
| 126 | |
|
| 127 | 20 | if (!this.iconSize.equals(oldIconSize)) |
| 128 | |
{ |
| 129 | 20 | firePropertyChange("iconSize", oldIconSize, this.iconSize); |
| 130 | 20 | setDirty(true); |
| 131 | |
} |
| 132 | 20 | } |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
public IconTextDirection getIconTextDirection() |
| 140 | |
{ |
| 141 | 10 | return iconTextDirection; |
| 142 | |
} |
| 143 | |
|
| 144 | |
@Override |
| 145 | |
public void setComponentOrientation(final ComponentOrientation orientation) |
| 146 | |
{ |
| 147 | 10 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 148 | |
|
| 149 | 10 | if (!oldOrientation.equals(orientation)) |
| 150 | |
{ |
| 151 | 10 | if (orientation != null) |
| 152 | |
{ |
| 153 | 8 | iconTextDirection = orientation.isLeftToRight() |
| 154 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 155 | |
|
| 156 | 8 | setDirty(true); |
| 157 | |
} |
| 158 | |
} |
| 159 | |
|
| 160 | 10 | super.setComponentOrientation(orientation); |
| 161 | 10 | } |
| 162 | |
|
| 163 | |
@Override |
| 164 | |
public void applyComponentOrientation(final ComponentOrientation orientation) |
| 165 | |
{ |
| 166 | 6 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 167 | |
|
| 168 | 6 | if (!oldOrientation.equals(orientation)) |
| 169 | |
{ |
| 170 | 6 | if (orientation != null) |
| 171 | |
{ |
| 172 | 4 | iconTextDirection = orientation.isLeftToRight() |
| 173 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 174 | |
|
| 175 | 4 | setDirty(true); |
| 176 | |
} |
| 177 | |
} |
| 178 | |
|
| 179 | 6 | super.applyComponentOrientation(orientation); |
| 180 | 4 | } |
| 181 | |
|
| 182 | |
@Override |
| 183 | |
protected void configurePropertiesFromAction(final Action action) |
| 184 | |
{ |
| 185 | 48 | super.configurePropertiesFromAction(action); |
| 186 | |
|
| 187 | 48 | if (isDirty()) |
| 188 | |
{ |
| 189 | 48 | rebuild(); |
| 190 | |
} |
| 191 | 48 | } |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
private void setDirty(final boolean dirty) |
| 200 | |
{ |
| 201 | 32 | this.dirty = (this.dirty || dirty); |
| 202 | 32 | } |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
private boolean isDirty() |
| 210 | |
{ |
| 211 | 48 | return dirty; |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
private void rebuild() |
| 219 | |
{ |
| 220 | 48 | Action action = getAction(); |
| 221 | 48 | if ((action != null) && (action instanceof IdentifiableAction)) |
| 222 | |
{ |
| 223 | 48 | IdentifiableAction identifiableAction = (IdentifiableAction) action; |
| 224 | 48 | IconBundle bndl = identifiableAction.getIconBundle(); |
| 225 | 48 | setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize))); |
| 226 | 48 | setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize))); |
| 227 | 48 | setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 228 | 48 | setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize))); |
| 229 | 48 | setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 230 | |
|
| 231 | |
} |
| 232 | 48 | dirty = false; |
| 233 | 48 | } |
| 234 | |
} |