KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.TreeMap JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Platform;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.core.runtime.jobs.Job;
32 import org.eclipse.ui.activities.ActivityEvent;
33 import org.eclipse.ui.activities.ActivityManagerEvent;
34 import org.eclipse.ui.activities.CategoryEvent;
35 import org.eclipse.ui.activities.IActivity;
36 import org.eclipse.ui.activities.IActivityPatternBinding;
37 import org.eclipse.ui.activities.IActivityRequirementBinding;
38 import org.eclipse.ui.activities.ICategory;
39 import org.eclipse.ui.activities.ICategoryActivityBinding;
40 import org.eclipse.ui.activities.IIdentifier;
41 import org.eclipse.ui.activities.IMutableActivityManager;
42 import org.eclipse.ui.activities.IdentifierEvent;
43 import org.eclipse.ui.internal.util.Util;
44
45 /**
46  * An activity registry that may be altered.
47  *
48  * @since 3.0
49  */

50 public final class MutableActivityManager extends AbstractActivityManager
51         implements IMutableActivityManager, Cloneable JavaDoc {
52
53     private Map JavaDoc activitiesById = new WeakHashMap JavaDoc();
54
55     private Map JavaDoc activityRequirementBindingsByActivityId = new HashMap JavaDoc();
56
57     private Map JavaDoc activityDefinitionsById = new HashMap JavaDoc();
58
59     private Map JavaDoc activityPatternBindingsByActivityId = new HashMap JavaDoc();
60
61     private IActivityRegistry activityRegistry;
62
63     private Map JavaDoc categoriesById = new WeakHashMap JavaDoc();
64
65     private Map JavaDoc categoryActivityBindingsByCategoryId = new HashMap JavaDoc();
66
67     private Map JavaDoc categoryDefinitionsById = new HashMap JavaDoc();
68
69     private Set JavaDoc definedActivityIds = new HashSet JavaDoc();
70
71     private Set JavaDoc definedCategoryIds = new HashSet JavaDoc();
72
73     private Set JavaDoc enabledActivityIds = new HashSet JavaDoc();
74
75     private Map JavaDoc identifiersById = new WeakHashMap JavaDoc();
76     
77     /**
78      * A list of identifiers that need to have their activity sets reconciled in the background job.
79      */

80     private List JavaDoc deferredIdentifiers = Collections.synchronizedList(new LinkedList JavaDoc());
81
82     /**
83      * The identifier update job. Lazily initialized.
84      */

85     private Job deferredIdentifierJob = null;
86     
87     private final IActivityRegistryListener activityRegistryListener = new IActivityRegistryListener() {
88                 public void activityRegistryChanged(
89                         ActivityRegistryEvent activityRegistryEvent) {
90                     readRegistry(false);
91                 }
92             };
93
94     /**
95      * Create a new instance of this class using the platform extension registry.
96      */

97     public MutableActivityManager() {
98         this(new ExtensionActivityRegistry(Platform.getExtensionRegistry()));
99     }
100
101     /**
102      * Create a new instance of this class using the provided registry.
103      *
104      * @param activityRegistry the activity registry
105      */

106     public MutableActivityManager(IActivityRegistry activityRegistry) {
107         if (activityRegistry == null) {
108             throw new NullPointerException JavaDoc();
109         }
110
111         this.activityRegistry = activityRegistry;
112
113         this.activityRegistry
114                 .addActivityRegistryListener(activityRegistryListener);
115
116         readRegistry(true);
117     }
118
119     public IActivity getActivity(String JavaDoc activityId) {
120         if (activityId == null) {
121             throw new NullPointerException JavaDoc();
122         }
123
124         Activity activity = (Activity) activitiesById.get(activityId);
125
126         if (activity == null) {
127             activity = new Activity(activityId);
128             updateActivity(activity);
129             activitiesById.put(activityId, activity);
130         }
131
132         return activity;
133     }
134
135     public ICategory getCategory(String JavaDoc categoryId) {
136         if (categoryId == null) {
137             throw new NullPointerException JavaDoc();
138         }
139
140         Category category = (Category) categoriesById.get(categoryId);
141
142         if (category == null) {
143             category = new Category(categoryId);
144             updateCategory(category);
145             categoriesById.put(categoryId, category);
146         }
147
148         return category;
149     }
150
151     public Set JavaDoc getDefinedActivityIds() {
152         return Collections.unmodifiableSet(definedActivityIds);
153     }
154
155     public Set JavaDoc getDefinedCategoryIds() {
156         return Collections.unmodifiableSet(definedCategoryIds);
157     }
158
159     public Set JavaDoc getEnabledActivityIds() {
160         return Collections.unmodifiableSet(enabledActivityIds);
161     }
162
163     public IIdentifier getIdentifier(String JavaDoc identifierId) {
164         if (identifierId == null) {
165             throw new NullPointerException JavaDoc();
166         }
167
168         Identifier identifier = (Identifier) identifiersById.get(identifierId);
169
170         if (identifier == null) {
171             identifier = new Identifier(identifierId);
172             updateIdentifier(identifier);
173             identifiersById.put(identifierId, identifier);
174         }
175
176         return identifier;
177     }
178
179     private void getRequiredActivityIds(Set JavaDoc activityIds, Set JavaDoc requiredActivityIds) {
180         for (Iterator JavaDoc iterator = activityIds.iterator(); iterator.hasNext();) {
181             String JavaDoc activityId = (String JavaDoc) iterator.next();
182             IActivity activity = getActivity(activityId);
183             Set JavaDoc childActivityIds = new HashSet JavaDoc();
184             Set JavaDoc activityRequirementBindings = activity
185                     .getActivityRequirementBindings();
186
187             for (Iterator JavaDoc iterator2 = activityRequirementBindings.iterator(); iterator2
188                     .hasNext();) {
189                 IActivityRequirementBinding activityRequirementBinding = (IActivityRequirementBinding) iterator2
190                         .next();
191                 childActivityIds.add(activityRequirementBinding
192                         .getRequiredActivityId());
193             }
194
195             childActivityIds.removeAll(requiredActivityIds);
196             requiredActivityIds.addAll(childActivityIds);
197             getRequiredActivityIds(childActivityIds, requiredActivityIds);
198         }
199     }
200
201     private void notifyActivities(Map JavaDoc activityEventsByActivityId) {
202         for (Iterator JavaDoc iterator = activityEventsByActivityId.entrySet()
203                 .iterator(); iterator.hasNext();) {
204             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
205             String JavaDoc activityId = (String JavaDoc) entry.getKey();
206             ActivityEvent activityEvent = (ActivityEvent) entry.getValue();
207             Activity activity = (Activity) activitiesById.get(activityId);
208
209             if (activity != null) {
210                 activity.fireActivityChanged(activityEvent);
211             }
212         }
213     }
214
215     private void notifyCategories(Map JavaDoc categoryEventsByCategoryId) {
216         for (Iterator JavaDoc iterator = categoryEventsByCategoryId.entrySet()
217                 .iterator(); iterator.hasNext();) {
218             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
219             String JavaDoc categoryId = (String JavaDoc) entry.getKey();
220             CategoryEvent categoryEvent = (CategoryEvent) entry.getValue();
221             Category category = (Category) categoriesById.get(categoryId);
222
223             if (category != null) {
224                 category.fireCategoryChanged(categoryEvent);
225             }
226         }
227     }
228
229     private void notifyIdentifiers(Map JavaDoc identifierEventsByIdentifierId) {
230         for (Iterator JavaDoc iterator = identifierEventsByIdentifierId.entrySet()
231                 .iterator(); iterator.hasNext();) {
232             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
233             String JavaDoc identifierId = (String JavaDoc) entry.getKey();
234             IdentifierEvent identifierEvent = (IdentifierEvent) entry
235                     .getValue();
236             Identifier identifier = (Identifier) identifiersById
237                     .get(identifierId);
238
239             if (identifier != null) {
240                 identifier.fireIdentifierChanged(identifierEvent);
241             }
242         }
243     }
244
245     private void readRegistry(boolean setDefaults) {
246         if (!isRegexpSupported()) {
247             return;
248         }
249         Collection JavaDoc activityDefinitions = new ArrayList JavaDoc();
250         activityDefinitions.addAll(activityRegistry.getActivityDefinitions());
251         Map JavaDoc activityDefinitionsById = new HashMap JavaDoc(ActivityDefinition
252                 .activityDefinitionsById(activityDefinitions, false));
253
254         for (Iterator JavaDoc iterator = activityDefinitionsById.values().iterator(); iterator
255                 .hasNext();) {
256             ActivityDefinition activityDefinition = (ActivityDefinition) iterator
257                     .next();
258             String JavaDoc name = activityDefinition.getName();
259
260             if (name == null || name.length() == 0) {
261                 iterator.remove();
262             }
263         }
264
265         Collection JavaDoc categoryDefinitions = new ArrayList JavaDoc();
266         categoryDefinitions.addAll(activityRegistry.getCategoryDefinitions());
267         Map JavaDoc categoryDefinitionsById = new HashMap JavaDoc(CategoryDefinition
268                 .categoryDefinitionsById(categoryDefinitions, false));
269
270         for (Iterator JavaDoc iterator = categoryDefinitionsById.values().iterator(); iterator
271                 .hasNext();) {
272             CategoryDefinition categoryDefinition = (CategoryDefinition) iterator
273                     .next();
274             String JavaDoc name = categoryDefinition.getName();
275
276             if (name == null || name.length() == 0) {
277                 iterator.remove();
278             }
279         }
280
281         Map JavaDoc activityRequirementBindingDefinitionsByActivityId = ActivityRequirementBindingDefinition
282                 .activityRequirementBindingDefinitionsByActivityId(activityRegistry
283                         .getActivityRequirementBindingDefinitions());
284         Map JavaDoc activityRequirementBindingsByActivityId = new HashMap JavaDoc();
285
286         for (Iterator JavaDoc iterator = activityRequirementBindingDefinitionsByActivityId
287                 .entrySet().iterator(); iterator.hasNext();) {
288             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
289             String JavaDoc parentActivityId = (String JavaDoc) entry.getKey();
290
291             if (activityDefinitionsById.containsKey(parentActivityId)) {
292                 Collection JavaDoc activityRequirementBindingDefinitions = (Collection JavaDoc) entry
293                         .getValue();
294
295                 if (activityRequirementBindingDefinitions != null) {
296                     for (Iterator JavaDoc iterator2 = activityRequirementBindingDefinitions
297                             .iterator(); iterator2.hasNext();) {
298                         ActivityRequirementBindingDefinition activityRequirementBindingDefinition = (ActivityRequirementBindingDefinition) iterator2
299                                 .next();
300                         String JavaDoc childActivityId = activityRequirementBindingDefinition
301                                 .getRequiredActivityId();
302
303                         if (activityDefinitionsById
304                                 .containsKey(childActivityId)) {
305                             IActivityRequirementBinding activityRequirementBinding = new ActivityRequirementBinding(
306                                     childActivityId, parentActivityId);
307                             Set JavaDoc activityRequirementBindings = (Set JavaDoc) activityRequirementBindingsByActivityId
308                                     .get(parentActivityId);
309
310                             if (activityRequirementBindings == null) {
311                                 activityRequirementBindings = new HashSet JavaDoc();
312                                 activityRequirementBindingsByActivityId.put(
313                                         parentActivityId,
314                                         activityRequirementBindings);
315                             }
316
317                             activityRequirementBindings
318                                     .add(activityRequirementBinding);
319                         }
320                     }
321                 }
322             }
323         }
324
325         Map JavaDoc activityPatternBindingDefinitionsByActivityId = ActivityPatternBindingDefinition
326                 .activityPatternBindingDefinitionsByActivityId(activityRegistry
327                         .getActivityPatternBindingDefinitions());
328         Map JavaDoc activityPatternBindingsByActivityId = new HashMap JavaDoc();
329
330         for (Iterator JavaDoc iterator = activityPatternBindingDefinitionsByActivityId
331                 .entrySet().iterator(); iterator.hasNext();) {
332             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
333             String JavaDoc activityId = (String JavaDoc) entry.getKey();
334
335             if (activityDefinitionsById.containsKey(activityId)) {
336                 Collection JavaDoc activityPatternBindingDefinitions = (Collection JavaDoc) entry
337                         .getValue();
338
339                 if (activityPatternBindingDefinitions != null) {
340                     for (Iterator JavaDoc iterator2 = activityPatternBindingDefinitions
341                             .iterator(); iterator2.hasNext();) {
342                         ActivityPatternBindingDefinition activityPatternBindingDefinition = (ActivityPatternBindingDefinition) iterator2
343                                 .next();
344                         String JavaDoc pattern = activityPatternBindingDefinition
345                                 .getPattern();
346
347                         if (pattern != null && pattern.length() != 0) {
348                             IActivityPatternBinding activityPatternBinding = new ActivityPatternBinding(
349                                     activityId, pattern);
350                             Set JavaDoc activityPatternBindings = (Set JavaDoc) activityPatternBindingsByActivityId
351                                     .get(activityId);
352
353                             if (activityPatternBindings == null) {
354                                 activityPatternBindings = new HashSet JavaDoc();
355                                 activityPatternBindingsByActivityId.put(
356                                         activityId, activityPatternBindings);
357                             }
358
359                             activityPatternBindings.add(activityPatternBinding);
360                         }
361                     }
362                 }
363             }
364         }
365
366         Map JavaDoc categoryActivityBindingDefinitionsByCategoryId = CategoryActivityBindingDefinition
367                 .categoryActivityBindingDefinitionsByCategoryId(activityRegistry
368                         .getCategoryActivityBindingDefinitions());
369         Map JavaDoc categoryActivityBindingsByCategoryId = new HashMap JavaDoc();
370
371         for (Iterator JavaDoc iterator = categoryActivityBindingDefinitionsByCategoryId
372                 .entrySet().iterator(); iterator.hasNext();) {
373             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
374             String JavaDoc categoryId = (String JavaDoc) entry.getKey();
375
376             if (categoryDefinitionsById.containsKey(categoryId)) {
377                 Collection JavaDoc categoryActivityBindingDefinitions = (Collection JavaDoc) entry
378                         .getValue();
379
380                 if (categoryActivityBindingDefinitions != null) {
381                     for (Iterator JavaDoc iterator2 = categoryActivityBindingDefinitions
382                             .iterator(); iterator2.hasNext();) {
383                         CategoryActivityBindingDefinition categoryActivityBindingDefinition = (CategoryActivityBindingDefinition) iterator2
384                                 .next();
385                         String JavaDoc activityId = categoryActivityBindingDefinition
386                                 .getActivityId();
387
388                         if (activityDefinitionsById.containsKey(activityId)) {
389                             ICategoryActivityBinding categoryActivityBinding = new CategoryActivityBinding(
390                                     activityId, categoryId);
391                             Set JavaDoc categoryActivityBindings = (Set JavaDoc) categoryActivityBindingsByCategoryId
392                                     .get(categoryId);
393
394                             if (categoryActivityBindings == null) {
395                                 categoryActivityBindings = new HashSet JavaDoc();
396                                 categoryActivityBindingsByCategoryId.put(
397                                         categoryId, categoryActivityBindings);
398                             }
399
400                             categoryActivityBindings
401                                     .add(categoryActivityBinding);
402                         }
403                     }
404                 }
405             }
406         }
407
408         this.activityRequirementBindingsByActivityId = activityRequirementBindingsByActivityId;
409         this.activityDefinitionsById = activityDefinitionsById;
410         this.activityPatternBindingsByActivityId = activityPatternBindingsByActivityId;
411         this.categoryActivityBindingsByCategoryId = categoryActivityBindingsByCategoryId;
412         this.categoryDefinitionsById = categoryDefinitionsById;
413         boolean definedActivityIdsChanged = false;
414         Set JavaDoc definedActivityIds = new HashSet JavaDoc(activityDefinitionsById.keySet());
415
416         Set JavaDoc previouslyDefinedActivityIds = null;
417         if (!definedActivityIds.equals(this.definedActivityIds)) {
418             previouslyDefinedActivityIds = this.definedActivityIds;
419             this.definedActivityIds = definedActivityIds;
420             definedActivityIdsChanged = true;
421         }
422
423         boolean definedCategoryIdsChanged = false;
424         Set JavaDoc definedCategoryIds = new HashSet JavaDoc(categoryDefinitionsById.keySet());
425
426         Set JavaDoc previouslyDefinedCategoryIds = null;
427         if (!definedCategoryIds.equals(this.definedCategoryIds)) {
428             previouslyDefinedCategoryIds = this.definedCategoryIds;
429             this.definedCategoryIds = definedCategoryIds;
430             definedCategoryIdsChanged = true;
431         }
432
433         Set JavaDoc enabledActivityIds = new HashSet JavaDoc(this.enabledActivityIds);
434         getRequiredActivityIds(this.enabledActivityIds, enabledActivityIds);
435         boolean enabledActivityIdsChanged = false;
436
437         Set JavaDoc previouslyEnabledActivityIds = null;
438         if (!this.enabledActivityIds.equals(enabledActivityIds)) {
439             previouslyEnabledActivityIds = this.enabledActivityIds;
440             this.enabledActivityIds = enabledActivityIds;
441             enabledActivityIdsChanged = true;
442         }
443
444         Map JavaDoc activityEventsByActivityId = updateActivities(activitiesById
445                 .keySet());
446
447         Map JavaDoc categoryEventsByCategoryId = updateCategories(categoriesById
448                 .keySet());
449
450         Map JavaDoc identifierEventsByIdentifierId = updateIdentifiers(identifiersById
451                 .keySet());
452
453         if (definedActivityIdsChanged || definedCategoryIdsChanged
454                 || enabledActivityIdsChanged) {
455             fireActivityManagerChanged(new ActivityManagerEvent(this,
456                     definedActivityIdsChanged, definedCategoryIdsChanged,
457                     enabledActivityIdsChanged, previouslyDefinedActivityIds,
458                     previouslyDefinedCategoryIds, previouslyEnabledActivityIds));
459         }
460
461         if (activityEventsByActivityId != null) {
462             notifyActivities(activityEventsByActivityId);
463         }
464
465         if (categoryEventsByCategoryId != null) {
466             notifyCategories(categoryEventsByCategoryId);
467         }
468
469         if (identifierEventsByIdentifierId != null) {
470             notifyIdentifiers(identifierEventsByIdentifierId);
471         }
472
473         if (setDefaults) {
474             setEnabledActivityIds(new HashSet JavaDoc(activityRegistry
475                     .getDefaultEnabledActivities()));
476         }
477     }
478
479     /**
480      * Returns whether the Java 1.4 regular expression support is available.
481      * Regexp support will not be available when running against JCL Foundation (see bug 80053).
482      *
483      * @return <code>true</code> if regexps are supported, <code>false</code> otherwise.
484      *
485      * @since 3.1
486      */

487     private boolean isRegexpSupported() {
488         try {
489             Class.forName("java.util.regex.Pattern"); //$NON-NLS-1$
490
return true;
491         }
492         catch (Exception JavaDoc e) {
493             return false;
494         }
495     }
496
497     public void setEnabledActivityIds(Set JavaDoc enabledActivityIds) {
498         enabledActivityIds = Util.safeCopy(enabledActivityIds, String JavaDoc.class);
499         Set JavaDoc requiredActivityIds = new HashSet JavaDoc(enabledActivityIds);
500         getRequiredActivityIds(enabledActivityIds, requiredActivityIds);
501         enabledActivityIds = requiredActivityIds;
502         boolean activityManagerChanged = false;
503         Map JavaDoc activityEventsByActivityId = null;
504         Set JavaDoc deltaActivityIds = null;
505         Set JavaDoc previouslyEnabledActivityIds = null;
506         if (!this.enabledActivityIds.equals(enabledActivityIds)) {
507             previouslyEnabledActivityIds = this.enabledActivityIds;
508             this.enabledActivityIds = enabledActivityIds;
509             activityManagerChanged = true;
510             
511             // compute delta of activity changes
512
deltaActivityIds = new HashSet JavaDoc(previouslyEnabledActivityIds);
513             deltaActivityIds.removeAll(enabledActivityIds);
514             Set JavaDoc temp = new HashSet JavaDoc(enabledActivityIds);
515             temp.removeAll(previouslyEnabledActivityIds);
516             deltaActivityIds.addAll(temp);
517             
518             activityEventsByActivityId = updateActivities(deltaActivityIds);
519         }
520
521         //don't update identifiers if the enabled activity set has not changed
522
if (activityManagerChanged) {
523             Map JavaDoc identifierEventsByIdentifierId = updateIdentifiers(identifiersById
524                     .keySet(), deltaActivityIds);
525             if (identifierEventsByIdentifierId != null) {
526                 notifyIdentifiers(identifierEventsByIdentifierId);
527             }
528         }
529         if (activityEventsByActivityId != null) {
530             notifyActivities(activityEventsByActivityId);
531         }
532
533         if (activityManagerChanged) {
534             fireActivityManagerChanged(new ActivityManagerEvent(this, false,
535                     false, true, null, null, previouslyEnabledActivityIds));
536         }
537     }
538
539     private Map JavaDoc updateActivities(Collection JavaDoc activityIds) {
540         Map JavaDoc activityEventsByActivityId = new TreeMap JavaDoc();
541
542         for (Iterator JavaDoc iterator = activityIds.iterator(); iterator.hasNext();) {
543             String JavaDoc activityId = (String JavaDoc) iterator.next();
544             Activity activity = (Activity) activitiesById.get(activityId);
545
546             if (activity != null) {
547                 ActivityEvent activityEvent = updateActivity(activity);
548
549                 if (activityEvent != null) {
550                     activityEventsByActivityId.put(activityId, activityEvent);
551                 }
552             }
553         }
554
555         return activityEventsByActivityId;
556     }
557
558     private ActivityEvent updateActivity(Activity activity) {
559         Set JavaDoc activityRequirementBindings = (Set JavaDoc) activityRequirementBindingsByActivityId
560                 .get(activity.getId());
561         boolean activityRequirementBindingsChanged = activity
562                 .setActivityRequirementBindings(activityRequirementBindings != null ? activityRequirementBindings
563                         : Collections.EMPTY_SET);
564         Set JavaDoc activityPatternBindings = (Set JavaDoc) activityPatternBindingsByActivityId
565                 .get(activity.getId());
566         boolean activityPatternBindingsChanged = activity
567                 .setActivityPatternBindings(activityPatternBindings != null ? activityPatternBindings
568                         : Collections.EMPTY_SET);
569         ActivityDefinition activityDefinition = (ActivityDefinition) activityDefinitionsById
570                 .get(activity.getId());
571         boolean definedChanged = activity
572                 .setDefined(activityDefinition != null);
573         boolean enabledChanged = activity.setEnabled(enabledActivityIds
574                 .contains(activity.getId()));
575         boolean nameChanged = activity
576                 .setName(activityDefinition != null ? activityDefinition
577                         .getName() : null);
578         boolean descriptionChanged = activity
579                 .setDescription(activityDefinition != null ? activityDefinition
580                         .getDescription() : null);
581         boolean defaultEnabledChanged = activity.setDefaultEnabled(activityRegistry
582                 .getDefaultEnabledActivities().contains(activity.getId()));
583         if (activityRequirementBindingsChanged
584                 || activityPatternBindingsChanged || definedChanged
585                 || enabledChanged || nameChanged || descriptionChanged
586                 || defaultEnabledChanged) {
587             return new ActivityEvent(activity,
588                     activityRequirementBindingsChanged,
589                     activityPatternBindingsChanged, definedChanged,
590                     descriptionChanged, enabledChanged, nameChanged, defaultEnabledChanged);
591         }
592         
593         return null;
594     }
595
596     private Map JavaDoc updateCategories(Collection JavaDoc categoryIds) {
597         Map JavaDoc categoryEventsByCategoryId = new TreeMap JavaDoc();
598
599         for (Iterator JavaDoc iterator = categoryIds.iterator(); iterator.hasNext();) {
600             String JavaDoc categoryId = (String JavaDoc) iterator.next();
601             Category category = (Category) categoriesById.get(categoryId);
602
603             if (category != null) {
604                 CategoryEvent categoryEvent = updateCategory(category);
605
606                 if (categoryEvent != null) {
607                     categoryEventsByCategoryId.put(categoryId, categoryEvent);
608                 }
609             }
610         }
611
612         return categoryEventsByCategoryId;
613     }
614
615     private CategoryEvent updateCategory(Category category) {
616         Set JavaDoc categoryActivityBindings = (Set JavaDoc) categoryActivityBindingsByCategoryId
617                 .get(category.getId());
618         boolean categoryActivityBindingsChanged = category
619                 .setCategoryActivityBindings(categoryActivityBindings != null ? categoryActivityBindings
620                         : Collections.EMPTY_SET);
621         CategoryDefinition categoryDefinition = (CategoryDefinition) categoryDefinitionsById
622                 .get(category.getId());
623         boolean definedChanged = category
624                 .setDefined(categoryDefinition != null);
625         boolean nameChanged = category
626                 .setName(categoryDefinition != null ? categoryDefinition
627                         .getName() : null);
628         boolean descriptionChanged = category
629                 .setDescription(categoryDefinition != null ? categoryDefinition
630                         .getDescription() : null);
631
632         if (categoryActivityBindingsChanged || definedChanged || nameChanged
633                 || descriptionChanged) {
634             return new CategoryEvent(category, categoryActivityBindingsChanged,
635                     definedChanged, descriptionChanged, nameChanged);
636         }
637         
638         return null;
639     }
640
641     private IdentifierEvent updateIdentifier(Identifier identifier) {
642         return updateIdentifier(identifier, definedActivityIds);
643     }
644     
645     private IdentifierEvent updateIdentifier(Identifier identifier, Set JavaDoc changedActivityIds) {
646         String JavaDoc id = identifier.getId();
647         Set JavaDoc activityIds = new HashSet JavaDoc();
648         
649         boolean enabled = false;
650         
651         boolean activityIdsChanged = false;
652         
653         boolean enabledChanged = false;
654         
655         // short-circut logic. If all activities are enabled, then the
656
// identifier must be as well. Return true and schedule the remainder of
657
// the work to run in a background job.
658
if (enabledActivityIds.size() == definedActivityIds.size()) {
659             enabled = true;
660             enabledChanged = identifier.setEnabled(enabled);
661             identifier.setActivityIds(Collections.EMPTY_SET);
662             deferredIdentifiers.add(identifier);
663             getUpdateJob().schedule();
664             if (enabledChanged) {
665                 return new IdentifierEvent(identifier, activityIdsChanged,
666                         enabledChanged);
667             }
668         }
669         else {
670             boolean matchesAtLeastOneEnabled = false;
671             boolean matchesAtLeastOneDisabled = false;
672             Set JavaDoc activityIdsToUpdate = new HashSet JavaDoc(changedActivityIds);
673             if (identifier.getActivityIds() != null) {
674                 activityIdsToUpdate.addAll(identifier.getActivityIds());
675             }
676             for (Iterator JavaDoc iterator = activityIdsToUpdate.iterator(); iterator
677                     .hasNext();) {
678                 String JavaDoc activityId = (String JavaDoc) iterator.next();
679                 Activity activity = (Activity) getActivity(activityId);
680     
681                 if (activity.isMatch(id)) {
682                     activityIds.add(activityId);
683                     if (activity.isEnabled()) {
684                         matchesAtLeastOneEnabled = true;
685                     } else {
686                         matchesAtLeastOneDisabled = true;
687                     }
688                 }
689             }
690             
691             enabled = matchesAtLeastOneEnabled ? true : !matchesAtLeastOneDisabled;
692             
693             activityIdsChanged = identifier.setActivityIds(activityIds);
694             enabledChanged = identifier.setEnabled(enabled);
695     
696             if (activityIdsChanged || enabledChanged) {
697                 return new IdentifierEvent(identifier, activityIdsChanged,
698                         enabledChanged);
699             }
700         }
701         return null;
702     }
703
704     private Map JavaDoc updateIdentifiers(Collection JavaDoc identifierIds) {
705         return updateIdentifiers(identifierIds, definedActivityIds);
706     }
707     
708     private Map JavaDoc updateIdentifiers(Collection JavaDoc identifierIds, Set JavaDoc changedActivityIds) {
709         Map JavaDoc identifierEventsByIdentifierId = new TreeMap JavaDoc();
710
711         for (Iterator JavaDoc iterator = identifierIds.iterator(); iterator.hasNext();) {
712             String JavaDoc identifierId = (String JavaDoc) iterator.next();
713             Identifier identifier = (Identifier) identifiersById
714                     .get(identifierId);
715
716             if (identifier != null) {
717                 IdentifierEvent identifierEvent = updateIdentifier(identifier, changedActivityIds);
718
719                 if (identifierEvent != null) {
720                     identifierEventsByIdentifierId.put(identifierId,
721                             identifierEvent);
722                 }
723             }
724         }
725
726         return identifierEventsByIdentifierId;
727     }
728     
729     /**
730      * Unhook this manager from its registry.
731      *
732      * @since 3.1
733      */

734     public void unhookRegistryListeners() {
735         activityRegistry.removeActivityRegistryListener(activityRegistryListener);
736     }
737     
738     /* (non-Javadoc)
739      * @see java.lang.Object#clone()
740      */

741     public Object JavaDoc clone() {
742         MutableActivityManager clone = new MutableActivityManager(activityRegistry);
743         clone.setEnabledActivityIds(getEnabledActivityIds());
744         return clone;
745     }
746     
747     /**
748      * Return the identifier update job.
749      *
750      * @return the job
751      * @since 3.1
752      */

753     private Job getUpdateJob() {
754         if (deferredIdentifierJob == null) {
755             deferredIdentifierJob = new Job("Identifier Update Job") { //$NON-NLS-1$
756

757                 /* (non-Javadoc)
758                  * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
759                  */

760                 protected IStatus run(IProgressMonitor monitor) {
761                     while (!deferredIdentifiers.isEmpty()) {
762                         Identifier identifier = (Identifier) deferredIdentifiers.remove(0);
763                         Set JavaDoc activityIds = new HashSet JavaDoc();
764                         for (Iterator JavaDoc iterator = definedActivityIds.iterator(); iterator
765                                 .hasNext();) {
766                             String JavaDoc activityId = (String JavaDoc) iterator.next();
767                             Activity activity = (Activity) getActivity(activityId);
768
769                             if (activity.isMatch(identifier.getId())) {
770                                 activityIds.add(activityId);
771                             }
772                         }
773                         
774                         boolean activityIdsChanged = identifier.setActivityIds(activityIds);
775                         if (activityIdsChanged) {
776                             IdentifierEvent identifierEvent = new IdentifierEvent(identifier, activityIdsChanged,
777                                     false);
778                             Map JavaDoc identifierEventsByIdentifierId = new HashMap JavaDoc(1);
779                             identifierEventsByIdentifierId.put(identifier.getId(),
780                                     identifierEvent);
781                             notifyIdentifiers(identifierEventsByIdentifierId);
782                         }
783                     }
784                     return Status.OK_STATUS;
785                 }
786             };
787             deferredIdentifierJob.setSystem(true);
788         }
789         return deferredIdentifierJob;
790     }
791 }
792
Popular Tags