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