001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.part;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.expressions.Expression;
017: import org.eclipse.core.runtime.Assert;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.jface.action.MenuManager;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.swt.widgets.Shell;
022: import org.eclipse.ui.IActionBars;
023: import org.eclipse.ui.IViewSite;
024: import org.eclipse.ui.IWorkbenchPage;
025: import org.eclipse.ui.IWorkbenchPart;
026: import org.eclipse.ui.IWorkbenchWindow;
027: import org.eclipse.ui.SubActionBars;
028: import org.eclipse.ui.commands.ICommandService;
029: import org.eclipse.ui.contexts.IContextService;
030: import org.eclipse.ui.handlers.IHandlerService;
031: import org.eclipse.ui.internal.PartSite;
032: import org.eclipse.ui.internal.PopupMenuExtender;
033: import org.eclipse.ui.internal.commands.SlaveCommandService;
034: import org.eclipse.ui.internal.contexts.NestableContextService;
035: import org.eclipse.ui.internal.expressions.ActivePartExpression;
036: import org.eclipse.ui.internal.handlers.NestableHandlerService;
037: import org.eclipse.ui.internal.services.INestable;
038: import org.eclipse.ui.internal.services.ServiceLocator;
039: import org.eclipse.ui.services.IServiceScopes;
040:
041: /**
042: * This implementation of <code>IPageSite</code> provides a site for a page
043: * within a <code>PageBookView</code>. Most methods are forwarded to the
044: * view's site.
045: */
046: public class PageSite implements IPageSite, INestable {
047:
048: /**
049: * The list of menu extender for each registered menu.
050: */
051: private ArrayList menuExtenders;
052:
053: /**
054: * The "parent" view site
055: */
056: private IViewSite parentSite;
057:
058: /**
059: * A selection provider set by the page. Value is <code>null</code> until
060: * set.
061: */
062: private ISelectionProvider selectionProvider;
063:
064: /**
065: * The localized service locator for this page site. This locator is never
066: * <code>null</code>.
067: */
068: private final ServiceLocator serviceLocator;
069:
070: /**
071: * The action bars for this site
072: */
073: private SubActionBars subActionBars;
074:
075: /**
076: * Creates a new sub view site of the given parent view site.
077: *
078: * @param parentViewSite
079: * the parent view site
080: */
081: public PageSite(IViewSite parentViewSite) {
082: Assert.isNotNull(parentViewSite);
083: parentSite = parentViewSite;
084: subActionBars = new SubActionBars(parentViewSite
085: .getActionBars(), this );
086:
087: // Initialize the service locator.
088: this .serviceLocator = new ServiceLocator(parentSite);
089: initializeDefaultServices();
090: }
091:
092: /**
093: * Initialize the slave services for this site.
094: */
095: private void initializeDefaultServices() {
096: final IWorkbenchPart parentPart = parentSite.getPart();
097: final Expression defaultExpression = new ActivePartExpression(
098: parentPart);
099:
100: final IHandlerService parentService = (IHandlerService) parentSite
101: .getService(IHandlerService.class);
102: final IHandlerService slave = new NestableHandlerService(
103: parentService, 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,
111: contextSlave);
112:
113: final ICommandService parentCommandService = (ICommandService) serviceLocator
114: .getService(ICommandService.class);
115: final ICommandService commandService = new SlaveCommandService(
116: parentCommandService, IServiceScopes.PAGESITE_SCOPE,
117: this );
118: serviceLocator.registerService(ICommandService.class,
119: commandService);
120:
121: }
122:
123: /**
124: * Disposes of the menu extender contributions.
125: */
126: protected void dispose() {
127: if (menuExtenders != null) {
128: HashSet managers = new HashSet(menuExtenders.size());
129: for (int i = 0; i < menuExtenders.size(); i++) {
130: PopupMenuExtender ext = (PopupMenuExtender) menuExtenders
131: .get(i);
132: managers.add(ext.getManager());
133: ext.dispose();
134: }
135: if (managers.size() > 0) {
136: for (Iterator iterator = managers.iterator(); iterator
137: .hasNext();) {
138: MenuManager mgr = (MenuManager) iterator.next();
139: mgr.dispose();
140: }
141: }
142: menuExtenders = null;
143: }
144: subActionBars.dispose();
145: serviceLocator.dispose();
146: }
147:
148: /**
149: * The PageSite implementation of this <code>IPageSite</code> method
150: * returns the <code>SubActionBars</code> for this site.
151: *
152: * @return the subactionbars for this site
153: */
154: public IActionBars getActionBars() {
155: return subActionBars;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
162: */
163: public Object getAdapter(Class adapter) {
164: return Platform.getAdapterManager().getAdapter(this , adapter);
165: }
166:
167: /*
168: * (non-Javadoc) Method declared on IPageSite.
169: */
170: public IWorkbenchPage getPage() {
171: return parentSite.getPage();
172: }
173:
174: /*
175: * (non-Javadoc) Method declared on IPageSite.
176: */
177: public ISelectionProvider getSelectionProvider() {
178: return selectionProvider;
179: }
180:
181: public final Object getService(final Class key) {
182: return serviceLocator.getService(key);
183: }
184:
185: /*
186: * (non-Javadoc) Method declared on IPageSite.
187: */
188: public Shell getShell() {
189: return parentSite.getShell();
190: }
191:
192: /*
193: * (non-Javadoc) Method declared on IPageSite.
194: */
195: public IWorkbenchWindow getWorkbenchWindow() {
196: return parentSite.getWorkbenchWindow();
197: }
198:
199: public final boolean hasService(final Class key) {
200: return serviceLocator.hasService(key);
201: }
202:
203: /*
204: * (non-Javadoc) Method declared on IPageSite.
205: */
206: public void registerContextMenu(String menuID, MenuManager menuMgr,
207: ISelectionProvider selProvider) {
208: if (menuExtenders == null) {
209: menuExtenders = new ArrayList(1);
210: }
211: PartSite.registerContextMenu(menuID, menuMgr, selProvider,
212: false, parentSite.getPart(), menuExtenders);
213: }
214:
215: /*
216: * (non-Javadoc) Method declared on IPageSite.
217: */
218: public void setSelectionProvider(ISelectionProvider provider) {
219: selectionProvider = provider;
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see org.eclipse.ui.internal.services.INestable#activate()
226: *
227: * @since 3.2
228: */
229: public void activate() {
230: serviceLocator.activate();
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see org.eclipse.ui.internal.services.INestable#deactivate()
237: *
238: * @since 3.2
239: */
240: public void deactivate() {
241: serviceLocator.deactivate();
242: }
243: }
|