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