| 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.Image; |
| 27 | |
import java.awt.Dimension; |
| 28 | |
import java.awt.Rectangle; |
| 29 | |
import java.awt.Graphics; |
| 30 | |
import java.awt.ComponentOrientation; |
| 31 | |
|
| 32 | |
import java.awt.event.MouseEvent; |
| 33 | |
import java.awt.event.MouseAdapter; |
| 34 | |
import java.awt.event.MouseListener; |
| 35 | |
|
| 36 | |
import javax.swing.Icon; |
| 37 | |
import javax.swing.JLabel; |
| 38 | |
import javax.swing.ImageIcon; |
| 39 | |
|
| 40 | |
import org.dishevelled.iconbundle.IconSize; |
| 41 | |
import org.dishevelled.iconbundle.IconState; |
| 42 | |
import org.dishevelled.iconbundle.IconBundle; |
| 43 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public final class IdLabel |
| 52 | |
extends JLabel |
| 53 | |
{ |
| 54 | |
|
| 55 | 2 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_32X32; |
| 56 | |
|
| 57 | |
|
| 58 | 2 | public static final IconState DEFAULT_ICON_STATE = IconState.NORMAL; |
| 59 | |
|
| 60 | |
|
| 61 | 2 | private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 62 | |
|
| 63 | |
|
| 64 | |
private Object value; |
| 65 | |
|
| 66 | |
|
| 67 | 410 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 68 | |
|
| 69 | |
|
| 70 | 410 | private IconState iconState = DEFAULT_ICON_STATE; |
| 71 | |
|
| 72 | |
|
| 73 | 410 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 74 | |
|
| 75 | |
|
| 76 | |
private transient ImageIcon imageIcon; |
| 77 | |
|
| 78 | |
|
| 79 | 410 | private transient boolean dirty = false; |
| 80 | |
|
| 81 | |
|
| 82 | 410 | private MouseListener mouseoverListener = new MouseAdapter() |
| 83 | 410 | { |
| 84 | |
@Override |
| 85 | |
public void mouseEntered(final MouseEvent e) |
| 86 | |
{ |
| 87 | 0 | if (getIconState().equals(IconState.NORMAL)) |
| 88 | |
{ |
| 89 | 0 | setIconState(IconState.MOUSEOVER); |
| 90 | 0 | repaint(); |
| 91 | |
} |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
@Override |
| 95 | |
public void mouseExited(final MouseEvent e) |
| 96 | |
{ |
| 97 | 0 | if (getIconState().equals(IconState.MOUSEOVER)) |
| 98 | |
{ |
| 99 | 0 | setIconState(IconState.NORMAL); |
| 100 | 0 | repaint(); |
| 101 | |
} |
| 102 | 0 | } |
| 103 | |
}; |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | 410 | private transient IconState previousState = DEFAULT_ICON_STATE; |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public IdLabel() |
| 116 | |
{ |
| 117 | 68 | super(); |
| 118 | |
|
| 119 | 68 | addMouseListener(mouseoverListener); |
| 120 | |
|
| 121 | 68 | setValue(null); |
| 122 | 68 | rebuild(); |
| 123 | 68 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public IdLabel(final Object value) |
| 131 | |
{ |
| 132 | 186 | super(); |
| 133 | |
|
| 134 | 186 | addMouseListener(mouseoverListener); |
| 135 | |
|
| 136 | 186 | setValue(value); |
| 137 | 186 | rebuild(); |
| 138 | 186 | } |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
public IdLabel(final Object value, final IconSize iconSize) |
| 147 | |
{ |
| 148 | 156 | super(); |
| 149 | |
|
| 150 | 156 | addMouseListener(mouseoverListener); |
| 151 | |
|
| 152 | 156 | setValue(value); |
| 153 | 156 | setIconSize(iconSize); |
| 154 | 154 | rebuild(); |
| 155 | 154 | } |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public Object getValue() |
| 164 | |
{ |
| 165 | 18 | return value; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public void setValue(final Object value) |
| 176 | |
{ |
| 177 | 450 | Object oldValue = this.value; |
| 178 | 450 | this.value = value; |
| 179 | 450 | firePropertyChange("value", oldValue, this.value); |
| 180 | |
|
| 181 | 450 | setDirty(true); |
| 182 | 450 | } |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
public IconSize getIconSize() |
| 190 | |
{ |
| 191 | 332 | return iconSize; |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
public void setIconSize(final IconSize iconSize) |
| 202 | |
{ |
| 203 | 486 | if (iconSize == null) |
| 204 | |
{ |
| 205 | 4 | throw new IllegalArgumentException("iconSize must not be null"); |
| 206 | |
} |
| 207 | |
|
| 208 | 482 | IconSize oldIconSize = this.iconSize; |
| 209 | 482 | this.iconSize = iconSize; |
| 210 | |
|
| 211 | 482 | if (!oldIconSize.equals(this.iconSize)) |
| 212 | |
{ |
| 213 | 330 | setDirty(true); |
| 214 | 330 | firePropertyChange("iconSize", oldIconSize, iconSize); |
| 215 | |
} |
| 216 | 482 | } |
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public IconState getIconState() |
| 224 | |
{ |
| 225 | 110 | return iconState; |
| 226 | |
} |
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public void setIconState(final IconState iconState) |
| 236 | |
{ |
| 237 | 76 | if (iconState == null) |
| 238 | |
{ |
| 239 | 2 | throw new IllegalArgumentException("iconState must not be null"); |
| 240 | |
} |
| 241 | |
|
| 242 | 74 | IconState oldIconState = this.iconState; |
| 243 | 74 | this.iconState = iconState; |
| 244 | |
|
| 245 | 74 | if (!oldIconState.equals(this.iconState)) |
| 246 | |
{ |
| 247 | 62 | setDirty(true); |
| 248 | 62 | firePropertyChange("iconState", oldIconState, this.iconState); |
| 249 | |
} |
| 250 | 74 | } |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
IconTextDirection getIconTextDirection() |
| 258 | |
{ |
| 259 | 10 | return iconTextDirection; |
| 260 | |
} |
| 261 | |
|
| 262 | |
@Override |
| 263 | |
public void setComponentOrientation(final ComponentOrientation orientation) |
| 264 | |
{ |
| 265 | 10 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 266 | |
|
| 267 | 10 | if (!oldOrientation.equals(orientation)) |
| 268 | |
{ |
| 269 | 10 | if (orientation != null) |
| 270 | |
{ |
| 271 | 8 | iconTextDirection = orientation.isLeftToRight() |
| 272 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 273 | |
|
| 274 | 8 | setDirty(true); |
| 275 | |
} |
| 276 | |
} |
| 277 | |
|
| 278 | 10 | super.setComponentOrientation(orientation); |
| 279 | 10 | } |
| 280 | |
|
| 281 | |
@Override |
| 282 | |
public void applyComponentOrientation(final ComponentOrientation orientation) |
| 283 | |
{ |
| 284 | 6 | ComponentOrientation oldOrientation = getComponentOrientation(); |
| 285 | |
|
| 286 | 6 | if (!oldOrientation.equals(orientation)) |
| 287 | |
{ |
| 288 | 6 | if (orientation != null) |
| 289 | |
{ |
| 290 | 4 | iconTextDirection = orientation.isLeftToRight() |
| 291 | |
? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT; |
| 292 | |
|
| 293 | 4 | setDirty(true); |
| 294 | |
} |
| 295 | |
} |
| 296 | |
|
| 297 | 6 | super.applyComponentOrientation(orientation); |
| 298 | 4 | } |
| 299 | |
|
| 300 | |
@Override |
| 301 | |
public void setEnabled(final boolean enabled) |
| 302 | |
{ |
| 303 | 28 | boolean previousEnabled = isEnabled(); |
| 304 | |
|
| 305 | 28 | if (enabled && !previousEnabled) |
| 306 | |
{ |
| 307 | 12 | setIconState(previousState); |
| 308 | |
} |
| 309 | 16 | else if (!enabled && previousEnabled) |
| 310 | |
{ |
| 311 | 12 | previousState = getIconState(); |
| 312 | 12 | setIconState(IconState.DISABLED); |
| 313 | |
} |
| 314 | |
|
| 315 | 28 | super.setEnabled(enabled); |
| 316 | |
|
| 317 | 28 | setDirty(true); |
| 318 | 28 | } |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
private void setDirty(final boolean dirty) |
| 327 | |
{ |
| 328 | 882 | this.dirty = (this.dirty || dirty); |
| 329 | 882 | } |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
private boolean isDirty() |
| 339 | |
{ |
| 340 | 2190 | return dirty; |
| 341 | |
} |
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
private void rebuild() |
| 349 | |
{ |
| 350 | 1180 | String name = IdentifyUtils.getNameFor(value); |
| 351 | 1180 | setText(name); |
| 352 | |
|
| 353 | 1180 | IconBundle iconBundle = IdentifyUtils.getIconBundleFor(value); |
| 354 | |
|
| 355 | 1180 | if (iconBundle == null) |
| 356 | |
{ |
| 357 | 432 | super.setIcon(null); |
| 358 | 432 | imageIcon = null; |
| 359 | |
} |
| 360 | |
else |
| 361 | |
{ |
| 362 | 748 | Image image = iconBundle.getImage(this, iconTextDirection, iconState, iconSize); |
| 363 | |
|
| 364 | 748 | if (imageIcon == null) |
| 365 | |
{ |
| 366 | 258 | imageIcon = new ImageIcon(image); |
| 367 | |
} |
| 368 | |
else |
| 369 | |
{ |
| 370 | 490 | imageIcon.setImage(image); |
| 371 | |
} |
| 372 | |
|
| 373 | 748 | super.setIcon(imageIcon); |
| 374 | |
} |
| 375 | |
|
| 376 | 1180 | dirty = false; |
| 377 | 1180 | } |
| 378 | |
|
| 379 | |
@Override |
| 380 | |
public String getText() |
| 381 | |
{ |
| 382 | 1070 | if (isDirty()) |
| 383 | |
{ |
| 384 | 432 | rebuild(); |
| 385 | |
} |
| 386 | 1070 | return super.getText(); |
| 387 | |
} |
| 388 | |
|
| 389 | |
@Override |
| 390 | |
public Icon getIcon() |
| 391 | |
{ |
| 392 | 236 | if (isDirty()) |
| 393 | |
{ |
| 394 | 8 | rebuild(); |
| 395 | |
} |
| 396 | 236 | return imageIcon; |
| 397 | |
} |
| 398 | |
|
| 399 | |
@Override |
| 400 | |
public Icon getDisabledIcon() |
| 401 | |
{ |
| 402 | 20 | if (isDirty()) |
| 403 | |
{ |
| 404 | 8 | rebuild(); |
| 405 | |
} |
| 406 | 20 | return imageIcon; |
| 407 | |
} |
| 408 | |
|
| 409 | |
@Override |
| 410 | |
public Rectangle getBounds() |
| 411 | |
{ |
| 412 | 144 | if (isDirty()) |
| 413 | |
{ |
| 414 | 36 | rebuild(); |
| 415 | |
} |
| 416 | 144 | return super.getBounds(); |
| 417 | |
} |
| 418 | |
|
| 419 | |
@Override |
| 420 | |
public Rectangle getBounds(final Rectangle rv) |
| 421 | |
{ |
| 422 | 72 | if (isDirty()) |
| 423 | |
{ |
| 424 | 36 | rebuild(); |
| 425 | |
} |
| 426 | 72 | return super.getBounds(rv); |
| 427 | |
} |
| 428 | |
|
| 429 | |
@Override |
| 430 | |
public Dimension getMaximumSize() |
| 431 | |
{ |
| 432 | 72 | if (isDirty()) |
| 433 | |
{ |
| 434 | 36 | rebuild(); |
| 435 | |
} |
| 436 | 72 | return super.getMaximumSize(); |
| 437 | |
} |
| 438 | |
|
| 439 | |
@Override |
| 440 | |
public Dimension getMinimumSize() |
| 441 | |
{ |
| 442 | 72 | if (isDirty()) |
| 443 | |
{ |
| 444 | 36 | rebuild(); |
| 445 | |
} |
| 446 | 72 | return super.getMinimumSize(); |
| 447 | |
} |
| 448 | |
|
| 449 | |
@Override |
| 450 | |
public Dimension getPreferredSize() |
| 451 | |
{ |
| 452 | 72 | if (isDirty()) |
| 453 | |
{ |
| 454 | 36 | rebuild(); |
| 455 | |
} |
| 456 | 72 | return super.getPreferredSize(); |
| 457 | |
} |
| 458 | |
|
| 459 | |
@Override |
| 460 | |
public Dimension getSize() |
| 461 | |
{ |
| 462 | 72 | if (isDirty()) |
| 463 | |
{ |
| 464 | 36 | rebuild(); |
| 465 | |
} |
| 466 | 72 | return super.getSize(); |
| 467 | |
} |
| 468 | |
|
| 469 | |
@Override |
| 470 | |
public Rectangle getVisibleRect() |
| 471 | |
{ |
| 472 | 72 | if (isDirty()) |
| 473 | |
{ |
| 474 | 36 | rebuild(); |
| 475 | |
} |
| 476 | 72 | return super.getVisibleRect(); |
| 477 | |
} |
| 478 | |
|
| 479 | |
@Override |
| 480 | |
public int getHeight() |
| 481 | |
{ |
| 482 | 144 | if (isDirty()) |
| 483 | |
{ |
| 484 | 36 | rebuild(); |
| 485 | |
} |
| 486 | 144 | return super.getHeight(); |
| 487 | |
} |
| 488 | |
|
| 489 | |
@Override |
| 490 | |
public int getWidth() |
| 491 | |
{ |
| 492 | 144 | if (isDirty()) |
| 493 | |
{ |
| 494 | 36 | rebuild(); |
| 495 | |
} |
| 496 | 144 | return super.getWidth(); |
| 497 | |
} |
| 498 | |
|
| 499 | |
@Override |
| 500 | |
public void paintComponent(final Graphics g) |
| 501 | |
{ |
| 502 | 0 | if (isDirty()) |
| 503 | |
{ |
| 504 | 0 | rebuild(); |
| 505 | |
} |
| 506 | 0 | super.paintComponent(g); |
| 507 | 0 | } |
| 508 | |
} |