1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Image;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.beans.PropertyChangeEvent;
|
---|
12 | import java.beans.PropertyChangeListener;
|
---|
13 |
|
---|
14 | import javax.swing.Action;
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.Icon;
|
---|
17 | import javax.swing.ImageIcon;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.SwingConstants;
|
---|
20 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Button that is usually used in toggle dialogs
|
---|
27 | */
|
---|
28 | public class SideButton extends JButton implements Destroyable {
|
---|
29 | private final static int iconHeight = 20;
|
---|
30 |
|
---|
31 | private PropertyChangeListener propertyChangeListener;
|
---|
32 |
|
---|
33 | public SideButton(Action action)
|
---|
34 | {
|
---|
35 | super(action);
|
---|
36 | fixIcon(action);
|
---|
37 | doStyle();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public SideButton(Action action, boolean usename)
|
---|
41 | {
|
---|
42 | super(action);
|
---|
43 | if(!usename) {
|
---|
44 | setText(null);
|
---|
45 | fixIcon(action);
|
---|
46 | doStyle();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public SideButton(Action action, String imagename)
|
---|
51 | {
|
---|
52 | super(action);
|
---|
53 | setIcon(makeIcon(imagename));
|
---|
54 | doStyle();
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void fixIcon(Action action) {
|
---|
58 | // need to listen for changes, so that putValue() that are called after the
|
---|
59 | // SideButton is constructed get the proper icon size
|
---|
60 | if (action != null) {
|
---|
61 | action.addPropertyChangeListener(propertyChangeListener = new PropertyChangeListener() {
|
---|
62 | @Override
|
---|
63 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
64 | if (evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
|
---|
65 | fixIcon(null);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | });
|
---|
69 | }
|
---|
70 | Icon i = getIcon();
|
---|
71 | if (i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
|
---|
72 | setIcon(getScaledImage(((ImageIcon) i).getImage()));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** scales the given image proportionally so that the height is "iconHeight" **/
|
---|
77 | private static ImageIcon getScaledImage(Image im) {
|
---|
78 | int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
|
---|
79 | return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static ImageIcon makeIcon(String imagename) {
|
---|
83 | Image im = ImageProvider.get("dialogs", imagename).getImage();
|
---|
84 | return getScaledImage(im);
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void setup(String name, String property, String tooltip, ActionListener actionListener)
|
---|
88 | {
|
---|
89 | doStyle();
|
---|
90 | setActionCommand(name);
|
---|
91 | addActionListener(actionListener);
|
---|
92 | setToolTipText(tooltip);
|
---|
93 | putClientProperty("help", "Dialog/"+property+"/"+name);
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void doStyle()
|
---|
97 | {
|
---|
98 | setLayout(new BorderLayout());
|
---|
99 | setIconTextGap(2);
|
---|
100 | setMargin(new Insets(-1,0,-1,0));
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void createArrow(ActionListener listener) {
|
---|
104 | setMargin(new Insets(0,0,0,0));
|
---|
105 | BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
106 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
107 | add(arrowButton, BorderLayout.EAST);
|
---|
108 | arrowButton.addActionListener(listener);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void destroy() {
|
---|
113 | Action action = getAction();
|
---|
114 | if (action instanceof Destroyable) {
|
---|
115 | ((Destroyable) action).destroy();
|
---|
116 | }
|
---|
117 | if (action != null) {
|
---|
118 | if (propertyChangeListener != null) {
|
---|
119 | action.removePropertyChangeListener(propertyChangeListener);
|
---|
120 | }
|
---|
121 | setAction(null);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|