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.2 2006-10-21 23:13:54 pombredanne 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 implements
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,
94 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
95 if (oldInput != newInput) {
96 if (newInput instanceof ISourceModel) {
97 model = (ISourceModel) newInput;
105 * This action toggles whether this outline page links its selection to the
108 private class ToggleLinkingAction extends ResourceAction {
111 * Constructs a new action.
113 public ToggleLinkingAction() {
114 super(WebUIMessages.getResourceBundle(),
115 "OutlinePage.linkWithEditor."); //$NON-NLS-1$
116 if ((preferenceStore != null)
117 && (linkWithEditorPreferenceKey != null)) {
118 boolean checked = preferenceStore
119 .getBoolean(linkWithEditorPreferenceKey);
120 valueChanged(checked, false);
127 * @see org.eclipse.jface.action.Action#run()
130 if ((preferenceStore != null)
131 && (linkWithEditorPreferenceKey != null)) {
132 valueChanged(isChecked(), true);
136 // Private Methods -----------------------------------------------------
139 * Updates whether the outline page is linked to the active editor.
142 * Whether linking is enabled
144 * Whether the new state should be written back as a
147 private void valueChanged(final boolean checked, boolean store) {
149 BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
152 editor.synchronizeOutlinePage();
156 preferenceStore.setValue(linkWithEditorPreferenceKey, checked);
162 // Instance Variables ------------------------------------------------------
165 * The associated editor.
167 private StructuredTextEditor editor;
170 * The structured source model.
172 private ISourceModel model;
175 * The preference store.
177 private IPreferenceStore preferenceStore;
180 * The preference key which specifies whether the outline page is linked to
183 private String linkWithEditorPreferenceKey;
185 // Constructors ------------------------------------------------------------
191 * The associated structured text editor
193 public ModelBasedOutlinePage(StructuredTextEditor editor) {
194 this.editor = editor;
197 // ContentOutlinePage Implementation ---------------------------------------
200 * @see org.eclipse.ui.part.IPage#createControl(Composite)
202 public void createControl(Composite parent) {
203 super.createControl(parent);
204 TreeViewer viewer = getTreeViewer();
205 viewer.setContentProvider(new ContentProvider());
209 * @see org.eclipse.ui.part.IPage#dispose()
211 public void dispose() {
212 if (editor != null) {
213 editor.outlinePageClosed();
220 * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager,
221 * IToolBarManager, IStatusLineManager)
223 public void makeContributions(IMenuManager menuManager,
224 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
225 if (toolBarManager != null) {
226 toolBarManager.add(new ToggleLinkingAction());
228 super.makeContributions(menuManager, toolBarManager, statusLineManager);
231 // IUpdate Implementation --------------------------------------------------
234 * @see IUpdate#update()
236 public void update() {
237 ISourceModel model = editor.getSourceModel();
239 TreeViewer viewer = getTreeViewer();
240 if (viewer != null) {
241 Control control = viewer.getControl();
242 if ((control != null) && !control.isDisposed()) {
243 control.setRedraw(false);
244 viewer.setInput(model);
246 control.setRedraw(true);
252 // Public Methods ----------------------------------------------------------
255 * Selects a specific element in the outline page.
258 * the element to select
260 public void select(ISourceReference element) {
261 TreeViewer viewer = getTreeViewer();
262 if (viewer != null) {
263 ISelection selection = viewer.getSelection();
264 if (selection instanceof IStructuredSelection) {
265 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
266 List elements = structuredSelection.toList();
267 if (!elements.contains(element)) {
268 if (element == null) {
269 selection = StructuredSelection.EMPTY;
271 selection = new StructuredSelection(element);
273 viewer.setSelection(selection, true);
279 // Protected Methods -------------------------------------------------------
281 protected final StructuredTextEditor getEditor() {
285 protected final void setPreferenceStore(IPreferenceStore store) {
286 preferenceStore = store;
289 protected final void setLinkWithEditorPreferenceKey(String key) {
290 linkWithEditorPreferenceKey = key;