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.ui.viewsupport.ImageDescriptorRegistry;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.jface.text.BadPositionCategoryException;
29 import org.eclipse.jface.text.DefaultPositionUpdater;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IPositionUpdater;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.ITreeContentProvider;
35 import org.eclipse.jface.viewers.LabelProvider;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.jface.viewers.TreeViewer;
38 import org.eclipse.jface.viewers.Viewer;
39 import org.eclipse.swt.graphics.Image;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.ui.texteditor.IDocumentProvider;
42 import org.eclipse.ui.texteditor.ITextEditor;
44 import test.PHPParserManager;
45 import test.PHPParserSuperclass;
48 * A content outline page which always represents the functions of the
49 * connected PHPEditor.
51 public class PHPContentOutlinePage extends AbstractContentOutlinePage {
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 OutlineableWithChildren && !(o2 instanceof OutlineableWithChildren)) {
60 if (o2 instanceof OutlineableWithChildren && !(o1 instanceof OutlineableWithChildren)) {
63 return ((Outlineable) o1).toString().compareToIgnoreCase(((Outlineable) 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 (Scanner.isPHPIdentifierPart(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 PHPParserSuperclass parser = PHPParserManager.getParser(null);
105 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
106 fVariables = outlineInfo.getVariables();
108 OutlineableWithChildren declarations = outlineInfo.getDeclarations();
110 for (int i = 0; i < declarations.size(); i++) {
111 temp = declarations.get(i);
114 Collections.sort(fContent, new SegmentComparator());
118 * @see IContentProvider#inputChanged(Viewer, Object, Object)
120 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
121 if (oldInput != null) {
122 IDocument document = fDocumentProvider.getDocument(oldInput);
123 if (document != null) {
125 document.removePositionCategory(SEGMENTS);
126 } catch (BadPositionCategoryException x) {
128 document.removePositionUpdater(fPositionUpdater);
135 if (newInput != null) {
136 IDocument document = fDocumentProvider.getDocument(newInput);
137 if (document != null) {
138 document.addPositionCategory(SEGMENTS);
139 document.addPositionUpdater(fPositionUpdater);
147 * @see IContentProvider#dispose
149 public void dispose() {
150 if (fContent != null) {
154 if (fVariables != null) {
161 * @see IContentProvider#isDeleted(Object)
163 public boolean isDeleted(Object element) {
168 * returns all PHP variables
170 public Object[] getVariables() {
171 return fVariables.toArray();
175 * @see IStructuredContentProvider#getElements(Object)
177 public Object[] getElements(Object element) {
178 return fContent.toArray();
182 * @see ITreeContentProvider#hasChildren(Object)
184 public boolean hasChildren(Object element) {
185 if (element instanceof OutlineableWithChildren) {
186 return !((OutlineableWithChildren) element).getList().isEmpty();
188 return element == fInput;
192 * @see ITreeContentProvider#getParent(Object)
194 public Object getParent(Object element) {
195 if (element instanceof Outlineable) {
196 return ((Outlineable) element).getParent();
202 * @see ITreeContentProvider#getChildren(Object)
204 public Object[] getChildren(Object element) {
205 if (element == fInput)
206 return fContent.toArray();
207 if (element instanceof OutlineableWithChildren)
208 return ((OutlineableWithChildren) element).getList().toArray();
209 return new Object[0];
213 protected class OutlineLabelProvider extends LabelProvider {
214 private ImageDescriptorRegistry fRegistry;
216 public OutlineLabelProvider() {
217 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 IDocumentProvider fDocumentProvider;
234 protected ITextEditor fTextEditor;
235 protected PHPEditor fEditor;
236 protected ContentProvider fContentProvider;
239 * Creates a content outline page using the given provider and the given editor.
241 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
243 fContentProvider = null;
244 fDocumentProvider = provider;
245 fTextEditor = editor;
246 if (editor instanceof PHPEditor)
247 fEditor = (PHPEditor) editor;
251 * Method declared on ContentOutlinePage
253 public void createControl(Composite parent) {
255 super.createControl(parent);
257 TreeViewer viewer = getTreeViewer();
259 fContentProvider = new ContentProvider();
260 viewer.setContentProvider(fContentProvider);
261 viewer.setLabelProvider(new OutlineLabelProvider());
263 viewer.addSelectionChangedListener(this);
266 viewer.setInput(fInput);
270 * Method declared on ContentOutlinePage
272 public void selectionChanged(SelectionChangedEvent event) {
274 super.selectionChanged(event);
276 ISelection selection = event.getSelection();
277 if (selection.isEmpty())
278 fTextEditor.resetHighlightRange();
280 Outlineable segment = (Outlineable) ((IStructuredSelection) selection).getFirstElement();
281 int start = segment.getPosition().getOffset();
282 int length = segment.getPosition().getLength();
284 fTextEditor.setHighlightRange(start, length, true);
285 } catch (IllegalArgumentException x) {
286 fTextEditor.resetHighlightRange();
291 public Object[] getVariables() {
292 if (fContentProvider != null) {
293 return fContentProvider.getVariables();
297 // public ContentProvider getContentProvider() {
298 // return contentProvider;