2 * Copyright (c) 2004 Christopher Lenz and others
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial implementation
11 * $Id: ModelBasedOutlinePage.java,v 1.1 2004-09-02 18:26:28 jsurfer Exp $
14 package net.sourceforge.phpeclipse.ui.views.outline;
16 import java.util.List;
18 import net.sourceforge.phpeclipse.core.model.ISourceModel;
19 import net.sourceforge.phpeclipse.core.model.ISourceReference;
20 import net.sourceforge.phpeclipse.ui.editor.StructuredTextEditor;
21 import net.sourceforge.phpeclipse.ui.internal.WebUIMessages;
23 import org.eclipse.jface.action.IMenuManager;
24 import org.eclipse.jface.action.IStatusLineManager;
25 import org.eclipse.jface.action.IToolBarManager;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.ITreeContentProvider;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.jface.viewers.TreeViewer;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.swt.custom.BusyIndicator;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.ui.texteditor.IUpdate;
37 import org.eclipse.ui.texteditor.ResourceAction;
38 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
43 public class ModelBasedOutlinePage extends ContentOutlinePage
46 // Inner Classes -----------------------------------------------------------
48 public class ContentProvider implements ITreeContentProvider {
51 * ITreeContentProvider#getChildren(Object)
53 public Object[] getChildren(Object parentElement) {
54 if (parentElement instanceof ISourceReference) {
55 return model.getChildren((ISourceReference) parentElement);
61 * @see ITreeContentProvider#getParent(Object)
63 public Object getParent(Object element) {
64 if (element instanceof ISourceReference) {
65 return model.getParent((ISourceReference) element);
71 * @see ITreeContentProvider#hasChildren(Object)
73 public boolean hasChildren(Object element) {
74 return getChildren(element).length > 0;
78 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
80 public Object[] getElements(Object inputElement) {
81 return model.getElements();
85 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
87 public void dispose() {
91 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
93 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
94 if (oldInput != newInput) {
95 if (newInput instanceof ISourceModel) {
96 model = (ISourceModel) newInput;
104 * This action toggles whether this outline page links its selection
105 * to the active editor.
107 private class ToggleLinkingAction extends ResourceAction {
110 * Constructs a new action.
112 public ToggleLinkingAction() {
113 super(WebUIMessages.getResourceBundle(),
114 "OutlinePage.linkWithEditor."); //$NON-NLS-1$
115 if ((preferenceStore != null)
116 && (linkWithEditorPreferenceKey != null)) {
117 boolean checked = preferenceStore.getBoolean(
118 linkWithEditorPreferenceKey);
119 valueChanged(checked, false);
126 * @see org.eclipse.jface.action.Action#run()
129 if ((preferenceStore != null)
130 && (linkWithEditorPreferenceKey != null)) {
131 valueChanged(isChecked(), true);
135 // Private Methods -----------------------------------------------------
138 * Updates whether the outline page is linked to the active editor.
140 * @param checked Whether linking is enabled
141 * @param store Whether the new state should be written back as a
144 private void valueChanged(final boolean checked, boolean store) {
146 BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
149 editor.synchronizeOutlinePage();
153 preferenceStore.setValue(
154 linkWithEditorPreferenceKey, checked);
160 // Instance Variables ------------------------------------------------------
163 * The associated editor.
165 private StructuredTextEditor editor;
168 * The structured source model.
170 private ISourceModel model;
173 * The preference store.
175 private IPreferenceStore preferenceStore;
178 * The preference key which specifies whether the outline page is linked to
181 private String linkWithEditorPreferenceKey;
183 // Constructors ------------------------------------------------------------
188 * @param editor The associated structured text editor
190 public ModelBasedOutlinePage(StructuredTextEditor editor) {
191 this.editor = editor;
194 // ContentOutlinePage Implementation ---------------------------------------
197 * @see org.eclipse.ui.part.IPage#createControl(Composite)
199 public void createControl(Composite parent) {
200 super.createControl(parent);
201 TreeViewer viewer = getTreeViewer();
202 viewer.setContentProvider(new ContentProvider());
206 * @see org.eclipse.ui.part.IPage#dispose()
208 public void dispose() {
209 if (editor != null) {
210 editor.outlinePageClosed();
217 * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager, IToolBarManager, IStatusLineManager)
219 public void makeContributions(IMenuManager menuManager,
220 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
221 if (toolBarManager != null) {
222 toolBarManager.add(new ToggleLinkingAction());
224 super.makeContributions(menuManager, toolBarManager, statusLineManager);
227 // IUpdate Implementation --------------------------------------------------
230 * @see IUpdate#update()
232 public void update() {
233 ISourceModel model = editor.getSourceModel();
235 TreeViewer viewer = getTreeViewer();
236 if (viewer != null) {
237 Control control = viewer.getControl();
238 if ((control != null) && !control.isDisposed()) {
239 control.setRedraw(false);
240 viewer.setInput(model);
242 control.setRedraw(true);
248 // Public Methods ----------------------------------------------------------
251 * Selects a specific element in the outline page.
253 * @param element the element to select
255 public void select(ISourceReference element) {
256 TreeViewer viewer = getTreeViewer();
257 if (viewer != null) {
258 ISelection selection = viewer.getSelection();
259 if (selection instanceof IStructuredSelection) {
260 IStructuredSelection structuredSelection =
261 (IStructuredSelection) selection;
262 List elements = structuredSelection.toList();
263 if (!elements.contains(element)) {
264 if (element == null) {
265 selection = StructuredSelection.EMPTY;
267 selection = new StructuredSelection(element);
269 viewer.setSelection(selection, true);
275 // Protected Methods -------------------------------------------------------
277 protected final StructuredTextEditor getEditor() {
281 protected final void setPreferenceStore(IPreferenceStore store) {
282 preferenceStore = store;
285 protected final void setLinkWithEditorPreferenceKey(String key) {
286 linkWithEditorPreferenceKey = key;