1 /*******************************************************************************
\r
2 * Copyright (c) 2006 Sybase, Inc. and others.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * Sybase, Inc. - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.eclipse.jst.pagedesigner.ui.common.sash;
\r
14 import org.eclipse.core.runtime.Assert;
\r
15 import org.eclipse.core.runtime.jobs.Job;
\r
16 import org.eclipse.jface.action.MenuManager;
\r
17 import org.eclipse.jface.viewers.ILabelDecorator;
\r
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
\r
19 import org.eclipse.jface.viewers.ISelectionProvider;
\r
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
\r
21 import org.eclipse.jst.pagedesigner.PDPlugin;
\r
22 import org.eclipse.swt.widgets.Shell;
\r
23 import org.eclipse.ui.IActionBars;
\r
24 import org.eclipse.ui.IEditorActionBarContributor;
\r
25 import org.eclipse.ui.IEditorPart;
\r
26 import org.eclipse.ui.IEditorSite;
\r
27 import org.eclipse.ui.IKeyBindingService;
\r
28 import org.eclipse.ui.INestableKeyBindingService;
\r
29 import org.eclipse.ui.IWorkbenchPage;
\r
30 import org.eclipse.ui.IWorkbenchPart;
\r
31 import org.eclipse.ui.IWorkbenchWindow;
\r
32 //import org.eclipse.ui.internal.PopupMenuExtender;
\r
37 public class SashEditorSite implements IEditorSite {
\r
40 * The nested editor.
\r
42 private IEditorPart _editor;
\r
45 * The multi-page editor.
\r
47 private SashEditorPart _sashEditor;
\r
50 * The selection provider; <code>null</code> if none.
\r
52 * @see SashEditorSite#setSelectionProvider(ISelectionProvider)
\r
54 private ISelectionProvider _selectionProvider = null;
\r
57 * The selection change listener, initialized lazily; <code>null</code> if
\r
60 private ISelectionChangedListener _selectionChangedListener = null;
\r
63 * The cached copy of the key binding service specific to this sash editor
\r
64 * site. This value is <code>null</code> if it is not yet initialized.
\r
66 private IKeyBindingService _service = null;
\r
69 * The list of popup menu extenders; <code>null</code> if none registered.
\r
71 // TODO: dead? private ArrayList _menuExtenders;
\r
74 * Creates a site for the given editor nested within the given multi-page
\r
78 * the multi-page editor
\r
82 public SashEditorSite(SashEditorPart sashEditor, IEditorPart editor) {
\r
83 Assert.isNotNull(sashEditor);
\r
84 Assert.isNotNull(editor);
\r
85 this._sashEditor = sashEditor;
\r
86 this._editor = editor;
\r
90 * Dispose the contributions.
\r
92 public void dispose() {
\r
93 // if (_menuExtenders != null) {
\r
94 // for (int i = 0, size = _menuExtenders.size(); i < size; i++) {
\r
95 // ((PopupMenuExtender) _menuExtenders.get(i)).dispose();
\r
97 // _menuExtenders = null;
\r
100 // Remove myself from the list of nested key binding services.
\r
101 if (_service != null) {
\r
102 IKeyBindingService parentService = getEditor().getSite()
\r
103 .getKeyBindingService();
\r
104 if (parentService instanceof INestableKeyBindingService) {
\r
105 INestableKeyBindingService nestableParent = (INestableKeyBindingService) parentService;
\r
106 nestableParent.removeKeyBindingService(this);
\r
113 * The <code>SashEditorSite</code> implementation of this
\r
114 * <code>IEditorSite</code> method returns <code>null</code>, since
\r
115 * nested editors do not have their own action bar contributor.
\r
117 * @return <code>null</code>
\r
119 public IEditorActionBarContributor getActionBarContributor() {
\r
124 * The <code>SashEditorSite</code> implementation of this
\r
125 * <code>IEditorSite</code> method forwards to the multi-page editor to
\r
126 * return the action bars.
\r
128 * @return The action bars from the parent multi-page editor.
\r
130 public IActionBars getActionBars() {
\r
131 return _sashEditor.getEditorSite().getActionBars();
\r
135 * The <code>SashEditorSite</code> implementation of this
\r
136 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
137 * editor to return the decorator manager.
\r
139 * @return The decorator from the workbench window.
\r
140 * @deprecated use IWorkbench.getDecoratorManager()
\r
142 public ILabelDecorator getDecoratorManager() {
\r
143 return getWorkbenchWindow().getWorkbench().getDecoratorManager()
\r
144 .getLabelDecorator();
\r
148 * Returns the nested editor.
\r
150 * @return the nested editor
\r
152 public IEditorPart getEditor() {
\r
157 * The <code>SashEditorSite</code> implementation of this
\r
158 * <code>IWorkbenchPartSite</code> method returns an empty string since
\r
159 * the nested editor is not created from the registry.
\r
161 * @return An empty string.
\r
163 public String getId() {
\r
164 return ""; //$NON-NLS-1$
\r
168 * (non-Javadoc) Method declared on IEditorSite.
\r
170 public IKeyBindingService getKeyBindingService() {
\r
171 if (_service == null) {
\r
172 _service = getSashEditor().getEditorSite().getKeyBindingService();
\r
173 if (_service instanceof INestableKeyBindingService) {
\r
174 INestableKeyBindingService nestableService = (INestableKeyBindingService) _service;
\r
175 _service = nestableService.getKeyBindingService(this);
\r
179 * This is an internal reference, and should not be copied by
\r
180 * client code. If you are thinking of copying this, DON'T DO
\r
184 .getLogger(SashEditorSite.class)
\r
186 "MultiPageEditorSite.getKeyBindingService() Parent key binding service was not an instance of INestableKeyBindingService. It was an instance of " + _service.getClass().getName() + " instead."); //$NON-NLS-1$ //$NON-NLS-2$
\r
194 * Returns the sash editor.
\r
196 * @return the sash editor
\r
198 public SashEditorPart getSashEditor() {
\r
199 return _sashEditor;
\r
203 * The <code>SashEditorSite</code> implementation of this
\r
204 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
205 * editor to return the workbench page.
\r
207 * @return The workbench page in which this editor site resides.
\r
209 public IWorkbenchPage getPage() {
\r
210 return getSashEditor().getSite().getPage();
\r
214 * The <code>SashEditorSite</code> implementation of this
\r
215 * <code>IWorkbenchPartSite</code> method returns an empty string since
\r
216 * the nested editor is not created from the registry.
\r
218 * @return An empty string.
\r
220 public String getPluginId() {
\r
221 return ""; //$NON-NLS-1$
\r
225 * The <code>SashEditorSite</code> implementation of this
\r
226 * <code>IWorkbenchPartSite</code> method returns an empty string since
\r
227 * the nested editor is not created from the registry.
\r
229 * @return An empty string.
\r
231 public String getRegisteredName() {
\r
232 return ""; //$NON-NLS-1$
\r
236 * Returns the selection changed listener which listens to the nested
\r
237 * editor's selection changes, and calls <code>handleSelectionChanged</code>.
\r
239 * @return the selection changed listener
\r
241 private ISelectionChangedListener getSelectionChangedListener() {
\r
242 if (_selectionChangedListener == null) {
\r
243 _selectionChangedListener = new ISelectionChangedListener() {
\r
244 public void selectionChanged(SelectionChangedEvent event) {
\r
245 SashEditorSite.this.handleSelectionChanged(event);
\r
249 return _selectionChangedListener;
\r
253 * The <code>SashEditorSite</code> implementation of this
\r
254 * <code>IWorkbenchPartSite</code> method returns the selection provider
\r
255 * set by <code>setSelectionProvider</code>.
\r
257 * @return The current selection provider.
\r
259 public ISelectionProvider getSelectionProvider() {
\r
260 return _selectionProvider;
\r
264 * The <code>SashEditorSite</code> implementation of this
\r
265 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
266 * editor to return the shell.
\r
268 * @return The shell in which this editor site resides.
\r
270 public Shell getShell() {
\r
271 return getSashEditor().getSite().getShell();
\r
275 * The <code>SashEditorSite</code> implementation of this
\r
276 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
277 * editor to return the workbench window.
\r
279 * @return The workbench window in which this editor site resides.
\r
281 public IWorkbenchWindow getWorkbenchWindow() {
\r
282 return getSashEditor().getSite().getWorkbenchWindow();
\r
286 * Handles a selection changed event from the nested editor. The default
\r
287 * implementation gets the selection provider from the multi-page editor's
\r
288 * site, and calls <code>fireSelectionChanged</code> on it (only if it is
\r
289 * an instance of <code>SashSelectionProvider</code>), passing a new
\r
292 * Subclasses may extend or reimplement this method.
\r
298 public void handleSelectionChanged(SelectionChangedEvent event) {
\r
299 // we'll only make the parent editor site fire the selection change
\r
301 // when we (the sasheditorsite) is the active editor in the parent site.
\r
302 if (getSashEditor().getActiveEditor() == this.getPart()) {
\r
303 ISelectionProvider parentProvider = getSashEditor().getSite()
\r
304 .getSelectionProvider();
\r
305 if (parentProvider instanceof SashEditorSelectionProvider) {
\r
306 SelectionChangedEvent newEvent = new SelectionChangedEvent(
\r
307 parentProvider, event.getSelection());
\r
308 ((SashEditorSelectionProvider) parentProvider)
\r
309 .fireSelectionChanged(newEvent);
\r
315 * The <code>SashEditorSite</code> implementation of this
\r
316 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
317 * editor for registration.
\r
320 * The identifier for the menu.
\r
323 * @param selProvider
\r
324 * The selection provider.
\r
326 public void registerContextMenu(String menuID, MenuManager menuMgr,
\r
327 ISelectionProvider selProvider) {
\r
328 // if (_menuExtenders == null) {
\r
329 // _menuExtenders = new ArrayList(1);
\r
331 // cancel the registration of PopupMenuExtender since the
\r
332 // PopupMenuExtender's behavior
\r
333 // is different between eclipse 3.0 and eclipse 3.1,and we always have
\r
335 // menu listener,no need add PopupMenuExtender as the second
\r
336 // listener(workaroud for bug 408295-1)
\r
337 // _menuExtenders.add(new PopupMenuExtender(menuID, menuMgr,
\r
343 * The <code>SashEditorSite</code> implementation of this
\r
344 * <code>IWorkbenchPartSite</code> method forwards to the multi-page
\r
345 * editor for registration.
\r
347 * @param menuManager
\r
349 * @param selProvider
\r
350 * The selection provider.
\r
352 public void registerContextMenu(MenuManager menuManager,
\r
353 ISelectionProvider selProvider) {
\r
354 getSashEditor().getSite().registerContextMenu(menuManager, selProvider);
\r
358 * The <code>SashEditorSite</code> implementation of this
\r
359 * <code>IWorkbenchPartSite</code> method remembers the selection
\r
360 * provider, and also hooks a listener on it, which calls
\r
361 * <code>handleSelectionChanged</code> when a selection changed event
\r
365 * The selection provider.
\r
366 * @see SashEditorSite#handleSelectionChanged(SelectionChangedEvent)
\r
368 public void setSelectionProvider(ISelectionProvider provider) {
\r
369 ISelectionProvider oldSelectionProvider = _selectionProvider;
\r
370 _selectionProvider = provider;
\r
371 if (oldSelectionProvider != null) {
\r
372 oldSelectionProvider
\r
373 .removeSelectionChangedListener(getSelectionChangedListener());
\r
375 if (_selectionProvider != null) {
\r
377 .addSelectionChangedListener(getSelectionChangedListener());
\r
384 public void progressEnd(Job job) {
\r
392 public void progressStart(Job job) {
\r
399 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
\r
401 public Object getAdapter(Class adapter) {
\r
408 * @see org.eclipse.ui.IWorkbenchPartSite#getPart()
\r
410 public IWorkbenchPart getPart() {
\r
414 public void registerContextMenu(MenuManager menuManager,
\r
415 ISelectionProvider selectionProvider, boolean includeEditorInput) {
\r
419 public void registerContextMenu(String menuId, MenuManager menuManager,
\r
420 ISelectionProvider selectionProvider, boolean includeEditorInput) {
\r
425 public Object getService(Class api) {
\r
430 public boolean hasService(Class api) {
\r