1 11 package org.eclipse.ui.internal.activities.ws; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IExtensionPoint; 20 import org.eclipse.core.runtime.IProduct; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Platform; 24 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; 25 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 26 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 27 import org.eclipse.jface.action.IContributionManager; 28 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.swt.custom.BusyIndicator; 32 import org.eclipse.ui.IActionBars; 33 import org.eclipse.ui.IViewPart; 34 import org.eclipse.ui.IViewReference; 35 import org.eclipse.ui.IViewSite; 36 import org.eclipse.ui.IWorkbench; 37 import org.eclipse.ui.IWorkbenchPage; 38 import org.eclipse.ui.IWorkbenchWindow; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.activities.ActivityManagerEvent; 41 import org.eclipse.ui.activities.IActivity; 42 import org.eclipse.ui.activities.IActivityManager; 43 import org.eclipse.ui.activities.IActivityManagerListener; 44 import org.eclipse.ui.activities.ICategory; 45 import org.eclipse.ui.activities.IMutableActivityManager; 46 import org.eclipse.ui.activities.ITriggerPointAdvisor; 47 import org.eclipse.ui.activities.ITriggerPointManager; 48 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 49 import org.eclipse.ui.activities.WorkbenchTriggerPointAdvisor; 50 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 51 import org.eclipse.ui.internal.WorkbenchImages; 52 import org.eclipse.ui.internal.WorkbenchPlugin; 53 import org.eclipse.ui.internal.WorkbenchWindow; 54 import org.eclipse.ui.internal.activities.ActivityManagerFactory; 55 import org.eclipse.ui.internal.activities.MutableActivityManager; 56 import org.eclipse.ui.internal.activities.ProxyActivityManager; 57 import org.eclipse.ui.internal.misc.StatusUtil; 58 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 59 60 64 public class WorkbenchActivitySupport implements IWorkbenchActivitySupport, IExtensionChangeHandler { 65 private MutableActivityManager mutableActivityManager; 66 67 private ProxyActivityManager proxyActivityManager; 68 69 private ImageBindingRegistry activityImageBindingRegistry; 70 71 private ImageBindingRegistry categoryImageBindingRegistry; 72 73 private ITriggerPointManager triggerPointManager; 74 75 private ITriggerPointAdvisor advisor; 76 77 80 public WorkbenchActivitySupport() { 81 mutableActivityManager = ActivityManagerFactory 82 .getMutableActivityManager(); 83 proxyActivityManager = new ProxyActivityManager(mutableActivityManager); 84 mutableActivityManager 85 .addActivityManagerListener(new IActivityManagerListener() { 86 87 private Set lastEnabled = new HashSet ( 88 mutableActivityManager.getEnabledActivityIds()); 89 90 93 public void activityManagerChanged( 94 ActivityManagerEvent activityManagerEvent) { 95 Set activityIds = mutableActivityManager 96 .getEnabledActivityIds(); 97 if (!activityIds.equals(lastEnabled)) { 99 lastEnabled = new HashSet (activityIds); 100 101 if (!PlatformUI.isWorkbenchRunning()) { 103 return; 104 } 105 106 final IWorkbench workbench = PlatformUI 108 .getWorkbench(); 109 IWorkbenchWindow[] windows = workbench 110 .getWorkbenchWindows(); 111 for (int i = 0; i < windows.length; i++) { 112 if (windows[i] instanceof WorkbenchWindow) { 113 final WorkbenchWindow window = (WorkbenchWindow) windows[i]; 114 115 final ProgressMonitorDialog dialog = new ProgressMonitorDialog( 116 window.getShell()); 117 118 final IRunnableWithProgress runnable = new IRunnableWithProgress() { 119 120 123 private long openTime; 124 125 128 private boolean dialogOpened = false; 129 130 133 public void run(IProgressMonitor monitor) { 134 135 openTime = System 136 .currentTimeMillis() 137 + workbench 138 .getProgressService() 139 .getLongOperationTime(); 140 141 monitor 143 .beginTask( 144 ActivityMessages.ManagerTask, 2); 145 146 monitor 147 .subTask(ActivityMessages.ManagerWindowSubTask); 148 149 updateWindowBars(window); 151 monitor.worked(1); 152 153 monitor 154 .subTask(ActivityMessages.ManagerViewsSubTask); 155 IWorkbenchPage[] pages = window 157 .getPages(); 158 for (int j = 0; j < pages.length; j++) { 159 IWorkbenchPage page = pages[j]; 160 IViewReference[] refs = page 161 .getViewReferences(); 162 for (int k = 0; k < refs.length; k++) { 163 IViewPart part = refs[k] 164 .getView(false); 165 if (part != null) { 166 updateViewBars(part); 167 } 168 } 169 } 170 monitor.worked(1); 171 172 monitor.done(); 173 } 174 175 180 private void updateViewBars( 181 IViewPart part) { 182 IViewSite viewSite = part 183 .getViewSite(); 184 if (viewSite == null) { 186 return; 187 } 188 IActionBars bars = viewSite 189 .getActionBars(); 190 IContributionManager manager = bars 191 .getMenuManager(); 192 if (manager != null) { 193 updateManager(manager); 194 } 195 manager = bars.getToolBarManager(); 196 if (manager != null) { 197 updateManager(manager); 198 } 199 manager = bars 200 .getStatusLineManager(); 201 if (manager != null) { 202 updateManager(manager); 203 } 204 } 205 206 211 private void updateWindowBars( 212 final WorkbenchWindow window) { 213 IContributionManager manager = window 214 .getMenuBarManager(); 215 if (manager != null) { 216 updateManager(manager); 217 } 218 manager = window 219 .getCoolBarManager2(); 220 if (manager != null) { 221 updateManager(manager); 222 } 223 manager = window 224 .getToolBarManager2(); 225 if (manager != null) { 226 updateManager(manager); 227 } 228 manager = window 229 .getStatusLineManager(); 230 if (manager != null) { 231 updateManager(manager); 232 } 233 } 234 235 242 private void updateManager( 243 final IContributionManager manager) { 244 if (!dialogOpened 245 && System 246 .currentTimeMillis() > openTime) { 247 dialog.open(); 248 dialogOpened = true; 249 } 250 251 manager.update(true); 252 } 253 }; 254 255 dialog.setOpenOnRun(false); 258 workbench.getDisplay().asyncExec( 260 new Runnable () { 261 262 265 public void run() { 266 BusyIndicator 267 .showWhile( 268 workbench 269 .getDisplay(), 270 new Runnable () { 271 272 275 public void run() { 276 try { 277 dialog 278 .run( 279 false, 280 false, 281 runnable); 282 } catch (InvocationTargetException e) { 283 log(e); 284 } catch (InterruptedException e) { 285 log(e); 286 } 287 } 288 }); 289 } 290 }); 291 } 292 } 293 } 294 } 295 296 301 private void log(Exception e) { 302 StatusUtil.newStatus(IStatus.ERROR, 303 "Could not update contribution managers", e); } 305 }); 306 triggerPointManager = new TriggerPointManager(); 307 IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker(); 308 tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getActivitySupportExtensionPoint())); 309 } 310 311 314 public IActivityManager getActivityManager() { 315 return proxyActivityManager; 316 } 317 318 321 public void setEnabledActivityIds(Set enabledActivityIds) { 322 mutableActivityManager.setEnabledActivityIds(enabledActivityIds); 323 } 324 325 328 public ImageDescriptor getImageDescriptor(IActivity activity) { 329 if (activity.isDefined()) { 330 ImageDescriptor descriptor = getActivityImageBindingRegistry() 331 .getImageDescriptor(activity.getId()); 332 if (descriptor != null) { 333 return descriptor; 334 } 335 } 336 return WorkbenchImages 337 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_ACTIVITY); 338 } 339 340 343 public ImageDescriptor getImageDescriptor(ICategory category) { 344 if (category.isDefined()) { 345 ImageDescriptor descriptor = getCategoryImageBindingRegistry() 346 .getImageDescriptor(category.getId()); 347 if (descriptor != null) { 348 return descriptor; 349 } 350 } 351 return WorkbenchImages 352 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_OBJ_ACTIVITY_CATEGORY); 353 } 354 355 356 362 private ImageBindingRegistry getActivityImageBindingRegistry() { 363 if (activityImageBindingRegistry == null) { 364 activityImageBindingRegistry = new ImageBindingRegistry(IWorkbenchRegistryConstants.TAG_ACTIVITY_IMAGE_BINDING); 365 PlatformUI 366 .getWorkbench() 367 .getExtensionTracker() 368 .registerHandler( 369 activityImageBindingRegistry, 370 ExtensionTracker 371 .createExtensionPointFilter(getActivitySupportExtensionPoint())); 372 } 373 return activityImageBindingRegistry; 374 } 375 376 382 private ImageBindingRegistry getCategoryImageBindingRegistry() { 383 if (categoryImageBindingRegistry == null) { 384 categoryImageBindingRegistry = new ImageBindingRegistry(IWorkbenchRegistryConstants.TAG_CATEGORY_IMAGE_BINDING); 385 PlatformUI 386 .getWorkbench() 387 .getExtensionTracker() 388 .registerHandler( 389 categoryImageBindingRegistry, 390 ExtensionTracker 391 .createExtensionPointFilter(getActivitySupportExtensionPoint())); 392 } 393 return categoryImageBindingRegistry; 394 } 395 396 401 public void dispose() { 402 if (activityImageBindingRegistry != null) { 403 activityImageBindingRegistry.dispose(); 404 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(activityImageBindingRegistry); 405 } 406 if (categoryImageBindingRegistry != null) { 407 categoryImageBindingRegistry.dispose(); 408 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(categoryImageBindingRegistry); 409 } 410 411 PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this); 412 } 413 414 422 public ITriggerPointAdvisor getTriggerPointAdvisor() { 423 if (advisor != null) { 424 return advisor; 425 } 426 427 IProduct product = Platform.getProduct(); 428 if (product != null) { 429 TriggerPointAdvisorDescriptor descriptor = TriggerPointAdvisorRegistry 430 .getInstance().getAdvisorForProduct(product.getId()); 431 if (descriptor != null) { 432 try { 433 advisor = descriptor.createAdvisor(); 434 } catch (CoreException e) { 435 WorkbenchPlugin.log("could not create trigger point advisor", e); } 437 } 438 } 439 440 if (advisor == null) { 441 advisor = new WorkbenchTriggerPointAdvisor(); 442 } 443 444 return advisor; 445 } 446 447 450 public ITriggerPointManager getTriggerPointManager() { 451 return triggerPointManager; 452 } 453 454 457 public void addExtension(IExtensionTracker tracker, IExtension extension) { 458 if (advisor != null && advisor.getClass().equals(WorkbenchTriggerPointAdvisor.class)) { 462 advisor = null; 463 } 464 } 465 466 472 private IExtensionPoint getActivitySupportExtensionPoint() { 473 return Platform.getExtensionRegistry().getExtensionPoint( 474 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT); 475 } 476 477 480 public void removeExtension(IExtension extension, Object [] objects) { 481 for (int i = 0; i < objects.length; i++) { 482 if (objects[i] == advisor) { 483 advisor = null; 484 break; 485 } 486 } 487 } 488 489 492 public IMutableActivityManager createWorkingCopy() { 493 MutableActivityManager clone = (MutableActivityManager) mutableActivityManager.clone(); 494 clone.unhookRegistryListeners(); 495 return clone; 496 } 497 } 498 | Popular Tags |