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 |
|
---|
9 | import javax.swing.AbstractAction;
|
---|
10 | import javax.swing.Action;
|
---|
11 | import javax.swing.BorderFactory;
|
---|
12 | import javax.swing.JButton;
|
---|
13 | import javax.swing.SwingConstants;
|
---|
14 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 | import org.openstreetmap.josm.tools.ImageResource;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Button that is usually used in toggle dialogs.
|
---|
22 | * @since 744
|
---|
23 | */
|
---|
24 | public class SideButton extends JButton implements Destroyable {
|
---|
25 |
|
---|
26 | private BasicArrowButton arrowButton;
|
---|
27 | private boolean arrowEnabledWithButton;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code SideButton}.
|
---|
31 | * @param action action used to specify the new button
|
---|
32 | * an icon must be provided with {@link ImageResource#attachImageIcon(AbstractAction, boolean)}
|
---|
33 | * @throws IllegalArgumentException if no icon provided
|
---|
34 | * @since 744
|
---|
35 | */
|
---|
36 | public SideButton(Action action) {
|
---|
37 | super(action);
|
---|
38 | ImageResource icon = (ImageResource) action.getValue("ImageResource");
|
---|
39 | if (icon != null) {
|
---|
40 | setIcon(icon.getImageIconBounded(
|
---|
41 | ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
|
---|
42 | } else {
|
---|
43 | throw new IllegalArgumentException("No icon provided");
|
---|
44 | }
|
---|
45 | doStyle();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a new {@code SideButton}.
|
---|
50 | * @param action action used to specify the new button
|
---|
51 | * @param usename use action name
|
---|
52 | * @since 2710
|
---|
53 | */
|
---|
54 | public SideButton(Action action, boolean usename) {
|
---|
55 | this(action);
|
---|
56 | if (!usename) {
|
---|
57 | setText(null);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
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
|
---|
65 | * @since 2747
|
---|
66 | */
|
---|
67 | public SideButton(Action action, String imagename) {
|
---|
68 | super(action);
|
---|
69 | setIcon(ImageProvider.get("dialogs", imagename, ImageProvider.ImageSizes.SIDEBUTTON));
|
---|
70 | doStyle();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Do the style settings for the side button layout
|
---|
75 | */
|
---|
76 | private void doStyle() {
|
---|
77 | setLayout(new BorderLayout());
|
---|
78 | setIconTextGap(2);
|
---|
79 | setMargin(new Insets(0, 0, 0, 0));
|
---|
80 | }
|
---|
81 |
|
---|
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 | */
|
---|
88 | public BasicArrowButton createArrow(ActionListener listener) {
|
---|
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) {
|
---|
100 | setMargin(new Insets(0, 0, 0, 0));
|
---|
101 | arrowEnabledWithButton = enabledWithButton;
|
---|
102 | arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
103 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
104 | add(arrowButton, BorderLayout.EAST);
|
---|
105 | arrowButton.addActionListener(listener);
|
---|
106 | if (arrowEnabledWithButton) {
|
---|
107 | arrowButton.setEnabled(isEnabled());
|
---|
108 | }
|
---|
109 | return arrowButton;
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public void setEnabled(boolean b) {
|
---|
114 | super.setEnabled(b);
|
---|
115 | if (arrowButton != null && arrowEnabledWithButton) {
|
---|
116 | arrowButton.setEnabled(b);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void destroy() {
|
---|
122 | Action action = getAction();
|
---|
123 | if (action instanceof Destroyable) {
|
---|
124 | ((Destroyable) action).destroy();
|
---|
125 | }
|
---|
126 | if (action != null) {
|
---|
127 | setAction(null);
|
---|
128 | }
|
---|
129 | arrowButton = null;
|
---|
130 | }
|
---|
131 | }
|
---|