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

Last change on this file since 13117 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

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