1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
19 import java.util.TreeSet;
21 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPOutlineInfo;
24 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPParser;
25 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPSegment;
26 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPSegmentWithChildren;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.jface.text.BadPositionCategoryException;
30 import org.eclipse.jface.text.DefaultPositionUpdater;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IPositionUpdater;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.jface.viewers.ITreeContentProvider;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.TreeViewer;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.swt.graphics.Image;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.ui.texteditor.IDocumentProvider;
44 import org.eclipse.ui.texteditor.ITextEditor;
45 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
48 * A content outline page which always represents the functions of the
49 * connected PHPEditor.
51 public class PHPContentOutlinePage extends ContentOutlinePage {
52 private static final String ERROR = "error"; //$NON-NLS-1$
53 private static final String WARNING = "warning"; //$NON-NLS-1$
55 protected static class SegmentComparator implements Comparator {
56 public int compare(Object o1, Object o2) {
57 if (o1 instanceof PHPSegmentWithChildren && !(o2 instanceof PHPSegmentWithChildren)) {
60 if (o2 instanceof PHPSegmentWithChildren && !(o1 instanceof PHPSegmentWithChildren)) {
63 return ((PHPSegment) o1).toString().compareToIgnoreCase(((PHPSegment) o2).toString());
68 * Divides the editor's document into ten segments and provides elements for them.
70 protected class ContentProvider implements ITreeContentProvider {
72 protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
73 protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
74 protected List fContent = new ArrayList(10);
75 protected TreeSet fVariables = new TreeSet();
77 // private String getIdentifier(String text, int firstIndex) {
78 // int i = firstIndex;
80 // int textLength = text.length();
81 // StringBuffer identifier = new StringBuffer();
82 // while (i < textLength) {
83 // c = text.charAt(i++);
84 // if (Character.isJavaIdentifierPart(c) || (c == '$')) {
85 // identifier.append(c);
86 // } else if ((i == firstIndex + 1) && (c == '$')) {
87 // identifier.append(c);
89 // return identifier.toString();
95 protected void parse(IDocument document) {
97 // int lines = document.getNumberOfLines();
98 // int increment = Math.max(Math.round((float) (lines / 10)), 10);
102 String text = document.get();
103 PHPParser parser = new PHPParser(null);
105 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
106 fVariables = outlineInfo.getVariables();
108 PHPSegmentWithChildren declarations = outlineInfo.getDeclarations();
110 for (int i = 0; i < declarations.size(); i++) {
111 temp = declarations.get(i);
114 Collections.sort(fContent, new SegmentComparator());
119 * @see IContentProvider#inputChanged(Viewer, Object, Object)
121 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
122 if (oldInput != null) {
123 IDocument document = fDocumentProvider.getDocument(oldInput);
124 if (document != null) {
126 document.removePositionCategory(SEGMENTS);
127 } catch (BadPositionCategoryException x) {
129 document.removePositionUpdater(fPositionUpdater);
136 if (newInput != null) {
137 IDocument document = fDocumentProvider.getDocument(newInput);
138 if (document != null) {
139 document.addPositionCategory(SEGMENTS);
140 document.addPositionUpdater(fPositionUpdater);
148 * @see IContentProvider#dispose
150 public void dispose() {
151 if (fContent != null) {
155 if (fVariables != null) {
162 * @see IContentProvider#isDeleted(Object)
164 public boolean isDeleted(Object element) {
169 * returns all PHP variables
171 public Object[] getVariables() {
172 return fVariables.toArray();
176 * @see IStructuredContentProvider#getElements(Object)
178 public Object[] getElements(Object element) {
179 return fContent.toArray();
183 * @see ITreeContentProvider#hasChildren(Object)
185 public boolean hasChildren(Object element) {
186 if (element instanceof PHPSegmentWithChildren) {
187 return !((PHPSegmentWithChildren) element).getList().isEmpty();
189 return element == fInput;
193 * @see ITreeContentProvider#getParent(Object)
195 public Object getParent(Object element) {
196 if (element instanceof PHPSegment) {
197 return ((PHPSegment) element).getParent();
203 * @see ITreeContentProvider#getChildren(Object)
205 public Object[] getChildren(Object element) {
206 if (element == fInput)
207 return fContent.toArray();
208 if (element instanceof PHPSegmentWithChildren)
209 return ((PHPSegmentWithChildren) element).getList().toArray();
210 return new Object[0];
214 protected class OutlineLabelProvider extends LabelProvider {
215 private ImageDescriptorRegistry fRegistry;
217 public OutlineLabelProvider() {
218 fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
222 * The <code>LabelProvider</code> implementation of this
223 * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may
226 public Image getImage(Object element) {
227 if (element instanceof PHPSegment) {
228 ImageDescriptor descriptor = ((PHPSegment) element).getImage();
229 return fRegistry.get(descriptor);
235 protected Object fInput;
236 protected IDocumentProvider fDocumentProvider;
237 protected ITextEditor fTextEditor;
238 protected PHPEditor fEditor;
239 protected ContentProvider contentProvider;
242 * Creates a content outline page using the given provider and the given editor.
244 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
246 contentProvider = null;
247 fDocumentProvider = provider;
248 fTextEditor = editor;
249 if (editor instanceof PHPEditor)
250 fEditor = (PHPEditor) editor;
254 * Method declared on ContentOutlinePage
256 public void createControl(Composite parent) {
258 super.createControl(parent);
260 TreeViewer viewer = getTreeViewer();
262 contentProvider = new ContentProvider();
263 viewer.setContentProvider(contentProvider);
264 viewer.setLabelProvider(new OutlineLabelProvider());
266 viewer.addSelectionChangedListener(this);
269 viewer.setInput(fInput);
273 * Method declared on ContentOutlinePage
275 public void selectionChanged(SelectionChangedEvent event) {
277 super.selectionChanged(event);
279 ISelection selection = event.getSelection();
280 if (selection.isEmpty())
281 fTextEditor.resetHighlightRange();
283 PHPSegment segment = (PHPSegment) ((IStructuredSelection) selection).getFirstElement();
284 int start = segment.getPosition().getOffset();
285 int length = segment.getPosition().getLength();
287 fTextEditor.setHighlightRange(start, length, true);
288 } catch (IllegalArgumentException x) {
289 fTextEditor.resetHighlightRange();
295 * Sets the input of the outline page
297 public void setInput(Object input) {
303 * Updates the outline page.
305 public void update() {
306 TreeViewer viewer = getTreeViewer();
308 if (viewer != null) {
309 Control control = viewer.getControl();
310 if (control != null && !control.isDisposed()) {
311 control.setRedraw(false);
312 viewer.setInput(fInput);
314 control.setRedraw(true);
319 public Object[] getVariables() {
320 if (contentProvider != null) {
321 return contentProvider.getVariables();
325 // public ContentProvider getContentProvider() {
326 // return contentProvider;