[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 |
|
---|
[5007] | 6 | import java.awt.BorderLayout;
|
---|
[4354] | 7 | import java.awt.Color;
|
---|
[2017] | 8 | import java.awt.Image;
|
---|
| 9 | import java.awt.Insets;
|
---|
[744] | 10 | import java.awt.event.ActionListener;
|
---|
[5007] | 11 | import java.beans.PropertyChangeEvent;
|
---|
| 12 | import java.beans.PropertyChangeListener;
|
---|
[2017] | 13 |
|
---|
[744] | 14 | import javax.swing.Action;
|
---|
[4354] | 15 | import javax.swing.BorderFactory;
|
---|
[2586] | 16 | import javax.swing.Icon;
|
---|
[2017] | 17 | import javax.swing.ImageIcon;
|
---|
[744] | 18 | import javax.swing.JButton;
|
---|
[4354] | 19 | import javax.swing.SwingConstants;
|
---|
| 20 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
[744] | 21 |
|
---|
[1103] | 22 | import org.openstreetmap.josm.Main;
|
---|
[744] | 23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[1102] | 24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[744] | 25 |
|
---|
| 26 | public class SideButton extends JButton {
|
---|
[5007] | 27 | private final static int iconHeight = 20;
|
---|
| 28 |
|
---|
[1169] | 29 | public SideButton(Action action)
|
---|
| 30 | {
|
---|
| 31 | super(action);
|
---|
[5007] | 32 | fixIcon(action);
|
---|
[1169] | 33 | doStyle();
|
---|
| 34 | }
|
---|
[1227] | 35 |
|
---|
[2710] | 36 | public SideButton(Action action, boolean usename)
|
---|
| 37 | {
|
---|
| 38 | super(action);
|
---|
[5028] | 39 | if(!usename) {
|
---|
[2710] | 40 | setText(null);
|
---|
[5028] | 41 | fixIcon(action);
|
---|
| 42 | doStyle();
|
---|
| 43 | }
|
---|
[2710] | 44 | }
|
---|
| 45 |
|
---|
[1247] | 46 | public SideButton(Action action, String imagename)
|
---|
| 47 | {
|
---|
[2586] | 48 | super(action);
|
---|
[1247] | 49 | setIcon(makeIcon(imagename));
|
---|
[2586] | 50 | doStyle();
|
---|
[1247] | 51 | }
|
---|
| 52 |
|
---|
[5007] | 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 | }
|
---|
[2586] | 66 | Icon i = getIcon();
|
---|
[5007] | 67 | if(i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
|
---|
| 68 | setIcon(getScaledImage(((ImageIcon) i).getImage()));
|
---|
[2586] | 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[5007] | 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 |
|
---|
[1246] | 78 | public static ImageIcon makeIcon(String imagename) {
|
---|
[1227] | 79 | Image im = ImageProvider.get("dialogs", imagename).getImage();
|
---|
[5007] | 80 | return getScaledImage(im);
|
---|
[1227] | 81 | }
|
---|
| 82 |
|
---|
[5028] | 83 | // Used constructor with Action
|
---|
| 84 | @Deprecated
|
---|
[1169] | 85 | public SideButton(String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
| 86 | {
|
---|
[1246] | 87 | super(makeIcon(imagename));
|
---|
[1169] | 88 | doStyle();
|
---|
| 89 | setActionCommand(imagename);
|
---|
| 90 | addActionListener(actionListener);
|
---|
| 91 | setToolTipText(tooltip);
|
---|
| 92 | }
|
---|
[5028] | 93 |
|
---|
| 94 | // Used constructor with Action
|
---|
| 95 | @Deprecated
|
---|
[1169] | 96 | public SideButton(String name, String imagename, String property, String tooltip, Shortcut shortcut, ActionListener actionListener)
|
---|
| 97 | {
|
---|
[1246] | 98 | super(tr(name), makeIcon(imagename));
|
---|
[1169] | 99 | if(shortcut != null)
|
---|
[1246] | 100 | {
|
---|
[1169] | 101 | shortcut.setMnemonic(this);
|
---|
[1790] | 102 | if(tooltip != null) {
|
---|
[1169] | 103 | tooltip = Main.platform.makeTooltip(tooltip, shortcut);
|
---|
[1790] | 104 | }
|
---|
[1246] | 105 | }
|
---|
[1169] | 106 | setup(name, property, tooltip, actionListener);
|
---|
| 107 | }
|
---|
[5028] | 108 |
|
---|
| 109 | // Used constructor with Action
|
---|
| 110 | @Deprecated
|
---|
[1169] | 111 | public SideButton(String name, String imagename, String property, String tooltip, ActionListener actionListener)
|
---|
| 112 | {
|
---|
[1246] | 113 | super(tr(name), makeIcon(imagename));
|
---|
[1169] | 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 | {
|
---|
[4354] | 126 | setLayout(new BorderLayout());
|
---|
[1169] | 127 | setIconTextGap(2);
|
---|
[4354] | 128 | setMargin(new Insets(-1,0,-1,0));
|
---|
[1169] | 129 | }
|
---|
[4354] | 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 | }
|
---|
[744] | 138 | }
|
---|