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

Last change on this file since 10413 was 10378, checked in by Don-vip, 8 years ago

Checkstyle 6.19: enable SingleSpaceSeparator and fix violations

  • Property svn:eol-style set to native
File size: 4.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.Main;
21import org.openstreetmap.josm.tools.Destroyable;
22import org.openstreetmap.josm.tools.ImageProvider;
23import org.openstreetmap.josm.tools.ImageResource;
24
25/**
26 * Button that is usually used in toggle dialogs.
27 * @since 744
28 */
29public class SideButton extends JButton implements Destroyable {
30
31 private transient PropertyChangeListener propertyChangeListener;
32
33 /**
34 * Constructs a new {@code SideButton}.
35 * @param action action used to specify the new button
36 * @since 744
37 */
38 public SideButton(Action action) {
39 super(action);
40 ImageResource icon = (ImageResource) action.getValue("ImageResource");
41 if (icon != null) {
42 setIcon(icon.getImageIconBounded(
43 ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
44 } else { /* TODO: remove when calling code is fixed, replace by exception */
45 Main.warn("Old style SideButton usage for action " + action);
46 fixIcon(action);
47 }
48 doStyle();
49 }
50
51 /**
52 * Constructs a new {@code SideButton}.
53 * @param action action used to specify the new button
54 * @param usename use action name
55 * @since 2710
56 */
57 public SideButton(Action action, boolean usename) {
58 super(action);
59 if (!usename) {
60 setText(null);
61 fixIcon(action);
62 doStyle();
63 }
64 }
65
66 /**
67 * Constructs a new {@code SideButton}.
68 * @param action action used to specify the new button
69 * @param imagename image name in "dialogs" directory
70 * @since 2747
71 */
72 public SideButton(Action action, String imagename) {
73 super(action);
74 ImageProvider prov = new ImageProvider("dialogs", imagename);
75 setIcon(prov.setSize(ImageProvider.ImageSizes.SIDEBUTTON).get());
76 doStyle();
77 }
78
79 /**
80 * Fix icon size
81 * @param action the action
82 * @deprecated This method is old style and will be removed together with the removal
83 * of old constructor code
84 */
85 @Deprecated
86 private void fixIcon(Action action) {
87 // need to listen for changes, so that putValue() that are called after the
88 // SideButton is constructed get the proper icon size
89 if (action != null) {
90 propertyChangeListener = new PropertyChangeListener() {
91 @Override
92 public void propertyChange(PropertyChangeEvent evt) {
93 if (javax.swing.Action.SMALL_ICON.equals(evt.getPropertyName())) {
94 fixIcon(null);
95 }
96 }
97 };
98 action.addPropertyChangeListener(propertyChangeListener);
99 }
100 int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension().height;
101 Icon i = getIcon();
102 if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
103 Image im = ((ImageIcon) i).getImage();
104 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
105 ImageIcon icon = new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
106 setIcon(icon);
107 }
108 }
109
110 /**
111 * Do the style settings for the side button layout
112 */
113 private void doStyle() {
114 setLayout(new BorderLayout());
115 setIconTextGap(2);
116 setMargin(new Insets(0, 0, 0, 0));
117 }
118
119 /**
120 * Create the arrow for opening a drop-down menu
121 * @param listener listener to use for button actions (e.g. pressing)
122 * @return the created button
123 * @since 9668
124 */
125 public BasicArrowButton createArrow(ActionListener listener) {
126 setMargin(new Insets(0, 0, 0, 0));
127 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
128 arrowButton.setBorder(BorderFactory.createEmptyBorder());
129 add(arrowButton, BorderLayout.EAST);
130 arrowButton.addActionListener(listener);
131 return arrowButton;
132 }
133
134 @Override
135 public void destroy() {
136 Action action = getAction();
137 if (action instanceof Destroyable) {
138 ((Destroyable) action).destroy();
139 }
140 if (action != null) {
141 if (propertyChangeListener != null) {
142 action.removePropertyChangeListener(propertyChangeListener);
143 }
144 setAction(null);
145 }
146 }
147}
Note: See TracBrowser for help on using the repository browser.