KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > activities > WorkbenchActivityHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.activities;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.ui.IPluginContribution;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.internal.activities.ws.WorkbenchActivitySupport;
22
23 /**
24  * A utility class that contains helpful methods for interacting with the
25  * activities API.
26  *
27  * @since 3.0
28  */

29 public final class WorkbenchActivityHelper {
30
31     /**
32      * Return the identifier that maps to the given contribution.
33      *
34      * @param contribution
35      * the contribution
36      * @return the identifier
37      * @since 3.1
38      */

39     public static IIdentifier getIdentifier(IPluginContribution contribution) {
40         IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
41                 .getWorkbench().getActivitySupport();
42         IIdentifier identifier = workbenchActivitySupport.getActivityManager()
43                 .getIdentifier(createUnifiedId(contribution));
44         return identifier;
45     }
46
47     /**
48      * Answers whether a given contribution is allowed to be used based on
49      * activity enablement. If it is currently disabled, then a dialog is opened
50      * and the user is prompted to activate the requried activities. If the user
51      * declines their activation then false is returned. In all other cases
52      * <code>true</code> is returned.
53      *
54      * @param object
55      * the contribution to test.
56      * @return whether the contribution is allowed to be used based on activity
57      * enablement.
58      * @deprecated
59      * @see #allowUseOf(ITriggerPoint, Object)
60      */

61     public static boolean allowUseOf(Object JavaDoc object) {
62         return allowUseOf(PlatformUI.getWorkbench().getActivitySupport()
63                 .getTriggerPointManager().getTriggerPoint(
64                         ITriggerPointManager.UNKNOWN_TRIGGER_POINT_ID), object);
65     }
66
67     /**
68      * Answers whether a given contribution is allowed to be used based on
69      * activity enablement. If it is currently disabled, then a dialog is opened
70      * and the user is prompted to activate the requried activities. If the user
71      * declines their activation then false is returned. In all other cases
72      * <code>true</code> is returned.
73      *
74      * @param triggerPoint
75      * the trigger point being hit
76      * @param object
77      * the contribution to test.
78      * @return whether the contribution is allowed to be used based on activity
79      * enablement.
80      */

81     public static boolean allowUseOf(ITriggerPoint triggerPoint, Object JavaDoc object) {
82         if (!isFiltering()) {
83             return true;
84         }
85         if (triggerPoint == null) {
86             return true;
87         }
88         if (object instanceof IPluginContribution) {
89             IPluginContribution contribution = (IPluginContribution) object;
90             IIdentifier identifier = getIdentifier(contribution);
91             return allow(triggerPoint, identifier);
92         }
93         return true;
94     }
95
96     /**
97      * Answers whether a given identifier is enabled. If it is not enabled, then
98      * a dialog is opened and the user is prompted to enable the associated
99      * activities.
100      *
101      * @param triggerPoint
102      * thr trigger point to test
103      * @param identifier
104      * the identifier to test.
105      * @return whether the identifier is enabled.
106      */

107     private static boolean allow(ITriggerPoint triggerPoint,
108             IIdentifier identifier) {
109         if (identifier.isEnabled()) {
110             return true;
111         }
112
113         ITriggerPointAdvisor advisor = ((WorkbenchActivitySupport) PlatformUI
114                 .getWorkbench().getActivitySupport()).getTriggerPointAdvisor();
115         Set JavaDoc activitiesToEnable = advisor.allow(triggerPoint, identifier);
116         if (activitiesToEnable == null) {
117             return false;
118         }
119
120         enableActivities(activitiesToEnable);
121         return true;
122     }
123
124     /**
125      * Utility method to create a <code>String</code> containing the plugin
126      * and extension ids of a contribution. This will have the form
127      *
128      * <pre>
129      * pluginId / extensionId
130      * </pre>. If the IPluginContribution does not define a plugin id then the
131      * extension id alone is returned.
132      *
133      * @param contribution
134      * the contribution to use
135      * @return the unified id
136      */

137     public static final String JavaDoc createUnifiedId(IPluginContribution contribution) {
138         if (contribution.getPluginId() != null) {
139             return contribution.getPluginId() + '/' + contribution.getLocalId();
140         }
141         return contribution.getLocalId();
142     }
143
144     /**
145      * Enables the set of activities.
146      *
147      * @param activities
148      * the activities to enable
149      */

150     private static void enableActivities(Collection JavaDoc activities) {
151         IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench()
152                 .getActivitySupport();
153         Set JavaDoc newSet = new HashSet JavaDoc(activitySupport.getActivityManager()
154                 .getEnabledActivityIds());
155         newSet.addAll(activities);
156         activitySupport.setEnabledActivityIds(newSet);
157     }
158
159     /**
160      * Answers whether the provided object should be filtered from the UI based
161      * on activity state. Returns <code>false</code> except when the object is
162      * an instance of <code>IPluginContribution</code> whos unified id matches
163      * an <code>IIdentifier</code> that is currently disabled.
164      *
165      * @param object
166      * the object to test
167      * @return whether the object should be filtered
168      * @see #createUnifiedId(IPluginContribution)
169      */

170     public static final boolean filterItem(Object JavaDoc object) {
171         if (object instanceof IPluginContribution) {
172             IPluginContribution contribution = (IPluginContribution) object;
173             IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
174                     .getWorkbench().getActivitySupport();
175             IIdentifier identifier = workbenchActivitySupport
176                     .getActivityManager().getIdentifier(
177                             createUnifiedId(contribution));
178             if (!identifier.isEnabled()) {
179                 return true;
180             }
181         }
182         return false;
183     }
184
185     /**
186      * Returns whether the UI is set up to filter contributions. This is the
187      * case if there are defined activities.
188      *
189      * @return whether the UI is set up to filter contributions
190      */

191     public static final boolean isFiltering() {
192         return !PlatformUI.getWorkbench().getActivitySupport()
193                 .getActivityManager().getDefinedActivityIds().isEmpty();
194     }
195
196     /**
197      * Return a list of category ids that will become implicity enabled if the
198      * given category becomes enabled Note that the set returned by this set
199      * represents the delta of categories that would be enabled - if the
200      * category is already enabled then it is omitted.
201      *
202      * @param activityManager
203      * the activity manager to test against
204      * @param categoryId
205      * the category to be enabled
206      * @return a list of category ids that will become implicity enabled if the
207      * given category becomes enabled
208      * @since 3.1
209      */

210     public static Set JavaDoc getEnabledCategories(IActivityManager activityManager,
211             String JavaDoc categoryId) {
212         ICategory category = activityManager.getCategory(categoryId);
213         if (!category.isDefined()) {
214             return Collections.EMPTY_SET;
215         }
216
217         Set JavaDoc activities = expandActivityDependencies(getActivityIdsForCategory(category));
218         Set JavaDoc otherEnabledCategories = new HashSet JavaDoc();
219         Set JavaDoc definedCategoryIds = activityManager.getDefinedCategoryIds();
220         for (Iterator JavaDoc i = definedCategoryIds.iterator(); i.hasNext();) {
221             String JavaDoc otherCategoryId = (String JavaDoc) i.next();
222             if (otherCategoryId.equals(categoryId)) {
223                 continue;
224             }
225             ICategory otherCategory = activityManager
226                     .getCategory(otherCategoryId);
227             Set JavaDoc otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
228             if (activityManager.getEnabledActivityIds().containsAll(
229                     otherCategoryActivityIds)) {
230                 continue;
231             }
232             if (activities.containsAll(otherCategoryActivityIds)) {
233                 otherEnabledCategories.add(otherCategoryId);
234             }
235
236         }
237         return otherEnabledCategories;
238     }
239
240     /**
241      * Return the expanded activities for the given activity set. This will
242      * resolve all activity requirement bindings.
243      *
244      * @param baseActivities
245      * the set of activities to expand
246      * @return the expanded activities
247      * @since 3.1
248      */

249     public static Set JavaDoc expandActivityDependencies(Set JavaDoc baseActivities) {
250         Set JavaDoc extendedActivities = new HashSet JavaDoc();
251         for (Iterator JavaDoc i = baseActivities.iterator(); i.hasNext();) {
252             String JavaDoc activityId = (String JavaDoc) i.next();
253             Set JavaDoc requiredActivities = getRequiredActivityIds(activityId);
254             extendedActivities.addAll(requiredActivities);
255         }
256         extendedActivities.addAll(baseActivities);
257         return extendedActivities;
258     }
259
260     /**
261      * Return the activities required for this activity.
262      *
263      * @param activityId
264      * the activity id
265      * @return the activities required for this activity
266      * @since 3.1
267      */

268     public static Set JavaDoc getRequiredActivityIds(String JavaDoc activityId) {
269         IActivityManager manager = PlatformUI.getWorkbench()
270                 .getActivitySupport().getActivityManager();
271         IActivity activity = manager.getActivity(activityId);
272         if (!activity.isDefined()) {
273             return Collections.EMPTY_SET;
274         }
275         Set JavaDoc requirementBindings = activity.getActivityRequirementBindings();
276         if (requirementBindings.isEmpty()) {
277             return Collections.EMPTY_SET;
278         }
279
280         Set JavaDoc requiredActivities = new HashSet JavaDoc(3);
281         for (Iterator JavaDoc i = requirementBindings.iterator(); i.hasNext();) {
282             IActivityRequirementBinding binding = (IActivityRequirementBinding) i
283                     .next();
284             requiredActivities.add(binding.getRequiredActivityId());
285             requiredActivities.addAll(getRequiredActivityIds(binding
286                     .getRequiredActivityId()));
287         }
288         return requiredActivities;
289     }
290
291     /**
292      * Return the activities directly required by a given category.
293      *
294      * @param category
295      * the category
296      * @return the activities directly required by a given category
297      * @since 3.1
298      */

299     public static Set JavaDoc getActivityIdsForCategory(ICategory category) {
300         Set JavaDoc bindings = category.getCategoryActivityBindings();
301         Set JavaDoc activityIds = new HashSet JavaDoc();
302         for (Iterator JavaDoc i = bindings.iterator(); i.hasNext();) {
303             ICategoryActivityBinding binding = (ICategoryActivityBinding) i
304                     .next();
305             activityIds.add(binding.getActivityId());
306         }
307         return activityIds;
308     }
309
310     /**
311      * Return a list of category ids that will become implicity disabled if the
312      * given category becomes disabled Note that the set returned by this set
313      * represents the delta of categories that would be enabled - if the
314      * category is already enabled then it is omitted.
315      *
316      * @param activityManager
317      * the activity manager to test against
318      * @param categoryId
319      * the category to be enabled
320      * @return a list of category ids that will become implicity enabled if the
321      * given category becomes enabled
322      * @since 3.1
323      */

324     public static Set JavaDoc getDisabledCategories(IActivityManager activityManager,
325             String JavaDoc categoryId) {
326         ICategory category = activityManager.getCategory(categoryId);
327         if (!category.isDefined()) {
328             return Collections.EMPTY_SET;
329         }
330
331         Set JavaDoc activities = expandActivityDependencies(getActivityIdsForCategory(category));
332         Set JavaDoc otherDisabledCategories = new HashSet JavaDoc();
333         Set JavaDoc definedCategoryIds = activityManager.getDefinedCategoryIds();
334         for (Iterator JavaDoc i = definedCategoryIds.iterator(); i.hasNext();) {
335             String JavaDoc otherCategoryId = (String JavaDoc) i.next();
336             if (otherCategoryId.equals(categoryId)) {
337                 continue;
338             }
339             ICategory otherCategory = activityManager
340                     .getCategory(otherCategoryId);
341             Set JavaDoc otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
342
343             if (otherCategoryActivityIds.isEmpty()) {
344                 continue;
345             }
346
347             if (!activities.containsAll(otherCategoryActivityIds)) {
348                 continue;
349             }
350
351             if (activityManager.getEnabledActivityIds().containsAll(
352                     otherCategoryActivityIds)) {
353                 otherDisabledCategories.add(otherCategoryId);
354             }
355
356         }
357         return otherDisabledCategories;
358     }
359
360     /**
361      * Return a list of category ids that are implicitly contained within the
362      * given category.
363      *
364      * @param activityManager
365      * the activity manager to test agaisnt
366      * @param categoryId
367      * the category to be enabled
368      * @return a list of category ids that will become implicity enabled if the
369      * given category becomes enabled
370      * @since 3.1
371      */

372     public static final Set JavaDoc getContainedCategories(
373             IActivityManager activityManager, String JavaDoc categoryId) {
374         ICategory category = activityManager.getCategory(categoryId);
375         if (!category.isDefined()) {
376             return Collections.EMPTY_SET;
377         }
378
379         Set JavaDoc activities = expandActivityDependencies(getActivityIdsForCategory(category));
380         Set JavaDoc containedCategories = new HashSet JavaDoc();
381         Set JavaDoc definedCategoryIds = activityManager.getDefinedCategoryIds();
382         for (Iterator JavaDoc i = definedCategoryIds.iterator(); i.hasNext();) {
383             String JavaDoc otherCategoryId = (String JavaDoc) i.next();
384             if (otherCategoryId.equals(categoryId)) {
385                 continue;
386             }
387             ICategory otherCategory = activityManager
388                     .getCategory(otherCategoryId);
389             Set JavaDoc otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
390
391             if (otherCategoryActivityIds.isEmpty()) {
392                 continue;
393             }
394
395             if (activities.containsAll(otherCategoryActivityIds)) {
396                 containedCategories.add(otherCategoryId);
397             }
398
399         }
400         return containedCategories;
401
402     }
403
404     /**
405      * Return the set of enabled categories. An enabled category is one in which
406      * all contained activities are enabled.
407      *
408      * @param activityManager
409      * the activity manager to test against
410      * @return the set of enabled categories.
411      * @since 3.1
412      */

413     public static Set JavaDoc getEnabledCategories(IActivityManager activityManager) {
414
415         Set JavaDoc definedCategoryIds = activityManager.getDefinedCategoryIds();
416         Set JavaDoc enabledCategories = new HashSet JavaDoc();
417         for (Iterator JavaDoc i = definedCategoryIds.iterator(); i.hasNext();) {
418             String JavaDoc categoryId = (String JavaDoc) i.next();
419             if (isEnabled(activityManager, categoryId)) {
420                 enabledCategories.add(categoryId);
421             }
422         }
423         return enabledCategories;
424     }
425     
426     /**
427      * Return the set of partially enabled categories.
428      *
429      * @param activityManager
430      * the activity manager to test against
431      * @return the set of partially enabled categories
432      * @since 3.2
433      */

434     public static Set JavaDoc getPartiallyEnabledCategories(
435             IActivityManager activityManager) {
436         Set JavaDoc definedCategoryIds = activityManager.getDefinedCategoryIds();
437         Set JavaDoc partialCategories = new HashSet JavaDoc();
438         for (Iterator JavaDoc i = definedCategoryIds.iterator(); i.hasNext();) {
439             String JavaDoc categoryId = (String JavaDoc) i.next();
440             if (isPartiallyEnabled(activityManager, categoryId)) {
441                 partialCategories.add(categoryId);
442             }
443         }
444
445         return partialCategories;
446     }
447
448     /**
449      * Returns whether the given category is partially enabled. A partially
450      * enabled category is one in which the number of enabled activites is both
451      * non-zero and less than the total number of activities in the category.
452      *
453      * @param activityManager
454      * the activity manager to test against
455      * @param categoryId
456      * the category id
457      * @return whether the category is enabled
458      * @since 3.2
459      */

460     public static boolean isPartiallyEnabled(IActivityManager activityManager,
461             String JavaDoc categoryId) {
462         Set JavaDoc activityIds = getActivityIdsForCategory(activityManager
463                 .getCategory(categoryId));
464         int foundCount = 0;
465         for (Iterator JavaDoc i = activityIds.iterator(); i.hasNext();) {
466             String JavaDoc activityId = (String JavaDoc) i.next();
467             if (activityManager.getEnabledActivityIds().contains(activityId)) {
468                 foundCount++;
469             }
470         }
471
472         return foundCount > 0 && foundCount != activityIds.size();
473     }
474
475     /**
476      * Return the number of enabled categories that this activity belongs to.
477      *
478      * @param activityManager
479      * the activity manager to test against *
480      * @param activityId
481      * the activity id to query on
482      * @return the set of enabled category ids that this activity belongs to
483      * @since 3.1
484      */

485     public static Set JavaDoc getEnabledCategoriesForActivity(
486             IActivityManager activityManager, String JavaDoc activityId) {
487         Set JavaDoc enabledCategoriesForActivity = new HashSet JavaDoc();
488         Set JavaDoc enabledCategories = getEnabledCategories(activityManager);
489         for (Iterator JavaDoc i = enabledCategories.iterator(); i.hasNext();) {
490             String JavaDoc categoryId = (String JavaDoc) i.next();
491             if (getActivityIdsForCategory(
492                     activityManager.getCategory(categoryId)).contains(
493                     activityId)) {
494                 enabledCategoriesForActivity.add(categoryId);
495             }
496         }
497         return enabledCategoriesForActivity;
498     }
499
500     /**
501      * Returns whether the given category is enabled. A category is enabled if
502      * all of its activities are enabled.
503      *
504      * @param activityManager
505      * the activity manager to test against
506      * @param categoryId
507      * the category id
508      * @return whether the category is enabled
509      * @since 3.1
510      */

511     public static boolean isEnabled(IActivityManager activityManager,
512             String JavaDoc categoryId) {
513
514         ICategory category = activityManager.getCategory(categoryId);
515         if (category.isDefined()) {
516             Set JavaDoc activityIds = getActivityIdsForCategory(category);
517             if (activityManager.getEnabledActivityIds().containsAll(activityIds)) {
518                 return true;
519             }
520         }
521
522         return false;
523     }
524
525     /**
526      * Resolve the collection of category ids to an array of
527      * <code>ICategory</code> objects.
528      *
529      * @param activityManager
530      * the activity manager to test against
531      * @param categoryIds
532      * the category ids
533      * @return the array of category ids resolved to <code>ICategory</code>
534      * objects
535      * @since 3.1
536      */

537     public static ICategory[] resolveCategories(
538             IMutableActivityManager activityManager, Set JavaDoc categoryIds) {
539         ICategory[] categories = new ICategory[categoryIds.size()];
540         String JavaDoc[] categoryIdArray = (String JavaDoc[]) categoryIds
541                 .toArray(new String JavaDoc[categoryIds.size()]);
542         for (int i = 0; i < categoryIdArray.length; i++) {
543             categories[i] = activityManager.getCategory(categoryIdArray[i]);
544         }
545         return categories;
546     }
547
548     /**
549      * Not intended to be instantiated.
550      */

551     private WorkbenchActivityHelper() {
552         // no-op
553
}
554 }
555
Popular Tags