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