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

Last change on this file since 13111 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
RevLine 
[3719]1// License: GPL. For details, see LICENSE file.
[744]2package org.openstreetmap.josm.gui;
3
[5007]4import java.awt.BorderLayout;
[4354]5import java.awt.Color;
[2017]6import java.awt.Image;
7import java.awt.Insets;
[744]8import java.awt.event.ActionListener;
[5007]9import java.beans.PropertyChangeListener;
[2017]10
[744]11import javax.swing.Action;
[4354]12import javax.swing.BorderFactory;
[2586]13import javax.swing.Icon;
[2017]14import javax.swing.ImageIcon;
[744]15import javax.swing.JButton;
[4354]16import javax.swing.SwingConstants;
17import javax.swing.plaf.basic.BasicArrowButton;
[744]18
[5463]19import org.openstreetmap.josm.tools.Destroyable;
[744]20import org.openstreetmap.josm.tools.ImageProvider;
[10356]21import org.openstreetmap.josm.tools.ImageResource;
[12620]22import org.openstreetmap.josm.tools.Logging;
[744]23
[5799]24/**
[10179]25 * Button that is usually used in toggle dialogs.
26 * @since 744
[5799]27 */
[5463]28public class SideButton extends JButton implements Destroyable {
[6070]29
[8308]30 private transient PropertyChangeListener propertyChangeListener;
[5007]31
[10179]32 /**
33 * Constructs a new {@code SideButton}.
34 * @param action action used to specify the new button
[10356]35 * @since 744
[10179]36 */
[6246]37 public SideButton(Action action) {
[1169]38 super(action);
[10356]39 ImageResource icon = (ImageResource) action.getValue("ImageResource");
40 if (icon != null) {
41 setIcon(icon.getImageIconBounded(
42 ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension()));
[10489]43 } else if (getIcon() != null) { /* TODO: remove when calling code is fixed, replace by exception */
[12620]44 Logging.warn("Old style SideButton usage for action " + action);
[10356]45 fixIcon(action);
46 }
[1169]47 doStyle();
48 }
[1227]49
[10179]50 /**
51 * Constructs a new {@code SideButton}.
52 * @param action action used to specify the new button
53 * @param usename use action name
[10356]54 * @since 2710
[10179]55 */
[6246]56 public SideButton(Action action, boolean usename) {
[10428]57 this(action);
[8510]58 if (!usename) {
[2710]59 setText(null);
[5028]60 }
[2710]61 }
62
[10179]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
[10356]67 * @since 2747
[10179]68 */
[6246]69 public SideButton(Action action, String imagename) {
[2586]70 super(action);
[10428]71 setIcon(ImageProvider.get("dialogs", imagename, ImageProvider.ImageSizes.SIDEBUTTON));
[2586]72 doStyle();
[1247]73 }
74
[10362]75 /**
76 * Fix icon size
77 * @param action the action
[10365]78 * @deprecated This method is old style and will be removed together with the removal
79 * of old constructor code
[10362]80 */
[10356]81 @Deprecated
[5471]82 private void fixIcon(Action action) {
[5007]83 // need to listen for changes, so that putValue() that are called after the
84 // SideButton is constructed get the proper icon size
[5471]85 if (action != null) {
[10611]86 propertyChangeListener = evt -> {
[10755]87 if (Action.SMALL_ICON.equals(evt.getPropertyName())) {
[10611]88 fixIcon(null);
[5007]89 }
[10179]90 };
91 action.addPropertyChangeListener(propertyChangeListener);
[5007]92 }
[10358]93 int iconHeight = ImageProvider.ImageSizes.SIDEBUTTON.getImageDimension().height;
[2586]94 Icon i = getIcon();
[6246]95 if (i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
[10356]96 Image im = ((ImageIcon) i).getImage();
[10378]97 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
[10356]98 ImageIcon icon = new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
99 setIcon(icon);
[2586]100 }
101 }
102
[8958]103 /**
[10356]104 * Do the style settings for the side button layout
[8958]105 */
[6367]106 private void doStyle() {
[4354]107 setLayout(new BorderLayout());
[1169]108 setIconTextGap(2);
[8510]109 setMargin(new Insets(0, 0, 0, 0));
[1169]110 }
[4354]111
[10356]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 */
[9668]118 public BasicArrowButton createArrow(ActionListener listener) {
[8510]119 setMargin(new Insets(0, 0, 0, 0));
[4354]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);
[9668]124 return arrowButton;
[4354]125 }
[5463]126
127 @Override
128 public void destroy() {
129 Action action = getAction();
130 if (action instanceof Destroyable) {
131 ((Destroyable) action).destroy();
132 }
[5471]133 if (action != null) {
134 if (propertyChangeListener != null) {
135 action.removePropertyChangeListener(propertyChangeListener);
136 }
137 setAction(null);
138 }
[5463]139 }
[744]140}
Note: See TracBrowser for help on using the repository browser.