| 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.Component; |
| 27 | |
|
| 28 | |
import java.awt.event.ActionEvent; |
| 29 | |
|
| 30 | |
import java.util.Arrays; |
| 31 | |
import java.util.ArrayList; |
| 32 | |
import java.util.Collections; |
| 33 | |
import java.util.HashMap; |
| 34 | |
import java.util.List; |
| 35 | |
import java.util.Map; |
| 36 | |
|
| 37 | |
import javax.swing.AbstractAction; |
| 38 | |
import javax.swing.Action; |
| 39 | |
import javax.swing.ButtonGroup; |
| 40 | |
import javax.swing.JCheckBoxMenuItem; |
| 41 | |
import javax.swing.JPopupMenu; |
| 42 | |
import javax.swing.JToolBar; |
| 43 | |
import javax.swing.SwingConstants; |
| 44 | |
import javax.swing.UIManager; |
| 45 | |
|
| 46 | |
import org.dishevelled.iconbundle.IconSize; |
| 47 | |
import org.dishevelled.iconbundle.IconTextDirection; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public final class IdToolBar |
| 57 | |
extends JToolBar |
| 58 | |
{ |
| 59 | |
|
| 60 | 34 | private final AbstractAction displayIcons = new AbstractAction("Display icons") |
| 61 | 34 | { |
| 62 | |
@Override |
| 63 | |
public void actionPerformed(final ActionEvent event) |
| 64 | |
{ |
| 65 | 0 | displayIcons(); |
| 66 | 0 | } |
| 67 | |
}; |
| 68 | |
|
| 69 | |
|
| 70 | 34 | private final AbstractAction displayText = new AbstractAction("Display text") |
| 71 | 34 | { |
| 72 | |
@Override |
| 73 | |
public void actionPerformed(final ActionEvent event) |
| 74 | |
{ |
| 75 | 0 | displayText(); |
| 76 | 0 | } |
| 77 | |
}; |
| 78 | |
|
| 79 | |
|
| 80 | 34 | private final AbstractAction displayIconsAndText = new AbstractAction("Display icons and text") |
| 81 | 34 | { |
| 82 | |
@Override |
| 83 | |
public void actionPerformed(final ActionEvent event) |
| 84 | |
{ |
| 85 | 0 | displayIconsAndText(); |
| 86 | 0 | } |
| 87 | |
}; |
| 88 | |
|
| 89 | |
|
| 90 | 34 | private int display = DISPLAY_ICONS; |
| 91 | |
|
| 92 | |
|
| 93 | 34 | private IconSize iconSize = DEFAULT_ICON_SIZE; |
| 94 | |
|
| 95 | |
|
| 96 | 34 | private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION; |
| 97 | |
|
| 98 | |
|
| 99 | |
private static final int DISPLAY_ICONS = 0; |
| 100 | |
|
| 101 | |
|
| 102 | |
private static final int DISPLAY_TEXT = 1; |
| 103 | |
|
| 104 | |
|
| 105 | |
private static final int DISPLAY_ICONS_AND_TEXT = 2; |
| 106 | |
|
| 107 | |
|
| 108 | 2 | public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16; |
| 109 | |
|
| 110 | |
|
| 111 | 2 | public static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT; |
| 112 | |
|
| 113 | |
|
| 114 | 34 | private final List<AbstractAction> displayActions = Arrays.asList(new AbstractAction[] { displayIcons, displayText, displayIconsAndText }); |
| 115 | |
|
| 116 | |
|
| 117 | 34 | private final List<JCheckBoxMenuItem> displayMenuItems = new ArrayList<JCheckBoxMenuItem>(displayActions.size()); |
| 118 | |
|
| 119 | |
|
| 120 | 34 | private final Map<IconSize, AbstractAction> iconSizeActions = new HashMap<IconSize, AbstractAction>(); |
| 121 | |
|
| 122 | |
|
| 123 | 34 | private final Map<IconSize, JCheckBoxMenuItem> iconSizeMenuItems = new HashMap<IconSize, JCheckBoxMenuItem>(); |
| 124 | |
|
| 125 | |
|
| 126 | 34 | private final ButtonGroup iconSizeButtonGroup = new ButtonGroup(); |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public IdToolBar() |
| 133 | |
{ |
| 134 | 18 | super(); |
| 135 | |
|
| 136 | 18 | UIManager.put("MenuBar.gradient", null); |
| 137 | 18 | UIManager.getDefaults().put("MenuBar.gradient", null); |
| 138 | 18 | UIManager.getLookAndFeelDefaults().put("MenuBar.gradient", null); |
| 139 | |
|
| 140 | |
|
| 141 | 18 | createDisplayMenuItems(); |
| 142 | 18 | displayIcons(); |
| 143 | 18 | setIconSize(iconSize); |
| 144 | 18 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public IdToolBar(final int orientation) |
| 153 | |
{ |
| 154 | 4 | super(orientation); |
| 155 | |
|
| 156 | 4 | UIManager.put("MenuBar.gradient", null); |
| 157 | 4 | UIManager.getDefaults().put("MenuBar.gradient", null); |
| 158 | 4 | UIManager.getLookAndFeelDefaults().put("MenuBar.gradient", null); |
| 159 | |
|
| 160 | 4 | createDisplayMenuItems(); |
| 161 | 4 | displayIcons(); |
| 162 | 4 | setIconSize(iconSize); |
| 163 | 4 | } |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public IdToolBar(final String name) |
| 171 | |
{ |
| 172 | 6 | super(name); |
| 173 | 6 | createDisplayMenuItems(); |
| 174 | 6 | displayIcons(); |
| 175 | 6 | setIconSize(iconSize); |
| 176 | 6 | } |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public IdToolBar(final String name, final int orientation) |
| 186 | |
{ |
| 187 | 6 | super(name, orientation); |
| 188 | 6 | createDisplayMenuItems(); |
| 189 | 6 | displayIcons(); |
| 190 | 6 | setIconSize(iconSize); |
| 191 | 6 | } |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
private void createDisplayMenuItems() { |
| 198 | 34 | ButtonGroup buttonGroup = new ButtonGroup(); |
| 199 | 34 | for (AbstractAction displayAction : displayActions) |
| 200 | |
{ |
| 201 | 102 | JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(displayAction); |
| 202 | 102 | displayMenuItems.add(menuItem); |
| 203 | 102 | buttonGroup.add(menuItem); |
| 204 | 102 | } |
| 205 | 34 | } |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public ContextMenuButton add(final JPopupMenu contextMenu) |
| 215 | |
{ |
| 216 | 0 | ContextMenuButton contextMenuButton = new ContextMenuButton(contextMenu); |
| 217 | |
|
| 218 | |
|
| 219 | 0 | contextMenuButton.setBorderPainted(false); |
| 220 | 0 | contextMenuButton.setFocusPainted(false); |
| 221 | |
|
| 222 | 0 | super.add(contextMenuButton); |
| 223 | 0 | return contextMenuButton; |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
public IdButton add(final IdentifiableAction identifiableAction) |
| 234 | |
{ |
| 235 | 2 | IdButton idButton = new IdButton(identifiableAction); |
| 236 | 0 | idButton.setIconSize(iconSize); |
| 237 | 0 | switch (display) |
| 238 | |
{ |
| 239 | |
case DISPLAY_ICONS: |
| 240 | 0 | idButton.displayIcon(); |
| 241 | 0 | break; |
| 242 | |
case DISPLAY_TEXT: |
| 243 | 0 | idButton.displayText(); |
| 244 | 0 | break; |
| 245 | |
case DISPLAY_ICONS_AND_TEXT: |
| 246 | 0 | idButton.displayIconAndText(); |
| 247 | 0 | break; |
| 248 | |
default: |
| 249 | |
break; |
| 250 | |
} |
| 251 | |
|
| 252 | 0 | if (IdentifyUtils.isGTKLookAndFeel()) |
| 253 | |
{ |
| 254 | 0 | idButton.setHorizontalAlignment(SwingConstants.LEFT); |
| 255 | 0 | idButton.setHorizontalTextPosition(SwingConstants.TRAILING); |
| 256 | 0 | idButton.setVerticalTextPosition(SwingConstants.CENTER); |
| 257 | |
} |
| 258 | |
else |
| 259 | |
{ |
| 260 | |
|
| 261 | 0 | idButton.setHorizontalAlignment(SwingConstants.CENTER); |
| 262 | 0 | idButton.setHorizontalTextPosition(SwingConstants.CENTER); |
| 263 | 0 | idButton.setVerticalTextPosition(SwingConstants.BOTTOM); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | 0 | idButton.setBorderPainted(false); |
| 268 | 0 | idButton.setFocusPainted(false); |
| 269 | |
|
| 270 | 0 | super.add(idButton); |
| 271 | 0 | return idButton; |
| 272 | |
} |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
public IdButton remove(final IdentifiableAction identifiableAction) |
| 282 | |
{ |
| 283 | 0 | for (int i = 0; i < getComponentCount(); i++) |
| 284 | |
{ |
| 285 | 0 | Component c = getComponent(i); |
| 286 | 0 | if (c instanceof IdButton) |
| 287 | |
{ |
| 288 | 0 | IdButton idButton = (IdButton) c; |
| 289 | 0 | Action action = idButton.getAction(); |
| 290 | 0 | if (action != null && action.equals(identifiableAction)) |
| 291 | |
{ |
| 292 | 0 | remove(idButton); |
| 293 | 0 | return idButton; |
| 294 | |
} |
| 295 | |
} |
| 296 | |
} |
| 297 | 0 | return null; |
| 298 | |
} |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public void displayIcons() |
| 304 | |
{ |
| 305 | 36 | display = DISPLAY_ICONS; |
| 306 | 36 | displayIcons.putValue(Action.SELECTED_KEY, Boolean.TRUE); |
| 307 | |
|
| 308 | 36 | for (int i = 0, size = getComponentCount(); i < size; i++) |
| 309 | |
{ |
| 310 | 0 | Component component = getComponentAtIndex(i); |
| 311 | 0 | if (component instanceof IdButton) |
| 312 | |
{ |
| 313 | 0 | IdButton idButton = (IdButton) component; |
| 314 | 0 | idButton.displayIcon(); |
| 315 | |
} |
| 316 | |
} |
| 317 | 36 | } |
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
public void displayText() |
| 323 | |
{ |
| 324 | 2 | display = DISPLAY_TEXT; |
| 325 | 2 | displayText.putValue(Action.SELECTED_KEY, Boolean.TRUE); |
| 326 | |
|
| 327 | 2 | for (int i = 0, size = getComponentCount(); i < size; i++) |
| 328 | |
{ |
| 329 | 0 | Component component = getComponentAtIndex(i); |
| 330 | 0 | if (component instanceof IdButton) |
| 331 | |
{ |
| 332 | 0 | IdButton idButton = (IdButton) component; |
| 333 | 0 | idButton.displayText(); |
| 334 | |
} |
| 335 | |
} |
| 336 | 2 | } |
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
public void displayIconsAndText() |
| 342 | |
{ |
| 343 | 2 | display = DISPLAY_ICONS_AND_TEXT; |
| 344 | 2 | displayIconsAndText.putValue(Action.SELECTED_KEY, Boolean.TRUE); |
| 345 | |
|
| 346 | 2 | for (int i = 0, size = getComponentCount(); i < size; i++) |
| 347 | |
{ |
| 348 | 0 | Component component = getComponentAtIndex(i); |
| 349 | 0 | if (component instanceof IdButton) |
| 350 | |
{ |
| 351 | 0 | IdButton idButton = (IdButton) component; |
| 352 | 0 | idButton.displayIconAndText(); |
| 353 | |
} |
| 354 | |
} |
| 355 | 2 | } |
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
public IconSize getIconSize() |
| 363 | |
{ |
| 364 | 0 | return iconSize; |
| 365 | |
} |
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
public void setIconSize(final IconSize iconSize) |
| 373 | |
{ |
| 374 | 50 | if (iconSize == null) |
| 375 | |
{ |
| 376 | 2 | throw new IllegalArgumentException("iconSize must not be null"); |
| 377 | |
} |
| 378 | 48 | Action iconSizeAction = createIconSizeAction(iconSize); |
| 379 | 48 | iconSizeAction.putValue(Action.SELECTED_KEY, Boolean.TRUE); |
| 380 | |
|
| 381 | 48 | for (int i = 0, size = getComponentCount(); i < size; i++) |
| 382 | |
{ |
| 383 | 0 | Component component = getComponentAtIndex(i); |
| 384 | 0 | if (component instanceof IdButton) |
| 385 | |
{ |
| 386 | 0 | IdButton idButton = (IdButton) component; |
| 387 | 0 | idButton.setIconSize(iconSize); |
| 388 | |
} |
| 389 | |
} |
| 390 | 48 | } |
| 391 | |
|
| 392 | |
|
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
public IconTextDirection getIconTextDirection() |
| 398 | |
{ |
| 399 | 0 | return iconTextDirection; |
| 400 | |
} |
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
public List<AbstractAction> getDisplayActions() |
| 408 | |
{ |
| 409 | 4 | return displayActions; |
| 410 | |
} |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
public List<JCheckBoxMenuItem> getDisplayMenuItems() |
| 419 | |
{ |
| 420 | 0 | return Collections.unmodifiableList(displayMenuItems); |
| 421 | |
} |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
public Action createIconSizeAction(final IconSize iconSize) |
| 431 | |
{ |
| 432 | 80 | if (iconSize == null) |
| 433 | |
{ |
| 434 | 2 | throw new IllegalArgumentException("iconSize must not be null"); |
| 435 | |
} |
| 436 | 78 | if (!iconSizeActions.containsKey(iconSize)) |
| 437 | |
{ |
| 438 | 58 | iconSizeActions.put(iconSize, new IconSizeAction(iconSize)); |
| 439 | |
} |
| 440 | 78 | return iconSizeActions.get(iconSize); |
| 441 | |
} |
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
|
| 450 | |
public JCheckBoxMenuItem createIconSizeMenuItem(final IconSize iconSize) |
| 451 | |
{ |
| 452 | 0 | if (iconSize == null) |
| 453 | |
{ |
| 454 | 0 | throw new IllegalArgumentException("iconSize must not be null"); |
| 455 | |
} |
| 456 | 0 | if (!iconSizeMenuItems.containsKey(iconSize)) |
| 457 | |
{ |
| 458 | 0 | AbstractAction iconSizeAction = (AbstractAction) createIconSizeAction(iconSize); |
| 459 | 0 | JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(iconSizeAction); |
| 460 | 0 | iconSizeButtonGroup.add(menuItem); |
| 461 | 0 | iconSizeMenuItems.put(iconSize, menuItem); |
| 462 | |
} |
| 463 | 0 | return iconSizeMenuItems.get(iconSize); |
| 464 | |
} |
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
public List<AbstractAction> getDefaultIconSizeActions() |
| 472 | |
{ |
| 473 | 4 | List<AbstractAction> actions = new ArrayList<AbstractAction>(IconSize.VALUES.size()); |
| 474 | 32 | for (int i = 0, size = IconSize.VALUES.size(); i < size; i++) |
| 475 | |
{ |
| 476 | 28 | AbstractAction iconSizeAction = (AbstractAction) createIconSizeAction((IconSize) IconSize.VALUES.get(i)); |
| 477 | 28 | actions.add(iconSizeAction); |
| 478 | |
} |
| 479 | 4 | return Collections.unmodifiableList(actions); |
| 480 | |
} |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
public List<JCheckBoxMenuItem> getDefaultIconSizeMenuItems() |
| 489 | |
{ |
| 490 | 0 | List<JCheckBoxMenuItem> menuItems = new ArrayList<JCheckBoxMenuItem>(IconSize.VALUES.size()); |
| 491 | 0 | for (int i = 0, size = IconSize.VALUES.size(); i < size; i++) |
| 492 | |
{ |
| 493 | 0 | JCheckBoxMenuItem iconSizeMenuItem = createIconSizeMenuItem((IconSize) IconSize.VALUES.get(i)); |
| 494 | 0 | menuItems.add(iconSizeMenuItem); |
| 495 | |
} |
| 496 | 0 | return Collections.unmodifiableList(menuItems); |
| 497 | |
} |
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
private class IconSizeAction extends AbstractAction |
| 503 | |
{ |
| 504 | |
|
| 505 | |
private final IconSize iconSize; |
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
IconSizeAction(final IconSize iconSize) |
| 514 | 58 | { |
| 515 | 58 | super(); |
| 516 | 58 | if (iconSize == null) |
| 517 | |
{ |
| 518 | 0 | throw new IllegalArgumentException("iconSize must not be null"); |
| 519 | |
} |
| 520 | 58 | this.iconSize = iconSize; |
| 521 | 58 | putValue(Action.NAME, "Use icon size " + iconSize); |
| 522 | 58 | } |
| 523 | |
|
| 524 | |
|
| 525 | |
@Override |
| 526 | |
public void actionPerformed(final ActionEvent event) |
| 527 | |
{ |
| 528 | 0 | setIconSize(iconSize); |
| 529 | 0 | } |
| 530 | |
} |
| 531 | |
} |