KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PartSite


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Dan Rubel (dan_rubel@instantiations.com) - accessor to get context menu ids
11  *******************************************************************************/

12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.IActionBars;
28 import org.eclipse.ui.IKeyBindingService;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchPartReference;
32 import org.eclipse.ui.IWorkbenchPartSite;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.SubActionBars;
35 import org.eclipse.ui.commands.ICommandService;
36 import org.eclipse.ui.contexts.IContextService;
37 import org.eclipse.ui.handlers.IHandlerService;
38 import org.eclipse.ui.internal.commands.SlaveCommandService;
39 import org.eclipse.ui.internal.contexts.SlaveContextService;
40 import org.eclipse.ui.internal.expressions.ActivePartExpression;
41 import org.eclipse.ui.internal.handlers.SlaveHandlerService;
42 import org.eclipse.ui.internal.progress.WorkbenchSiteProgressService;
43 import org.eclipse.ui.internal.services.ServiceLocator;
44 import org.eclipse.ui.internal.testing.WorkbenchPartTestable;
45 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
46 import org.eclipse.ui.services.IServiceLocator;
47 import org.eclipse.ui.services.IServiceScopes;
48 import org.eclipse.ui.testing.IWorkbenchPartTestable;
49
50 /**
51  * <code>PartSite</code> is the general implementation for an
52  * <code>IWorkbenchPartSite</code>. A site maintains the context for a part,
53  * including the part, its pane, active contributions, selection provider, etc.
54  * Together, these components make up the complete behavior for a part as if it
55  * was implemented by one person.
56  *
57  * The <code>PartSite</code> lifecycle is as follows ..
58  *
59  * <ol>
60  * <li>a site is constructed </li>
61  * <li>a part is constructed and stored in the part </li>
62  * <li>the site calls part.init() </li>
63  * <li>a pane is constructed and stored in the site </li>
64  * <li>the action bars for a part are constructed and stored in the site </li>
65  * <li>the pane is added to a presentation </li>
66  * <li>the SWT widgets for the pane and part are created </li>
67  * <li>the site is activated, causing the actions to become visible </li>
68  * </ol>
69  */

70 public abstract class PartSite implements IWorkbenchPartSite {
71
72     /**
73      * This is a helper method for the register context menu functionality. It
74      * is provided so that different implementations of the
75      * <code>IWorkbenchPartSite</code> interface don't have to worry about how
76      * context menus should work.
77      *
78      * @param menuId
79      * the menu id
80      * @param menuManager
81      * the menu manager
82      * @param selectionProvider
83      * the selection provider
84      * @param includeEditorInput
85      * whether editor inputs should be included in the structured
86      * selection when calculating contributions
87      * @param part
88      * the part for this site
89      * @param menuExtenders
90      * the collection of menu extenders for this site
91      * @see IWorkbenchPartSite#registerContextMenu(MenuManager,
92      * ISelectionProvider)
93      */

94     public static final void registerContextMenu(final String JavaDoc menuId,
95             final MenuManager menuManager,
96             final ISelectionProvider selectionProvider,
97             final boolean includeEditorInput, final IWorkbenchPart part,
98             final Collection JavaDoc menuExtenders) {
99         /*
100          * Check to see if the same menu manager and selection provider have
101          * already been used. If they have, then we can just add another menu
102          * identifier to the existing PopupMenuExtender.
103          */

104         final Iterator JavaDoc extenderItr = menuExtenders.iterator();
105         boolean foundMatch = false;
106         while (extenderItr.hasNext()) {
107             final PopupMenuExtender existingExtender = (PopupMenuExtender) extenderItr
108                     .next();
109             if (existingExtender.matches(menuManager, selectionProvider, part)) {
110                 existingExtender.addMenuId(menuId);
111                 foundMatch = true;
112                 break;
113             }
114         }
115
116         if (!foundMatch) {
117             menuExtenders.add(new PopupMenuExtender(menuId, menuManager,
118                     selectionProvider, part, includeEditorInput));
119         }
120     }
121
122     private IWorkbenchPartReference partReference;
123
124     private IWorkbenchPart part;
125
126     private IWorkbenchPage page;
127
128     private String JavaDoc extensionID;
129
130     private String JavaDoc pluginID;
131
132     private String JavaDoc extensionName;
133
134     private ISelectionProvider selectionProvider;
135
136     private SubActionBars actionBars;
137
138     private KeyBindingService keyBindingService;
139
140     protected ArrayList JavaDoc menuExtenders;
141
142     private WorkbenchSiteProgressService progressService;
143
144     protected final ServiceLocator serviceLocator;
145
146     /**
147      * Build the part site.
148      *
149      * @param ref
150      * the part reference
151      * @param part
152      * the part
153      * @param page
154      * the page it belongs to
155      */

156     public PartSite(IWorkbenchPartReference ref, IWorkbenchPart part,
157             IWorkbenchPage page) {
158         this.partReference = ref;
159         this.part = part;
160         this.page = page;
161         extensionID = "org.eclipse.ui.UnknownID"; //$NON-NLS-1$
162
extensionName = "Unknown Name"; //$NON-NLS-1$
163

164         // Initialize the service locator.
165
final IServiceLocator parentServiceLocator = page.getWorkbenchWindow();
166         this.serviceLocator = new ServiceLocator(parentServiceLocator);
167
168         initializeDefaultServices();
169     }
170
171     /**
172      * Initialize the local services.
173      */

174     private void initializeDefaultServices() {
175         serviceLocator.registerService(IWorkbenchPartSite.class, this);
176         final IHandlerService parentService = (IHandlerService) serviceLocator
177                 .getService(IHandlerService.class);
178         final Expression defaultExpression = new ActivePartExpression(part);
179         final IHandlerService slave = new SlaveHandlerService(parentService,
180                 defaultExpression);
181         serviceLocator.registerService(IHandlerService.class, slave);
182
183         final IContextService parentContextService = (IContextService) serviceLocator
184                 .getService(IContextService.class);
185         final IContextService contextService = new SlaveContextService(
186                 parentContextService, defaultExpression);
187         serviceLocator.registerService(IContextService.class, contextService);
188
189         final ICommandService parentCommandService = (ICommandService) serviceLocator
190                 .getService(ICommandService.class);
191         final ICommandService commandService = new SlaveCommandService(
192                 parentCommandService, IServiceScopes.PARTSITE_SCOPE,
193                 this);
194         serviceLocator.registerService(ICommandService.class, commandService);
195     }
196
197     /**
198      * Dispose the contributions.
199      */

200     public void dispose() {
201         if (menuExtenders != null) {
202             HashSet JavaDoc managers = new HashSet JavaDoc(menuExtenders.size());
203             for (int i = 0; i < menuExtenders.size(); i++) {
204                 PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i);
205                 managers.add(ext.getManager());
206                 ext.dispose();
207             }
208             if (managers.size()>0) {
209                 for (Iterator JavaDoc iterator = managers.iterator(); iterator
210                         .hasNext();) {
211                     MenuManager mgr = (MenuManager) iterator.next();
212                     mgr.dispose();
213                 }
214             }
215             menuExtenders = null;
216         }
217
218          if (keyBindingService != null) {
219             keyBindingService.dispose();
220          }
221
222         if (progressService != null) {
223             progressService.dispose();
224         }
225
226         if (serviceLocator != null) {
227             serviceLocator.dispose();
228         }
229     }
230
231     /**
232      * Returns the action bars for the part. If this part is a view then it has
233      * exclusive use of the action bars. If this part is an editor then the
234      * action bars are shared among this editor and other editors of the same
235      * type.
236      */

237     public IActionBars getActionBars() {
238         return actionBars;
239     }
240
241     /**
242      * Returns the part registry extension ID.
243      *
244      * @return the registry extension ID
245      */

246     public String JavaDoc getId() {
247         return extensionID;
248     }
249
250     /**
251      * Returns the page containing this workbench site's part.
252      *
253      * @return the page containing this part
254      */

255     public IWorkbenchPage getPage() {
256         return page;
257     }
258
259     /**
260      * Gets the part pane.
261      */

262     public PartPane getPane() {
263         return ((WorkbenchPartReference) partReference).getPane();
264     }
265
266     /**
267      * Returns the part.
268      */

269     public IWorkbenchPart getPart() {
270         return part;
271     }
272
273     /**
274      * Returns the part reference.
275      */

276     public IWorkbenchPartReference getPartReference() {
277         return partReference;
278     }
279
280     /**
281      * Returns the part registry plugin ID. It cannot be <code>null</code>.
282      *
283      * @return the registry plugin ID
284      */

285     public String JavaDoc getPluginId() {
286         return pluginID;
287     }
288
289     /**
290      * Returns the registered name for this part.
291      */

292     public String JavaDoc getRegisteredName() {
293         return extensionName;
294     }
295
296     /**
297      * Returns the selection provider for a part.
298      */

299     public ISelectionProvider getSelectionProvider() {
300         return selectionProvider;
301     }
302
303     /**
304      * Returns the shell containing this part.
305      *
306      * @return the shell containing this part
307      */

308     public Shell getShell() {
309         PartPane pane = getPane();
310
311         // Compatibility: This method should not be used outside the UI
312
// thread... but since this condition
313
// was not always in the JavaDoc, we still try to return our best guess
314
// about the shell if it is
315
// called from the wrong thread.
316
Display currentDisplay = Display.getCurrent();
317         if (currentDisplay == null
318                 || currentDisplay != getWorkbenchWindow().getWorkbench()
319                         .getDisplay()) {
320             // Uncomment this to locate places that try to access the shell from
321
// a background thread
322
// WorkbenchPlugin.log(new Exception("Error:
323
// IWorkbenchSite.getShell() was called outside the UI thread. Fix
324
// this code.")); //$NON-NLS-1$
325

326             return getWorkbenchWindow().getShell();
327         }
328
329         if (pane == null) {
330             return getWorkbenchWindow().getShell();
331         }
332
333         Shell s = pane.getShell();
334
335         if (s == null) {
336             return getWorkbenchWindow().getShell();
337         }
338
339         return s;
340     }
341
342     /**
343      * Returns the workbench window containing this part.
344      *
345      * @return the workbench window containing this part
346      */

347     public IWorkbenchWindow getWorkbenchWindow() {
348         return page.getWorkbenchWindow();
349     }
350
351     /**
352      * Register a popup menu for extension.
353      */

354     public void registerContextMenu(String JavaDoc menuID, MenuManager menuMgr,
355             ISelectionProvider selProvider) {
356         if (menuExtenders == null) {
357             menuExtenders = new ArrayList JavaDoc(1);
358         }
359
360         registerContextMenu(menuID, menuMgr, selProvider, true, getPart(),
361                 menuExtenders);
362     }
363
364     /**
365      * Register a popup menu with the default id for extension.
366      */

367     public void registerContextMenu(MenuManager menuMgr,
368             ISelectionProvider selProvider) {
369         registerContextMenu(getId(), menuMgr, selProvider);
370     }
371
372     // getContextMenuIds() added by Dan Rubel (dan_rubel@instantiations.com)
373
/**
374      * Get the registered popup menu identifiers
375      */

376     public String JavaDoc[] getContextMenuIds() {
377         if (menuExtenders == null) {
378             return new String JavaDoc[0];
379         }
380         ArrayList JavaDoc menuIds = new ArrayList JavaDoc(menuExtenders.size());
381         for (Iterator JavaDoc iter = menuExtenders.iterator(); iter.hasNext();) {
382             final PopupMenuExtender extender = (PopupMenuExtender) iter.next();
383             menuIds.addAll(extender.getMenuIds());
384         }
385         return (String JavaDoc[]) menuIds.toArray(new String JavaDoc[menuIds.size()]);
386     }
387
388     /**
389      * Sets the action bars for the part.
390      */

391     public void setActionBars(SubActionBars bars) {
392         actionBars = bars;
393     }
394
395     /**
396      * Sets the configuration element for a part.
397      */

398     public void setConfigurationElement(IConfigurationElement configElement) {
399
400         // Get extension ID.
401
extensionID = configElement.getAttribute("id"); //$NON-NLS-1$
402

403         // Get plugin ID.
404
pluginID = configElement.getNamespace();
405
406         // Get extension name.
407
String JavaDoc name = configElement.getAttribute("name"); //$NON-NLS-1$
408
if (name != null) {
409             extensionName = name;
410         }
411     }
412
413     protected void setPluginId(String JavaDoc pluginId) {
414         this.pluginID = pluginId;
415     }
416
417     /**
418      * Sets the part registry extension ID.
419      *
420      * @param id
421      * the registry extension ID
422      */

423     protected void setId(String JavaDoc id) {
424         extensionID = id;
425     }
426
427     /**
428      * Sets the part.
429      */

430     public void setPart(IWorkbenchPart newPart) {
431         part = newPart;
432     }
433
434     /**
435      * Sets the registered name for this part.
436      *
437      * @param name
438      * the registered name
439      */

440     protected void setRegisteredName(String JavaDoc name) {
441         extensionName = name;
442     }
443
444     /**
445      * Set the selection provider for a part.
446      */

447     public void setSelectionProvider(ISelectionProvider provider) {
448         selectionProvider = provider;
449     }
450
451     /*
452      * @see IWorkbenchPartSite#getKeyBindingService()
453      */

454     public IKeyBindingService getKeyBindingService() {
455         if (keyBindingService == null) {
456             keyBindingService = new KeyBindingService(this);
457
458             // TODO why is this here? and it should be using HandlerSubmissions
459
// directly..
460
if (this instanceof EditorSite) {
461                 EditorActionBuilder.ExternalContributor contributor = (EditorActionBuilder.ExternalContributor) ((EditorSite) this)
462                         .getExtensionActionBarContributor();
463
464                 if (contributor != null) {
465                     ActionDescriptor[] actionDescriptors = contributor
466                             .getExtendedActions();
467
468                     if (actionDescriptors != null) {
469                         for (int i = 0; i < actionDescriptors.length; i++) {
470                             ActionDescriptor actionDescriptor = actionDescriptors[i];
471
472                             if (actionDescriptor != null) {
473                                 IAction action = actionDescriptors[i]
474                                         .getAction();
475
476                                 if (action != null
477                                         && action.getActionDefinitionId() != null) {
478                                     keyBindingService.registerAction(action);
479                                 }
480                             }
481                         }
482                     }
483                 }
484             }
485         }
486
487         return keyBindingService;
488     }
489
490     protected String JavaDoc getInitialScopeId() {
491         return null;
492     }
493
494     /**
495      * Get an adapter for this type.
496      *
497      * @param adapter
498      * @return
499      */

500     public final Object JavaDoc getAdapter(Class JavaDoc adapter) {
501
502         if (IWorkbenchSiteProgressService.class == adapter) {
503             return getSiteProgressService();
504         }
505         
506         if (IWorkbenchPartTestable.class == adapter) {
507             return new WorkbenchPartTestable(this);
508         }
509
510         return Platform.getAdapterManager().getAdapter(this, adapter);
511     }
512
513     public void activateActionBars(boolean forceVisibility) {
514         if (actionBars != null) {
515             actionBars.activate(forceVisibility);
516         }
517     }
518
519     public void deactivateActionBars(boolean forceHide) {
520         if (actionBars != null) {
521             actionBars.deactivate(forceHide);
522         }
523     }
524
525     /**
526      * Get a progress service for the receiver.
527      *
528      * @return WorkbenchSiteProgressService
529      */

530     private WorkbenchSiteProgressService getSiteProgressService() {
531         if (progressService == null) {
532             progressService = new WorkbenchSiteProgressService(this);
533         }
534         return progressService;
535     }
536
537     public final Object JavaDoc getService(final Class JavaDoc key) {
538         return serviceLocator.getService(key);
539     }
540
541     public final boolean hasService(final Class JavaDoc key) {
542         return serviceLocator.hasService(key);
543     }
544
545     /**
546      * Prints out the identifier, the plug-in identifier and the registered
547      * name. This is for debugging purposes only.
548      *
549      * @since 3.2
550      */

551     public String JavaDoc toString() {
552         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
553         buffer.append("PartSite(id="); //$NON-NLS-1$
554
buffer.append(getId());
555         buffer.append(",pluginId="); //$NON-NLS-1$
556
buffer.append(getPluginId());
557         buffer.append(",registeredName="); //$NON-NLS-1$
558
buffer.append(getRegisteredName());
559         buffer.append(",hashCode="); //$NON-NLS-1$
560
buffer.append(hashCode());
561         buffer.append(')');
562         return buffer.toString();
563     }
564 }
565
Popular Tags