KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.ui.views.contentoutline;
12
13 import org.eclipse.core.runtime.ListenerList;
14 import org.eclipse.core.runtime.Platform;
15 import org.eclipse.jface.util.SafeRunnable;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.ui.part.IPageSite;
25 import org.eclipse.ui.part.Page;
26
27 /**
28  * An abstract base class for content outline pages.
29  * <p>
30  * Clients who are defining an editor may elect to provide a corresponding
31  * content outline page. This content outline page will be presented to the
32  * user via the standard Content Outline View (the user decides whether their
33  * workbench window contains this view) whenever that editor is active.
34  * This class should be subclassed.
35  * </p>
36  * <p>
37  * Internally, each content outline page consists of a standard tree viewer;
38  * selections made in the tree viewer are reported as selection change events
39  * by the page (which is a selection provider). The tree viewer is not created
40  * until <code>createPage</code> is called; consequently, subclasses must extend
41  * <code>createControl</code> to configure the tree viewer with a proper content
42  * provider, label provider, and input element.
43  * </p>
44  * <p>
45  * Note that those wanting to use a control other than internally created
46  * <code>TreeViewer</code> will need to implement
47  * <code>IContentOutlinePage</code> directly rather than subclassing this class.
48  * </p>
49  */

50 public abstract class ContentOutlinePage extends Page implements
51         IContentOutlinePage, ISelectionChangedListener {
52     private ListenerList selectionChangedListeners = new ListenerList();
53
54     private TreeViewer treeViewer;
55
56     /**
57      * Create a new content outline page.
58      */

59     protected ContentOutlinePage() {
60         super();
61     }
62
63     /* (non-Javadoc)
64      * Method declared on ISelectionProvider.
65      */

66     public void addSelectionChangedListener(ISelectionChangedListener listener) {
67         selectionChangedListeners.add(listener);
68     }
69
70     /**
71      * The <code>ContentOutlinePage</code> implementation of this
72      * <code>IContentOutlinePage</code> method creates a tree viewer. Subclasses
73      * must extend this method configure the tree viewer with a proper content
74      * provider, label provider, and input element.
75      * @param parent
76      */

77     public void createControl(Composite parent) {
78         treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
79                 | SWT.V_SCROLL);
80         treeViewer.addSelectionChangedListener(this);
81     }
82
83     /**
84      * Fires a selection changed event.
85      *
86      * @param selection the new selection
87      */

88     protected void fireSelectionChanged(ISelection selection) {
89         // create an event
90
final SelectionChangedEvent event = new SelectionChangedEvent(this,
91                 selection);
92
93         // fire the event
94
Object JavaDoc[] listeners = selectionChangedListeners.getListeners();
95         for (int i = 0; i < listeners.length; ++i) {
96             final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
97             Platform.run(new SafeRunnable() {
98                 public void run() {
99                     l.selectionChanged(event);
100                 }
101             });
102         }
103     }
104
105     /* (non-Javadoc)
106      * Method declared on IPage (and Page).
107      */

108     public Control getControl() {
109         if (treeViewer == null) {
110             return null;
111         }
112         return treeViewer.getControl();
113     }
114
115     /* (non-Javadoc)
116      * Method declared on ISelectionProvider.
117      */

118     public ISelection getSelection() {
119         if (treeViewer == null) {
120             return StructuredSelection.EMPTY;
121         }
122         return treeViewer.getSelection();
123     }
124
125     /**
126      * Returns this page's tree viewer.
127      *
128      * @return this page's tree viewer, or <code>null</code> if
129      * <code>createControl</code> has not been called yet
130      */

131     protected TreeViewer getTreeViewer() {
132         return treeViewer;
133     }
134
135     /*
136      * (non-Javadoc)
137      * @see org.eclipse.ui.part.IPageBookViewPage#init(org.eclipse.ui.part.IPageSite)
138      */

139     public void init(IPageSite pageSite) {
140         super.init(pageSite);
141         pageSite.setSelectionProvider(this);
142     }
143
144     /* (non-Javadoc)
145      * Method declared on ISelectionProvider.
146      */

147     public void removeSelectionChangedListener(
148             ISelectionChangedListener listener) {
149         selectionChangedListeners.remove(listener);
150     }
151
152     /* (non-Javadoc)
153      * Method declared on ISelectionChangeListener.
154      * Gives notification that the tree selection has changed.
155      */

156     public void selectionChanged(SelectionChangedEvent event) {
157         fireSelectionChanged(event.getSelection());
158     }
159
160     /**
161      * Sets focus to a part in the page.
162      */

163     public void setFocus() {
164         treeViewer.getControl().setFocus();
165     }
166
167     /* (non-Javadoc)
168      * Method declared on ISelectionProvider.
169      */

170     public void setSelection(ISelection selection) {
171         if (treeViewer != null) {
172             treeViewer.setSelection(selection);
173         }
174     }
175 }
176
Popular Tags