KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > activity > ActivityMap


1 package prefuse.activity;
2
3 import java.util.HashMap JavaDoc;
4
5 /**
6  * <p>Maps between Activity instances and user-defined keys. Can be used to
7  * maintain and schedule Activity instances through a layer of indirection.</p>
8  *
9  * <p>
10  * For example, an Activity could be stored in the map using the method
11  * call put("activity", activityRef). The Activity pointed to by activityRef
12  * could then be subsequently scheduled using the method call
13  * run("activity"). Furthermore, the Activity referred to by the
14  * key "activity" could be changed later by another call to put(), changing
15  * a visualization's behavior without modifying any other application code.
16  * </p>
17  *
18  * @author <a HREF="http://jheer.org">jeffrey heer</a>
19  */

20 public class ActivityMap {
21
22     private HashMap JavaDoc m_map;
23     private ActivityMap m_parent;
24     
25     /**
26      * Creates a new ActivityMap instance.
27      */

28     public ActivityMap() {
29         this(null);
30     }
31     
32     /**
33      * Creates a new ActivityMap instance with the specified parent map.
34      * @param parent The parent map to use. This map is referred to to resolve
35      * keys that are not found within this, the child map.
36      */

37     public ActivityMap(ActivityMap parent) {
38         m_map = new HashMap JavaDoc();
39         m_parent = parent;
40     }
41     
42     /**
43      * Clears the contents of this ActivityMap. Does not affect the parent map.
44      */

45     public void clear() {
46         m_map.clear();
47     }
48     
49     /**
50      * Returns the number of mappings in this ActivityMap. Does not include
51      * mappings stored in the parent map.
52      * @return the number of mappings in this ActivityMap
53      */

54     public int size() {
55         return m_map.size();
56     }
57     
58     /**
59      * Returns the Activity associated with the given key. If the key is not
60      * found in this map, the parent map is consulted. If no result is found,
61      * null is returned.
62      * @param key the key corresponding to a requested Activity instance
63      * @return the requested Activity instance, or null if not found by this map
64      * or the parent map.
65      */

66     public Activity get(String JavaDoc key) {
67         Activity a = (Activity)m_map.get(key);
68         return (a==null && m_parent!=null ? m_parent.get(key) : a);
69     }
70
71     /**
72      * Runs the Activity corresponding to the given key with the
73      * ActivityManager to begin at the specified time.
74      * @param key the key corresponding to the Activity to run
75      * @param time the start time at which to begin the Activity
76      * @return the scheduled Activity, or null if not found
77      */

78     public Activity runAt(String JavaDoc key, long time) {
79         Activity a = get(key);
80         if ( a != null )
81             ActivityManager.scheduleAt(a,time);
82         return a;
83     }
84     
85     /**
86      * Schedules the Activity corresponding to the given key to be run
87      * immediately by the ActivityManager.
88      * @param key the key corresponding to the Activity to run
89      * @return the scheduled Activity, or null if not found
90      */

91     public Activity run(String JavaDoc key) {
92         Activity a = get(key);
93         if ( a != null )
94             ActivityManager.scheduleNow(a);
95         return a;
96     }
97     
98     /**
99      * Schedules the Activity corresponding to the afterKey to be run
100      * immediately after the completion of the Activity corresponding to
101      * the beforeKey. This method has no scheduling effect on the Activity
102      * corresponding to the before key.
103      * @param beforeKey the key corresponding to the first Activity
104      * @param afterKey the key corresponding to the Activity to be scheduled
105      * after the completion of the first.
106      * @return the second, newly scheduled Activity, or null if either of the
107      * keys are not found
108      */

109     public Activity runAfter(String JavaDoc beforeKey, String JavaDoc afterKey) {
110         Activity before = get(beforeKey);
111         Activity after = get(afterKey);
112         if ( before != null && after != null )
113             ActivityManager.scheduleAfter(before, after);
114         return after;
115     }
116     
117     /**
118      * Schedules the Activity corresponding to the afterKey to always be run
119      * immediately after the completion of the Activity corresponding to
120      * the beforeKey. This method has no scheduling effect on the Activity
121      * corresponding to the before key.
122      * @param beforeKey the key corresponding to the first Activity
123      * @param afterKey the key corresponding to the Activity to be scheduled
124      * after the completion of the first.
125      * @return the second, newly scheduled Activity, or null if either of the
126      * keys are not found
127      */

128     public Activity alwaysRunAfter(String JavaDoc beforeKey, String JavaDoc afterKey) {
129         Activity before = get(beforeKey);
130         Activity after = get(afterKey);
131         if ( before != null && after != null )
132             ActivityManager.alwaysScheduleAfter(before, after);
133         return after;
134     }
135     
136     /**
137      * Cancels the Activity corresponding to the given key.
138      * @param key the lookup key for the Activity to cancel
139      * @return the cancelled Activity, or null if no Activity
140      * was found for the given key.
141      */

142     public Activity cancel(String JavaDoc key) {
143         Activity a = get(key);
144         if ( a != null )
145             a.cancel();
146         return a;
147     }
148     
149     /**
150      * Associates the given key with the given Activity
151      * @param key the key to associate with the Activity
152      * @param activity an Activity instance
153      * @return the Activity previously mapped to by the key, or null if none
154      */

155     public Activity put(String JavaDoc key, Activity activity) {
156         return (Activity)m_map.put(key, activity);
157     }
158     
159     /**
160      * Removes a mapping from this ActivityMap. The parent map, if any,
161      * is not effected by this method.
162      * @param key the key of the mapping to remove
163      */

164     public void remove(Object JavaDoc key) {
165         m_map.remove(key);
166     }
167     
168     /**
169      * Returns an array consisting of all the keys associated with this
170      * map. This does not include any mappings in the parent map.
171      * @return an array of all keys in this ActivityMap
172      */

173     public Object JavaDoc[] keys() {
174         return m_map.keySet().toArray();
175     }
176     
177     /**
178      * Returns all keys in this ActivityMap, and in the parent map, and the
179      * parent's parent, etc.
180      * @return an array of all keys in this ActivityMap and its parents
181      */

182     public Object JavaDoc[] allKeys() {
183         Object JavaDoc[] a1 = m_map.keySet().toArray();
184         if ( m_parent != null ) {
185             Object JavaDoc[] a2 = m_parent.allKeys();
186             if ( a2 != null && a2.length > 0 ) {
187                 Object JavaDoc[] o = new Object JavaDoc[a1.length+a2.length];
188                 System.arraycopy(a1,0,o,0,a1.length);
189                 System.arraycopy(a2,0,o,a1.length,a2.length);
190                 return o;
191             }
192         }
193         return a1;
194     }
195     
196     /**
197      * Sets this ActivityMap's parent. null values are legal, and
198      * indicate this map has no parent.
199      * @param parent the new parent for this map, or null for no parent
200      */

201     public void setParent(ActivityMap parent) {
202         m_parent = parent;
203     }
204     
205     /**
206      * Returns this ActivityMap's parent map. This method return null if
207      * this map has no parent.
208      * @return this map's parent map
209      */

210     public ActivityMap getParent() {
211         return m_parent;
212     }
213     
214 } // end of class ActivityMap
215
Popular Tags