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.Outlineable;
22 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
23 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
24 import net.sourceforge.phpdt.internal.compiler.parser.PHPSegment;
25 import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren;
26 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
27 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.text.BadPositionCategoryException;
31 import org.eclipse.jface.text.DefaultPositionUpdater;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.IPositionUpdater;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.ITreeContentProvider;
37 import org.eclipse.jface.viewers.LabelProvider;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.jface.viewers.TreeViewer;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.ui.texteditor.IDocumentProvider;
44 import org.eclipse.ui.texteditor.ITextEditor;
46 import test.PHPParserManager;
47 import test.PHPParserSuperclass;
50 * A content outline page which always represents the functions of the
51 * connected PHPEditor.
53 public class PHPContentOutlinePage extends AbstractContentOutlinePage {
54 private static final String ERROR = "error"; //$NON-NLS-1$
55 private static final String WARNING = "warning"; //$NON-NLS-1$
57 protected static class SegmentComparator implements Comparator {
58 public int compare(Object o1, Object o2) {
59 if (o1 instanceof PHPSegmentWithChildren && !(o2 instanceof PHPSegmentWithChildren)) {
62 if (o2 instanceof PHPSegmentWithChildren && !(o1 instanceof PHPSegmentWithChildren)) {
65 return ((Outlineable) o1).toString().compareToIgnoreCase(((Outlineable) o2).toString());
70 * Divides the editor's document into ten segments and provides elements for them.
72 protected class ContentProvider implements ITreeContentProvider {
74 protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
75 protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
76 protected List fContent = new ArrayList(10);
77 protected TreeSet fVariables = new TreeSet();
79 // private String getIdentifier(String text, int firstIndex) {
80 // int i = firstIndex;
82 // int textLength = text.length();
83 // StringBuffer identifier = new StringBuffer();
84 // while (i < textLength) {
85 // c = text.charAt(i++);
86 // if (Scanner.isPHPIdentifierPart(c) || (c == '$')) {
87 // identifier.append(c);
88 // } else if ((i == firstIndex + 1) && (c == '$')) {
89 // identifier.append(c);
91 // return identifier.toString();
97 protected void parse(IDocument document) {
99 // int lines = document.getNumberOfLines();
100 // int increment = Math.max(Math.round((float) (lines / 10)), 10);
104 String text = document.get();
105 PHPParserSuperclass parser = PHPParserManager.getParser(null);
107 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
108 fVariables = outlineInfo.getVariables();
110 OutlineableWithChildren declarations = outlineInfo.getDeclarations();
112 for (int i = 0; i < declarations.size(); i++) {
113 temp = declarations.get(i);
116 Collections.sort(fContent, new SegmentComparator());
120 * @see IContentProvider#inputChanged(Viewer, Object, Object)
122 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
123 if (oldInput != null) {
124 IDocument document = fDocumentProvider.getDocument(oldInput);
125 if (document != null) {
127 document.removePositionCategory(SEGMENTS);
128 } catch (BadPositionCategoryException x) {
130 document.removePositionUpdater(fPositionUpdater);
137 if (newInput != null) {
138 IDocument document = fDocumentProvider.getDocument(newInput);
139 if (document != null) {
140 document.addPositionCategory(SEGMENTS);
141 document.addPositionUpdater(fPositionUpdater);
149 * @see IContentProvider#dispose
151 public void dispose() {
152 if (fContent != null) {
156 if (fVariables != null) {
163 * @see IContentProvider#isDeleted(Object)
165 public boolean isDeleted(Object element) {
170 * returns all PHP variables
172 public Object[] getVariables() {
173 return fVariables.toArray();
177 * @see IStructuredContentProvider#getElements(Object)
179 public Object[] getElements(Object element) {
180 return fContent.toArray();
184 * @see ITreeContentProvider#hasChildren(Object)
186 public boolean hasChildren(Object element) {
187 if (element instanceof PHPSegmentWithChildren) {
188 return !((PHPSegmentWithChildren) element).getList().isEmpty();
190 return element == fInput;
194 * @see ITreeContentProvider#getParent(Object)
196 public Object getParent(Object element) {
197 if (element instanceof Outlineable) {
198 return ((Outlineable) element).getParent();
204 * @see ITreeContentProvider#getChildren(Object)
206 public Object[] getChildren(Object element) {
207 if (element == fInput)
208 return fContent.toArray();
209 if (element instanceof PHPSegmentWithChildren)
210 return ((PHPSegmentWithChildren) element).getList().toArray();
211 return new Object[0];
215 protected class OutlineLabelProvider extends LabelProvider {
216 private ImageDescriptorRegistry fRegistry;
218 public OutlineLabelProvider() {
219 fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
223 * The <code>LabelProvider</code> implementation of this
224 * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may
227 public Image getImage(Object element) {
228 if (element instanceof Outlineable) {
229 ImageDescriptor descriptor = ((Outlineable) element).getImage();
230 return fRegistry.get(descriptor);
236 protected IDocumentProvider fDocumentProvider;
237 protected ITextEditor fTextEditor;
238 protected PHPEditor fEditor;
239 protected ContentProvider fContentProvider;
242 * Creates a content outline page using the given provider and the given editor.
244 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
246 fContentProvider = 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 fContentProvider = new ContentProvider();
263 viewer.setContentProvider(fContentProvider);
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();
294 public Object[] getVariables() {
295 if (fContentProvider != null) {
296 return fContentProvider.getVariables();
300 // public ContentProvider getContentProvider() {
301 // return contentProvider;