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, String imagename)
|
---|
27 | {
|
---|
28 | super(action);
|
---|
29 | setIcon(makeIcon(imagename));
|
---|
30 | doStyle();
|
---|
31 | }
|
---|
32 |
|
---|
33 | void fixIcon() {
|
---|
34 | Icon i = getIcon();
|
---|
35 | if(i != null && i instanceof ImageIcon)
|
---|
36 | {
|
---|
37 | Image im = ((ImageIcon) i).getImage();
|
---|
38 | setIcon(new ImageIcon(im.getScaledInstance(20 , 20, Image.SCALE_SMOOTH)));
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static ImageIcon makeIcon(String imagename) {
|
---|
43 | Image im = ImageProvider.get("dialogs", imagename).getImage();
|
---|
44 | return new ImageIcon(im.getScaledInstance(20 , 20, Image.SCALE_SMOOTH));
|
---|
45 | }
|
---|
46 |
|
---|
47 | public SideButton(String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
48 | {
|
---|
49 | super(makeIcon(imagename));
|
---|
50 | doStyle();
|
---|
51 | setActionCommand(imagename);
|
---|
52 | addActionListener(actionListener);
|
---|
53 | setToolTipText(tooltip);
|
---|
54 | }
|
---|
55 | public SideButton(String name, String imagename, String property, String tooltip, Shortcut shortcut, ActionListener actionListener)
|
---|
56 | {
|
---|
57 | super(tr(name), makeIcon(imagename));
|
---|
58 | if(shortcut != null)
|
---|
59 | {
|
---|
60 | shortcut.setMnemonic(this);
|
---|
61 | if(tooltip != null) {
|
---|
62 | tooltip = Main.platform.makeTooltip(tooltip, shortcut);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | setup(name, property, tooltip, actionListener);
|
---|
66 | }
|
---|
67 | public SideButton(String name, String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
68 | {
|
---|
69 | super(tr(name), makeIcon(imagename));
|
---|
70 | setup(name, property, tooltip, actionListener);
|
---|
71 | }
|
---|
72 | private void setup(String name, String property, String tooltip, ActionListener actionListener)
|
---|
73 | {
|
---|
74 | doStyle();
|
---|
75 | setActionCommand(name);
|
---|
76 | addActionListener(actionListener);
|
---|
77 | setToolTipText(tooltip);
|
---|
78 | putClientProperty("help", "Dialog/"+property+"/"+name);
|
---|
79 | }
|
---|
80 | private void doStyle()
|
---|
81 | {
|
---|
82 | setMargin(new Insets(1,1,1,1));
|
---|
83 | setIconTextGap(2);
|
---|
84 | }
|
---|
85 | }
|
---|