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

Revision 5028, 4.4 KB checked in by jttt, 3 months ago (diff)

Add possibility to hide side buttons in toggle dialogs permanently, show actions from buttons in popup menu

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Color;
8import java.awt.Image;
9import java.awt.Insets;
10import java.awt.event.ActionListener;
11import java.beans.PropertyChangeEvent;
12import java.beans.PropertyChangeListener;
13
14import javax.swing.Action;
15import javax.swing.BorderFactory;
16import javax.swing.Icon;
17import javax.swing.ImageIcon;
18import javax.swing.JButton;
19import javax.swing.SwingConstants;
20import javax.swing.plaf.basic.BasicArrowButton;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.tools.ImageProvider;
24import org.openstreetmap.josm.tools.Shortcut;
25
26public class SideButton extends JButton {
27    private final static int iconHeight = 20;
28
29    public SideButton(Action action)
30    {
31        super(action);
32        fixIcon(action);
33        doStyle();
34    }
35
36    public SideButton(Action action, boolean usename)
37    {
38        super(action);
39        if(!usename) {
40            setText(null);
41            fixIcon(action);
42            doStyle();
43        }
44    }
45
46    public SideButton(Action action, String imagename)
47    {
48        super(action);
49        setIcon(makeIcon(imagename));
50        doStyle();
51    }
52
53    void fixIcon(Action action) {
54        // need to listen for changes, so that putValue() that are called after the
55        // SideButton is constructed get the proper icon size
56        if(action != null) {
57            action.addPropertyChangeListener(new PropertyChangeListener() {
58                @Override
59                public void propertyChange(PropertyChangeEvent evt) {
60                    if(evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
61                        fixIcon(null);
62                    }
63                }
64            });
65        }
66        Icon i = getIcon();
67        if(i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
68            setIcon(getScaledImage(((ImageIcon) i).getImage()));
69        }
70    }
71
72    /** scales the given image proportionally so that the height is "iconHeight" **/
73    private static ImageIcon getScaledImage(Image im) {
74        int newWidth = im.getWidth(null) *  iconHeight / im.getHeight(null);
75        return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
76    }
77
78    public static ImageIcon makeIcon(String imagename) {
79        Image im = ImageProvider.get("dialogs", imagename).getImage();
80        return getScaledImage(im);
81    }
82
83    // Used constructor with Action
84    @Deprecated
85    public SideButton(String imagename, String property, String tooltip, ActionListener actionListener)
86    {
87        super(makeIcon(imagename));
88        doStyle();
89        setActionCommand(imagename);
90        addActionListener(actionListener);
91        setToolTipText(tooltip);
92    }
93
94    // Used constructor with Action
95    @Deprecated
96    public SideButton(String name, String imagename, String property, String tooltip, Shortcut shortcut, ActionListener actionListener)
97    {
98        super(tr(name), makeIcon(imagename));
99        if(shortcut != null)
100        {
101            shortcut.setMnemonic(this);
102            if(tooltip != null) {
103                tooltip = Main.platform.makeTooltip(tooltip, shortcut);
104            }
105        }
106        setup(name, property, tooltip, actionListener);
107    }
108
109    // Used constructor with Action
110    @Deprecated
111    public SideButton(String name, String imagename, String property, String tooltip, ActionListener actionListener)
112    {
113        super(tr(name), makeIcon(imagename));
114        setup(name, property, tooltip, actionListener);
115    }
116    private void setup(String name, String property, String tooltip, ActionListener actionListener)
117    {
118        doStyle();
119        setActionCommand(name);
120        addActionListener(actionListener);
121        setToolTipText(tooltip);
122        putClientProperty("help", "Dialog/"+property+"/"+name);
123    }
124    private void doStyle()
125    {
126        setLayout(new BorderLayout());
127        setIconTextGap(2);
128        setMargin(new Insets(-1,0,-1,0));
129    }
130
131    public void createArrow(ActionListener listener) {
132        setMargin(new Insets(0,0,0,0));
133        BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
134        arrowButton.setBorder(BorderFactory.createEmptyBorder());
135        add(arrowButton, BorderLayout.EAST);
136        arrowButton.addActionListener(listener);
137    }
138}
Note: See TracBrowser for help on using the repository browser.