intial source from http://www.sf.net/projects/wdte
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / views / outline / ModelBasedOutlinePage.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial implementation
10  * 
11  * $Id: ModelBasedOutlinePage.java,v 1.1 2004-09-02 18:26:28 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.views.outline;
15
16 import java.util.List;
17
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;
22
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;
39
40 /**
41  * 
42  */
43 public class ModelBasedOutlinePage extends ContentOutlinePage
44         implements IUpdate {
45
46         // Inner Classes -----------------------------------------------------------
47
48         public class ContentProvider implements ITreeContentProvider {
49
50                 /*
51                  * ITreeContentProvider#getChildren(Object)
52                  */
53                 public Object[] getChildren(Object parentElement) {
54                         if (parentElement instanceof ISourceReference) {
55                                 return model.getChildren((ISourceReference) parentElement);
56                         }
57                         return new Object[0];
58                 }
59
60                 /*
61                  * @see ITreeContentProvider#getParent(Object)
62                  */
63                 public Object getParent(Object element) {
64                         if (element instanceof ISourceReference) {
65                                 return model.getParent((ISourceReference) element);
66                         }
67                         return null;
68                 }
69
70                 /*
71                  * @see ITreeContentProvider#hasChildren(Object)
72                  */
73                 public boolean hasChildren(Object element) {
74                         return getChildren(element).length > 0;
75                 }
76
77                 /*
78                  * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
79                  */
80                 public Object[] getElements(Object inputElement) {
81                         return model.getElements();
82                 }
83
84                 /* 
85                  * @see org.eclipse.jface.viewers.IContentProvider#dispose()
86                  */
87                 public void dispose() {
88                 }
89
90                 /*
91                  * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
92                  */
93                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
94                         if (oldInput != newInput) {
95                                 if (newInput instanceof ISourceModel) {
96                                         model = (ISourceModel) newInput;
97                                 }          
98                         }
99                 }
100
101         }
102
103         /**
104          * This action toggles whether this outline page links its selection
105          * to the active editor.
106          */
107         private class ToggleLinkingAction extends ResourceAction {
108                 
109                 /**
110                  * Constructs a new action.
111                  */
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);
120                         } else {
121                                 setEnabled(false);
122                         }
123                 }
124
125                 /*
126                  * @see org.eclipse.jface.action.Action#run()
127                  */
128                 public void run() {
129                         if ((preferenceStore != null)
130                          && (linkWithEditorPreferenceKey != null)) {
131                                 valueChanged(isChecked(), true);
132                         }
133                 }
134         
135                 // Private Methods -----------------------------------------------------
136
137                 /**
138                  * Updates whether the outline page is linked to the active editor.
139                  * 
140                  * @param checked Whether linking is enabled
141                  * @param store Whether the new state should be written back as a 
142                  *        preference
143                  */
144                 private void valueChanged(final boolean checked, boolean store) {
145                         setChecked(checked);
146                         BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
147                                 new Runnable() {
148                                         public void run() {
149                                                 editor.synchronizeOutlinePage();
150                                         }
151                                 });
152                         if (store) {
153                                 preferenceStore.setValue(
154                                         linkWithEditorPreferenceKey, checked);
155                         }
156                 }
157
158         }
159
160         // Instance Variables ------------------------------------------------------
161
162         /**
163          * The associated editor.
164          */
165         private StructuredTextEditor editor;
166
167         /**
168          * The structured source model.
169          */
170         private ISourceModel model;
171
172         /**
173          * The preference store.
174          */
175         private IPreferenceStore preferenceStore;
176
177         /**
178          * The preference key which specifies whether the outline page is linked to
179          * the active editor.
180          */
181         private String linkWithEditorPreferenceKey;
182
183         // Constructors ------------------------------------------------------------
184
185         /**
186          * Constructor.
187          * 
188          * @param editor The associated structured text editor
189          */
190         public ModelBasedOutlinePage(StructuredTextEditor editor) {
191                 this.editor = editor;
192         }
193
194         // ContentOutlinePage Implementation ---------------------------------------
195
196         /*
197          * @see org.eclipse.ui.part.IPage#createControl(Composite)
198          */
199         public void createControl(Composite parent) {
200                 super.createControl(parent);
201                 TreeViewer viewer = getTreeViewer();
202                 viewer.setContentProvider(new ContentProvider());
203         }
204
205         /*
206          * @see org.eclipse.ui.part.IPage#dispose()
207          */
208         public void dispose() {
209                 if (editor != null) {
210                         editor.outlinePageClosed();
211                         editor = null;
212                 }
213                 super.dispose();
214         }
215
216         /*
217          * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager, IToolBarManager, IStatusLineManager)
218          */
219         public void makeContributions(IMenuManager menuManager,
220                 IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
221                 if (toolBarManager != null) {
222                         toolBarManager.add(new ToggleLinkingAction());
223                 }
224                 super.makeContributions(menuManager, toolBarManager, statusLineManager);
225         }
226
227         // IUpdate Implementation --------------------------------------------------
228
229         /**
230          * @see IUpdate#update()
231          */
232         public void update() {
233                 ISourceModel model = editor.getSourceModel();
234                 if (model != null) {
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);
241                                         viewer.expandAll();
242                                         control.setRedraw(true);
243                                 }
244                         }
245                 }
246         }
247
248         // Public Methods ----------------------------------------------------------
249
250         /**
251          * Selects a specific element in the outline page.
252          * 
253          * @param element the element to select
254          */
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;
266                                         } else {
267                                                 selection = new StructuredSelection(element);
268                                         }
269                                         viewer.setSelection(selection, true);
270                                 }
271                         }
272                 }
273         }
274
275         // Protected Methods -------------------------------------------------------
276
277         protected final StructuredTextEditor getEditor() {
278                 return editor;
279         }
280
281         protected final void setPreferenceStore(IPreferenceStore store) {
282                 preferenceStore = store;
283         }
284
285         protected final void setLinkWithEditorPreferenceKey(String key) {
286                 linkWithEditorPreferenceKey = key;
287         }
288
289 }