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.PHPOutlineInfo;
22 import net.sourceforge.phpdt.internal.compiler.parser.PHPSegment;
23 import net.sourceforge.phpdt.internal.compiler.parser.PHPSegmentWithChildren;
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.swt.widgets.Control;
42 import org.eclipse.ui.texteditor.IDocumentProvider;
43 import org.eclipse.ui.texteditor.ITextEditor;
44 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
45 import test.PHPParserSuperclass;
46 import test.PHPParserManager;
49 * A content outline page which always represents the functions of the
50 * connected PHPEditor.
52 public class PHPContentOutlinePage extends ContentOutlinePage {
53 private static final String ERROR = "error"; //$NON-NLS-1$
54 private static final String WARNING = "warning"; //$NON-NLS-1$
56 protected static class SegmentComparator implements Comparator {
57 public int compare(Object o1, Object o2) {
58 if (o1 instanceof PHPSegmentWithChildren && !(o2 instanceof PHPSegmentWithChildren)) {
61 if (o2 instanceof PHPSegmentWithChildren && !(o1 instanceof PHPSegmentWithChildren)) {
64 return ((PHPSegment) o1).toString().compareToIgnoreCase(((PHPSegment) o2).toString());
69 * Divides the editor's document into ten segments and provides elements for them.
71 protected class ContentProvider implements ITreeContentProvider {
73 protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
74 protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
75 protected List fContent = new ArrayList(10);
76 protected TreeSet fVariables = new TreeSet();
78 // private String getIdentifier(String text, int firstIndex) {
79 // int i = firstIndex;
81 // int textLength = text.length();
82 // StringBuffer identifier = new StringBuffer();
83 // while (i < textLength) {
84 // c = text.charAt(i++);
85 // if (Character.isJavaIdentifierPart(c) || (c == '$')) {
86 // identifier.append(c);
87 // } else if ((i == firstIndex + 1) && (c == '$')) {
88 // identifier.append(c);
90 // return identifier.toString();
96 protected void parse(IDocument document) {
98 // int lines = document.getNumberOfLines();
99 // int increment = Math.max(Math.round((float) (lines / 10)), 10);
103 String text = document.get();
104 PHPParserSuperclass parser = PHPParserManager.getParser(null);
106 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
107 fVariables = outlineInfo.getVariables();
109 PHPSegmentWithChildren declarations = outlineInfo.getDeclarations();
111 for (int i = 0; i < declarations.size(); i++) {
112 temp = declarations.get(i);
115 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 PHPSegment) {
198 return ((PHPSegment) 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 PHPSegment) {
229 ImageDescriptor descriptor = ((PHPSegment) element).getImage();
230 return fRegistry.get(descriptor);
236 protected Object fInput;
237 protected IDocumentProvider fDocumentProvider;
238 protected ITextEditor fTextEditor;
239 protected PHPEditor fEditor;
240 protected ContentProvider contentProvider;
243 * Creates a content outline page using the given provider and the given editor.
245 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
247 contentProvider = null;
248 fDocumentProvider = provider;
249 fTextEditor = editor;
250 if (editor instanceof PHPEditor)
251 fEditor = (PHPEditor) editor;
255 * Method declared on ContentOutlinePage
257 public void createControl(Composite parent) {
259 super.createControl(parent);
261 TreeViewer viewer = getTreeViewer();
263 contentProvider = new ContentProvider();
264 viewer.setContentProvider(contentProvider);
265 viewer.setLabelProvider(new OutlineLabelProvider());
267 viewer.addSelectionChangedListener(this);
270 viewer.setInput(fInput);
274 * Method declared on ContentOutlinePage
276 public void selectionChanged(SelectionChangedEvent event) {
278 super.selectionChanged(event);
280 ISelection selection = event.getSelection();
281 if (selection.isEmpty())
282 fTextEditor.resetHighlightRange();
284 PHPSegment segment = (PHPSegment) ((IStructuredSelection) selection).getFirstElement();
285 int start = segment.getPosition().getOffset();
286 int length = segment.getPosition().getLength();
288 fTextEditor.setHighlightRange(start, length, true);
289 } catch (IllegalArgumentException x) {
290 fTextEditor.resetHighlightRange();
296 * Sets the input of the outline page
298 public void setInput(Object input) {
304 * Updates the outline page.
306 public void update() {
307 TreeViewer viewer = getTreeViewer();
309 if (viewer != null) {
310 Control control = viewer.getControl();
311 if (control != null && !control.isDisposed()) {
312 control.setRedraw(false);
313 viewer.setInput(fInput);
315 control.setRedraw(true);
320 public Object[] getVariables() {
321 if (contentProvider != null) {
322 return contentProvider.getVariables();
326 // public ContentProvider getContentProvider() {
327 // return contentProvider;