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.Main;
|
---|
23 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
24 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 |
|
---|
27 | public class SideButton extends JButton implements Destroyable {
|
---|
28 | private final static int iconHeight = 20;
|
---|
29 |
|
---|
30 | private PropertyChangeListener propertyChangeListener;
|
---|
31 |
|
---|
32 | public SideButton(Action action)
|
---|
33 | {
|
---|
34 | super(action);
|
---|
35 | fixIcon(action);
|
---|
36 | doStyle();
|
---|
37 | }
|
---|
38 |
|
---|
39 | public SideButton(Action action, boolean usename)
|
---|
40 | {
|
---|
41 | super(action);
|
---|
42 | if(!usename) {
|
---|
43 | setText(null);
|
---|
44 | fixIcon(action);
|
---|
45 | doStyle();
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public SideButton(Action action, String imagename)
|
---|
50 | {
|
---|
51 | super(action);
|
---|
52 | setIcon(makeIcon(imagename));
|
---|
53 | doStyle();
|
---|
54 | }
|
---|
55 |
|
---|
56 | private void fixIcon(Action action) {
|
---|
57 | // need to listen for changes, so that putValue() that are called after the
|
---|
58 | // SideButton is constructed get the proper icon size
|
---|
59 | if (action != null) {
|
---|
60 | action.addPropertyChangeListener(propertyChangeListener = new PropertyChangeListener() {
|
---|
61 | @Override
|
---|
62 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
63 | if (evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
|
---|
64 | fixIcon(null);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | });
|
---|
68 | }
|
---|
69 | Icon i = getIcon();
|
---|
70 | if (i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
|
---|
71 | setIcon(getScaledImage(((ImageIcon) i).getImage()));
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** scales the given image proportionally so that the height is "iconHeight" **/
|
---|
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 | // Used constructor with Action
|
---|
87 | @Deprecated
|
---|
88 | public SideButton(String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
89 | {
|
---|
90 | super(makeIcon(imagename));
|
---|
91 | doStyle();
|
---|
92 | setActionCommand(imagename);
|
---|
93 | addActionListener(actionListener);
|
---|
94 | setToolTipText(tooltip);
|
---|
95 | }
|
---|
96 |
|
---|
97 | // Used constructor with Action
|
---|
98 | @Deprecated
|
---|
99 | public SideButton(String name, String imagename, String property, String tooltip, Shortcut shortcut, ActionListener actionListener)
|
---|
100 | {
|
---|
101 | super(tr(name), makeIcon(imagename));
|
---|
102 | if(shortcut != null)
|
---|
103 | {
|
---|
104 | shortcut.setMnemonic(this);
|
---|
105 | if(tooltip != null) {
|
---|
106 | tooltip = Main.platform.makeTooltip(tooltip, shortcut);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | setup(name, property, tooltip, actionListener);
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Used constructor with Action
|
---|
113 | @Deprecated
|
---|
114 | public SideButton(String name, String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
115 | {
|
---|
116 | super(tr(name), makeIcon(imagename));
|
---|
117 | setup(name, property, tooltip, actionListener);
|
---|
118 | }
|
---|
119 | private void setup(String name, String property, String tooltip, ActionListener actionListener)
|
---|
120 | {
|
---|
121 | doStyle();
|
---|
122 | setActionCommand(name);
|
---|
123 | addActionListener(actionListener);
|
---|
124 | setToolTipText(tooltip);
|
---|
125 | putClientProperty("help", "Dialog/"+property+"/"+name);
|
---|
126 | }
|
---|
127 | private void doStyle()
|
---|
128 | {
|
---|
129 | setLayout(new BorderLayout());
|
---|
130 | setIconTextGap(2);
|
---|
131 | setMargin(new Insets(-1,0,-1,0));
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void createArrow(ActionListener listener) {
|
---|
135 | setMargin(new Insets(0,0,0,0));
|
---|
136 | BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
137 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
138 | add(arrowButton, BorderLayout.EAST);
|
---|
139 | arrowButton.addActionListener(listener);
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public void destroy() {
|
---|
144 | Action action = getAction();
|
---|
145 | if (action instanceof Destroyable) {
|
---|
146 | ((Destroyable) action).destroy();
|
---|
147 | }
|
---|
148 | if (action != null) {
|
---|
149 | if (propertyChangeListener != null) {
|
---|
150 | action.removePropertyChangeListener(propertyChangeListener);
|
---|
151 | }
|
---|
152 | setAction(null);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|