KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sshtools.ui.awt;
2
3 import java.beans.PropertyChangeEvent JavaDoc;
4 import java.beans.PropertyChangeListener JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 /**
9  * Abstract implementation of an {@link Action}.
10  */

11 public abstract class AbstractAction
12     implements Action {
13   
14   // Private instance variables
15
private Hashtable JavaDoc attributes = new Hashtable JavaDoc();
16   private boolean enabled;
17   private Vector JavaDoc listeners;
18
19   /**
20    * Construct a new action with a name.
21    *
22    * @param name
23    */

24   public AbstractAction(String JavaDoc name) {
25     super();
26     listeners = new Vector JavaDoc();
27     putValue(NAME, name);
28     enabled = true;
29   }
30
31   /* (non-Javadoc)
32    * @see com.sshtools.ui.awt.Action#isEnabled()
33    */

34   public boolean isEnabled() {
35     return enabled;
36   }
37   /* (non-Javadoc)
38    * @see com.sshtools.ui.awt.Action#setEnabled(boolean)
39    */

40   public void setEnabled(boolean enabled) {
41     if(this.enabled != enabled) {
42       boolean oldVal = this.enabled;
43       this.enabled = enabled;
44       firePropertyChanged(this, "enabled", oldVal ? Boolean.TRUE : Boolean.FALSE, enabled ? Boolean.TRUE : Boolean.FALSE); //$NON-NLS-1$
45
}
46   }
47
48   /* (non-Javadoc)
49    * @see com.sshtools.ui.awt.Action#getName()
50    */

51   public String JavaDoc getName() {
52     return (String JavaDoc) getValue(NAME);
53   }
54
55   /* (non-Javadoc)
56        * @see com.sshtools.ui.awt.Action#putValue(java.lang.String, java.lang.Object)
57    */

58   public void putValue(String JavaDoc key, Object JavaDoc value) {
59     Object JavaDoc oldVal = attributes.put(key, value);
60     firePropertyChanged(this, key, oldVal, value);
61   }
62
63
64   /**
65    * Get the value for an attribute. <code>null</code> will be returned
66    * if no such attribute can be found.
67    *
68    * @param key attribute key
69    * @return attribute value
70    */

71   public Object JavaDoc getValue(String JavaDoc key) {
72     return attributes.get(key);
73   }
74
75   /* (non-Javadoc)
76    * @see com.sshtools.ui.awt.Action#addPropertyChangeListener(java.beans.PropertyChangeListener)
77    */

78   public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
79     listeners.addElement(l);
80   }
81   
82   /* (non-Javadoc)
83    * @see com.sshtools.ui.awt.Action#removePropertyChangeListener(java.beans.PropertyChangeListener)
84    */

85   public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
86     listeners.removeElement(l);
87   }
88   
89   /**
90    * @param event
91    */

92   private void firePropertyChanged(Object JavaDoc source, String JavaDoc key, Object JavaDoc oldVal, Object JavaDoc newVal) {
93     PropertyChangeEvent JavaDoc evt = null;
94     synchronized(listeners) {
95       for(int i = listeners.size() - 1; i >= 0 ; i-- ) {
96         if(evt == null) {
97           evt = new PropertyChangeEvent JavaDoc(source, key, oldVal, newVal);
98         }
99         ((PropertyChangeListener JavaDoc)listeners.elementAt(i)).propertyChange(evt);
100       }
101     }
102   }
103 }
104
Popular Tags