adding plug-in provider xdebug.core and xdebug.ui to fix ticket #640
[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.2 2006-10-21 23:13:54 pombredanne 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 implements
44                 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,
92                  *      Object, Object)
93                  */
94                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
95                         if (oldInput != newInput) {
96                                 if (newInput instanceof ISourceModel) {
97                                         model = (ISourceModel) newInput;
98                                 }
99                         }
100                 }
101
102         }
103
104         /**
105          * This action toggles whether this outline page links its selection to the
106          * active editor.
107          */
108         private class ToggleLinkingAction extends ResourceAction {
109
110                 /**
111                  * Constructs a new action.
112                  */
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);
121                         } else {
122                                 setEnabled(false);
123                         }
124                 }
125
126                 /*
127                  * @see org.eclipse.jface.action.Action#run()
128                  */
129                 public void run() {
130                         if ((preferenceStore != null)
131                                         && (linkWithEditorPreferenceKey != null)) {
132                                 valueChanged(isChecked(), true);
133                         }
134                 }
135
136                 // Private Methods -----------------------------------------------------
137
138                 /**
139                  * Updates whether the outline page is linked to the active editor.
140                  * 
141                  * @param checked
142                  *            Whether linking is enabled
143                  * @param store
144                  *            Whether the new state should be written back as a
145                  *            preference
146                  */
147                 private void valueChanged(final boolean checked, boolean store) {
148                         setChecked(checked);
149                         BusyIndicator.showWhile(getTreeViewer().getControl().getDisplay(),
150                                         new Runnable() {
151                                                 public void run() {
152                                                         editor.synchronizeOutlinePage();
153                                                 }
154                                         });
155                         if (store) {
156                                 preferenceStore.setValue(linkWithEditorPreferenceKey, checked);
157                         }
158                 }
159
160         }
161
162         // Instance Variables ------------------------------------------------------
163
164         /**
165          * The associated editor.
166          */
167         private StructuredTextEditor editor;
168
169         /**
170          * The structured source model.
171          */
172         private ISourceModel model;
173
174         /**
175          * The preference store.
176          */
177         private IPreferenceStore preferenceStore;
178
179         /**
180          * The preference key which specifies whether the outline page is linked to
181          * the active editor.
182          */
183         private String linkWithEditorPreferenceKey;
184
185         // Constructors ------------------------------------------------------------
186
187         /**
188          * Constructor.
189          * 
190          * @param editor
191          *            The associated structured text editor
192          */
193         public ModelBasedOutlinePage(StructuredTextEditor editor) {
194                 this.editor = editor;
195         }
196
197         // ContentOutlinePage Implementation ---------------------------------------
198
199         /*
200          * @see org.eclipse.ui.part.IPage#createControl(Composite)
201          */
202         public void createControl(Composite parent) {
203                 super.createControl(parent);
204                 TreeViewer viewer = getTreeViewer();
205                 viewer.setContentProvider(new ContentProvider());
206         }
207
208         /*
209          * @see org.eclipse.ui.part.IPage#dispose()
210          */
211         public void dispose() {
212                 if (editor != null) {
213                         editor.outlinePageClosed();
214                         editor = null;
215                 }
216                 super.dispose();
217         }
218
219         /*
220          * @see org.eclipse.ui.part.Page#makeContributions(IMenuManager,
221          *      IToolBarManager, IStatusLineManager)
222          */
223         public void makeContributions(IMenuManager menuManager,
224                         IToolBarManager toolBarManager, IStatusLineManager statusLineManager) {
225                 if (toolBarManager != null) {
226                         toolBarManager.add(new ToggleLinkingAction());
227                 }
228                 super.makeContributions(menuManager, toolBarManager, statusLineManager);
229         }
230
231         // IUpdate Implementation --------------------------------------------------
232
233         /**
234          * @see IUpdate#update()
235          */
236         public void update() {
237                 ISourceModel model = editor.getSourceModel();
238                 if (model != null) {
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);
245                                         viewer.expandAll();
246                                         control.setRedraw(true);
247                                 }
248                         }
249                 }
250         }
251
252         // Public Methods ----------------------------------------------------------
253
254         /**
255          * Selects a specific element in the outline page.
256          * 
257          * @param element
258          *            the element to select
259          */
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;
270                                         } else {
271                                                 selection = new StructuredSelection(element);
272                                         }
273                                         viewer.setSelection(selection, true);
274                                 }
275                         }
276                 }
277         }
278
279         // Protected Methods -------------------------------------------------------
280
281         protected final StructuredTextEditor getEditor() {
282                 return editor;
283         }
284
285         protected final void setPreferenceStore(IPreferenceStore store) {
286                 preferenceStore = store;
287         }
288
289         protected final void setLinkWithEditorPreferenceKey(String key) {
290                 linkWithEditorPreferenceKey = key;
291         }
292
293 }