001: /*******************************************************************************
002: * Copyright (c) 2003, 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.activities;
011:
012: import java.util.Collection;
013: import java.util.Collections;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.Set;
017:
018: import org.eclipse.ui.IPluginContribution;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.internal.activities.ws.WorkbenchActivitySupport;
021:
022: /**
023: * A utility class that contains helpful methods for interacting with the
024: * activities API.
025: *
026: * @since 3.0
027: */
028: public final class WorkbenchActivityHelper {
029:
030: /**
031: * Return the identifier that maps to the given contribution.
032: *
033: * @param contribution
034: * the contribution
035: * @return the identifier
036: * @since 3.1
037: */
038: public static IIdentifier getIdentifier(
039: IPluginContribution contribution) {
040: IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
041: .getWorkbench().getActivitySupport();
042: IIdentifier identifier = workbenchActivitySupport
043: .getActivityManager().getIdentifier(
044: createUnifiedId(contribution));
045: return identifier;
046: }
047:
048: /**
049: * Answers whether a given contribution is allowed to be used based on
050: * activity enablement. If it is currently disabled, then a dialog is opened
051: * and the user is prompted to activate the requried activities. If the user
052: * declines their activation then false is returned. In all other cases
053: * <code>true</code> is returned.
054: *
055: * @param object
056: * the contribution to test.
057: * @return whether the contribution is allowed to be used based on activity
058: * enablement.
059: * @deprecated
060: * @see #allowUseOf(ITriggerPoint, Object)
061: */
062: public static boolean allowUseOf(Object object) {
063: return allowUseOf(PlatformUI.getWorkbench()
064: .getActivitySupport().getTriggerPointManager()
065: .getTriggerPoint(
066: ITriggerPointManager.UNKNOWN_TRIGGER_POINT_ID),
067: object);
068: }
069:
070: /**
071: * Answers whether a given contribution is allowed to be used based on
072: * activity enablement. If it is currently disabled, then a dialog is opened
073: * and the user is prompted to activate the requried activities. If the user
074: * declines their activation then false is returned. In all other cases
075: * <code>true</code> is returned.
076: *
077: * @param triggerPoint
078: * the trigger point being hit
079: * @param object
080: * the contribution to test.
081: * @return whether the contribution is allowed to be used based on activity
082: * enablement.
083: */
084: public static boolean allowUseOf(ITriggerPoint triggerPoint,
085: Object object) {
086: if (!isFiltering()) {
087: return true;
088: }
089: if (triggerPoint == null) {
090: return true;
091: }
092: if (object instanceof IPluginContribution) {
093: IPluginContribution contribution = (IPluginContribution) object;
094: IIdentifier identifier = getIdentifier(contribution);
095: return allow(triggerPoint, identifier);
096: }
097: return true;
098: }
099:
100: /**
101: * Answers whether a given identifier is enabled. If it is not enabled, then
102: * a dialog is opened and the user is prompted to enable the associated
103: * activities.
104: *
105: * @param triggerPoint
106: * thr trigger point to test
107: * @param identifier
108: * the identifier to test.
109: * @return whether the identifier is enabled.
110: */
111: private static boolean allow(ITriggerPoint triggerPoint,
112: IIdentifier identifier) {
113: if (identifier.isEnabled()) {
114: return true;
115: }
116:
117: ITriggerPointAdvisor advisor = ((WorkbenchActivitySupport) PlatformUI
118: .getWorkbench().getActivitySupport())
119: .getTriggerPointAdvisor();
120: Set activitiesToEnable = advisor
121: .allow(triggerPoint, identifier);
122: if (activitiesToEnable == null) {
123: return false;
124: }
125:
126: enableActivities(activitiesToEnable);
127: return true;
128: }
129:
130: /**
131: * Utility method to create a <code>String</code> containing the plugin
132: * and extension ids of a contribution. This will have the form
133: *
134: * <pre>
135: * pluginId / extensionId
136: * </pre>. If the IPluginContribution does not define a plugin id then the
137: * extension id alone is returned.
138: *
139: * @param contribution
140: * the contribution to use
141: * @return the unified id
142: */
143: public static final String createUnifiedId(
144: IPluginContribution contribution) {
145: if (contribution.getPluginId() != null) {
146: return contribution.getPluginId() + '/'
147: + contribution.getLocalId();
148: }
149: return contribution.getLocalId();
150: }
151:
152: /**
153: * Enables the set of activities.
154: *
155: * @param activities
156: * the activities to enable
157: */
158: private static void enableActivities(Collection activities) {
159: IWorkbenchActivitySupport activitySupport = PlatformUI
160: .getWorkbench().getActivitySupport();
161: Set newSet = new HashSet(activitySupport.getActivityManager()
162: .getEnabledActivityIds());
163: newSet.addAll(activities);
164: activitySupport.setEnabledActivityIds(newSet);
165: }
166:
167: /**
168: * Answers whether the provided object should be filtered from the UI based
169: * on activity state. Returns <code>false</code> except when the object is
170: * an instance of <code>IPluginContribution</code> whos unified id matches
171: * an <code>IIdentifier</code> that is currently disabled.
172: *
173: * @param object
174: * the object to test
175: * @return whether the object should be filtered
176: * @see #createUnifiedId(IPluginContribution)
177: */
178: public static final boolean filterItem(Object object) {
179: if (object instanceof IPluginContribution) {
180: IPluginContribution contribution = (IPluginContribution) object;
181: IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI
182: .getWorkbench().getActivitySupport();
183: IIdentifier identifier = workbenchActivitySupport
184: .getActivityManager().getIdentifier(
185: createUnifiedId(contribution));
186: if (!identifier.isEnabled()) {
187: return true;
188: }
189: }
190: return false;
191: }
192:
193: /**
194: * Returns whether the UI is set up to filter contributions. This is the
195: * case if there are defined activities.
196: *
197: * @return whether the UI is set up to filter contributions
198: */
199: public static final boolean isFiltering() {
200: return !PlatformUI.getWorkbench().getActivitySupport()
201: .getActivityManager().getDefinedActivityIds().isEmpty();
202: }
203:
204: /**
205: * Return a list of category ids that will become implicity enabled if the
206: * given category becomes enabled Note that the set returned by this set
207: * represents the delta of categories that would be enabled - if the
208: * category is already enabled then it is omitted.
209: *
210: * @param activityManager
211: * the activity manager to test against
212: * @param categoryId
213: * the category to be enabled
214: * @return a list of category ids that will become implicity enabled if the
215: * given category becomes enabled
216: * @since 3.1
217: */
218: public static Set getEnabledCategories(
219: IActivityManager activityManager, String categoryId) {
220: ICategory category = activityManager.getCategory(categoryId);
221: if (!category.isDefined()) {
222: return Collections.EMPTY_SET;
223: }
224:
225: Set activities = expandActivityDependencies(getActivityIdsForCategory(category));
226: Set otherEnabledCategories = new HashSet();
227: Set definedCategoryIds = activityManager
228: .getDefinedCategoryIds();
229: for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) {
230: String otherCategoryId = (String) i.next();
231: if (otherCategoryId.equals(categoryId)) {
232: continue;
233: }
234: ICategory otherCategory = activityManager
235: .getCategory(otherCategoryId);
236: Set otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
237: if (activityManager.getEnabledActivityIds().containsAll(
238: otherCategoryActivityIds)) {
239: continue;
240: }
241: if (activities.containsAll(otherCategoryActivityIds)) {
242: otherEnabledCategories.add(otherCategoryId);
243: }
244:
245: }
246: return otherEnabledCategories;
247: }
248:
249: /**
250: * Return the expanded activities for the given activity set. This will
251: * resolve all activity requirement bindings.
252: *
253: * @param baseActivities
254: * the set of activities to expand
255: * @return the expanded activities
256: * @since 3.1
257: */
258: public static Set expandActivityDependencies(Set baseActivities) {
259: Set extendedActivities = new HashSet();
260: for (Iterator i = baseActivities.iterator(); i.hasNext();) {
261: String activityId = (String) i.next();
262: Set requiredActivities = getRequiredActivityIds(activityId);
263: extendedActivities.addAll(requiredActivities);
264: }
265: extendedActivities.addAll(baseActivities);
266: return extendedActivities;
267: }
268:
269: /**
270: * Return the activities required for this activity.
271: *
272: * @param activityId
273: * the activity id
274: * @return the activities required for this activity
275: * @since 3.1
276: */
277: public static Set getRequiredActivityIds(String activityId) {
278: IActivityManager manager = PlatformUI.getWorkbench()
279: .getActivitySupport().getActivityManager();
280: IActivity activity = manager.getActivity(activityId);
281: if (!activity.isDefined()) {
282: return Collections.EMPTY_SET;
283: }
284: Set requirementBindings = activity
285: .getActivityRequirementBindings();
286: if (requirementBindings.isEmpty()) {
287: return Collections.EMPTY_SET;
288: }
289:
290: Set requiredActivities = new HashSet(3);
291: for (Iterator i = requirementBindings.iterator(); i.hasNext();) {
292: IActivityRequirementBinding binding = (IActivityRequirementBinding) i
293: .next();
294: requiredActivities.add(binding.getRequiredActivityId());
295: requiredActivities.addAll(getRequiredActivityIds(binding
296: .getRequiredActivityId()));
297: }
298: return requiredActivities;
299: }
300:
301: /**
302: * Return the activities directly required by a given category.
303: *
304: * @param category
305: * the category
306: * @return the activities directly required by a given category
307: * @since 3.1
308: */
309: public static Set getActivityIdsForCategory(ICategory category) {
310: Set bindings = category.getCategoryActivityBindings();
311: Set activityIds = new HashSet();
312: for (Iterator i = bindings.iterator(); i.hasNext();) {
313: ICategoryActivityBinding binding = (ICategoryActivityBinding) i
314: .next();
315: activityIds.add(binding.getActivityId());
316: }
317: return activityIds;
318: }
319:
320: /**
321: * Return a list of category ids that will become implicity disabled if the
322: * given category becomes disabled Note that the set returned by this set
323: * represents the delta of categories that would be enabled - if the
324: * category is already enabled then it is omitted.
325: *
326: * @param activityManager
327: * the activity manager to test against
328: * @param categoryId
329: * the category to be enabled
330: * @return a list of category ids that will become implicity enabled if the
331: * given category becomes enabled
332: * @since 3.1
333: */
334: public static Set getDisabledCategories(
335: IActivityManager activityManager, String categoryId) {
336: ICategory category = activityManager.getCategory(categoryId);
337: if (!category.isDefined()) {
338: return Collections.EMPTY_SET;
339: }
340:
341: Set activities = expandActivityDependencies(getActivityIdsForCategory(category));
342: Set otherDisabledCategories = new HashSet();
343: Set definedCategoryIds = activityManager
344: .getDefinedCategoryIds();
345: for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) {
346: String otherCategoryId = (String) i.next();
347: if (otherCategoryId.equals(categoryId)) {
348: continue;
349: }
350: ICategory otherCategory = activityManager
351: .getCategory(otherCategoryId);
352: Set otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
353:
354: if (otherCategoryActivityIds.isEmpty()) {
355: continue;
356: }
357:
358: if (!activities.containsAll(otherCategoryActivityIds)) {
359: continue;
360: }
361:
362: if (activityManager.getEnabledActivityIds().containsAll(
363: otherCategoryActivityIds)) {
364: otherDisabledCategories.add(otherCategoryId);
365: }
366:
367: }
368: return otherDisabledCategories;
369: }
370:
371: /**
372: * Return a list of category ids that are implicitly contained within the
373: * given category.
374: *
375: * @param activityManager
376: * the activity manager to test agaisnt
377: * @param categoryId
378: * the category to be enabled
379: * @return a list of category ids that will become implicity enabled if the
380: * given category becomes enabled
381: * @since 3.1
382: */
383: public static final Set getContainedCategories(
384: IActivityManager activityManager, String categoryId) {
385: ICategory category = activityManager.getCategory(categoryId);
386: if (!category.isDefined()) {
387: return Collections.EMPTY_SET;
388: }
389:
390: Set activities = expandActivityDependencies(getActivityIdsForCategory(category));
391: Set containedCategories = new HashSet();
392: Set definedCategoryIds = activityManager
393: .getDefinedCategoryIds();
394: for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) {
395: String otherCategoryId = (String) i.next();
396: if (otherCategoryId.equals(categoryId)) {
397: continue;
398: }
399: ICategory otherCategory = activityManager
400: .getCategory(otherCategoryId);
401: Set otherCategoryActivityIds = expandActivityDependencies(getActivityIdsForCategory(otherCategory));
402:
403: if (otherCategoryActivityIds.isEmpty()) {
404: continue;
405: }
406:
407: if (activities.containsAll(otherCategoryActivityIds)) {
408: containedCategories.add(otherCategoryId);
409: }
410:
411: }
412: return containedCategories;
413:
414: }
415:
416: /**
417: * Return the set of enabled categories. An enabled category is one in which
418: * all contained activities are enabled.
419: *
420: * @param activityManager
421: * the activity manager to test against
422: * @return the set of enabled categories.
423: * @since 3.1
424: */
425: public static Set getEnabledCategories(
426: IActivityManager activityManager) {
427:
428: Set definedCategoryIds = activityManager
429: .getDefinedCategoryIds();
430: Set enabledCategories = new HashSet();
431: for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) {
432: String categoryId = (String) i.next();
433: if (isEnabled(activityManager, categoryId)) {
434: enabledCategories.add(categoryId);
435: }
436: }
437: return enabledCategories;
438: }
439:
440: /**
441: * Return the set of partially enabled categories.
442: *
443: * @param activityManager
444: * the activity manager to test against
445: * @return the set of partially enabled categories
446: * @since 3.2
447: */
448: public static Set getPartiallyEnabledCategories(
449: IActivityManager activityManager) {
450: Set definedCategoryIds = activityManager
451: .getDefinedCategoryIds();
452: Set partialCategories = new HashSet();
453: for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) {
454: String categoryId = (String) i.next();
455: if (isPartiallyEnabled(activityManager, categoryId)) {
456: partialCategories.add(categoryId);
457: }
458: }
459:
460: return partialCategories;
461: }
462:
463: /**
464: * Returns whether the given category is partially enabled. A partially
465: * enabled category is one in which the number of enabled activites is both
466: * non-zero and less than the total number of activities in the category.
467: *
468: * @param activityManager
469: * the activity manager to test against
470: * @param categoryId
471: * the category id
472: * @return whether the category is enabled
473: * @since 3.2
474: */
475: public static boolean isPartiallyEnabled(
476: IActivityManager activityManager, String categoryId) {
477: Set activityIds = getActivityIdsForCategory(activityManager
478: .getCategory(categoryId));
479: int foundCount = 0;
480: for (Iterator i = activityIds.iterator(); i.hasNext();) {
481: String activityId = (String) i.next();
482: if (activityManager.getEnabledActivityIds().contains(
483: activityId)) {
484: foundCount++;
485: }
486: }
487:
488: return foundCount > 0 && foundCount != activityIds.size();
489: }
490:
491: /**
492: * Return the number of enabled categories that this activity belongs to.
493: *
494: * @param activityManager
495: * the activity manager to test against *
496: * @param activityId
497: * the activity id to query on
498: * @return the set of enabled category ids that this activity belongs to
499: * @since 3.1
500: */
501: public static Set getEnabledCategoriesForActivity(
502: IActivityManager activityManager, String activityId) {
503: Set enabledCategoriesForActivity = new HashSet();
504: Set enabledCategories = getEnabledCategories(activityManager);
505: for (Iterator i = enabledCategories.iterator(); i.hasNext();) {
506: String categoryId = (String) i.next();
507: if (getActivityIdsForCategory(
508: activityManager.getCategory(categoryId)).contains(
509: activityId)) {
510: enabledCategoriesForActivity.add(categoryId);
511: }
512: }
513: return enabledCategoriesForActivity;
514: }
515:
516: /**
517: * Returns whether the given category is enabled. A category is enabled if
518: * all of its activities are enabled.
519: *
520: * @param activityManager
521: * the activity manager to test against
522: * @param categoryId
523: * the category id
524: * @return whether the category is enabled
525: * @since 3.1
526: */
527: public static boolean isEnabled(IActivityManager activityManager,
528: String categoryId) {
529:
530: ICategory category = activityManager.getCategory(categoryId);
531: if (category.isDefined()) {
532: Set activityIds = getActivityIdsForCategory(category);
533: if (activityManager.getEnabledActivityIds().containsAll(
534: activityIds)) {
535: return true;
536: }
537: }
538:
539: return false;
540: }
541:
542: /**
543: * Resolve the collection of category ids to an array of
544: * <code>ICategory</code> objects.
545: *
546: * @param activityManager
547: * the activity manager to test against
548: * @param categoryIds
549: * the category ids
550: * @return the array of category ids resolved to <code>ICategory</code>
551: * objects
552: * @since 3.1
553: */
554: public static ICategory[] resolveCategories(
555: IMutableActivityManager activityManager, Set categoryIds) {
556: ICategory[] categories = new ICategory[categoryIds.size()];
557: String[] categoryIdArray = (String[]) categoryIds
558: .toArray(new String[categoryIds.size()]);
559: for (int i = 0; i < categoryIdArray.length; i++) {
560: categories[i] = activityManager
561: .getCategory(categoryIdArray[i]);
562: }
563: return categories;
564: }
565:
566: /**
567: * Not intended to be instantiated.
568: */
569: private WorkbenchActivityHelper() {
570: // no-op
571: }
572: }
|