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