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