]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/PageSite.java
Merge commit '876ede6'
[simantics/platform.git] / bundles / org.simantics.browsing.ui.platform / src / org / simantics / browsing / ui / platform / PageSite.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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.simantics.browsing.ui.platform;
12
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.e4.core.contexts.ContextFunction;
20 import org.eclipse.e4.core.contexts.IEclipseContext;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IViewSite;
26 import org.eclipse.ui.IWorkbenchPage;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.SubActionBars;
29 import org.eclipse.ui.contexts.IContextService;
30 import org.eclipse.ui.handlers.IHandlerService;
31 import org.eclipse.ui.internal.PartSite;
32 import org.eclipse.ui.internal.PopupMenuExtender;
33 import org.eclipse.ui.internal.contexts.NestableContextService;
34 import org.eclipse.ui.internal.expressions.ActivePartExpression;
35 import org.eclipse.ui.internal.handlers.LegacyHandlerService;
36 import org.eclipse.ui.internal.part.IPageSiteHolder;
37 import org.eclipse.ui.internal.services.INestable;
38 import org.eclipse.ui.internal.services.IServiceLocatorCreator;
39 import org.eclipse.ui.internal.services.IWorkbenchLocationService;
40 import org.eclipse.ui.internal.services.ServiceLocator;
41 import org.eclipse.ui.internal.services.WorkbenchLocationService;
42 import org.eclipse.ui.part.IPageSite;
43 import org.eclipse.ui.services.IDisposable;
44 import org.eclipse.ui.services.IServiceScopes;
45
46 /**
47  * This implementation of <code>IPageSite</code> provides a site for a page
48  * within a <code>PageBookView</code>. Most methods are forwarded to the view's
49  * site.
50  * 
51  * Carbon copy of {@link org.eclipse.ui.part.PageSite}, had to copy this too
52  * when PageBookView was copied, PageBookView refers to protected
53  * PageSite.dispose().
54  * 
55  * @author Tuukka Lehtonen
56  */
57 @SuppressWarnings({"restriction","rawtypes"})
58 public class PageSite implements IPageSite, INestable {
59
60     /**
61      * The list of menu extender for each registered menu.
62      */
63     private ArrayList menuExtenders;
64
65     /**
66      * The "parent" view site
67      */
68     private final IViewSite parentSite;
69
70     /**
71      * A selection provider set by the page. Value is <code>null</code> until
72      * set.
73      */
74     private ISelectionProvider selectionProvider;
75
76     /**
77      * The localized service locator for this page site. This locator is never
78      * <code>null</code>.
79      */
80     private final ServiceLocator serviceLocator;
81
82     /**
83      * The action bars for this site
84      */
85     private final SubActionBars subActionBars;
86
87     private IEclipseContext e4Context;
88
89     private NestableContextService contextService;
90
91     private boolean active = false;
92
93     /**
94      * Creates a new sub view site of the given parent view site.
95      * 
96      * @param parentViewSite
97      *            the parent view site
98      */
99     public PageSite(final IViewSite parentViewSite) {
100         Assert.isNotNull(parentViewSite);
101         parentSite = parentViewSite;
102         subActionBars = new SubActionBars(parentViewSite.getActionBars(), this);
103
104         // Initialize the service locator.
105         IServiceLocatorCreator slc = (IServiceLocatorCreator) parentSite
106                 .getService(IServiceLocatorCreator.class);
107         this.serviceLocator = (ServiceLocator) slc.createServiceLocator(parentViewSite, null,
108                 new IDisposable() {
109                     public void dispose() {
110                         // final Control control =
111                         // ((PartSite)parentViewSite).getPane().getControl();
112                         // if (control != null && !control.isDisposed()) {
113                         // ((PartSite)parentViewSite).getPane().doHide();
114                         // }
115                         // TODO compat: not tsure what this should do
116                     }
117                 });
118         e4Context = ((PartSite) parentViewSite).getContext().createChild("PageSite"); //$NON-NLS-1$
119         serviceLocator.setContext(e4Context);
120         initializeDefaultServices();
121     }
122
123     /**
124      * Initialize the slave services for this site.
125      */
126     private void initializeDefaultServices() {
127         serviceLocator.registerService(IWorkbenchLocationService.class,
128                 new WorkbenchLocationService(IServiceScopes.PAGESITE_SCOPE,
129                         getWorkbenchWindow().getWorkbench(),
130                         getWorkbenchWindow(), parentSite, null, this, 3));
131         serviceLocator.registerService(IPageSiteHolder.class,
132                 new IPageSiteHolder() {
133                     public IPageSite getSite() {
134                         return PageSite.this;
135                     }
136                 });
137
138         // create a local handler service so that when this page
139         // activates/deactivates, its handlers will also be taken into/out of
140         // consideration during handler lookups
141         IHandlerService handlerService = new LegacyHandlerService(e4Context);
142         e4Context.set(IHandlerService.class, handlerService);
143
144         e4Context.set(IContextService.class.getName(), new ContextFunction() {
145             @Override
146             public Object compute(IEclipseContext context) {
147                 if (contextService == null) {
148                     contextService = new NestableContextService(context.getParent().get(
149                             IContextService.class), new ActivePartExpression(parentSite.getPart()));
150                 }
151                 return contextService;
152             }
153         });
154     }
155
156     /**
157      * Disposes of the menu extender contributions.
158      */
159     @SuppressWarnings("unchecked")
160     protected void dispose() {
161         if (menuExtenders != null) {
162             HashSet managers = new HashSet(menuExtenders.size());
163             for (int i = 0; i < menuExtenders.size(); i++) {
164                 PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i);
165                 managers.add(ext.getManager());
166                 ext.dispose();
167             }
168             if (managers.size()>0) {
169                 for (Iterator iterator = managers.iterator(); iterator
170                 .hasNext();) {
171                     MenuManager mgr = (MenuManager) iterator.next();
172                     mgr.dispose();
173                 }
174             }
175             menuExtenders = null;
176         }
177         subActionBars.dispose();
178
179         if (contextService != null) {
180             contextService.dispose();
181         }
182
183         serviceLocator.dispose();
184         e4Context.dispose();
185     }
186
187     /**
188      * The PageSite implementation of this <code>IPageSite</code> method
189      * returns the <code>SubActionBars</code> for this site.
190      * 
191      * @return the subactionbars for this site
192      */
193     @Override
194     public IActionBars getActionBars() {
195         return subActionBars;
196     }
197
198     /*
199      * (non-Javadoc)
200      * 
201      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
202      */
203     @Override
204     public <T> T getAdapter(Class<T> adapter) {
205         return Platform.getAdapterManager().getAdapter(this, adapter);
206     }
207
208     /*
209      * (non-Javadoc) Method declared on IPageSite.
210      */
211     @Override
212     public IWorkbenchPage getPage() {
213         return parentSite.getPage();
214     }
215
216     /*
217      * (non-Javadoc) Method declared on IPageSite.
218      */
219     @Override
220     public ISelectionProvider getSelectionProvider() {
221         return selectionProvider;
222     }
223
224     @Override
225     public final Object getService(final Class key) {
226         Object service = serviceLocator.getService(key);
227         if (active && service instanceof INestable) {
228             ((INestable) service).activate();
229         }
230         return service;
231     }
232
233     /*
234      * (non-Javadoc) Method declared on IPageSite.
235      */
236     @Override
237     public Shell getShell() {
238         return parentSite.getShell();
239     }
240
241     /*
242      * (non-Javadoc) Method declared on IPageSite.
243      */
244     @Override
245     public IWorkbenchWindow getWorkbenchWindow() {
246         return parentSite.getWorkbenchWindow();
247     }
248
249     @Override
250     public final boolean hasService(final Class key) {
251         return serviceLocator.hasService(key);
252     }
253
254     /*
255      * (non-Javadoc) Method declared on IPageSite.
256      */
257     @Override
258     public void registerContextMenu(String menuID, MenuManager menuMgr,
259             ISelectionProvider selProvider) {
260         if (menuExtenders == null) {
261             menuExtenders = new ArrayList(1);
262         }
263         PartSite.registerContextMenu(menuID, menuMgr, selProvider, false,
264                 parentSite.getPart(), e4Context, menuExtenders);
265     }
266
267     /*
268      * (non-Javadoc) Method declared on IPageSite.
269      */
270     @Override
271     public void setSelectionProvider(ISelectionProvider provider) {
272         selectionProvider = provider;
273     }
274
275     /*
276      * (non-Javadoc)
277      * 
278      * @see org.eclipse.ui.internal.services.INestable#activate()
279      * 
280      * @since 3.2
281      */
282     @Override
283     public void activate() {
284         active = true;
285         e4Context.activate();
286         serviceLocator.activate();
287
288         if (contextService != null) {
289             contextService.activate();
290         }
291     }
292
293     /*
294      * (non-Javadoc)
295      * 
296      * @see org.eclipse.ui.internal.services.INestable#deactivate()
297      * 
298      * @since 3.2
299      */
300     @Override
301     public void deactivate() {
302         active = false;
303         if (contextService != null) {
304             contextService.deactivate();
305         }
306
307         serviceLocator.deactivate();
308         e4Context.deactivate();
309     }
310 }