001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.contentoutline;
011:
012: import org.eclipse.core.runtime.ListenerList;
013: import org.eclipse.core.runtime.Platform;
014: import org.eclipse.jface.util.SafeRunnable;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.jface.viewers.ISelectionChangedListener;
017: import org.eclipse.jface.viewers.SelectionChangedEvent;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.jface.viewers.TreeViewer;
020: import org.eclipse.swt.SWT;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Control;
023: import org.eclipse.ui.part.IPageSite;
024: import org.eclipse.ui.part.Page;
025:
026: /**
027: * An abstract base class for content outline pages.
028: * <p>
029: * Clients who are defining an editor may elect to provide a corresponding
030: * content outline page. This content outline page will be presented to the
031: * user via the standard Content Outline View (the user decides whether their
032: * workbench window contains this view) whenever that editor is active.
033: * This class should be subclassed.
034: * </p>
035: * <p>
036: * Internally, each content outline page consists of a standard tree viewer;
037: * selections made in the tree viewer are reported as selection change events
038: * by the page (which is a selection provider). The tree viewer is not created
039: * until <code>createPage</code> is called; consequently, subclasses must extend
040: * <code>createControl</code> to configure the tree viewer with a proper content
041: * provider, label provider, and input element.
042: * </p>
043: * <p>
044: * Note that those wanting to use a control other than internally created
045: * <code>TreeViewer</code> will need to implement
046: * <code>IContentOutlinePage</code> directly rather than subclassing this class.
047: * </p>
048: */
049: public abstract class ContentOutlinePage extends Page implements
050: IContentOutlinePage, ISelectionChangedListener {
051: private ListenerList selectionChangedListeners = new ListenerList();
052:
053: private TreeViewer treeViewer;
054:
055: /**
056: * Create a new content outline page.
057: */
058: protected ContentOutlinePage() {
059: super ();
060: }
061:
062: /* (non-Javadoc)
063: * Method declared on ISelectionProvider.
064: */
065: public void addSelectionChangedListener(
066: ISelectionChangedListener listener) {
067: selectionChangedListeners.add(listener);
068: }
069:
070: /**
071: * The <code>ContentOutlinePage</code> implementation of this
072: * <code>IContentOutlinePage</code> method creates a tree viewer. Subclasses
073: * must extend this method configure the tree viewer with a proper content
074: * provider, label provider, and input element.
075: * @param parent
076: */
077: public void createControl(Composite parent) {
078: treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL
079: | SWT.V_SCROLL);
080: treeViewer.addSelectionChangedListener(this );
081: }
082:
083: /**
084: * Fires a selection changed event.
085: *
086: * @param selection the new selection
087: */
088: protected void fireSelectionChanged(ISelection selection) {
089: // create an event
090: final SelectionChangedEvent event = new SelectionChangedEvent(
091: this , selection);
092:
093: // fire the event
094: Object[] listeners = selectionChangedListeners.getListeners();
095: for (int i = 0; i < listeners.length; ++i) {
096: final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i];
097: Platform.run(new SafeRunnable() {
098: public void run() {
099: 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: }
|