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.Image;
|
---|
7 | import java.awt.Insets;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.beans.PropertyChangeListener;
|
---|
10 |
|
---|
11 | import javax.swing.Action;
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.Icon;
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JButton;
|
---|
16 | import javax.swing.SwingConstants;
|
---|
17 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 | import org.openstreetmap.josm.tools.ImageResource;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Button that is usually used in toggle dialogs.
|
---|
26 | * @since 744
|
---|
27 | */
|
---|
28 | public class SideButton extends JButton implements Destroyable {
|
---|
29 |
|
---|
30 | private transient PropertyChangeListener propertyChangeListener;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code SideButton}.
|
---|
34 | * @param action action used to specify the new button
|
---|
35 | * @since 744
|
---|
36 | */
|
---|
37 | public SideButton(Action action) {
|
---|
38 | super(action);
|
---|
39 | ImageResource icon = (ImageResource) action.getValue("ImageResource");
|
---|
40 | if (icon != null) {
|
---|
41 | setIcon(icon.getImageIconBounded(
|
---|
42 | ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
|
---|
43 | } else if (getIcon() != null) { /* TODO: remove when calling code is fixed, replace by exception */
|
---|
44 | Main.warn("Old style SideButton usage for action " + action);
|
---|
45 | fixIcon(action);
|
---|
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 | * Fix icon size
|
---|
77 | * @param action the action
|
---|
78 | * @deprecated This method is old style and will be removed together with the removal
|
---|
79 | * of old constructor code
|
---|
80 | */
|
---|
81 | @Deprecated
|
---|
82 | private void fixIcon(Action action) {
|
---|
83 | // need to listen for changes, so that putValue() that are called after the
|
---|
84 | // SideButton is constructed get the proper icon size
|
---|
85 | if (action != null) {
|
---|
86 | propertyChangeListener = evt -> {
|
---|
87 | if (Action.SMALL_ICON.equals(evt.getPropertyName())) {
|
---|
88 | fixIcon(null);
|
---|
89 | }
|
---|
90 | };
|
---|
91 | action.addPropertyChangeListener(propertyChangeListener);
|
---|
92 | }
|
---|
93 | int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension().height;
|
---|
94 | Icon i = getIcon();
|
---|
95 | if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
|
---|
96 | Image im = ((ImageIcon) i).getImage();
|
---|
97 | int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
|
---|
98 | ImageIcon icon = new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
|
---|
99 | setIcon(icon);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Do the style settings for the side button layout
|
---|
105 | */
|
---|
106 | private void doStyle() {
|
---|
107 | setLayout(new BorderLayout());
|
---|
108 | setIconTextGap(2);
|
---|
109 | setMargin(new Insets(0, 0, 0, 0));
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Create the arrow for opening a drop-down menu
|
---|
114 | * @param listener listener to use for button actions (e.g. pressing)
|
---|
115 | * @return the created button
|
---|
116 | * @since 9668
|
---|
117 | */
|
---|
118 | public BasicArrowButton createArrow(ActionListener listener) {
|
---|
119 | setMargin(new Insets(0, 0, 0, 0));
|
---|
120 | BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
121 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
122 | add(arrowButton, BorderLayout.EAST);
|
---|
123 | arrowButton.addActionListener(listener);
|
---|
124 | return arrowButton;
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public void destroy() {
|
---|
129 | Action action = getAction();
|
---|
130 | if (action instanceof Destroyable) {
|
---|
131 | ((Destroyable) action).destroy();
|
---|
132 | }
|
---|
133 | if (action != null) {
|
---|
134 | if (propertyChangeListener != null) {
|
---|
135 | action.removePropertyChangeListener(propertyChangeListener);
|
---|
136 | }
|
---|
137 | setAction(null);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|