KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > ActionButton


1 package com.sshtools.ui.awt;
2
3 import java.awt.Image JavaDoc;
4 import java.awt.Insets JavaDoc;
5 import java.awt.SystemColor JavaDoc;
6 import java.beans.PropertyChangeEvent JavaDoc;
7 import java.beans.PropertyChangeListener JavaDoc;
8
9 /**
10  * A utility class that extends {@link ImageButton} to create a button from
11  * an {@link Action}.
12  *
13  * @author $Author: lee $
14  * @author $Revision: 1.11 $
15  */

16
17 public class ActionButton
18     extends ImageButton implements PropertyChangeListener JavaDoc {
19
20   public final static String JavaDoc LARGE_ICONS = "large"; //$NON-NLS-1$
21
public final static String JavaDoc NO_ICONS = "none"; //$NON-NLS-1$
22
public final static String JavaDoc SMALL_ICONS = "small"; //$NON-NLS-1$
23

24   public final static String JavaDoc SHOW_TEXT = "show"; //$NON-NLS-1$
25
public final static String JavaDoc NO_TEXT = "none"; //$NON-NLS-1$
26
public final static String JavaDoc SELECTIVE_TEXT = "selective"; //$NON-NLS-1$
27

28   // Private statics
29
private static final Insets JavaDoc DEFAULT_MARGIN = new Insets JavaDoc(3, 3, 3, 3);
30
31   // Private instance variables
32

33   private Action action;
34   private String JavaDoc iconDisplay;
35   private String JavaDoc textDisplay;
36
37   /**
38    * Construct a new button from an action
39    *
40    * @param action action
41    */

42   public ActionButton(Action action) {
43     this(action, SMALL_ICONS, SHOW_TEXT);
44   }
45
46   /**
47    * Construct a new button from an action
48    *
49    * @param action action
50    * @param iconDisplay icon display type
51    * @parma textDisplay text display text
52    */

53   public ActionButton(Action action, String JavaDoc iconDisplay, String JavaDoc textDisplay) {
54     super();
55     this.iconDisplay = iconDisplay;
56     this.textDisplay = textDisplay;
57     setMargin(DEFAULT_MARGIN);
58     setAction(action);
59     setHoverButton(true);
60   }
61
62   /**
63    * Get the action used to build this component.
64    *
65    * @return action
66    */

67   public Action getAction() {
68     return action;
69   }
70
71   /**
72        * Set the image, text, action listener etc from the action. Any previous action
73    * will be deregistered.
74    *
75    * @param action action
76    */

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 JavaDoc imgName = LARGE_ICONS.equals(iconDisplay) ? (String JavaDoc) action.getValue(Action.IMAGE_PATH) :
84         ( SMALL_ICONS.equals(iconDisplay) ? (String JavaDoc) action.getValue(Action.SMALL_IMAGE_PATH) : null);
85     Image JavaDoc 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 JavaDoc hide = (Boolean JavaDoc) action.getValue(Action.HIDE_TOOLBAR_TEXT);
93     setToolTipText((String JavaDoc)action.getValue(Action.LONG_DESCRIPTION));
94     setText( (String JavaDoc) action.getValue(Action.NAME));
95     // Also show text if icons were requested but this one is not available - prevents empty buttons
96
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   /* (non-Javadoc)
103    * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
104    */

105   public void propertyChange(PropertyChangeEvent JavaDoc evt) {
106     if("enabled".equals(evt.getPropertyName())) { //$NON-NLS-1$
107
setEnabled(((Boolean JavaDoc)evt.getNewValue()).booleanValue());
108     }
109     repaint();
110   }
111 }
Popular Tags