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.compiler.parser.*;
22 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.text.BadPositionCategoryException;
27 import org.eclipse.jface.text.DefaultPositionUpdater;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.IPositionUpdater;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.ITreeContentProvider;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.viewers.SelectionChangedEvent;
35 import org.eclipse.jface.viewers.TreeViewer;
36 import org.eclipse.jface.viewers.Viewer;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.ui.texteditor.IDocumentProvider;
41 import org.eclipse.ui.texteditor.ITextEditor;
42 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
43 import test.PHPParserSuperclass;
44 import test.PHPParserManager;
47 * A content outline page which always represents the functions of the
48 * connected PHPEditor.
50 public class PHPContentOutlinePage extends ContentOutlinePage {
51 private static final String ERROR = "error"; //$NON-NLS-1$
52 private static final String WARNING = "warning"; //$NON-NLS-1$
54 protected static class SegmentComparator implements Comparator {
55 public int compare(Object o1, Object o2) {
56 if (o1 instanceof PHPSegmentWithChildren && !(o2 instanceof PHPSegmentWithChildren)) {
59 if (o2 instanceof PHPSegmentWithChildren && !(o1 instanceof PHPSegmentWithChildren)) {
62 return ((Outlineable) o1).toString().compareToIgnoreCase(((Outlineable) o2).toString());
67 * Divides the editor's document into ten segments and provides elements for them.
69 protected class ContentProvider implements ITreeContentProvider {
71 protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
72 protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
73 protected List fContent = new ArrayList(10);
74 protected TreeSet fVariables = new TreeSet();
76 // private String getIdentifier(String text, int firstIndex) {
77 // int i = firstIndex;
79 // int textLength = text.length();
80 // StringBuffer identifier = new StringBuffer();
81 // while (i < textLength) {
82 // c = text.charAt(i++);
83 // if (Scanner.isPHPIdentifierPart(c) || (c == '$')) {
84 // identifier.append(c);
85 // } else if ((i == firstIndex + 1) && (c == '$')) {
86 // identifier.append(c);
88 // return identifier.toString();
94 protected void parse(IDocument document) {
96 // int lines = document.getNumberOfLines();
97 // int increment = Math.max(Math.round((float) (lines / 10)), 10);
101 String text = document.get();
102 PHPParserSuperclass parser = PHPParserManager.getParser(null);
104 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
105 fVariables = outlineInfo.getVariables();
107 OutlineableWithChildren declarations = outlineInfo.getDeclarations();
109 for (int i = 0; i < declarations.size(); i++) {
110 temp = declarations.get(i);
113 Collections.sort(fContent, new SegmentComparator());
117 * @see IContentProvider#inputChanged(Viewer, Object, Object)
119 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
120 if (oldInput != null) {
121 IDocument document = fDocumentProvider.getDocument(oldInput);
122 if (document != null) {
124 document.removePositionCategory(SEGMENTS);
125 } catch (BadPositionCategoryException x) {
127 document.removePositionUpdater(fPositionUpdater);
134 if (newInput != null) {
135 IDocument document = fDocumentProvider.getDocument(newInput);
136 if (document != null) {
137 document.addPositionCategory(SEGMENTS);
138 document.addPositionUpdater(fPositionUpdater);
146 * @see IContentProvider#dispose
148 public void dispose() {
149 if (fContent != null) {
153 if (fVariables != null) {
160 * @see IContentProvider#isDeleted(Object)
162 public boolean isDeleted(Object element) {
167 * returns all PHP variables
169 public Object[] getVariables() {
170 return fVariables.toArray();
174 * @see IStructuredContentProvider#getElements(Object)
176 public Object[] getElements(Object element) {
177 return fContent.toArray();
181 * @see ITreeContentProvider#hasChildren(Object)
183 public boolean hasChildren(Object element) {
184 if (element instanceof PHPSegmentWithChildren) {
185 return !((PHPSegmentWithChildren) element).getList().isEmpty();
187 return element == fInput;
191 * @see ITreeContentProvider#getParent(Object)
193 public Object getParent(Object element) {
194 if (element instanceof Outlineable) {
195 return ((Outlineable) element).getParent();
201 * @see ITreeContentProvider#getChildren(Object)
203 public Object[] getChildren(Object element) {
204 if (element == fInput)
205 return fContent.toArray();
206 if (element instanceof PHPSegmentWithChildren)
207 return ((PHPSegmentWithChildren) element).getList().toArray();
208 return new Object[0];
212 protected class OutlineLabelProvider extends LabelProvider {
213 private ImageDescriptorRegistry fRegistry;
215 public OutlineLabelProvider() {
216 fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
220 * The <code>LabelProvider</code> implementation of this
221 * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may
224 public Image getImage(Object element) {
225 if (element instanceof Outlineable) {
226 ImageDescriptor descriptor = ((Outlineable) element).getImage();
227 return fRegistry.get(descriptor);
233 protected Object fInput;
234 protected IDocumentProvider fDocumentProvider;
235 protected ITextEditor fTextEditor;
236 protected PHPEditor fEditor;
237 protected ContentProvider contentProvider;
240 * Creates a content outline page using the given provider and the given editor.
242 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
244 contentProvider = null;
245 fDocumentProvider = provider;
246 fTextEditor = editor;
247 if (editor instanceof PHPEditor)
248 fEditor = (PHPEditor) editor;
252 * Method declared on ContentOutlinePage
254 public void createControl(Composite parent) {
256 super.createControl(parent);
258 TreeViewer viewer = getTreeViewer();
260 contentProvider = new ContentProvider();
261 viewer.setContentProvider(contentProvider);
262 viewer.setLabelProvider(new OutlineLabelProvider());
264 viewer.addSelectionChangedListener(this);
267 viewer.setInput(fInput);
271 * Method declared on ContentOutlinePage
273 public void selectionChanged(SelectionChangedEvent event) {
275 super.selectionChanged(event);
277 ISelection selection = event.getSelection();
278 if (selection.isEmpty())
279 fTextEditor.resetHighlightRange();
281 PHPSegment segment = (PHPSegment) ((IStructuredSelection) selection).getFirstElement();
282 int start = segment.getPosition().getOffset();
283 int length = segment.getPosition().getLength();
285 fTextEditor.setHighlightRange(start, length, true);
286 } catch (IllegalArgumentException x) {
287 fTextEditor.resetHighlightRange();
293 * Sets the input of the outline page
295 public void setInput(Object input) {
301 * Updates the outline page.
303 public void update() {
304 TreeViewer viewer = getTreeViewer();
306 if (viewer != null) {
307 Control control = viewer.getControl();
308 if (control != null && !control.isDisposed()) {
309 control.setRedraw(false);
310 viewer.setInput(fInput);
312 control.setRedraw(true);
317 public Object[] getVariables() {
318 if (contentProvider != null) {
319 return contentProvider.getVariables();
323 // public ContentProvider getContentProvider() {
324 // return contentProvider;