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.tools.Destroyable;
|
---|
21 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Button that is usually used in toggle dialogs
|
---|
25 | */
|
---|
26 | public class SideButton extends JButton implements Destroyable {
|
---|
27 | private static final int iconHeight = ImageProvider.getImageSizes(ImageProvider.ImageSizes.SIDEBUTTON).height;
|
---|
28 |
|
---|
29 | private transient PropertyChangeListener propertyChangeListener;
|
---|
30 |
|
---|
31 | public SideButton(Action action) {
|
---|
32 | super(action);
|
---|
33 | fixIcon(action);
|
---|
34 | doStyle();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public SideButton(Action action, boolean usename) {
|
---|
38 | super(action);
|
---|
39 | if (!usename) {
|
---|
40 | setText(null);
|
---|
41 | fixIcon(action);
|
---|
42 | doStyle();
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public SideButton(Action action, String imagename) {
|
---|
47 | super(action);
|
---|
48 | setIcon(makeIcon(imagename));
|
---|
49 | doStyle();
|
---|
50 | }
|
---|
51 |
|
---|
52 | private void fixIcon(Action action) {
|
---|
53 | // need to listen for changes, so that putValue() that are called after the
|
---|
54 | // SideButton is constructed get the proper icon size
|
---|
55 | if (action != null) {
|
---|
56 | action.addPropertyChangeListener(propertyChangeListener = new PropertyChangeListener() {
|
---|
57 | @Override
|
---|
58 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
59 | if (evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
|
---|
60 | fixIcon(null);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | });
|
---|
64 | }
|
---|
65 | Icon i = getIcon();
|
---|
66 | if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
|
---|
67 | setIcon(getScaledImage(((ImageIcon) i).getImage()));
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Scales the given image proportionally so that the height is "iconHeight"
|
---|
73 | * @param im original image
|
---|
74 | * @return scaled image
|
---|
75 | */
|
---|
76 | private static ImageIcon getScaledImage(Image im) {
|
---|
77 | int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
|
---|
78 | return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static ImageIcon makeIcon(String imagename) {
|
---|
82 | Image im = ImageProvider.get("dialogs", imagename).getImage();
|
---|
83 | return getScaledImage(im);
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void doStyle() {
|
---|
87 | setLayout(new BorderLayout());
|
---|
88 | setIconTextGap(2);
|
---|
89 | setMargin(new Insets(0, 0, 0, 0));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void createArrow(ActionListener listener) {
|
---|
93 | setMargin(new Insets(0, 0, 0, 0));
|
---|
94 | BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
95 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
96 | add(arrowButton, BorderLayout.EAST);
|
---|
97 | arrowButton.addActionListener(listener);
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public void destroy() {
|
---|
102 | Action action = getAction();
|
---|
103 | if (action instanceof Destroyable) {
|
---|
104 | ((Destroyable) action).destroy();
|
---|
105 | }
|
---|
106 | if (action != null) {
|
---|
107 | if (propertyChangeListener != null) {
|
---|
108 | action.removePropertyChangeListener(propertyChangeListener);
|
---|
109 | }
|
---|
110 | setAction(null);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|