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

Last change on this file since 6070 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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