1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Image;
|
---|
7 | import java.awt.Insets;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 |
|
---|
10 | import javax.swing.Action;
|
---|
11 | import javax.swing.Icon;
|
---|
12 | import javax.swing.ImageIcon;
|
---|
13 | import javax.swing.JButton;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | public class SideButton extends JButton {
|
---|
20 | public SideButton(Action action)
|
---|
21 | {
|
---|
22 | super(action);
|
---|
23 | fixIcon();
|
---|
24 | doStyle();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public SideButton(Action action, boolean usename)
|
---|
28 | {
|
---|
29 | super(action);
|
---|
30 | if(!usename)
|
---|
31 | setText(null);
|
---|
32 | fixIcon();
|
---|
33 | doStyle();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public SideButton(Action action, String imagename)
|
---|
37 | {
|
---|
38 | super(action);
|
---|
39 | setIcon(makeIcon(imagename));
|
---|
40 | doStyle();
|
---|
41 | }
|
---|
42 |
|
---|
43 | void fixIcon() {
|
---|
44 | Icon i = getIcon();
|
---|
45 | if(i != null && i instanceof ImageIcon)
|
---|
46 | {
|
---|
47 | Image im = ((ImageIcon) i).getImage();
|
---|
48 | setIcon(new ImageIcon(im.getScaledInstance(20 , 20, Image.SCALE_SMOOTH)));
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static ImageIcon makeIcon(String imagename) {
|
---|
53 | Image im = ImageProvider.get("dialogs", imagename).getImage();
|
---|
54 | return new ImageIcon(im.getScaledInstance(20 , 20, Image.SCALE_SMOOTH));
|
---|
55 | }
|
---|
56 |
|
---|
57 | public SideButton(String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
58 | {
|
---|
59 | super(makeIcon(imagename));
|
---|
60 | doStyle();
|
---|
61 | setActionCommand(imagename);
|
---|
62 | addActionListener(actionListener);
|
---|
63 | setToolTipText(tooltip);
|
---|
64 | }
|
---|
65 | public SideButton(String name, String imagename, String property, String tooltip, Shortcut shortcut, ActionListener actionListener)
|
---|
66 | {
|
---|
67 | super(tr(name), makeIcon(imagename));
|
---|
68 | if(shortcut != null)
|
---|
69 | {
|
---|
70 | shortcut.setMnemonic(this);
|
---|
71 | if(tooltip != null) {
|
---|
72 | tooltip = Main.platform.makeTooltip(tooltip, shortcut);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | setup(name, property, tooltip, actionListener);
|
---|
76 | }
|
---|
77 | public SideButton(String name, String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
78 | {
|
---|
79 | super(tr(name), makeIcon(imagename));
|
---|
80 | setup(name, property, tooltip, actionListener);
|
---|
81 | }
|
---|
82 | private void setup(String name, String property, String tooltip, ActionListener actionListener)
|
---|
83 | {
|
---|
84 | doStyle();
|
---|
85 | setActionCommand(name);
|
---|
86 | addActionListener(actionListener);
|
---|
87 | setToolTipText(tooltip);
|
---|
88 | putClientProperty("help", "Dialog/"+property+"/"+name);
|
---|
89 | }
|
---|
90 | private void doStyle()
|
---|
91 | {
|
---|
92 | setMargin(new Insets(1,1,1,1));
|
---|
93 | setIconTextGap(2);
|
---|
94 | }
|
---|
95 | }
|
---|