KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > Activity


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.activities;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.ui.activities.ActivityEvent;
21 import org.eclipse.ui.activities.IActivity;
22 import org.eclipse.ui.activities.IActivityListener;
23 import org.eclipse.ui.activities.IActivityPatternBinding;
24 import org.eclipse.ui.activities.IActivityRequirementBinding;
25 import org.eclipse.ui.activities.NotDefinedException;
26 import org.eclipse.ui.internal.util.Util;
27
28 final class Activity implements IActivity {
29     private final static int HASH_FACTOR = 89;
30
31     private final static int HASH_INITIAL = Activity.class.getName().hashCode();
32
33     private final static Set JavaDoc strongReferences = new HashSet JavaDoc();
34
35     private Set JavaDoc activityRequirementBindings;
36
37     private transient IActivityRequirementBinding[] activityRequirementBindingsAsArray;
38
39     private List JavaDoc activityListeners;
40
41     private Set JavaDoc activityPatternBindings;
42
43     private transient IActivityPatternBinding[] activityPatternBindingsAsArray;
44
45     private boolean defined;
46
47     private boolean enabled;
48
49     private transient int hashCode = HASH_INITIAL;
50
51     private String JavaDoc id;
52
53     private String JavaDoc name;
54
55     private transient String JavaDoc string;
56
57     private String JavaDoc description;
58     
59     private boolean defaultEnabled;
60
61     Activity(String JavaDoc id) {
62         if (id == null) {
63             throw new NullPointerException JavaDoc();
64         }
65
66         this.id = id;
67     }
68
69     public void addActivityListener(IActivityListener activityListener) {
70         if (activityListener == null) {
71             throw new NullPointerException JavaDoc();
72         }
73
74         if (activityListeners == null) {
75             activityListeners = new ArrayList JavaDoc();
76         }
77
78         if (!activityListeners.contains(activityListener)) {
79             activityListeners.add(activityListener);
80         }
81
82         strongReferences.add(this);
83     }
84
85     public int compareTo(Object JavaDoc object) {
86         Activity castedObject = (Activity) object;
87
88         int compareTo = Util.compare(
89                 activityRequirementBindingsAsArray,
90                 castedObject.activityRequirementBindingsAsArray);
91
92         if (compareTo == 0) {
93             compareTo = Util.compare(
94                     activityPatternBindingsAsArray,
95                     castedObject.activityPatternBindingsAsArray);
96
97             if (compareTo == 0) {
98                 compareTo = Util.compare(defined, castedObject.defined);
99
100                 if (compareTo == 0) {
101                     compareTo = Util.compare(enabled, castedObject.enabled);
102
103                     if (compareTo == 0) {
104                         compareTo = Util.compare(id, castedObject.id);
105
106                         if (compareTo == 0) {
107                             compareTo = Util.compare(name, castedObject.name);
108                         }
109                     }
110                 }
111             }
112         }
113
114         return compareTo;
115     }
116
117     public boolean equals(Object JavaDoc object) {
118         if (!(object instanceof Activity)) {
119             return false;
120         }
121
122         final Activity castedObject = (Activity) object;
123
124         if (!Util.equals(activityRequirementBindings,
125                 castedObject.activityRequirementBindings)) {
126             return false;
127         }
128
129         if (!Util.equals(activityPatternBindings,
130                 castedObject.activityPatternBindings)) {
131             return false;
132         }
133
134         if (!Util.equals(defined, castedObject.defined)) {
135             return false;
136         }
137
138         if (!Util.equals(enabled, castedObject.enabled)) {
139             return false;
140         }
141
142         if (!Util.equals(id, castedObject.id)) {
143             return false;
144         }
145
146         return Util.equals(name, castedObject.name);
147     }
148
149     void fireActivityChanged(ActivityEvent activityEvent) {
150         if (activityEvent == null) {
151             throw new NullPointerException JavaDoc();
152         }
153
154         if (activityListeners != null) {
155             for (int i = 0; i < activityListeners.size(); i++) {
156                 ((IActivityListener) activityListeners.get(i))
157                         .activityChanged(activityEvent);
158             }
159         }
160     }
161
162     public Set JavaDoc getActivityRequirementBindings() {
163         return activityRequirementBindings;
164     }
165
166     public Set JavaDoc getActivityPatternBindings() {
167         return activityPatternBindings;
168     }
169
170     public String JavaDoc getId() {
171         return id;
172     }
173
174     public String JavaDoc getName() throws NotDefinedException {
175         if (!defined) {
176             throw new NotDefinedException();
177         }
178
179         return name;
180     }
181
182     public int hashCode() {
183         if (hashCode == HASH_INITIAL) {
184             hashCode = hashCode * HASH_FACTOR
185                     + Util.hashCode(activityRequirementBindings);
186             hashCode = hashCode * HASH_FACTOR
187                     + Util.hashCode(activityPatternBindings);
188             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
189             hashCode = hashCode * HASH_FACTOR + Util.hashCode(enabled);
190             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
191             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
192             if (hashCode == HASH_INITIAL) {
193                 hashCode++;
194             }
195         }
196
197         return hashCode;
198     }
199
200     public boolean isDefined() {
201         return defined;
202     }
203
204     public boolean isEnabled() {
205         return enabled;
206     }
207
208     public boolean isMatch(String JavaDoc string) {
209         if (isDefined()) {
210             for (Iterator JavaDoc iterator = activityPatternBindings.iterator(); iterator
211                     .hasNext();) {
212                 ActivityPatternBinding activityPatternBinding = (ActivityPatternBinding) iterator
213                         .next();
214
215                 if (activityPatternBinding.isMatch(string)) {
216                     return true;
217                 }
218             }
219         }
220
221         return false;
222     }
223
224     public void removeActivityListener(IActivityListener activityListener) {
225         if (activityListener == null) {
226             throw new NullPointerException JavaDoc();
227         }
228
229         if (activityListeners != null) {
230             activityListeners.remove(activityListener);
231         }
232
233         if (activityListeners.isEmpty()) {
234             strongReferences.remove(this);
235         }
236     }
237
238     boolean setActivityRequirementBindings(Set JavaDoc activityRequirementBindings) {
239         activityRequirementBindings = Util.safeCopy(
240                 activityRequirementBindings, IActivityRequirementBinding.class);
241
242         if (!Util.equals(activityRequirementBindings,
243                 this.activityRequirementBindings)) {
244             this.activityRequirementBindings = activityRequirementBindings;
245             this.activityRequirementBindingsAsArray = (IActivityRequirementBinding[]) this.activityRequirementBindings
246                     .toArray(new IActivityRequirementBinding[this.activityRequirementBindings
247                             .size()]);
248             hashCode = HASH_INITIAL;
249             string = null;
250             return true;
251         }
252
253         return false;
254     }
255
256     boolean setActivityPatternBindings(Set JavaDoc activityPatternBindings) {
257         activityPatternBindings = Util.safeCopy(activityPatternBindings,
258                 IActivityPatternBinding.class);
259
260         if (!Util.equals(activityPatternBindings, this.activityPatternBindings)) {
261             this.activityPatternBindings = activityPatternBindings;
262             this.activityPatternBindingsAsArray = (IActivityPatternBinding[]) this.activityPatternBindings
263                     .toArray(new IActivityPatternBinding[this.activityPatternBindings
264                             .size()]);
265             hashCode = HASH_INITIAL;
266             string = null;
267             return true;
268         }
269
270         return false;
271     }
272
273     boolean setDefined(boolean defined) {
274         if (defined != this.defined) {
275             this.defined = defined;
276             hashCode = HASH_INITIAL;
277             string = null;
278             return true;
279         }
280
281         return false;
282     }
283
284     boolean setEnabled(boolean enabled) {
285         if (enabled != this.enabled) {
286             this.enabled = enabled;
287             hashCode = HASH_INITIAL;
288             string = null;
289             return true;
290         }
291
292         return false;
293     }
294
295     boolean setName(String JavaDoc name) {
296         if (!Util.equals(name, this.name)) {
297             this.name = name;
298             hashCode = HASH_INITIAL;
299             string = null;
300             return true;
301         }
302
303         return false;
304     }
305
306     boolean setDescription(String JavaDoc description) {
307         if (!Util.equals(description, this.description)) {
308             this.description = description;
309             hashCode = HASH_INITIAL;
310             string = null;
311             return true;
312         }
313
314         return false;
315     }
316
317     public String JavaDoc toString() {
318         if (string == null) {
319             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
320             stringBuffer.append('[');
321             stringBuffer.append(activityRequirementBindings);
322             stringBuffer.append(',');
323             stringBuffer.append(activityPatternBindings);
324             stringBuffer.append(',');
325             stringBuffer.append(defined);
326             stringBuffer.append(',');
327             stringBuffer.append(enabled);
328             stringBuffer.append(',');
329             stringBuffer.append(id);
330             stringBuffer.append(',');
331             stringBuffer.append(name);
332             stringBuffer.append(']');
333             string = stringBuffer.toString();
334         }
335
336         return string;
337     }
338
339     /* (non-Javadoc)
340      * @see org.eclipse.ui.activities.IActivity#getDescription()
341      */

342     public String JavaDoc getDescription() throws NotDefinedException {
343         if (!defined) {
344             throw new NotDefinedException();
345         }
346
347         return description;
348     }
349     
350     /* (non-Javadoc)
351      * @see org.eclipse.ui.activities.IActivity#isDefaultEnabled()
352      */

353     public boolean isDefaultEnabled() {
354         return defaultEnabled;
355     }
356     
357     boolean setDefaultEnabled(boolean defaultEnabled) {
358         if (!Util.equals(defaultEnabled, this.defaultEnabled)) {
359             this.defaultEnabled = defaultEnabled;
360             hashCode = HASH_INITIAL;
361             string = null;
362             return true;
363         }
364
365         return false;
366     }
367 }
368
Popular Tags