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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.browsing.ui.platform;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
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;
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
51 * Carbon copy of {@link org.eclipse.ui.part.PageSite}, had to copy this too
52 * when PageBookView was copied, PageBookView refers to protected
55 * @author Tuukka Lehtonen
57 @SuppressWarnings({"restriction","rawtypes"})
58 public class PageSite implements IPageSite, INestable {
61 * The list of menu extender for each registered menu.
63 private ArrayList menuExtenders;
66 * The "parent" view site
68 private final IViewSite parentSite;
71 * A selection provider set by the page. Value is <code>null</code> until
74 private ISelectionProvider selectionProvider;
77 * The localized service locator for this page site. This locator is never
80 private final ServiceLocator serviceLocator;
83 * The action bars for this site
85 private final SubActionBars subActionBars;
87 private IEclipseContext e4Context;
89 private NestableContextService contextService;
91 private boolean active = false;
94 * Creates a new sub view site of the given parent view site.
96 * @param parentViewSite
97 * the parent view site
99 public PageSite(final IViewSite parentViewSite) {
100 Assert.isNotNull(parentViewSite);
101 parentSite = parentViewSite;
102 subActionBars = new SubActionBars(parentViewSite.getActionBars(), this);
104 // Initialize the service locator.
105 IServiceLocatorCreator slc = (IServiceLocatorCreator) parentSite
106 .getService(IServiceLocatorCreator.class);
107 this.serviceLocator = (ServiceLocator) slc.createServiceLocator(parentViewSite, null,
109 public void dispose() {
110 // final Control control =
111 // ((PartSite)parentViewSite).getPane().getControl();
112 // if (control != null && !control.isDisposed()) {
113 // ((PartSite)parentViewSite).getPane().doHide();
115 // TODO compat: not tsure what this should do
118 e4Context = ((PartSite) parentViewSite).getContext().createChild("PageSite"); //$NON-NLS-1$
119 serviceLocator.setContext(e4Context);
120 initializeDefaultServices();
124 * Initialize the slave services for this site.
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;
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);
144 e4Context.set(IContextService.class.getName(), new ContextFunction() {
146 public Object compute(IEclipseContext context) {
147 if (contextService == null) {
148 contextService = new NestableContextService(context.getParent().get(
149 IContextService.class), new ActivePartExpression(parentSite.getPart()));
151 return contextService;
157 * Disposes of the menu extender contributions.
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());
168 if (managers.size()>0) {
169 for (Iterator iterator = managers.iterator(); iterator
171 MenuManager mgr = (MenuManager) iterator.next();
175 menuExtenders = null;
177 subActionBars.dispose();
179 if (contextService != null) {
180 contextService.dispose();
183 serviceLocator.dispose();
188 * The PageSite implementation of this <code>IPageSite</code> method
189 * returns the <code>SubActionBars</code> for this site.
191 * @return the subactionbars for this site
194 public IActionBars getActionBars() {
195 return subActionBars;
201 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
204 public <T> T getAdapter(Class<T> adapter) {
205 return Platform.getAdapterManager().getAdapter(this, adapter);
209 * (non-Javadoc) Method declared on IPageSite.
212 public IWorkbenchPage getPage() {
213 return parentSite.getPage();
217 * (non-Javadoc) Method declared on IPageSite.
220 public ISelectionProvider getSelectionProvider() {
221 return selectionProvider;
225 public final Object getService(final Class key) {
226 Object service = serviceLocator.getService(key);
227 if (active && service instanceof INestable) {
228 ((INestable) service).activate();
234 * (non-Javadoc) Method declared on IPageSite.
237 public Shell getShell() {
238 return parentSite.getShell();
242 * (non-Javadoc) Method declared on IPageSite.
245 public IWorkbenchWindow getWorkbenchWindow() {
246 return parentSite.getWorkbenchWindow();
250 public final boolean hasService(final Class key) {
251 return serviceLocator.hasService(key);
255 * (non-Javadoc) Method declared on IPageSite.
258 public void registerContextMenu(String menuID, MenuManager menuMgr,
259 ISelectionProvider selProvider) {
260 if (menuExtenders == null) {
261 menuExtenders = new ArrayList(1);
263 PartSite.registerContextMenu(menuID, menuMgr, selProvider, false,
264 parentSite.getPart(), e4Context, menuExtenders);
268 * (non-Javadoc) Method declared on IPageSite.
271 public void setSelectionProvider(ISelectionProvider provider) {
272 selectionProvider = provider;
278 * @see org.eclipse.ui.internal.services.INestable#activate()
283 public void activate() {
285 e4Context.activate();
286 serviceLocator.activate();
288 if (contextService != null) {
289 contextService.activate();
296 * @see org.eclipse.ui.internal.services.INestable#deactivate()
301 public void deactivate() {
303 if (contextService != null) {
304 contextService.deactivate();
307 serviceLocator.deactivate();
308 e4Context.deactivate();