- Timestamp:
- 2012-02-20T21:44:45+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/SideButton.java
r4354 r5007 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.BorderLayout; 6 7 import java.awt.Color; 7 import java.awt.BorderLayout;8 8 import java.awt.Image; 9 9 import java.awt.Insets; 10 10 import java.awt.event.ActionListener; 11 import java.beans.PropertyChangeEvent; 12 import java.beans.PropertyChangeListener; 11 13 12 14 import javax.swing.Action; … … 23 25 24 26 public class SideButton extends JButton { 27 private final static int iconHeight = 20; 28 25 29 public SideButton(Action action) 26 30 { 27 31 super(action); 28 fixIcon( );32 fixIcon(action); 29 33 doStyle(); 30 34 } … … 35 39 if(!usename) 36 40 setText(null); 37 fixIcon( );41 fixIcon(action); 38 42 doStyle(); 39 43 } … … 46 50 } 47 51 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 } 49 65 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())); 54 68 } 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)); 55 75 } 56 76 57 77 public static ImageIcon makeIcon(String imagename) { 58 78 Image im = ImageProvider.get("dialogs", imagename).getImage(); 59 return new ImageIcon(im.getScaledInstance(20, 20, Image.SCALE_SMOOTH));79 return getScaledImage(im); 60 80 } 61 81
Note:
See TracChangeset
for help on using the changeset viewer.