Changeset 5007 in josm for trunk


Ignore:
Timestamp:
2012-02-20T21:44:45+01:00 (8 years ago)
Author:
xeen
Message:

fix #3409

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/SideButton.java

    r4354 r5007  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.BorderLayout;
    67import java.awt.Color;
    7 import java.awt.BorderLayout;
    88import java.awt.Image;
    99import java.awt.Insets;
    1010import java.awt.event.ActionListener;
     11import java.beans.PropertyChangeEvent;
     12import java.beans.PropertyChangeListener;
    1113
    1214import javax.swing.Action;
     
    2325
    2426public class SideButton extends JButton {
     27    private final static int iconHeight = 20;
     28
    2529    public SideButton(Action action)
    2630    {
    2731        super(action);
    28         fixIcon();
     32        fixIcon(action);
    2933        doStyle();
    3034    }
     
    3539        if(!usename)
    3640            setText(null);
    37         fixIcon();
     41        fixIcon(action);
    3842        doStyle();
    3943    }
     
    4650    }
    4751
    48     void fixIcon() {
     52    void fixIcon(Action action) {
     53        // need to listen for changes, so that putValue() that are called after the
     54        // SideButton is constructed get the proper icon size
     55        if(action != null) {
     56            action.addPropertyChangeListener(new PropertyChangeListener() {
     57                @Override
     58                public void propertyChange(PropertyChangeEvent evt) {
     59                    if(evt.getPropertyName() == javax.swing.Action.SMALL_ICON) {
     60                        fixIcon(null);
     61                    }
     62                }
     63            });
     64        }
    4965        Icon i = getIcon();
    50         if(i != null && i instanceof ImageIcon)
    51         {
    52             Image im = ((ImageIcon) i).getImage();
    53             setIcon(new ImageIcon(im.getScaledInstance(20, 20, Image.SCALE_SMOOTH)));
     66        if(i != null && i instanceof ImageIcon && i.getIconHeight() != iconHeight) {
     67            setIcon(getScaledImage(((ImageIcon) i).getImage()));
    5468        }
     69    }
     70
     71    /** scales the given image proportionally so that the height is "iconHeight" **/
     72    private static ImageIcon getScaledImage(Image im) {
     73        int newWidth = im.getWidth(null) *  iconHeight / im.getHeight(null);
     74        return new ImageIcon(im.getScaledInstance(newWidth, iconHeight, Image.SCALE_SMOOTH));
    5575    }
    5676
    5777    public static ImageIcon makeIcon(String imagename) {
    5878        Image im = ImageProvider.get("dialogs", imagename).getImage();
    59         return new ImageIcon(im.getScaledInstance(20, 20, Image.SCALE_SMOOTH));
     79        return getScaledImage(im);
    6080    }
    6181
Note: See TracChangeset for help on using the changeset viewer.