| 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.ImageIcon; |
| 29 | |
import javax.swing.JMenu; |
| 30 | |
|
| 31 | |
import org.dishevelled.iconbundle.IconBundle; |
| 32 | |
import org.dishevelled.iconbundle.IconSize; |
| 33 | |
import org.dishevelled.iconbundle.IconState; |
| 34 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public final class IdMenu |
| 43 | |
extends JMenu |
| 44 | |
{ |
| 45 | |
|
| 46 | 2 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 47 | |
|
| 48 | |
|
| 49 | 2 | private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 50 | |
|
| 51 | |
|
| 52 | 24 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 53 | |
|
| 54 | |
|
| 55 | 24 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 56 | |
|
| 57 | |
|
| 58 | |
private Object value; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public IdMenu() |
| 65 | |
{ |
| 66 | 2 | this(null); |
| 67 | 2 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public IdMenu(final Object value) |
| 75 | |
{ |
| 76 | 12 | super(); |
| 77 | 12 | setValue(value); |
| 78 | 12 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public IdMenu(final Object value, final IconSize iconSize) |
| 87 | |
{ |
| 88 | 12 | super(); |
| 89 | 12 | setValue(value); |
| 90 | |
|
| 91 | 12 | setIconSize(iconSize); |
| 92 | 10 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public Object getValue() |
| 101 | |
{ |
| 102 | 0 | return value; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public void setValue(final Object value) |
| 113 | |
{ |
| 114 | 24 | Object oldValue = this.value; |
| 115 | 24 | this.value = value; |
| 116 | 24 | firePropertyChange("value", oldValue, this.value); |
| 117 | 24 | rebuild(); |
| 118 | 24 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public IconSize getIconSize() |
| 126 | |
{ |
| 127 | 8 | return iconSize; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public void setIconSize(final IconSize iconSize) |
| 138 | |
{ |
| 139 | 18 | if (iconSize == null) |
| 140 | |
{ |
| 141 | 4 | throw new IllegalArgumentException("iconSize must not be null"); |
| 142 | |
} |
| 143 | 14 | IconSize oldIconSize = this.iconSize; |
| 144 | 14 | this.iconSize = iconSize; |
| 145 | |
|
| 146 | 14 | if (!this.iconSize.equals(oldIconSize)) |
| 147 | |
{ |
| 148 | 8 | firePropertyChange("iconSize", oldIconSize, this.iconSize); |
| 149 | 8 | rebuild(); |
| 150 | |
} |
| 151 | 14 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public IconTextDirection getIconTextDirection() |
| 159 | |
{ |
| 160 | 0 | return iconTextDirection; |
| 161 | |
} |
| 162 | |
|
| 163 | |
@Override |
| 164 | |
public void setComponentOrientation(final ComponentOrientation orientation) |
| 165 | |
{ |
| 166 | 0 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 167 | |
|
| 168 | 0 | if (!oldOrientation.equals(orientation)) |
| 169 | |
{ |
| 170 | 0 | if (orientation != null) |
| 171 | |
{ |
| 172 | 0 | iconTextDirection = orientation.isLeftToRight() |
| 173 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 174 | |
|
| 175 | 0 | rebuild(); |
| 176 | |
} |
| 177 | |
} |
| 178 | |
|
| 179 | 0 | super.setComponentOrientation(orientation); |
| 180 | 0 | } |
| 181 | |
|
| 182 | |
@Override |
| 183 | |
public void applyComponentOrientation(final ComponentOrientation orientation) |
| 184 | |
{ |
| 185 | 0 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 186 | |
|
| 187 | 0 | if (!oldOrientation.equals(orientation)) |
| 188 | |
{ |
| 189 | 0 | if (orientation != null) |
| 190 | |
{ |
| 191 | 0 | iconTextDirection = orientation.isLeftToRight() |
| 192 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 193 | |
|
| 194 | 0 | rebuild(); |
| 195 | |
} |
| 196 | |
} |
| 197 | |
|
| 198 | 0 | super.applyComponentOrientation(orientation); |
| 199 | 0 | } |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
private void rebuild() |
| 207 | |
{ |
| 208 | 32 | String name = IdentifyUtils.getNameFor(value); |
| 209 | 32 | setText(name); |
| 210 | 32 | IconBundle bndl = IdentifyUtils.getIconBundleFor(value); |
| 211 | |
|
| 212 | 32 | if (bndl == null) |
| 213 | |
{ |
| 214 | 14 | setIcon(null); |
| 215 | |
} |
| 216 | |
else |
| 217 | |
{ |
| 218 | 18 | setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize))); |
| 219 | 18 | setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize))); |
| 220 | 18 | setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 221 | 18 | setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize))); |
| 222 | 18 | setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize))); |
| 223 | |
|
| 224 | |
} |
| 225 | 32 | } |
| 226 | |
} |