1 package com.sshtools.ui.awt; 2 3 import java.awt.Image ; 4 import java.awt.Insets ; 5 import java.awt.SystemColor ; 6 import java.beans.PropertyChangeEvent ; 7 import java.beans.PropertyChangeListener ; 8 9 16 17 public class ActionButton 18 extends ImageButton implements PropertyChangeListener { 19 20 public final static String LARGE_ICONS = "large"; public final static String NO_ICONS = "none"; public final static String SMALL_ICONS = "small"; 24 public final static String SHOW_TEXT = "show"; public final static String NO_TEXT = "none"; public final static String SELECTIVE_TEXT = "selective"; 28 private static final Insets DEFAULT_MARGIN = new Insets (3, 3, 3, 3); 30 31 33 private Action action; 34 private String iconDisplay; 35 private String textDisplay; 36 37 42 public ActionButton(Action action) { 43 this(action, SMALL_ICONS, SHOW_TEXT); 44 } 45 46 53 public ActionButton(Action action, String iconDisplay, String textDisplay) { 54 super(); 55 this.iconDisplay = iconDisplay; 56 this.textDisplay = textDisplay; 57 setMargin(DEFAULT_MARGIN); 58 setAction(action); 59 setHoverButton(true); 60 } 61 62 67 public Action getAction() { 68 return action; 69 } 70 71 77 public void setAction(final Action action) { 78 if (this.action != null) { 79 removeActionListener(this.action); 80 action.removePropertyChangeListener(this); 81 } 82 this.action = action; 83 String imgName = LARGE_ICONS.equals(iconDisplay) ? (String ) action.getValue(Action.IMAGE_PATH) : 84 ( SMALL_ICONS.equals(iconDisplay) ? (String ) action.getValue(Action.SMALL_IMAGE_PATH) : null); 85 Image img = null; 86 if (imgName != null) { 87 img = UIUtil.loadImage(action.getClass(), imgName); 88 if (img != null) { 89 setImage(UIUtil.waitFor(img, this)); 90 } 91 } 92 Boolean hide = (Boolean ) action.getValue(Action.HIDE_TOOLBAR_TEXT); 93 setToolTipText((String )action.getValue(Action.LONG_DESCRIPTION)); 94 setText( (String ) action.getValue(Action.NAME)); 95 setTextVisible( img == null || SHOW_TEXT.equals(textDisplay) || ( SELECTIVE_TEXT.equals(textDisplay) && ( hide == null || !hide.booleanValue() ) )); 97 addActionListener(action); 98 setEnabled(action.isEnabled()); 99 action.addPropertyChangeListener(this); 100 } 101 102 105 public void propertyChange(PropertyChangeEvent evt) { 106 if("enabled".equals(evt.getPropertyName())) { setEnabled(((Boolean )evt.getNewValue()).booleanValue()); 108 } 109 repaint(); 110 } 111 } | Popular Tags |