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

Last change on this file since 7337 was 6889, checked in by Don-vip, 10 years ago

fix some Sonar issues (JLS order)

  • Property svn:eol-style set to native
File size: 3.4 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 static final int iconHeight = 20;
28
29 private PropertyChangeListener propertyChangeListener;
30
31 public SideButton(Action action) {
32 super(action);
33 fixIcon(action);
34 doStyle();
35 }
36
37 public SideButton(Action action, boolean usename) {
38 super(action);
39 if(!usename) {
40 setText(null);
41 fixIcon(action);
42 doStyle();
43 }
44 }
45
46 public SideButton(Action action, String imagename) {
47 super(action);
48 setIcon(makeIcon(imagename));
49 doStyle();
50 }
51
52 private void fixIcon(Action action) {
53 // need to listen for changes, so that putValue() that are called after the
54 // SideButton is constructed get the proper icon size
55 if (action != null) {
56 action.addPropertyChangeListener(propertyChangeListener = new PropertyChangeListener() {
57 @Override
58 public void propertyChange(PropertyChangeEvent evt) {
59 if (evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
60 fixIcon(null);
61 }
62 }
63 });
64 }
65 Icon i = getIcon();
66 if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
67 setIcon(getScaledImage(((ImageIcon) i).getImage()));
68 }
69 }
70
71 /** scales the given image proportionally so that the height is "iconHeight" **/
72 private static ImageIcon getScaledImage(Image im) {
73 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
74 return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
75 }
76
77 public static ImageIcon makeIcon(String imagename) {
78 Image im = ImageProvider.get("dialogs", imagename).getImage();
79 return getScaledImage(im);
80 }
81
82 private void doStyle() {
83 setLayout(new BorderLayout());
84 setIconTextGap(2);
85 setMargin(new Insets(-1,0,-1,0));
86 }
87
88 public void createArrow(ActionListener listener) {
89 setMargin(new Insets(0,0,0,0));
90 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
91 arrowButton.setBorder(BorderFactory.createEmptyBorder());
92 add(arrowButton, BorderLayout.EAST);
93 arrowButton.addActionListener(listener);
94 }
95
96 @Override
97 public void destroy() {
98 Action action = getAction();
99 if (action instanceof Destroyable) {
100 ((Destroyable) action).destroy();
101 }
102 if (action != null) {
103 if (propertyChangeListener != null) {
104 action.removePropertyChangeListener(propertyChangeListener);
105 }
106 setAction(null);
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.