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

Last change on this file since 5799 was 5799, checked in by akks, 11 years ago

Membership tabled in properties toggle dialog supports multiselect (and multiple membership deletion)
Property toggle dialog refactoring - methods splitting and reordering
see #7846: more RelationListDialog refactoring, all other relation-related actions separated from dialogs, @Override, JavaDocs

  • Property svn:eol-style set to native
File size: 3.8 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.tools.Destroyable;
23import org.openstreetmap.josm.tools.ImageProvider;
24
25/**
26 * Button that is usually used in toggle dialogs
27 */
28public 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}
Note: See TracBrowser for help on using the repository browser.