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