[3719] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[744] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[5007] | 4 | import java.awt.BorderLayout;
|
---|
[4354] | 5 | import java.awt.Color;
|
---|
[2017] | 6 | import java.awt.Insets;
|
---|
[744] | 7 | import java.awt.event.ActionListener;
|
---|
[2017] | 8 |
|
---|
[13188] | 9 | import javax.swing.AbstractAction;
|
---|
[744] | 10 | import javax.swing.Action;
|
---|
[4354] | 11 | import javax.swing.BorderFactory;
|
---|
[744] | 12 | import javax.swing.JButton;
|
---|
[4354] | 13 | import javax.swing.SwingConstants;
|
---|
| 14 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
[744] | 15 |
|
---|
[5463] | 16 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
[744] | 17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[10356] | 18 | import org.openstreetmap.josm.tools.ImageResource;
|
---|
[744] | 19 |
|
---|
[5799] | 20 | /**
|
---|
[10179] | 21 | * Button that is usually used in toggle dialogs.
|
---|
| 22 | * @since 744
|
---|
[5799] | 23 | */
|
---|
[5463] | 24 | public class SideButton extends JButton implements Destroyable {
|
---|
[6070] | 25 |
|
---|
[13545] | 26 | private BasicArrowButton arrowButton;
|
---|
| 27 | private boolean arrowEnabledWithButton;
|
---|
[5007] | 28 |
|
---|
[10179] | 29 | /**
|
---|
| 30 | * Constructs a new {@code SideButton}.
|
---|
| 31 | * @param action action used to specify the new button
|
---|
[13188] | 32 | * an icon must be provided with {@link ImageResource#attachImageIcon(AbstractAction, boolean)}
|
---|
[13187] | 33 | * @throws IllegalArgumentException if no icon provided
|
---|
[10356] | 34 | * @since 744
|
---|
[10179] | 35 | */
|
---|
[6246] | 36 | public SideButton(Action action) {
|
---|
[1169] | 37 | super(action);
|
---|
[10356] | 38 | ImageResource icon = (ImageResource) action.getValue("ImageResource");
|
---|
| 39 | if (icon != null) {
|
---|
| 40 | setIcon(icon.getImageIconBounded(
|
---|
| 41 | ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
|
---|
[13187] | 42 | } else {
|
---|
| 43 | throw new IllegalArgumentException("No icon provided");
|
---|
[10356] | 44 | }
|
---|
[1169] | 45 | doStyle();
|
---|
| 46 | }
|
---|
[1227] | 47 |
|
---|
[10179] | 48 | /**
|
---|
| 49 | * Constructs a new {@code SideButton}.
|
---|
| 50 | * @param action action used to specify the new button
|
---|
| 51 | * @param usename use action name
|
---|
[10356] | 52 | * @since 2710
|
---|
[10179] | 53 | */
|
---|
[6246] | 54 | public SideButton(Action action, boolean usename) {
|
---|
[10428] | 55 | this(action);
|
---|
[8510] | 56 | if (!usename) {
|
---|
[2710] | 57 | setText(null);
|
---|
[5028] | 58 | }
|
---|
[2710] | 59 | }
|
---|
| 60 |
|
---|
[10179] | 61 | /**
|
---|
| 62 | * Constructs a new {@code SideButton}.
|
---|
| 63 | * @param action action used to specify the new button
|
---|
| 64 | * @param imagename image name in "dialogs" directory
|
---|
[10356] | 65 | * @since 2747
|
---|
[10179] | 66 | */
|
---|
[6246] | 67 | public SideButton(Action action, String imagename) {
|
---|
[2586] | 68 | super(action);
|
---|
[10428] | 69 | setIcon(ImageProvider.get("dialogs", imagename, ImageProvider.ImageSizes.SIDEBUTTON));
|
---|
[2586] | 70 | doStyle();
|
---|
[1247] | 71 | }
|
---|
| 72 |
|
---|
[10362] | 73 | /**
|
---|
[10356] | 74 | * Do the style settings for the side button layout
|
---|
[8958] | 75 | */
|
---|
[6367] | 76 | private void doStyle() {
|
---|
[4354] | 77 | setLayout(new BorderLayout());
|
---|
[1169] | 78 | setIconTextGap(2);
|
---|
[8510] | 79 | setMargin(new Insets(0, 0, 0, 0));
|
---|
[1169] | 80 | }
|
---|
[4354] | 81 |
|
---|
[10356] | 82 | /**
|
---|
| 83 | * Create the arrow for opening a drop-down menu
|
---|
| 84 | * @param listener listener to use for button actions (e.g. pressing)
|
---|
| 85 | * @return the created button
|
---|
| 86 | * @since 9668
|
---|
| 87 | */
|
---|
[9668] | 88 | public BasicArrowButton createArrow(ActionListener listener) {
|
---|
[13545] | 89 | return createArrow(listener, false);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Create the arrow for opening a drop-down menu
|
---|
| 94 | * @param listener listener to use for button actions (e.g. pressing)
|
---|
| 95 | * @param enabledWithButton determines if the button arrow enabled state is the same as main button
|
---|
| 96 | * @return the created button
|
---|
| 97 | * @since 13545
|
---|
| 98 | */
|
---|
| 99 | public BasicArrowButton createArrow(ActionListener listener, boolean enabledWithButton) {
|
---|
[8510] | 100 | setMargin(new Insets(0, 0, 0, 0));
|
---|
[13545] | 101 | arrowEnabledWithButton = enabledWithButton;
|
---|
| 102 | arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
[4354] | 103 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
| 104 | add(arrowButton, BorderLayout.EAST);
|
---|
| 105 | arrowButton.addActionListener(listener);
|
---|
[13545] | 106 | if (arrowEnabledWithButton) {
|
---|
| 107 | arrowButton.setEnabled(isEnabled());
|
---|
| 108 | }
|
---|
[9668] | 109 | return arrowButton;
|
---|
[4354] | 110 | }
|
---|
[5463] | 111 |
|
---|
| 112 | @Override
|
---|
[13545] | 113 | public void setEnabled(boolean b) {
|
---|
| 114 | super.setEnabled(b);
|
---|
| 115 | if (arrowButton != null && arrowEnabledWithButton) {
|
---|
| 116 | arrowButton.setEnabled(b);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | @Override
|
---|
[5463] | 121 | public void destroy() {
|
---|
| 122 | Action action = getAction();
|
---|
| 123 | if (action instanceof Destroyable) {
|
---|
| 124 | ((Destroyable) action).destroy();
|
---|
| 125 | }
|
---|
[5471] | 126 | if (action != null) {
|
---|
| 127 | setAction(null);
|
---|
| 128 | }
|
---|
[13545] | 129 | arrowButton = null;
|
---|
[5463] | 130 | }
|
---|
[744] | 131 | }
|
---|