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.PHPClassDeclaration;
24 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPOutlineInfo;
25 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPParser;
26 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPSegment;
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;
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 PHPClassDeclaration && !(o2 instanceof PHPClassDeclaration)) {
59 if (o2 instanceof PHPClassDeclaration && !(o1 instanceof PHPClassDeclaration)) {
62 return ((PHPSegment) o1).toString().compareToIgnoreCase(((PHPSegment) 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 (Character.isJavaIdentifierPart(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 PHPParser parser = new PHPParser(null);
104 PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
105 fVariables = outlineInfo.getVariables();
107 PHPClassDeclaration declarations = outlineInfo.getDeclarations();
109 for (int i = 0; i < declarations.size(); i++) {
110 temp = declarations.get(i);
113 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 PHPClassDeclaration) {
186 return !((PHPClassDeclaration) element).getList().isEmpty();
188 return element == fInput;
192 * @see ITreeContentProvider#getParent(Object)
194 public Object getParent(Object element) {
195 if (element instanceof PHPSegment) {
196 return ((PHPSegment) 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 PHPClassDeclaration)
208 return ((PHPClassDeclaration) 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();
221 * The <code>LabelProvider</code> implementation of this
222 * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may
225 public Image getImage(Object element) {
226 if (element instanceof PHPSegment) {
227 ImageDescriptor descriptor = ((PHPSegment) element).getImage();
228 return fRegistry.get(descriptor);
234 protected Object fInput;
235 protected IDocumentProvider fDocumentProvider;
236 protected ITextEditor fTextEditor;
237 protected PHPEditor fEditor;
238 protected ContentProvider contentProvider;
241 * Creates a content outline page using the given provider and the given editor.
243 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
245 contentProvider = null;
246 fDocumentProvider = provider;
247 fTextEditor = editor;
248 if (editor instanceof PHPEditor)
249 fEditor = (PHPEditor) editor;
253 * Method declared on ContentOutlinePage
255 public void createControl(Composite parent) {
257 super.createControl(parent);
259 TreeViewer viewer = getTreeViewer();
261 contentProvider = new ContentProvider();
262 viewer.setContentProvider(contentProvider);
263 viewer.setLabelProvider(new OutlineLabelProvider());
265 viewer.addSelectionChangedListener(this);
268 viewer.setInput(fInput);
272 * Method declared on ContentOutlinePage
274 public void selectionChanged(SelectionChangedEvent event) {
276 super.selectionChanged(event);
278 ISelection selection = event.getSelection();
279 if (selection.isEmpty())
280 fTextEditor.resetHighlightRange();
282 PHPSegment segment = (PHPSegment) ((IStructuredSelection) selection).getFirstElement();
283 int start = segment.getPosition().getOffset();
284 int length = segment.getPosition().getLength();
286 fTextEditor.setHighlightRange(start, length, true);
287 } catch (IllegalArgumentException x) {
288 fTextEditor.resetHighlightRange();
294 * Sets the input of the outline page
296 public void setInput(Object input) {
302 * Updates the outline page.
304 public void update() {
305 TreeViewer viewer = getTreeViewer();
307 if (viewer != null) {
308 Control control = viewer.getControl();
309 if (control != null && !control.isDisposed()) {
310 control.setRedraw(false);
311 viewer.setInput(fInput);
313 control.setRedraw(true);
318 public Object[] getVariables() {
319 if (contentProvider != null) {
320 return contentProvider.getVariables();
324 // public ContentProvider getContentProvider() {
325 // return contentProvider;