KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ws > WorkbenchActivitySupport


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.internal.activities.ws;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
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 /**
61  * Implementation of {@link org.eclipse.ui.activities.IWorkbenchActivitySupport}.
62  * @since 3.0
63  */

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     /**
78      * Create a new instance of this class.
79      */

80     public WorkbenchActivitySupport() {
81         mutableActivityManager = ActivityManagerFactory
82                 .getMutableActivityManager();
83         proxyActivityManager = new ProxyActivityManager(mutableActivityManager);
84         mutableActivityManager
85                 .addActivityManagerListener(new IActivityManagerListener() {
86
87                     private Set JavaDoc lastEnabled = new HashSet JavaDoc(
88                             mutableActivityManager.getEnabledActivityIds());
89
90                     /* (non-Javadoc)
91                      * @see org.eclipse.ui.activities.IActivityManagerListener#activityManagerChanged(org.eclipse.ui.activities.ActivityManagerEvent)
92                      */

93                     public void activityManagerChanged(
94                             ActivityManagerEvent activityManagerEvent) {
95                         Set JavaDoc activityIds = mutableActivityManager
96                                 .getEnabledActivityIds();
97                         // only update the windows if we've not processed this new enablement state already.
98
if (!activityIds.equals(lastEnabled)) {
99                             lastEnabled = new HashSet JavaDoc(activityIds);
100
101                             // abort if the workbench isn't running
102
if (!PlatformUI.isWorkbenchRunning()) {
103                                 return;
104                             }
105
106                             // refresh the managers on all windows.
107
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                                         /**
121                                          * When this operation should open a dialog
122                                          */

123                                         private long openTime;
124
125                                         /**
126                                          * Whether the dialog has been opened yet.
127                                          */

128                                         private boolean dialogOpened = false;
129
130                                         /* (non-Javadoc)
131                                          * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
132                                          */

133                                         public void run(IProgressMonitor monitor) {
134
135                                             openTime = System
136                                                     .currentTimeMillis()
137                                                     + workbench
138                                                             .getProgressService()
139                                                             .getLongOperationTime();
140
141                                             //two work units - updating the window bars, and updating view bars
142
monitor
143                                                     .beginTask(
144                                                             ActivityMessages.ManagerTask, 2);
145
146                                             monitor
147                                                     .subTask(ActivityMessages.ManagerWindowSubTask);
148
149                                             //update window managers...
150
updateWindowBars(window);
151                                             monitor.worked(1);
152
153                                             monitor
154                                                     .subTask(ActivityMessages.ManagerViewsSubTask);
155                                             // update all of the (realized) views in all of the pages
156
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                                         /**
176                                          * Update the managers on the given given view.
177                                          *
178                                          * @param part the view to update
179                                          */

180                                         private void updateViewBars(
181                                                 IViewPart part) {
182                                             IViewSite viewSite = part
183                                                     .getViewSite();
184                                             // check for badly behaving or badly initialized views
185
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                                         /**
207                                          * Update the managers on the given window.
208                                          *
209                                          * @param window the window to update
210                                          */

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                                         /**
236                                          * Update the given manager in the UI thread.
237                                          * This may also open the progress dialog if
238                                          * the operation is taking too long.
239                                          *
240                                          * @param manager the manager to update
241                                          */

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                                     // don't open the dialog by default - that'll be
256
// handled by the runnable if we take too long
257
dialog.setOpenOnRun(false);
258                                     // run this in the UI thread
259
workbench.getDisplay().asyncExec(
260                                             new Runnable JavaDoc() {
261
262                                                 /* (non-Javadoc)
263                                                  * @see java.lang.Runnable#run()
264                                                  */

265                                                 public void run() {
266                                                     BusyIndicator
267                                                             .showWhile(
268                                                                     workbench
269                                                                             .getDisplay(),
270                                                                     new Runnable JavaDoc() {
271
272                                                                         /* (non-Javadoc)
273                                                                          * @see java.lang.Runnable#run()
274                                                                          */

275                                                                         public void run() {
276                                                                             try {
277                                                                                 dialog
278                                                                                         .run(
279                                                                                                 false,
280                                                                                                 false,
281                                                                                                 runnable);
282                                                                             } catch (InvocationTargetException JavaDoc e) {
283                                                                                 log(e);
284                                                                             } catch (InterruptedException JavaDoc e) {
285                                                                                 log(e);
286                                                                             }
287                                                                         }
288                                                                     });
289                                                 }
290                                             });
291                                 }
292                             }
293                         }
294                     }
295
296                     /**
297                      * Logs an error message to the workbench log.
298                      *
299                      * @param e the exception to log
300                      */

301                     private void log(Exception JavaDoc e) {
302                         StatusUtil.newStatus(IStatus.ERROR,
303                                 "Could not update contribution managers", e); //$NON-NLS-1$
304
}
305                 });
306         triggerPointManager = new TriggerPointManager();
307         IExtensionTracker tracker = PlatformUI.getWorkbench().getExtensionTracker();
308         tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getActivitySupportExtensionPoint()));
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#getActivityManager()
313      */

314     public IActivityManager getActivityManager() {
315         return proxyActivityManager;
316     }
317
318     /* (non-Javadoc)
319      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#setEnabledActivityIds(java.util.Set)
320      */

321     public void setEnabledActivityIds(Set JavaDoc enabledActivityIds) {
322         mutableActivityManager.setEnabledActivityIds(enabledActivityIds);
323     }
324
325     /* (non-Javadoc)
326      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#getImageDescriptor(org.eclipse.ui.activities.IActivity)
327      */

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     /* (non-Javadoc)
341      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#getImageDescriptor(org.eclipse.ui.activities.ICategory)
342      */

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     /**
357      * Return the activity image registry.
358      *
359      * @return the activity image registry
360      * @since 3.1
361      */

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     /**
377      * Return the category image registry.
378      *
379      * @return the category image registry
380      * @since 3.1
381      */

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     /**
397      * Dispose of the image registries.
398      *
399      * @since 3.1
400      */

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     /**
415      * Return the trigger point advisor.
416      *
417      * TODO: should this be part of the interface?
418      *
419      * @return the trigger point advisor
420      * @since 3.1
421      */

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); //$NON-NLS-1$
436
}
437             }
438         }
439         
440         if (advisor == null) {
441             advisor = new WorkbenchTriggerPointAdvisor();
442         }
443         
444         return advisor;
445     }
446
447     /* (non-Javadoc)
448      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#getTriggerPointManager()
449      */

450     public ITriggerPointManager getTriggerPointManager() {
451         return triggerPointManager;
452     }
453
454     /* (non-Javadoc)
455      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
456      */

457     public void addExtension(IExtensionTracker tracker, IExtension extension) {
458         // reset the advisor if it's the "default" advisor.
459
// this will give getAdvisor the chance to find a proper trigger/binding if
460
// it exists.
461
if (advisor != null && advisor.getClass().equals(WorkbenchTriggerPointAdvisor.class)) {
462             advisor = null;
463         }
464     }
465
466     /**
467      * Return the activity support extension point.
468      *
469      * @return the activity support extension point.
470      * @since 3.1
471      */

472     private IExtensionPoint getActivitySupportExtensionPoint() {
473         return Platform.getExtensionRegistry().getExtensionPoint(
474                 PlatformUI.PLUGIN_ID, IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
475     }
476
477     /* (non-Javadoc)
478      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
479      */

480     public void removeExtension(IExtension extension, Object JavaDoc[] 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     /* (non-Javadoc)
490      * @see org.eclipse.ui.activities.IWorkbenchActivitySupport#createWorkingCopy()
491      */

492     public IMutableActivityManager createWorkingCopy() {
493         MutableActivityManager clone = (MutableActivityManager) mutableActivityManager.clone();
494         clone.unhookRegistryListeners();
495         return clone;
496     }
497 }
498
Popular Tags