001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.activities;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Set;
017:
018: import org.eclipse.ui.activities.ActivityEvent;
019: import org.eclipse.ui.activities.IActivity;
020: import org.eclipse.ui.activities.IActivityListener;
021: import org.eclipse.ui.activities.IActivityPatternBinding;
022: import org.eclipse.ui.activities.IActivityRequirementBinding;
023: import org.eclipse.ui.activities.NotDefinedException;
024: import org.eclipse.ui.internal.util.Util;
025:
026: final class Activity implements IActivity {
027: private final static int HASH_FACTOR = 89;
028:
029: private final static int HASH_INITIAL = Activity.class.getName()
030: .hashCode();
031:
032: private final static Set strongReferences = new HashSet();
033:
034: private Set activityRequirementBindings;
035:
036: private transient IActivityRequirementBinding[] activityRequirementBindingsAsArray;
037:
038: private List activityListeners;
039:
040: private Set activityPatternBindings;
041:
042: private transient IActivityPatternBinding[] activityPatternBindingsAsArray;
043:
044: private boolean defined;
045:
046: private boolean enabled;
047:
048: private transient int hashCode = HASH_INITIAL;
049:
050: private String id;
051:
052: private String name;
053:
054: private transient String string;
055:
056: private String description;
057:
058: private boolean defaultEnabled;
059:
060: Activity(String id) {
061: if (id == null) {
062: throw new NullPointerException();
063: }
064:
065: this .id = id;
066: }
067:
068: public void addActivityListener(IActivityListener activityListener) {
069: if (activityListener == null) {
070: throw new NullPointerException();
071: }
072:
073: if (activityListeners == null) {
074: activityListeners = new ArrayList();
075: }
076:
077: if (!activityListeners.contains(activityListener)) {
078: activityListeners.add(activityListener);
079: }
080:
081: strongReferences.add(this );
082: }
083:
084: public int compareTo(Object object) {
085: Activity castedObject = (Activity) object;
086:
087: int compareTo = Util.compare(
088: activityRequirementBindingsAsArray,
089: castedObject.activityRequirementBindingsAsArray);
090:
091: if (compareTo == 0) {
092: compareTo = Util.compare(activityPatternBindingsAsArray,
093: castedObject.activityPatternBindingsAsArray);
094:
095: if (compareTo == 0) {
096: compareTo = Util.compare(defined, castedObject.defined);
097:
098: if (compareTo == 0) {
099: compareTo = Util.compare(enabled,
100: castedObject.enabled);
101:
102: if (compareTo == 0) {
103: compareTo = Util.compare(id, castedObject.id);
104:
105: if (compareTo == 0) {
106: compareTo = Util.compare(name,
107: castedObject.name);
108: }
109: }
110: }
111: }
112: }
113:
114: return compareTo;
115: }
116:
117: public boolean equals(Object 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();
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 getActivityRequirementBindings() {
163: return activityRequirementBindings;
164: }
165:
166: public Set getActivityPatternBindings() {
167: return activityPatternBindings;
168: }
169:
170: public String getId() {
171: return id;
172: }
173:
174: public String 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 string) {
209: if (isDefined()) {
210: for (Iterator 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(
225: IActivityListener activityListener) {
226: if (activityListener == null) {
227: throw new NullPointerException();
228: }
229:
230: if (activityListeners != null) {
231: activityListeners.remove(activityListener);
232: }
233:
234: if (activityListeners.isEmpty()) {
235: strongReferences.remove(this );
236: }
237: }
238:
239: boolean setActivityRequirementBindings(
240: Set activityRequirementBindings) {
241: activityRequirementBindings = Util.safeCopy(
242: activityRequirementBindings,
243: IActivityRequirementBinding.class);
244:
245: if (!Util.equals(activityRequirementBindings,
246: this .activityRequirementBindings)) {
247: this .activityRequirementBindings = activityRequirementBindings;
248: this .activityRequirementBindingsAsArray = (IActivityRequirementBinding[]) this .activityRequirementBindings
249: .toArray(new IActivityRequirementBinding[this .activityRequirementBindings
250: .size()]);
251: hashCode = HASH_INITIAL;
252: string = null;
253: return true;
254: }
255:
256: return false;
257: }
258:
259: boolean setActivityPatternBindings(Set activityPatternBindings) {
260: activityPatternBindings = Util.safeCopy(
261: activityPatternBindings, IActivityPatternBinding.class);
262:
263: if (!Util.equals(activityPatternBindings,
264: this .activityPatternBindings)) {
265: this .activityPatternBindings = activityPatternBindings;
266: this .activityPatternBindingsAsArray = (IActivityPatternBinding[]) this .activityPatternBindings
267: .toArray(new IActivityPatternBinding[this .activityPatternBindings
268: .size()]);
269: hashCode = HASH_INITIAL;
270: string = null;
271: return true;
272: }
273:
274: return false;
275: }
276:
277: boolean setDefined(boolean defined) {
278: if (defined != this .defined) {
279: this .defined = defined;
280: hashCode = HASH_INITIAL;
281: string = null;
282: return true;
283: }
284:
285: return false;
286: }
287:
288: boolean setEnabled(boolean enabled) {
289: if (enabled != this .enabled) {
290: this .enabled = enabled;
291: hashCode = HASH_INITIAL;
292: string = null;
293: return true;
294: }
295:
296: return false;
297: }
298:
299: boolean setName(String name) {
300: if (!Util.equals(name, this .name)) {
301: this .name = name;
302: hashCode = HASH_INITIAL;
303: string = null;
304: return true;
305: }
306:
307: return false;
308: }
309:
310: boolean setDescription(String description) {
311: if (!Util.equals(description, this .description)) {
312: this .description = description;
313: hashCode = HASH_INITIAL;
314: string = null;
315: return true;
316: }
317:
318: return false;
319: }
320:
321: public String toString() {
322: if (string == null) {
323: final StringBuffer stringBuffer = new StringBuffer();
324: stringBuffer.append('[');
325: stringBuffer.append(activityRequirementBindings);
326: stringBuffer.append(',');
327: stringBuffer.append(activityPatternBindings);
328: stringBuffer.append(',');
329: stringBuffer.append(defined);
330: stringBuffer.append(',');
331: stringBuffer.append(enabled);
332: stringBuffer.append(',');
333: stringBuffer.append(id);
334: stringBuffer.append(',');
335: stringBuffer.append(name);
336: stringBuffer.append(']');
337: string = stringBuffer.toString();
338: }
339:
340: return string;
341: }
342:
343: /* (non-Javadoc)
344: * @see org.eclipse.ui.activities.IActivity#getDescription()
345: */
346: public String getDescription() throws NotDefinedException {
347: if (!defined) {
348: throw new NotDefinedException();
349: }
350:
351: return description;
352: }
353:
354: /* (non-Javadoc)
355: * @see org.eclipse.ui.activities.IActivity#isDefaultEnabled()
356: */
357: public boolean isDefaultEnabled() {
358: return defaultEnabled;
359: }
360:
361: boolean setDefaultEnabled(boolean defaultEnabled) {
362: if (!Util.equals(defaultEnabled, this .defaultEnabled)) {
363: this .defaultEnabled = defaultEnabled;
364: hashCode = HASH_INITIAL;
365: string = null;
366: return true;
367: }
368:
369: return false;
370: }
371: }
|