KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > PageSite


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  *******************************************************************************/

11 package org.eclipse.ui.part;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.expressions.Expression;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.action.MenuManager;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IActionBars;
24 import org.eclipse.ui.IViewSite;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.SubActionBars;
29 import org.eclipse.ui.commands.ICommandService;
30 import org.eclipse.ui.contexts.IContextService;
31 import org.eclipse.ui.handlers.IHandlerService;
32 import org.eclipse.ui.internal.PartSite;
33 import org.eclipse.ui.internal.PopupMenuExtender;
34 import org.eclipse.ui.internal.commands.SlaveCommandService;
35 import org.eclipse.ui.internal.contexts.NestableContextService;
36 import org.eclipse.ui.internal.expressions.ActivePartExpression;
37 import org.eclipse.ui.internal.handlers.NestableHandlerService;
38 import org.eclipse.ui.internal.services.INestable;
39 import org.eclipse.ui.internal.services.ServiceLocator;
40 import org.eclipse.ui.services.IServiceScopes;
41
42 /**
43  * This implementation of <code>IPageSite</code> provides a site for a page
44  * within a <code>PageBookView</code>. Most methods are forwarded to the
45  * view's site.
46  */

47 public class PageSite implements IPageSite, INestable {
48
49     /**
50      * The list of menu extender for each registered menu.
51      */

52     private ArrayList JavaDoc menuExtenders;
53
54     /**
55      * The "parent" view site
56      */

57     private IViewSite parentSite;
58
59     /**
60      * A selection provider set by the page. Value is <code>null</code> until
61      * set.
62      */

63     private ISelectionProvider selectionProvider;
64
65     /**
66      * The localized service locator for this page site. This locator is never
67      * <code>null</code>.
68      */

69     private final ServiceLocator serviceLocator;
70
71     /**
72      * The action bars for this site
73      */

74     private SubActionBars subActionBars;
75
76     /**
77      * Creates a new sub view site of the given parent view site.
78      *
79      * @param parentViewSite
80      * the parent view site
81      */

82     public PageSite(IViewSite parentViewSite) {
83         Assert.isNotNull(parentViewSite);
84         parentSite = parentViewSite;
85         subActionBars = new SubActionBars(parentViewSite.getActionBars(), this);
86
87         // Initialize the service locator.
88
this.serviceLocator = new ServiceLocator(parentSite);
89         initializeDefaultServices();
90     }
91
92     /**
93      * Initialize the slave services for this site.
94      */

95     private void initializeDefaultServices() {
96         final IWorkbenchPart parentPart = parentSite.getPart();
97         final Expression defaultExpression = new ActivePartExpression(
98                 parentPart);
99
100         final IHandlerService parentService = (IHandlerService) parentSite
101                 .getService(IHandlerService.class);
102         final IHandlerService slave = new NestableHandlerService(parentService,
103                 defaultExpression);
104         serviceLocator.registerService(IHandlerService.class, slave);
105
106         final IContextService contextParent = (IContextService) serviceLocator
107                 .getService(IContextService.class);
108         final IContextService contextSlave = new NestableContextService(
109                 contextParent, defaultExpression);
110         serviceLocator.registerService(IContextService.class, contextSlave);
111         
112         final ICommandService parentCommandService = (ICommandService) serviceLocator
113                 .getService(ICommandService.class);
114         final ICommandService commandService = new SlaveCommandService(
115                 parentCommandService, IServiceScopes.PAGESITE_SCOPE,
116                 this);
117         serviceLocator.registerService(ICommandService.class, commandService);
118
119     }
120
121     /**
122      * Disposes of the menu extender contributions.
123      */

124     protected void dispose() {
125         if (menuExtenders != null) {
126             HashSet JavaDoc managers = new HashSet JavaDoc(menuExtenders.size());
127             for (int i = 0; i < menuExtenders.size(); i++) {
128                 PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i);
129                 managers.add(ext.getManager());
130                 ext.dispose();
131             }
132             if (managers.size()>0) {
133                 for (Iterator JavaDoc iterator = managers.iterator(); iterator
134                         .hasNext();) {
135                     MenuManager mgr = (MenuManager) iterator.next();
136                     mgr.dispose();
137                 }
138             }
139             menuExtenders = null;
140         }
141         subActionBars.dispose();
142         serviceLocator.dispose();
143     }
144
145     /**
146      * The PageSite implementation of this <code>IPageSite</code> method
147      * returns the <code>SubActionBars</code> for this site.
148      *
149      * @return the subactionbars for this site
150      */

151     public IActionBars getActionBars() {
152         return subActionBars;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
159      */

160     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
161         return Platform.getAdapterManager().getAdapter(this, adapter);
162     }
163
164     /*
165      * (non-Javadoc) Method declared on IPageSite.
166      */

167     public IWorkbenchPage getPage() {
168         return parentSite.getPage();
169     }
170
171     /*
172      * (non-Javadoc) Method declared on IPageSite.
173      */

174     public ISelectionProvider getSelectionProvider() {
175         return selectionProvider;
176     }
177
178     public final Object JavaDoc getService(final Class JavaDoc key) {
179         return serviceLocator.getService(key);
180     }
181
182     /*
183      * (non-Javadoc) Method declared on IPageSite.
184      */

185     public Shell getShell() {
186         return parentSite.getShell();
187     }
188
189     /*
190      * (non-Javadoc) Method declared on IPageSite.
191      */

192     public IWorkbenchWindow getWorkbenchWindow() {
193         return parentSite.getWorkbenchWindow();
194     }
195
196     public final boolean hasService(final Class JavaDoc key) {
197         return serviceLocator.hasService(key);
198     }
199
200     /*
201      * (non-Javadoc) Method declared on IPageSite.
202      */

203     public void registerContextMenu(String JavaDoc menuID, MenuManager menuMgr,
204             ISelectionProvider selProvider) {
205         if (menuExtenders == null) {
206             menuExtenders = new ArrayList JavaDoc(1);
207         }
208         PartSite.registerContextMenu(menuID, menuMgr, selProvider, false,
209                 parentSite.getPart(), menuExtenders);
210     }
211
212     /*
213      * (non-Javadoc) Method declared on IPageSite.
214      */

215     public void setSelectionProvider(ISelectionProvider provider) {
216         selectionProvider = provider;
217     }
218
219     /*
220      * (non-Javadoc)
221      *
222      * @see org.eclipse.ui.internal.services.INestable#activate()
223      *
224      * @since 3.2
225      */

226     public void activate() {
227         serviceLocator.activate();
228     }
229
230     /*
231      * (non-Javadoc)
232      *
233      * @see org.eclipse.ui.internal.services.INestable#deactivate()
234      *
235      * @since 3.2
236      */

237     public void deactivate() {
238         serviceLocator.deactivate();
239     }
240 }
241
Popular Tags