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

Last change on this file since 10428 was 10428, checked in by stoecker, 8 years ago

see #9995 - patch mainly by strump - improve HIDPI behaviour

  • Property svn:eol-style set to native
File size: 4.7 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 this(action);
59 if (!usename) {
60 setText(null);
61 }
62 }
63
64 /**
65 * Constructs a new {@code SideButton}.
66 * @param action action used to specify the new button
67 * @param imagename image name in "dialogs" directory
68 * @since 2747
69 */
70 public SideButton(Action action, String imagename) {
71 super(action);
72 setIcon(ImageProvider.get("dialogs", imagename, ImageProvider.ImageSizes.SIDEBUTTON));
73 doStyle();
74 }
75
76 /**
77 * Fix icon size
78 * @param action the action
79 * @deprecated This method is old style and will be removed together with the removal
80 * of old constructor code
81 */
82 @Deprecated
83 private void fixIcon(Action action) {
84 // need to listen for changes, so that putValue() that are called after the
85 // SideButton is constructed get the proper icon size
86 if (action != null) {
87 propertyChangeListener = new PropertyChangeListener() {
88 @Override
89 public void propertyChange(PropertyChangeEvent evt) {
90 if (javax.swing.Action.SMALL_ICON.equals(evt.getPropertyName())) {
91 fixIcon(null);
92 }
93 }
94 };
95 action.addPropertyChangeListener(propertyChangeListener);
96 }
97 int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension().height;
98 Icon i = getIcon();
99 if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
100 Image im = ((ImageIcon) i).getImage();
101 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
102 ImageIcon icon = new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
103 setIcon(icon);
104 }
105 }
106
107 /**
108 * Do the style settings for the side button layout
109 */
110 private void doStyle() {
111 setLayout(new BorderLayout());
112 setIconTextGap(2);
113 setMargin(new Insets(0, 0, 0, 0));
114 }
115
116 /**
117 * Create the arrow for opening a drop-down menu
118 * @param listener listener to use for button actions (e.g. pressing)
119 * @return the created button
120 * @since 9668
121 */
122 public BasicArrowButton createArrow(ActionListener listener) {
123 setMargin(new Insets(0, 0, 0, 0));
124 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
125 arrowButton.setBorder(BorderFactory.createEmptyBorder());
126 add(arrowButton, BorderLayout.EAST);
127 arrowButton.addActionListener(listener);
128 return arrowButton;
129 }
130
131 @Override
132 public void destroy() {
133 Action action = getAction();
134 if (action instanceof Destroyable) {
135 ((Destroyable) action).destroy();
136 }
137 if (action != null) {
138 if (propertyChangeListener != null) {
139 action.removePropertyChangeListener(propertyChangeListener);
140 }
141 setAction(null);
142 }
143 }
144}
Note: See TracBrowser for help on using the repository browser.