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

Last change on this file since 6041 was 5926, checked in by bastiK, 11 years ago

clean up imports

  • Property svn:eol-style set to native
File size: 3.8 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.PropertyChangeEvent;
10import java.beans.PropertyChangeListener;
[2017]11
[744]12import javax.swing.Action;
[4354]13import javax.swing.BorderFactory;
[2586]14import javax.swing.Icon;
[2017]15import javax.swing.ImageIcon;
[744]16import javax.swing.JButton;
[4354]17import javax.swing.SwingConstants;
18import javax.swing.plaf.basic.BasicArrowButton;
[744]19
[5463]20import org.openstreetmap.josm.tools.Destroyable;
[744]21import org.openstreetmap.josm.tools.ImageProvider;
22
[5799]23/**
24 * Button that is usually used in toggle dialogs
25 */
[5463]26public class SideButton extends JButton implements Destroyable {
[5007]27 private final static int iconHeight = 20;
[5471]28
29 private PropertyChangeListener propertyChangeListener;
[5007]30
[1169]31 public SideButton(Action action)
32 {
33 super(action);
[5007]34 fixIcon(action);
[1169]35 doStyle();
36 }
[1227]37
[2710]38 public SideButton(Action action, boolean usename)
39 {
40 super(action);
[5028]41 if(!usename) {
[2710]42 setText(null);
[5028]43 fixIcon(action);
44 doStyle();
45 }
[2710]46 }
47
[1247]48 public SideButton(Action action, String imagename)
49 {
[2586]50 super(action);
[1247]51 setIcon(makeIcon(imagename));
[2586]52 doStyle();
[1247]53 }
54
[5471]55 private void fixIcon(Action action) {
[5007]56 // need to listen for changes, so that putValue() that are called after the
57 // SideButton is constructed get the proper icon size
[5471]58 if (action != null) {
59 action.addPropertyChangeListener(propertyChangeListener = new PropertyChangeListener() {
[5007]60 @Override
61 public void propertyChange(PropertyChangeEvent evt) {
[5471]62 if (evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
[5007]63 fixIcon(null);
64 }
65 }
66 });
67 }
[2586]68 Icon i = getIcon();
[5471]69 if (i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
[5007]70 setIcon(getScaledImage(((ImageIcon) i).getImage()));
[2586]71 }
72 }
73
[5007]74 /** scales the given image proportionally so that the height is "iconHeight" **/
75 private static ImageIcon getScaledImage(Image im) {
76 int newWidth = im.getWidth(null) * iconHeight / im.getHeight(null);
77 return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
78 }
79
[1246]80 public static ImageIcon makeIcon(String imagename) {
[1227]81 Image im = ImageProvider.get("dialogs", imagename).getImage();
[5007]82 return getScaledImage(im);
[1227]83 }
84
[1169]85 private void setup(String name, String property, String tooltip, ActionListener actionListener)
86 {
87 doStyle();
88 setActionCommand(name);
89 addActionListener(actionListener);
90 setToolTipText(tooltip);
91 putClientProperty("help", "Dialog/"+property+"/"+name);
92 }
[5762]93
[1169]94 private void doStyle()
95 {
[4354]96 setLayout(new BorderLayout());
[1169]97 setIconTextGap(2);
[4354]98 setMargin(new Insets(-1,0,-1,0));
[1169]99 }
[4354]100
101 public void createArrow(ActionListener listener) {
102 setMargin(new Insets(0,0,0,0));
103 BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
104 arrowButton.setBorder(BorderFactory.createEmptyBorder());
105 add(arrowButton, BorderLayout.EAST);
106 arrowButton.addActionListener(listener);
107 }
[5463]108
109 @Override
110 public void destroy() {
111 Action action = getAction();
112 if (action instanceof Destroyable) {
113 ((Destroyable) action).destroy();
114 }
[5471]115 if (action != null) {
116 if (propertyChangeListener != null) {
117 action.removePropertyChangeListener(propertyChangeListener);
118 }
119 setAction(null);
120 }
[5463]121 }
[744]122}
Note: See TracBrowser for help on using the repository browser.