KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > part > MultiPageEditorPart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.eclipse.ui.part;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.util.Tracing;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.ISafeRunnable;
20 import org.eclipse.core.runtime.SafeRunner;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.custom.CTabFolder;
25 import org.eclipse.swt.custom.CTabItem;
26 import org.eclipse.swt.events.SelectionAdapter;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.layout.FillLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Item;
34 import org.eclipse.ui.IEditorActionBarContributor;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IEditorSite;
38 import org.eclipse.ui.IKeyBindingService;
39 import org.eclipse.ui.INestableKeyBindingService;
40 import org.eclipse.ui.IPropertyListener;
41 import org.eclipse.ui.IWorkbenchPart;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.internal.WorkbenchPlugin;
44 import org.eclipse.ui.internal.misc.Policy;
45 import org.eclipse.ui.internal.services.INestable;
46 import org.eclipse.ui.internal.util.Util;
47 import org.eclipse.ui.services.IServiceLocator;
48
49 /**
50  * A multi-page editor is an editor with multiple pages, each of which may
51  * contain an editor or an arbitrary SWT control.
52  * <p>
53  * Subclasses must implement the following methods:
54  * <ul>
55  * <li><code>createPages</code> - to create the required pages by calling one
56  * of the <code>addPage</code> methods</li>
57  * <li><code>IEditorPart.doSave</code> - to save contents of editor</li>
58  * <li><code>IEditorPart.doSaveAs</code> - to save contents of editor</li>
59  * <li><code>IEditorPart.isSaveAsAllowed</code> - to enable Save As</li>
60  * <li><code>IEditorPart.gotoMarker</code> - to scroll to a marker</li>
61  * </ul>
62  * </p>
63  * <p>
64  * Multi-page editors have a single action bar contributor, which manages
65  * contributions for all the pages. The contributor must be a subclass of
66  * <code>AbstractMultiPageEditorActionBarContributor</code>. Note that since
67  * any nested editors are created directly in code by callers of
68  * <code>addPage(IEditorPart,IEditorInput)</code>, nested editors do not have
69  * their own contributors.
70  * </p>
71  *
72  * @see org.eclipse.ui.part.MultiPageEditorActionBarContributor
73  */

74 public abstract class MultiPageEditorPart extends EditorPart {
75
76     /**
77      *
78      */

79     private static final String JavaDoc TRACING_COMPONENT = "MPE"; //$NON-NLS-1$
80

81     /**
82      * The active service locator. This value may be <code>null</code> if
83      * there is no selected page, or if the selected page is a control.
84      */

85     private INestable activeServiceLocator;
86
87     /**
88      * The container widget.
89      */

90     private CTabFolder container;
91
92     /**
93      * List of nested editors. Element type: IEditorPart. Need to hang onto them
94      * here, in addition to using get/setData on the items, because dispose()
95      * needs to access them, but widgetry has already been disposed at that
96      * point.
97      */

98     private ArrayList JavaDoc nestedEditors = new ArrayList JavaDoc(3);
99
100     /**
101      * Creates an empty multi-page editor with no pages.
102      */

103     protected MultiPageEditorPart() {
104         super();
105     }
106
107     /**
108      * Creates and adds a new page containing the given control to this
109      * multi-page editor. The control may be <code>null</code>, allowing it
110      * to be created and set later using <code>setControl</code>.
111      *
112      * @param control
113      * the control, or <code>null</code>
114      * @return the index of the new page
115      *
116      * @see MultiPageEditorPart#setControl(int, Control)
117      */

118     public int addPage(Control control) {
119         int index = getPageCount();
120         addPage(index, control);
121         return index;
122     }
123
124     /**
125      * Creates and adds a new page containing the given control to this
126      * multi-page editor. The page is added at the given index. The control may
127      * be <code>null</code>, allowing it to be created and set later using
128      * <code>setControl</code>.
129      *
130      * @param index
131      * the index at which to add the page (0-based)
132      * @param control
133      * the control, or <code>null</code>
134      *
135      * @see MultiPageEditorPart#setControl(int, Control)
136      */

137     public void addPage(int index, Control control) {
138         createItem(index, control);
139     }
140
141     /**
142      * Creates and adds a new page containing the given editor to this
143      * multi-page editor. This also hooks a property change listener on the
144      * nested editor.
145      *
146      * @param editor
147      * the nested editor
148      * @param input
149      * the input for the nested editor
150      * @return the index of the new page
151      * @exception PartInitException
152      * if a new page could not be created
153      *
154      * @see MultiPageEditorPart#handlePropertyChange(int) the handler for
155      * property change events from the nested editor
156      */

157     public int addPage(IEditorPart editor, IEditorInput input)
158             throws PartInitException {
159         int index = getPageCount();
160         addPage(index, editor, input);
161         return index;
162     }
163
164     /**
165      * Creates and adds a new page containing the given editor to this
166      * multi-page editor. The page is added at the given index. This also hooks
167      * a property change listener on the nested editor.
168      *
169      * @param index
170      * the index at which to add the page (0-based)
171      * @param editor
172      * the nested editor
173      * @param input
174      * the input for the nested editor
175      * @exception PartInitException
176      * if a new page could not be created
177      *
178      * @see MultiPageEditorPart#handlePropertyChange(int) the handler for
179      * property change events from the nested editor
180      */

181     public void addPage(int index, IEditorPart editor, IEditorInput input)
182             throws PartInitException {
183         IEditorSite site = createSite(editor);
184         // call init first so that if an exception is thrown, we have created no
185
// new widgets
186
editor.init(site, input);
187         Composite parent2 = new Composite(getContainer(),
188                 getOrientation(editor));
189         parent2.setLayout(new FillLayout());
190         editor.createPartControl(parent2);
191         editor.addPropertyListener(new IPropertyListener() {
192             public void propertyChanged(Object JavaDoc source, int propertyId) {
193                 MultiPageEditorPart.this.handlePropertyChange(propertyId);
194             }
195         });
196         // create item for page only after createPartControl has succeeded
197
Item item = createItem(index, parent2);
198         // remember the editor, as both data on the item, and in the list of
199
// editors (see field comment)
200
item.setData(editor);
201         nestedEditors.add(editor);
202     }
203
204     /**
205      * Get the orientation of the editor.
206      *
207      * @param editor
208      * @return int the orientation flag
209      * @see SWT#RIGHT_TO_LEFT
210      * @see SWT#LEFT_TO_RIGHT
211      * @see SWT#NONE
212      */

213     private int getOrientation(IEditorPart editor) {
214         if (editor instanceof IWorkbenchPartOrientation) {
215             return ((IWorkbenchPartOrientation) editor).getOrientation();
216         }
217         return getOrientation();
218     }
219
220     /**
221      * Creates an empty container. Creates a CTabFolder with no style bits set,
222      * and hooks a selection listener which calls <code>pageChange()</code>
223      * whenever the selected tab changes.
224      *
225      * @param parent
226      * The composite in which the container tab folder should be
227      * created; must not be <code>null</code>.
228      * @return a new container
229      */

230     private CTabFolder createContainer(Composite parent) {
231         // use SWT.FLAT style so that an extra 1 pixel border is not reserved
232
// inside the folder
233
parent.setLayout(new FillLayout());
234         final CTabFolder newContainer = new CTabFolder(parent, SWT.BOTTOM
235                 | SWT.FLAT);
236         newContainer.addSelectionListener(new SelectionAdapter() {
237             public void widgetSelected(SelectionEvent e) {
238                 int newPageIndex = newContainer.indexOf((CTabItem) e.item);
239                 pageChange(newPageIndex);
240             }
241         });
242         return newContainer;
243     }
244
245     /**
246      * Creates a tab item at the given index and places the given control in the
247      * new item. The item is a CTabItem with no style bits set.
248      *
249      * @param index
250      * the index at which to add the control
251      * @param control
252      * is the control to be placed in an item
253      * @return a new item
254      */

255     private CTabItem createItem(int index, Control control) {
256         CTabItem item = new CTabItem(getTabFolder(), SWT.NONE, index);
257         item.setControl(control);
258         return item;
259     }
260
261     /**
262      * Creates the pages of this multi-page editor.
263      * <p>
264      * Subclasses must implement this method.
265      * </p>
266      */

267     protected abstract void createPages();
268
269     /**
270      * The <code>MultiPageEditor</code> implementation of this
271      * <code>IWorkbenchPart</code> method creates the control for the
272      * multi-page editor by calling <code>createContainer</code>, then
273      * <code>createPages</code>. Subclasses should implement
274      * <code>createPages</code> rather than overriding this method.
275      *
276      * @param parent
277      * The parent in which the editor should be created; must not be
278      * <code>null</code>.
279      */

280     public final void createPartControl(Composite parent) {
281         Composite pageContainer = createPageContainer(parent);
282         this.container = createContainer(pageContainer);
283         createPages();
284         // set the active page (page 0 by default), unless it has already been
285
// done
286
if (getActivePage() == -1) {
287             setActivePage(0);
288             IEditorPart part = getEditor(0);
289             if (part!=null) {
290                 final IServiceLocator serviceLocator = part.getEditorSite();
291                 if (serviceLocator instanceof INestable) {
292                     activeServiceLocator = (INestable) serviceLocator;
293                     activeServiceLocator.activate();
294                 }
295             }
296         }
297     }
298
299     /**
300      * Creates the parent control for the container returned by
301      * {@link #getContainer() }.
302      *
303      * <p>
304      * Subclasses may extend and must call super implementation first.
305      * </p>
306      *
307      * @param parent
308      * the parent for all of the editors contents.
309      * @return the parent for this editor's container. Must not be
310      * <code>null</code>.
311      *
312      * @since 3.2
313      */

314     protected Composite createPageContainer(Composite parent) {
315         return parent;
316     }
317
318     /**
319      * Creates the site for the given nested editor. The
320      * <code>MultiPageEditorPart</code> implementation of this method creates
321      * an instance of <code>MultiPageEditorSite</code>. Subclasses may
322      * reimplement to create more specialized sites.
323      *
324      * @param editor
325      * the nested editor
326      * @return the editor site
327      */

328     protected IEditorSite createSite(IEditorPart editor) {
329         return new MultiPageEditorSite(this, editor);
330     }
331
332     /**
333      * The <code>MultiPageEditorPart</code> implementation of this
334      * <code>IWorkbenchPart</code> method disposes all nested editors.
335      * Subclasses may extend.
336      */

337     public void dispose() {
338         for (int i = 0; i < nestedEditors.size(); ++i) {
339             IEditorPart editor = (IEditorPart) nestedEditors.get(i);
340             disposePart(editor);
341         }
342         nestedEditors.clear();
343     }
344
345     /**
346      * Returns the active nested editor if there is one.
347      * <p>
348      * Subclasses should not override this method
349      * </p>
350      *
351      * @return the active nested editor, or <code>null</code> if none
352      */

353     protected IEditorPart getActiveEditor() {
354         int index = getActivePage();
355         if (index != -1) {
356             return getEditor(index);
357         }
358         return null;
359     }
360
361     /**
362      * Returns the index of the currently active page, or -1 if there is no
363      * active page.
364      * <p>
365      * Subclasses should not override this method
366      * </p>
367      *
368      * @return the index of the active page, or -1 if there is no active page
369      */

370     protected int getActivePage() {
371         CTabFolder tabFolder = getTabFolder();
372         if (tabFolder != null && !tabFolder.isDisposed()) {
373             return tabFolder.getSelectionIndex();
374         }
375         return -1;
376     }
377
378     /**
379      * Returns the composite control containing this multi-page editor's pages.
380      * This should be used as the parent when creating controls for the
381      * individual pages. That is, when calling <code>addPage(Control)</code>,
382      * the passed control should be a child of this container.
383      * <p>
384      * Warning: Clients should not assume that the container is any particular
385      * subclass of Composite. The actual class used may change in order to
386      * improve the look and feel of multi-page editors. Any code making
387      * assumptions on the particular subclass would thus be broken.
388      * </p>
389      * <p>
390      * Subclasses should not override this method
391      * </p>
392      *
393      * @return the composite, or <code>null</code> if
394      * <code>createPartControl</code> has not been called yet
395      */

396     protected Composite getContainer() {
397         return container;
398     }
399
400     /**
401      * Returns the control for the given page index, or <code>null</code> if
402      * no control has been set for the page. The page index must be valid.
403      * <p>
404      * Subclasses should not override this method
405      * </p>
406      *
407      * @param pageIndex
408      * the index of the page
409      * @return the control for the specified page, or <code>null</code> if
410      * none has been set
411      */

412     protected Control getControl(int pageIndex) {
413         return getItem(pageIndex).getControl();
414     }
415
416     /**
417      * Returns the editor for the given page index. The page index must be
418      * valid.
419      *
420      * @param pageIndex
421      * the index of the page
422      * @return the editor for the specified page, or <code>null</code> if the
423      * specified page was not created with
424      * <code>addPage(IEditorPart,IEditorInput)</code>
425      */

426     protected IEditorPart getEditor(int pageIndex) {
427         Item item = getItem(pageIndex);
428         if (item != null) {
429             Object JavaDoc data = item.getData();
430             if (data instanceof IEditorPart) {
431                 return (IEditorPart) data;
432             }
433         }
434         return null;
435     }
436
437     /**
438      * Returns the tab item for the given page index (page index is 0-based).
439      * The page index must be valid.
440      *
441      * @param pageIndex
442      * the index of the page
443      * @return the tab item for the given page index
444      */

445     private CTabItem getItem(int pageIndex) {
446         return getTabFolder().getItem(pageIndex);
447     }
448
449     /**
450      * Returns the number of pages in this multi-page editor.
451      *
452      * @return the number of pages
453      */

454     protected int getPageCount() {
455         CTabFolder folder = getTabFolder();
456         // May not have been created yet, or may have been disposed.
457
if (folder != null && !folder.isDisposed()) {
458             return folder.getItemCount();
459         }
460         return 0;
461     }
462
463     /**
464      * Returns the image for the page with the given index, or <code>null</code>
465      * if no image has been set for the page. The page index must be valid.
466      *
467      * @param pageIndex
468      * the index of the page
469      * @return the image, or <code>null</code> if none
470      */

471     protected Image getPageImage(int pageIndex) {
472         return getItem(pageIndex).getImage();
473     }
474
475     /**
476      * Returns the text label for the page with the given index. Returns the
477      * empty string if no text label has been set for the page. The page index
478      * must be valid.
479      *
480      * @param pageIndex
481      * the index of the page
482      * @return the text label for the page
483      */

484     protected String JavaDoc getPageText(int pageIndex) {
485         return getItem(pageIndex).getText();
486     }
487
488     /**
489      * Returns the tab folder containing this multi-page editor's pages.
490      *
491      * @return the tab folder, or <code>null</code> if
492      * <code>createPartControl</code> has not been called yet
493      */

494     private CTabFolder getTabFolder() {
495         return container;
496     }
497
498     /**
499      * Handles a property change notification from a nested editor. The default
500      * implementation simply forwards the change to listeners on this multi-page
501      * editor by calling <code>firePropertyChange</code> with the same
502      * property id. For example, if the dirty state of a nested editor changes
503      * (property id <code>IEditorPart.PROP_DIRTY</code>), this method handles
504      * it by firing a property change event for
505      * <code>IEditorPart.PROP_DIRTY</code> to property listeners on this
506      * multi-page editor.
507      * <p>
508      * Subclasses may extend or reimplement this method.
509      * </p>
510      *
511      * @param propertyId
512      * the id of the property that changed
513      */

514     protected void handlePropertyChange(int propertyId) {
515         firePropertyChange(propertyId);
516     }
517
518     /**
519      * The <code>MultiPageEditorPart</code> implementation of this
520      * <code>IEditorPart</code> method sets its site to the given site, its
521      * input to the given input, and the site's selection provider to a
522      * <code>MultiPageSelectionProvider</code>. Subclasses may extend this
523      * method.
524      *
525      * @param site
526      * The site for which this part is being created; must not be
527      * <code>null</code>.
528      * @param input
529      * The input on which this editor should be created; must not be
530      * <code>null</code>.
531      * @throws PartInitException
532      * If the initialization of the part fails -- currently never.
533      */

534     public void init(IEditorSite site, IEditorInput input)
535             throws PartInitException {
536         setSite(site);
537         setInput(input);
538         site.setSelectionProvider(new MultiPageSelectionProvider(this));
539     }
540
541     /**
542      * The <code>MultiPageEditorPart</code> implementation of this
543      * <code>IEditorPart</code> method returns whether the contents of any of
544      * this multi-page editor's nested editors have changed since the last save.
545      * Pages created with <code>addPage(Control)</code> are ignored.
546      * <p>
547      * Subclasses may extend or reimplement this method.
548      * </p>
549      *
550      * @return <code>true</code> if any of the nested editors are dirty;
551      * <code>false</code> otherwise.
552      */

553     public boolean isDirty() {
554         // use nestedEditors to avoid SWT requests; see bug 12996
555
for (Iterator JavaDoc i = nestedEditors.iterator(); i.hasNext();) {
556             IEditorPart editor = (IEditorPart) i.next();
557             if (editor.isDirty()) {
558                 return true;
559             }
560         }
561         return false;
562     }
563
564     /**
565      * Notifies this multi-page editor that the page with the given id has been
566      * activated. This method is called when the user selects a different tab.
567      * <p>
568      * The <code>MultiPageEditorPart</code> implementation of this method sets
569      * focus to the new page, and notifies the action bar contributor (if there
570      * is one). This checks whether the action bar contributor is an instance of
571      * <code>MultiPageEditorActionBarContributor</code>, and, if so, calls
572      * <code>setActivePage</code> with the active nested editor. This also
573      * fires a selection change event if required.
574      * </p>
575      * <p>
576      * Subclasses may extend this method.
577      * </p>
578      *
579      * @param newPageIndex
580      * the index of the activated page
581      */

582     protected void pageChange(int newPageIndex) {
583         // Deactivate the nested services from the last active service locator.
584
if (activeServiceLocator != null) {
585             activeServiceLocator.deactivate();
586             activeServiceLocator = null;
587         }
588
589         setFocus();
590         IEditorPart activeEditor = getEditor(newPageIndex);
591         IEditorActionBarContributor contributor = getEditorSite()
592                 .getActionBarContributor();
593         if (contributor != null
594                 && contributor instanceof MultiPageEditorActionBarContributor) {
595             ((MultiPageEditorActionBarContributor) contributor)
596                     .setActivePage(activeEditor);
597         }
598         if (activeEditor != null) {
599
600             // Activate the services for the new service locator.
601
final IServiceLocator serviceLocator = activeEditor.getEditorSite();
602             if (serviceLocator instanceof INestable) {
603                 activeServiceLocator = (INestable) serviceLocator;
604                 activeServiceLocator.activate();
605             }
606
607             ISelectionProvider selectionProvider = activeEditor.getSite()
608                     .getSelectionProvider();
609             if (selectionProvider != null) {
610                 ISelectionProvider outerProvider = getSite()
611                         .getSelectionProvider();
612                 if (outerProvider instanceof MultiPageSelectionProvider) {
613                     SelectionChangedEvent event = new SelectionChangedEvent(
614                             selectionProvider, selectionProvider.getSelection());
615
616                     MultiPageSelectionProvider provider = (MultiPageSelectionProvider) outerProvider;
617                     provider.fireSelectionChanged(event);
618                     provider.firePostSelectionChanged(event);
619                 } else {
620                     if (Policy.DEBUG_MPE) {
621                         Tracing.printTrace(TRACING_COMPONENT,
622                                 "MultiPageEditorPart " + getTitle() //$NON-NLS-1$
623
+ " did not propogate selection for " //$NON-NLS-1$
624
+ activeEditor.getTitle());
625                     }
626                 }
627             }
628         }
629     }
630
631     /**
632      * Disposes the given part and its site.
633      *
634      * @param part
635      * The part to dispose; must not be <code>null</code>.
636      */

637     private void disposePart(final IWorkbenchPart part) {
638         SafeRunner.run(new ISafeRunnable() {
639             public void run() {
640                 if (part.getSite() instanceof MultiPageEditorSite) {
641                     MultiPageEditorSite partSite = (MultiPageEditorSite) part
642                             .getSite();
643                     partSite.dispose();
644                 }
645                 part.dispose();
646             }
647
648             public void handleException(Throwable JavaDoc e) {
649                 // Exception has already being logged by Core. Do nothing.
650
}
651         });
652     }
653
654     /**
655      * Removes the page with the given index from this multi-page editor. The
656      * controls for the page are disposed of; if the page has an editor, it is
657      * disposed of too. The page index must be valid.
658      *
659      * @param pageIndex
660      * the index of the page
661      * @see MultiPageEditorPart#addPage(Control)
662      * @see MultiPageEditorPart#addPage(IEditorPart, IEditorInput)
663      */

664     public void removePage(int pageIndex) {
665         Assert.isTrue(pageIndex >= 0 && pageIndex < getPageCount());
666         // get editor (if any) before disposing item
667
IEditorPart editor = getEditor(pageIndex);
668
669         // get control for the item if it's not an editor
670
CTabItem item = getItem(pageIndex);
671         Control pageControl = item.getControl();
672
673         // dispose item before disposing editor, in case there's an exception
674
// in editor's dispose
675
item.dispose();
676
677         if (pageControl != null) {
678             pageControl.dispose();
679         }
680
681         // dispose editor (if any)
682
if (editor != null) {
683             nestedEditors.remove(editor);
684             disposePart(editor);
685         }
686     }
687
688     /**
689      * Sets the currently active page.
690      *
691      * @param pageIndex
692      * the index of the page to be activated; the index must be valid
693      */

694     protected void setActivePage(int pageIndex) {
695         Assert.isTrue(pageIndex >= 0 && pageIndex < getPageCount());
696         getTabFolder().setSelection(pageIndex);
697         pageChange(pageIndex);
698     }
699
700     /**
701      * Sets the control for the given page index. The page index must be valid.
702      *
703      * @param pageIndex
704      * the index of the page
705      * @param control
706      * the control for the specified page, or <code>null</code> to
707      * clear the control
708      */

709     protected void setControl(int pageIndex, Control control) {
710         getItem(pageIndex).setControl(control);
711     }
712
713     /**
714      * The <code>MultiPageEditor</code> implementation of this
715      * <code>IWorkbenchPart</code> method sets focus on the active nested
716      * editor, if there is one.
717      * <p>
718      * Subclasses may extend or reimplement.
719      * </p>
720      */

721     public void setFocus() {
722         setFocus(getActivePage());
723     }
724
725     /**
726      * Sets focus to the control for the given page. If the page has an editor,
727      * this calls its <code>setFocus()</code> method. Otherwise, this calls
728      * <code>setFocus</code> on the control for the page.
729      *
730      * @param pageIndex
731      * the index of the page
732      */

733     private void setFocus(int pageIndex) {
734         final IKeyBindingService service = getSite().getKeyBindingService();
735         if (pageIndex < 0 || pageIndex >= getPageCount()) {
736             // There is no selected page, so deactivate the active service.
737
if (service instanceof INestableKeyBindingService) {
738                 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service;
739                 nestableService.activateKeyBindingService(null);
740             } else {
741                 WorkbenchPlugin
742                         .log("MultiPageEditorPart.setFocus() 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$
743
}
744             return;
745         }
746
747         final IEditorPart editor = getEditor(pageIndex);
748         if (editor != null) {
749             editor.setFocus();
750             // There is no selected page, so deactivate the active service.
751
if (service instanceof INestableKeyBindingService) {
752                 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service;
753                 if (editor != null) {
754                     nestableService.activateKeyBindingService(editor
755                             .getEditorSite());
756                 } else {
757                     nestableService.activateKeyBindingService(null);
758                 }
759             } else {
760                 WorkbenchPlugin
761                         .log("MultiPageEditorPart.setFocus() 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$
762
}
763
764         } else {
765             // There is no selected editor, so deactivate the active service.
766
if (service instanceof INestableKeyBindingService) {
767                 final INestableKeyBindingService nestableService = (INestableKeyBindingService) service;
768                 nestableService.activateKeyBindingService(null);
769             } else {
770                 WorkbenchPlugin
771                         .log("MultiPageEditorPart.setFocus() 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$
772
}
773
774             // Give the page's control focus.
775
final Control control = getControl(pageIndex);
776             if (control != null) {
777                 control.setFocus();
778             }
779         }
780     }
781
782     /**
783      * Sets the image for the page with the given index, or <code>null</code>
784      * to clear the image for the page. The page index must be valid.
785      *
786      * @param pageIndex
787      * the index of the page
788      * @param image
789      * the image, or <code>null</code>
790      */

791     protected void setPageImage(int pageIndex, Image image) {
792         getItem(pageIndex).setImage(image);
793     }
794
795     /**
796      * Sets the text label for the page with the given index. The page index
797      * must be valid. The text label must not be null.
798      *
799      * @param pageIndex
800      * the index of the page
801      * @param text
802      * the text label
803      */

804     protected void setPageText(int pageIndex, String JavaDoc text) {
805         getItem(pageIndex).setText(text);
806     }
807
808     /**
809      * If there is an adapter registered against the subclass of
810      * MultiPageEditorPart return that. Otherwise, delegate to the internal
811      * editor.
812      *
813      * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
814      */

815     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
816         Object JavaDoc result = super.getAdapter(adapter);
817         // restrict delegating to the UI thread for bug 144851
818
// TODO to be fixed properly in 3.2.1
819
if (result == null && Display.getCurrent()!=null) {
820             IEditorPart innerEditor = getActiveEditor();
821             // see bug 138823 - this is a hack
822
if (innerEditor != null && innerEditor != this) {
823                 result = Util.getAdapter(innerEditor, adapter);
824             }
825         }
826         return result;
827     }
828     
829     /**
830      * Find the editors contained in this multi-page editor
831      * whose editor input match the provided input.
832      * @param input the editor input
833      * @return the editors contained in this multi-page editor
834      * whose editor input match the provided input
835      * @since 3.3
836      */

837     public final IEditorPart[] findEditors(IEditorInput input) {
838         List JavaDoc result = new ArrayList JavaDoc();
839         int count = getPageCount();
840         for (int i = 0; i < count; i++) {
841             IEditorPart editor = getEditor(i);
842             if (editor != null
843                     && editor.getEditorInput() != null
844                     && editor.getEditorInput().equals(input)) {
845                 result.add(editor);
846             }
847         }
848         return (IEditorPart[]) result.toArray(new IEditorPart[result.size()]);
849     }
850     
851     /**
852      * Set the active page of this multi-page editor to the
853      * page that contains the given editor part. This method has
854      * no effect of the given editor part is not contained in this
855      * multi-page editor.
856      * @param editorPart the editor part
857      * @since 3.3
858      */

859     public final void setActiveEditor(IEditorPart editorPart) {
860         int count = getPageCount();
861         for (int i = 0; i < count; i++) {
862             IEditorPart editor = getEditor(i);
863             if (editor == editorPart) {
864                 setActivePage(i);
865                 break;
866             }
867         }
868     }
869 }
870
Popular Tags