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

Last change on this file since 10078 was 9705, checked in by simon04, 8 years ago

fix #12409 - Refactor ImageProvider.ImageSizes

  • Property svn:eol-style set to native
File size: 3.6 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 = ImageProvider.ImageSizes.SIDEBUTTON.getImageSize();
28
29 private transient 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 (javax.swing.Action.SMALL_ICON.equals(evt.getPropertyName())) {
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 /**
72 * Scales the given image proportionally so that the height is "iconHeight"
73 * @param im original image
74 * @return scaled image
75 */
76 private static ImageIcon getScaledImage(Image im) {
77 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
78 return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
79 }
80
81 public static ImageIcon makeIcon(String imagename) {
82 Image im = ImageProvider.get("dialogs", imagename).getImage();
83 return getScaledImage(im);
84 }
85
86 private void doStyle() {
87 setLayout(new BorderLayout());
88 setIconTextGap(2);
89 setMargin(new Insets(0, 0, 0, 0));
90 }
91
92 public BasicArrowButton createArrow(ActionListener listener) {
93 setMargin(new Insets(0, 0, 0, 0));
94 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
95 arrowButton.setBorder(BorderFactory.createEmptyBorder());
96 add(arrowButton, BorderLayout.EAST);
97 arrowButton.addActionListener(listener);
98 return arrowButton;
99 }
100
101 @Override
102 public void destroy() {
103 Action action = getAction();
104 if (action instanceof Destroyable) {
105 ((Destroyable) action).destroy();
106 }
107 if (action != null) {
108 if (propertyChangeListener != null) {
109 action.removePropertyChangeListener(propertyChangeListener);
110 }
111 setAction(null);
112 }
113 }
114}
Note: See TracBrowser for help on using the repository browser.