Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / ui / common / sash / SashEditorSite.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\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
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.ui.common.sash;\r
13 \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
33 \r
34 /**\r
35  * @author mengbo\r
36  */\r
37 public class SashEditorSite implements IEditorSite {\r
38 \r
39         /**\r
40          * The nested editor.\r
41          */\r
42         private IEditorPart _editor;\r
43 \r
44         /**\r
45          * The multi-page editor.\r
46          */\r
47         private SashEditorPart _sashEditor;\r
48 \r
49         /**\r
50          * The selection provider; <code>null</code> if none.\r
51          * \r
52          * @see SashEditorSite#setSelectionProvider(ISelectionProvider)\r
53          */\r
54         private ISelectionProvider _selectionProvider = null;\r
55 \r
56         /**\r
57          * The selection change listener, initialized lazily; <code>null</code> if\r
58          * not yet created.\r
59          */\r
60         private ISelectionChangedListener _selectionChangedListener = null;\r
61 \r
62         /**\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
65          */\r
66         private IKeyBindingService _service = null;\r
67 \r
68         /**\r
69          * The list of popup menu extenders; <code>null</code> if none registered.\r
70          */\r
71         // TODO: dead? private ArrayList _menuExtenders;\r
72 \r
73         /**\r
74          * Creates a site for the given editor nested within the given multi-page\r
75          * editor.\r
76          * \r
77          * @param sashEditor\r
78          *            the multi-page editor\r
79          * @param editor\r
80          *            the nested editor\r
81          */\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
87         }\r
88 \r
89         /**\r
90          * Dispose the contributions.\r
91          */\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
96 //                      }\r
97 //                      _menuExtenders = null;\r
98 //              }\r
99 \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
107                         }\r
108                         _service = null;\r
109                 }\r
110         }\r
111 \r
112         /**\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
116          * \r
117          * @return <code>null</code>\r
118          */\r
119         public IEditorActionBarContributor getActionBarContributor() {\r
120                 return null;\r
121         }\r
122 \r
123         /**\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
127          * \r
128          * @return The action bars from the parent multi-page editor.\r
129          */\r
130         public IActionBars getActionBars() {\r
131                 return _sashEditor.getEditorSite().getActionBars();\r
132         }\r
133 \r
134         /**\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
138          * \r
139          * @return The decorator from the workbench window.\r
140          * @deprecated use IWorkbench.getDecoratorManager()\r
141          */\r
142         public ILabelDecorator getDecoratorManager() {\r
143                 return getWorkbenchWindow().getWorkbench().getDecoratorManager()\r
144                                 .getLabelDecorator();\r
145         }\r
146 \r
147         /**\r
148          * Returns the nested editor.\r
149          * \r
150          * @return the nested editor\r
151          */\r
152         public IEditorPart getEditor() {\r
153                 return _editor;\r
154         }\r
155 \r
156         /**\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
160          * \r
161          * @return An empty string.\r
162          */\r
163         public String getId() {\r
164                 return ""; //$NON-NLS-1$\r
165         }\r
166 \r
167         /*\r
168          * (non-Javadoc) Method declared on IEditorSite.\r
169          */\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
176 \r
177                         } else {\r
178                                 /*\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
181                                  * IT.\r
182                                  */\r
183                                 PDPlugin\r
184                                                 .getLogger(SashEditorSite.class)\r
185                                                 .info(\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
187                         }\r
188                 }\r
189 \r
190                 return _service;\r
191         }\r
192 \r
193         /**\r
194          * Returns the sash editor.\r
195          * \r
196          * @return the sash editor\r
197          */\r
198         public SashEditorPart getSashEditor() {\r
199                 return _sashEditor;\r
200         }\r
201 \r
202         /**\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
206          * \r
207          * @return The workbench page in which this editor site resides.\r
208          */\r
209         public IWorkbenchPage getPage() {\r
210                 return getSashEditor().getSite().getPage();\r
211         }\r
212 \r
213         /**\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
217          * \r
218          * @return An empty string.\r
219          */\r
220         public String getPluginId() {\r
221                 return ""; //$NON-NLS-1$\r
222         }\r
223 \r
224         /**\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
228          * \r
229          * @return An empty string.\r
230          */\r
231         public String getRegisteredName() {\r
232                 return ""; //$NON-NLS-1$\r
233         }\r
234 \r
235         /**\r
236          * Returns the selection changed listener which listens to the nested\r
237          * editor's selection changes, and calls <code>handleSelectionChanged</code>.\r
238          * \r
239          * @return the selection changed listener\r
240          */\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
246                                 }\r
247                         };\r
248                 }\r
249                 return _selectionChangedListener;\r
250         }\r
251 \r
252         /**\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
256          * \r
257          * @return The current selection provider.\r
258          */\r
259         public ISelectionProvider getSelectionProvider() {\r
260                 return _selectionProvider;\r
261         }\r
262 \r
263         /**\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
267          * \r
268          * @return The shell in which this editor site resides.\r
269          */\r
270         public Shell getShell() {\r
271                 return getSashEditor().getSite().getShell();\r
272         }\r
273 \r
274         /**\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
278          * \r
279          * @return The workbench window in which this editor site resides.\r
280          */\r
281         public IWorkbenchWindow getWorkbenchWindow() {\r
282                 return getSashEditor().getSite().getWorkbenchWindow();\r
283         }\r
284 \r
285         /**\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
290          * event object.\r
291          * <p>\r
292          * Subclasses may extend or reimplement this method.\r
293          * </p>\r
294          * \r
295          * @param event\r
296          *            the event\r
297          */\r
298         public void handleSelectionChanged(SelectionChangedEvent event) {\r
299                 // we'll only make the parent editor site fire the selection change\r
300                 // event\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
310                         }\r
311                 }\r
312         }\r
313 \r
314         /**\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
318          * \r
319          * @param menuID\r
320          *            The identifier for the menu.\r
321          * @param menuMgr\r
322          *            The menu manager\r
323          * @param selProvider\r
324          *            The selection provider.\r
325          */\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
330 //              }\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
334                 // one context\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
338                 // selProvider,\r
339                 // _editor));\r
340         }\r
341 \r
342         /**\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
346          * \r
347          * @param menuManager\r
348          *            The menu manager\r
349          * @param selProvider\r
350          *            The selection provider.\r
351          */\r
352         public void registerContextMenu(MenuManager menuManager,\r
353                         ISelectionProvider selProvider) {\r
354                 getSashEditor().getSite().registerContextMenu(menuManager, selProvider);\r
355         }\r
356 \r
357         /**\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
362          * occurs.\r
363          * \r
364          * @param provider\r
365          *            The selection provider.\r
366          * @see SashEditorSite#handleSelectionChanged(SelectionChangedEvent)\r
367          */\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
374                 }\r
375                 if (_selectionProvider != null) {\r
376                         _selectionProvider\r
377                                         .addSelectionChangedListener(getSelectionChangedListener());\r
378                 }\r
379         }\r
380 \r
381         /**\r
382          * @param job\r
383          */\r
384         public void progressEnd(Job job) {\r
385                 // Do nothing\r
386         }\r
387 \r
388 \r
389         /**\r
390          * @param job\r
391          */\r
392         public void progressStart(Job job) {\r
393                 // Do nothing\r
394         }\r
395 \r
396         /*\r
397          * (non-Javadoc)\r
398          * \r
399          * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)\r
400          */\r
401         public Object getAdapter(Class adapter) {\r
402                 return null;\r
403         }\r
404 \r
405         /*\r
406          * (non-Javadoc)\r
407          * \r
408          * @see org.eclipse.ui.IWorkbenchPartSite#getPart()\r
409          */\r
410         public IWorkbenchPart getPart() {\r
411                 return _editor;\r
412         }\r
413 \r
414         public void registerContextMenu(MenuManager menuManager,\r
415                         ISelectionProvider selectionProvider, boolean includeEditorInput) {\r
416             // do nothing\r
417         }\r
418 \r
419         public void registerContextMenu(String menuId, MenuManager menuManager,\r
420                         ISelectionProvider selectionProvider, boolean includeEditorInput) {\r
421                 // do nothing\r
422 \r
423         }\r
424 \r
425         public Object getService(Class api) {\r
426                 // return null\r
427                 return null;\r
428         }\r
429 \r
430         public boolean hasService(Class api) {\r
431                 // no service\r
432                 return false;\r
433         }\r
434 \r
435 }\r