source: josm/trunk/src/org/openstreetmap/josm/gui/SideButton.java@ 5463

Last change on this file since 5463 was 5463, checked in by jttt, 13 years ago

Fix some memory leaks (see #7980)

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Color;
8import java.awt.Image;
9import java.awt.Insets;
10import java.awt.event.ActionListener;
11import java.beans.PropertyChangeEvent;
12import java.beans.PropertyChangeListener;
13
14import javax.swing.Action;
15import javax.swing.BorderFactory;
16import javax.swing.Icon;
17import javax.swing.ImageIcon;
18import javax.swing.JButton;
19import javax.swing.SwingConstants;
20import javax.swing.plaf.basic.BasicArrowButton;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.tools.Destroyable;
24import org.openstreetmap.josm.tools.ImageProvider;
25import org.openstreetmap.josm.tools.Shortcut;
26
27public 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}
Note: See TracBrowser for help on using the repository browser.