1 11 package org.eclipse.ui.activities; 12 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Set ; 18 19 import org.eclipse.ui.IPluginContribution; 20 import org.eclipse.ui.PlatformUI; 21 import org.eclipse.ui.internal.activities.ws.WorkbenchActivitySupport; 22 23 29 public final class WorkbenchActivityHelper { 30 31 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 61 public static boolean allowUseOf(Object object) { 62 return allowUseOf(PlatformUI.getWorkbench().getActivitySupport() 63 .getTriggerPointManager().getTriggerPoint( 64 ITriggerPointManager.UNKNOWN_TRIGGER_POINT_ID), object); 65 } 66 67 81 public static boolean allowUseOf(ITriggerPoint triggerPoint, Object 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 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 activitiesToEnable = advisor.allow(triggerPoint, identifier); 116 if (activitiesToEnable == null) { 117 return false; 118 } 119 120 enableActivities(activitiesToEnable); 121 return true; 122 } 123 124 137 public static final String createUnifiedId(IPluginContribution contribution) { 138 if (contribution.getPluginId() != null) { 139 return contribution.getPluginId() + '/' + contribution.getLocalId(); 140 } 141 return contribution.getLocalId(); 142 } 143 144 150 private static void enableActivities(Collection activities) { 151 IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench() 152 .getActivitySupport(); 153 Set newSet = new HashSet (activitySupport.getActivityManager() 154 .getEnabledActivityIds()); 155 newSet.addAll(activities); 156 activitySupport.setEnabledActivityIds(newSet); 157 } 158 159 170 public static final boolean filterItem(Object 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 191 public static final boolean isFiltering() { 192 return !PlatformUI.getWorkbench().getActivitySupport() 193 .getActivityManager().getDefinedActivityIds().isEmpty(); 194 } 195 196 210 public static Set getEnabledCategories(IActivityManager activityManager, 211 String categoryId) { 212 ICategory category = activityManager.getCategory(categoryId); 213 if (!category.isDefined()) { 214 return Collections.EMPTY_SET; 215 } 216 217 Set activities = expandActivityDependencies(getActivityIdsForCategory(category)); 218 Set otherEnabledCategories = new HashSet (); 219 Set definedCategoryIds = activityManager.getDefinedCategoryIds(); 220 for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) { 221 String otherCategoryId = (String ) i.next(); 222 if (otherCategoryId.equals(categoryId)) { 223 continue; 224 } 225 ICategory otherCategory = activityManager 226 .getCategory(otherCategoryId); 227 Set 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 249 public static Set expandActivityDependencies(Set baseActivities) { 250 Set extendedActivities = new HashSet (); 251 for (Iterator i = baseActivities.iterator(); i.hasNext();) { 252 String activityId = (String ) i.next(); 253 Set requiredActivities = getRequiredActivityIds(activityId); 254 extendedActivities.addAll(requiredActivities); 255 } 256 extendedActivities.addAll(baseActivities); 257 return extendedActivities; 258 } 259 260 268 public static Set getRequiredActivityIds(String 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 requirementBindings = activity.getActivityRequirementBindings(); 276 if (requirementBindings.isEmpty()) { 277 return Collections.EMPTY_SET; 278 } 279 280 Set requiredActivities = new HashSet (3); 281 for (Iterator 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 299 public static Set getActivityIdsForCategory(ICategory category) { 300 Set bindings = category.getCategoryActivityBindings(); 301 Set activityIds = new HashSet (); 302 for (Iterator i = bindings.iterator(); i.hasNext();) { 303 ICategoryActivityBinding binding = (ICategoryActivityBinding) i 304 .next(); 305 activityIds.add(binding.getActivityId()); 306 } 307 return activityIds; 308 } 309 310 324 public static Set getDisabledCategories(IActivityManager activityManager, 325 String categoryId) { 326 ICategory category = activityManager.getCategory(categoryId); 327 if (!category.isDefined()) { 328 return Collections.EMPTY_SET; 329 } 330 331 Set activities = expandActivityDependencies(getActivityIdsForCategory(category)); 332 Set otherDisabledCategories = new HashSet (); 333 Set definedCategoryIds = activityManager.getDefinedCategoryIds(); 334 for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) { 335 String otherCategoryId = (String ) i.next(); 336 if (otherCategoryId.equals(categoryId)) { 337 continue; 338 } 339 ICategory otherCategory = activityManager 340 .getCategory(otherCategoryId); 341 Set 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 372 public static final Set getContainedCategories( 373 IActivityManager activityManager, String categoryId) { 374 ICategory category = activityManager.getCategory(categoryId); 375 if (!category.isDefined()) { 376 return Collections.EMPTY_SET; 377 } 378 379 Set activities = expandActivityDependencies(getActivityIdsForCategory(category)); 380 Set containedCategories = new HashSet (); 381 Set definedCategoryIds = activityManager.getDefinedCategoryIds(); 382 for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) { 383 String otherCategoryId = (String ) i.next(); 384 if (otherCategoryId.equals(categoryId)) { 385 continue; 386 } 387 ICategory otherCategory = activityManager 388 .getCategory(otherCategoryId); 389 Set 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 413 public static Set getEnabledCategories(IActivityManager activityManager) { 414 415 Set definedCategoryIds = activityManager.getDefinedCategoryIds(); 416 Set enabledCategories = new HashSet (); 417 for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) { 418 String categoryId = (String ) i.next(); 419 if (isEnabled(activityManager, categoryId)) { 420 enabledCategories.add(categoryId); 421 } 422 } 423 return enabledCategories; 424 } 425 426 434 public static Set getPartiallyEnabledCategories( 435 IActivityManager activityManager) { 436 Set definedCategoryIds = activityManager.getDefinedCategoryIds(); 437 Set partialCategories = new HashSet (); 438 for (Iterator i = definedCategoryIds.iterator(); i.hasNext();) { 439 String categoryId = (String ) i.next(); 440 if (isPartiallyEnabled(activityManager, categoryId)) { 441 partialCategories.add(categoryId); 442 } 443 } 444 445 return partialCategories; 446 } 447 448 460 public static boolean isPartiallyEnabled(IActivityManager activityManager, 461 String categoryId) { 462 Set activityIds = getActivityIdsForCategory(activityManager 463 .getCategory(categoryId)); 464 int foundCount = 0; 465 for (Iterator i = activityIds.iterator(); i.hasNext();) { 466 String activityId = (String ) i.next(); 467 if (activityManager.getEnabledActivityIds().contains(activityId)) { 468 foundCount++; 469 } 470 } 471 472 return foundCount > 0 && foundCount != activityIds.size(); 473 } 474 475 485 public static Set getEnabledCategoriesForActivity( 486 IActivityManager activityManager, String activityId) { 487 Set enabledCategoriesForActivity = new HashSet (); 488 Set enabledCategories = getEnabledCategories(activityManager); 489 for (Iterator i = enabledCategories.iterator(); i.hasNext();) { 490 String categoryId = (String ) i.next(); 491 if (getActivityIdsForCategory( 492 activityManager.getCategory(categoryId)).contains( 493 activityId)) { 494 enabledCategoriesForActivity.add(categoryId); 495 } 496 } 497 return enabledCategoriesForActivity; 498 } 499 500 511 public static boolean isEnabled(IActivityManager activityManager, 512 String categoryId) { 513 514 ICategory category = activityManager.getCategory(categoryId); 515 if (category.isDefined()) { 516 Set activityIds = getActivityIdsForCategory(category); 517 if (activityManager.getEnabledActivityIds().containsAll(activityIds)) { 518 return true; 519 } 520 } 521 522 return false; 523 } 524 525 537 public static ICategory[] resolveCategories( 538 IMutableActivityManager activityManager, Set categoryIds) { 539 ICategory[] categories = new ICategory[categoryIds.size()]; 540 String [] categoryIdArray = (String []) categoryIds 541 .toArray(new String [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 551 private WorkbenchActivityHelper() { 552 } 554 } 555 | Popular Tags |