KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > contentoutline > ContentOutline


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.views.contentoutline;
13
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.views.ViewsPlugin;
24 import org.eclipse.ui.internal.views.contentoutline.ContentOutlineMessages;
25 import org.eclipse.ui.part.IContributedContentsView;
26 import org.eclipse.ui.part.IPage;
27 import org.eclipse.ui.part.IPageBookViewPage;
28 import org.eclipse.ui.part.IPageSite;
29 import org.eclipse.ui.part.MessagePage;
30 import org.eclipse.ui.part.PageBook;
31 import org.eclipse.ui.part.PageBookView;
32
33 /**
34  * Main class for the Content Outline View.
35  * <p>
36  * This standard view has id <code>"org.eclipse.ui.views.ContentOutline"</code>.
37  * </p>
38  * When a <b>content outline view</b> notices an editor being activated, it
39  * asks the editor whether it has a <b>content outline page</b> to include
40  * in the outline view. This is done using <code>getAdapter</code>:
41  * <pre>
42  * IEditorPart editor = ...;
43  * IContentOutlinePage outlinePage = (IContentOutlinePage) editor.getAdapter(IContentOutlinePage.class);
44  * if (outlinePage != null) {
45  * // editor wishes to contribute outlinePage to content outline view
46  * }
47  * </pre>
48  * If the editor supports a content outline page, the editor instantiates
49  * and configures the page, and returns it. This page is then added to the
50  * content outline view (a pagebook which presents one page at a time) and
51  * immediately made the current page (the content outline view need not be
52  * visible). If the editor does not support a content outline page, the content
53  * outline view shows a special default page which makes it clear to the user
54  * that the content outline view is disengaged. A content outline page is free
55  * to report selection events; the content outline view forwards these events
56  * along to interested parties. When the content outline view notices a
57  * different editor being activated, it flips to the editor's corresponding
58  * content outline page. When the content outline view notices an editor being
59  * closed, it destroys the editor's corresponding content outline page.
60  * </p>
61  * <p>
62  * The workbench will automatically instantiate this class when a Content
63  * Outline view is needed for a workbench window. This class was not intended
64  * to be instantiated or subclassed by clients.
65  * </p>
66  */

67 public class ContentOutline extends PageBookView implements ISelectionProvider,
68         ISelectionChangedListener {
69
70
71
72     /**
73      * The plugin prefix.
74      */

75     public static final String JavaDoc PREFIX = PlatformUI.PLUGIN_ID + "."; //$NON-NLS-1$
76

77     /**
78      * Help context id used for the content outline view
79      * (value <code>"org.eclipse.ui.content_outline_context"</code>).
80      */

81     public static final String JavaDoc CONTENT_OUTLINE_VIEW_HELP_CONTEXT_ID = PREFIX
82             + "content_outline_context";//$NON-NLS-1$
83

84     /**
85      * Message to show on the default page.
86      */

87     private String JavaDoc defaultText =ContentOutlineMessages.ContentOutline_noOutline;
88
89     /**
90      * Creates a content outline view with no content outline pages.
91      */

92     public ContentOutline() {
93         super();
94     }
95
96     /* (non-Javadoc)
97      * Method declared on ISelectionProvider.
98      */

99     public void addSelectionChangedListener(ISelectionChangedListener listener) {
100         getSelectionProvider().addSelectionChangedListener(listener);
101     }
102
103     /* (non-Javadoc)
104      * Method declared on PageBookView.
105      */

106     protected IPage createDefaultPage(PageBook book) {
107         MessagePage page = new MessagePage();
108         initPage(page);
109         page.createControl(book);
110         page.setMessage(defaultText);
111         return page;
112     }
113
114     /**
115      * The <code>PageBookView</code> implementation of this <code>IWorkbenchPart</code>
116      * method creates a <code>PageBook</code> control with its default page showing.
117      */

118     public void createPartControl(Composite parent) {
119         super.createPartControl(parent);
120         PlatformUI.getWorkbench().getHelpSystem().setHelp(getPageBook(),
121                 CONTENT_OUTLINE_VIEW_HELP_CONTEXT_ID);
122     }
123
124     /* (non-Javadoc)
125      * Method declared on PageBookView.
126      */

127     protected PageRec doCreatePage(IWorkbenchPart part) {
128         // Try to get an outline page.
129
Object JavaDoc obj = ViewsPlugin.getAdapter(part, IContentOutlinePage.class, false);
130         if (obj instanceof IContentOutlinePage) {
131             IContentOutlinePage page = (IContentOutlinePage) obj;
132             if (page instanceof IPageBookViewPage) {
133                 initPage((IPageBookViewPage) page);
134             }
135             page.createControl(getPageBook());
136             return new PageRec(part, page);
137         }
138         // There is no content outline
139
return null;
140     }
141
142     /* (non-Javadoc)
143      * Method declared on PageBookView.
144      */

145     protected void doDestroyPage(IWorkbenchPart part, PageRec rec) {
146         IContentOutlinePage page = (IContentOutlinePage) rec.page;
147         page.dispose();
148         rec.dispose();
149     }
150
151     /* (non-Javadoc)
152      * Method declared on IAdaptable.
153      */

154     public Object JavaDoc getAdapter(Class JavaDoc key) {
155         if (key == IContributedContentsView.class) {
156             return new IContributedContentsView() {
157                 public IWorkbenchPart getContributingPart() {
158                     return getContributingEditor();
159                 }
160             };
161         }
162         return super.getAdapter(key);
163     }
164
165     /* (non-Javadoc)
166      * Method declared on PageBookView.
167      */

168     protected IWorkbenchPart getBootstrapPart() {
169         IWorkbenchPage page = getSite().getPage();
170         if (page != null) {
171             return page.getActiveEditor();
172         }
173
174         return null;
175     }
176
177     /**
178      * Returns the editor which contributed the current
179      * page to this view.
180      *
181      * @return the editor which contributed the current page
182      * or <code>null</code> if no editor contributed the current page
183      */

184     private IWorkbenchPart getContributingEditor() {
185         return getCurrentContributingPart();
186     }
187
188     /* (non-Javadoc)
189      * Method declared on ISelectionProvider.
190      */

191     public ISelection getSelection() {
192         // get the selection from the selection provider
193
return getSelectionProvider().getSelection();
194     }
195
196     /* (non-Javadoc)
197      * Method declared on PageBookView.
198      * We only want to track editors.
199      */

200     protected boolean isImportant(IWorkbenchPart part) {
201         //We only care about editors
202
return (part instanceof IEditorPart);
203     }
204
205     /* (non-Javadoc)
206      * Method declared on IViewPart.
207      * Treat this the same as part activation.
208      */

209     public void partBroughtToTop(IWorkbenchPart part) {
210         partActivated(part);
211     }
212
213     /* (non-Javadoc)
214      * Method declared on ISelectionProvider.
215      */

216     public void removeSelectionChangedListener(
217             ISelectionChangedListener listener) {
218         getSelectionProvider().removeSelectionChangedListener(listener);
219     }
220
221     /* (non-Javadoc)
222      * Method declared on ISelectionChangedListener.
223      */

224     public void selectionChanged(SelectionChangedEvent event) {
225         getSelectionProvider().selectionChanged(event);
226     }
227
228     /* (non-Javadoc)
229      * Method declared on ISelectionProvider.
230      */

231     public void setSelection(ISelection selection) {
232         getSelectionProvider().setSelection(selection);
233     }
234
235     /**
236      * The <code>ContentOutline</code> implementation of this <code>PageBookView</code> method
237      * extends the behavior of its parent to use the current page as a selection provider.
238      *
239      * @param pageRec the page record containing the page to show
240      */

241     protected void showPageRec(PageRec pageRec) {
242         IPageSite pageSite = getPageSite(pageRec.page);
243         ISelectionProvider provider = pageSite.getSelectionProvider();
244         if (provider == null && (pageRec.page instanceof IContentOutlinePage)) {
245             // This means that the page did not set a provider during its initialization
246
// so for backward compatibility we will set the page itself as the provider.
247
pageSite.setSelectionProvider((IContentOutlinePage) pageRec.page);
248         }
249         super.showPageRec(pageRec);
250     }
251 }
252
Popular Tags