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.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.BadPositionCategoryException;
24 import org.eclipse.jface.text.DefaultPositionUpdater;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IPositionUpdater;
27 import org.eclipse.jface.text.Position;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.ITreeContentProvider;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.viewers.SelectionChangedEvent;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.jface.viewers.Viewer;
36 import org.eclipse.ui.texteditor.IDocumentProvider;
37 import org.eclipse.ui.texteditor.ITextEditor;
38 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
41 * A content outline page which always represents the content of the
42 * connected editor in 10 segments.
44 public class PHPContentOutlinePage extends ContentOutlinePage {
49 protected static class Segment {
51 public Position position;
53 public Segment(String name, Position position) {
55 this.position = position;
58 public String toString() {
64 * Divides the editor's document into ten segments and provides elements for them.
66 protected class ContentProvider implements ITreeContentProvider {
68 protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
69 protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
70 protected List fContent = new ArrayList(10);
72 protected void parse(IDocument document) {
74 int lines = document.getNumberOfLines();
75 int increment = Math.max(Math.round((float) (lines / 10)), 10);
77 String text = document.get();
80 lastIndex = text.indexOf("function ", lastIndex);
81 while (lastIndex > 0) {
85 while ((i<text.length())&&Character.isJavaIdentifierPart(text.charAt(i))) {
88 Position p = new Position(lastIndex, i-lastIndex);
89 document.addPosition(SEGMENTS, p);
90 fContent.add(new Segment(text.substring(lastIndex,i), p));
91 // MessageFormat.format("function", new Object[] { new Integer(lastIndex)}), p)); //$NON-NLS-1$
92 lastIndex = text.indexOf("function", lastIndex+1);
93 } catch (BadLocationException e) {
94 } catch (BadPositionCategoryException e) {
99 // for (int line = 0; line < lines; line += increment) {
101 // int length = increment;
102 // if (line + increment > lines)
103 // length = lines - line;
107 // int offset = document.getLineOffset(line);
108 // int end = document.getLineOffset(line + length);
109 // length = end - offset;
110 // Position p = new Position(offset, length);
111 // document.addPosition(SEGMENTS, p);
112 // fContent.add(new Segment(MessageFormat.format(PHPEditorMessages.getString("OutlinePage.segment.title_pattern"), new Object[] { new Integer(offset)}), p)); //$NON-NLS-1$
114 // } catch (BadPositionCategoryException x) {
115 // } catch (BadLocationException x) {
121 * @see IContentProvider#inputChanged(Viewer, Object, Object)
123 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
124 if (oldInput != null) {
125 IDocument document = fDocumentProvider.getDocument(oldInput);
126 if (document != null) {
128 document.removePositionCategory(SEGMENTS);
129 } catch (BadPositionCategoryException x) {
131 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) {
159 * @see IContentProvider#isDeleted(Object)
161 public boolean isDeleted(Object element) {
166 * @see IStructuredContentProvider#getElements(Object)
168 public Object[] getElements(Object element) {
169 return fContent.toArray();
173 * @see ITreeContentProvider#hasChildren(Object)
175 public boolean hasChildren(Object element) {
176 return element == fInput;
180 * @see ITreeContentProvider#getParent(Object)
182 public Object getParent(Object element) {
183 if (element instanceof Segment)
189 * @see ITreeContentProvider#getChildren(Object)
191 public Object[] getChildren(Object element) {
192 if (element == fInput)
193 return fContent.toArray();
194 return new Object[0];
198 protected Object fInput;
199 protected IDocumentProvider fDocumentProvider;
200 protected ITextEditor fTextEditor;
203 * Creates a content outline page using the given provider and the given editor.
205 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
207 fDocumentProvider = provider;
208 fTextEditor = editor;
212 * Method declared on ContentOutlinePage
214 public void createControl(Composite parent) {
216 super.createControl(parent);
218 TreeViewer viewer = getTreeViewer();
219 viewer.setContentProvider(new ContentProvider());
220 viewer.setLabelProvider(new LabelProvider());
221 viewer.addSelectionChangedListener(this);
224 viewer.setInput(fInput);
228 * Method declared on ContentOutlinePage
230 public void selectionChanged(SelectionChangedEvent event) {
232 super.selectionChanged(event);
234 ISelection selection = event.getSelection();
235 if (selection.isEmpty())
236 fTextEditor.resetHighlightRange();
238 Segment segment = (Segment) ((IStructuredSelection) selection).getFirstElement();
239 int start = segment.position.getOffset();
240 int length = segment.position.getLength();
242 fTextEditor.setHighlightRange(start, length, true);
243 } catch (IllegalArgumentException x) {
244 fTextEditor.resetHighlightRange();
250 * Sets the input of the outline page
252 public void setInput(Object input) {
258 * Updates the outline page.
260 public void update() {
261 TreeViewer viewer = getTreeViewer();
263 if (viewer != null) {
264 Control control = viewer.getControl();
265 if (control != null && !control.isDisposed()) {
266 control.setRedraw(false);
267 viewer.setInput(fInput);
269 control.setRedraw(true);