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= "__java_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 for (int line= 0; line < lines; line += increment) {
79 int length= increment;
80 if (line + increment > lines)
85 int offset= document.getLineOffset(line);
86 int end= document.getLineOffset(line + length);
88 Position p= new Position(offset, length);
89 document.addPosition(SEGMENTS, p);
90 fContent.add(new Segment(MessageFormat.format(PHPEditorMessages.getString("OutlinePage.segment.title_pattern"), new Object[] { new Integer(offset) }), p)); //$NON-NLS-1$
92 } catch (BadPositionCategoryException x) {
93 } catch (BadLocationException x) {
99 * @see IContentProvider#inputChanged(Viewer, Object, Object)
101 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
102 if (oldInput != null) {
103 IDocument document= fDocumentProvider.getDocument(oldInput);
104 if (document != null) {
106 document.removePositionCategory(SEGMENTS);
107 } catch (BadPositionCategoryException x) {
109 document.removePositionUpdater(fPositionUpdater);
115 if (newInput != null) {
116 IDocument document= fDocumentProvider.getDocument(newInput);
117 if (document != null) {
118 document.addPositionCategory(SEGMENTS);
119 document.addPositionUpdater(fPositionUpdater);
127 * @see IContentProvider#dispose
129 public void dispose() {
130 if (fContent != null) {
137 * @see IContentProvider#isDeleted(Object)
139 public boolean isDeleted(Object element) {
144 * @see IStructuredContentProvider#getElements(Object)
146 public Object[] getElements(Object element) {
147 return fContent.toArray();
151 * @see ITreeContentProvider#hasChildren(Object)
153 public boolean hasChildren(Object element) {
154 return element == fInput;
158 * @see ITreeContentProvider#getParent(Object)
160 public Object getParent(Object element) {
161 if (element instanceof Segment)
167 * @see ITreeContentProvider#getChildren(Object)
169 public Object[] getChildren(Object element) {
170 if (element == fInput)
171 return fContent.toArray();
172 return new Object[0];
176 protected Object fInput;
177 protected IDocumentProvider fDocumentProvider;
178 protected ITextEditor fTextEditor;
181 * Creates a content outline page using the given provider and the given editor.
183 public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
185 fDocumentProvider= provider;
190 * Method declared on ContentOutlinePage
192 public void createControl(Composite parent) {
194 super.createControl(parent);
196 TreeViewer viewer= getTreeViewer();
197 viewer.setContentProvider(new ContentProvider());
198 viewer.setLabelProvider(new LabelProvider());
199 viewer.addSelectionChangedListener(this);
202 viewer.setInput(fInput);
206 * Method declared on ContentOutlinePage
208 public void selectionChanged(SelectionChangedEvent event) {
210 super.selectionChanged(event);
212 ISelection selection= event.getSelection();
213 if (selection.isEmpty())
214 fTextEditor.resetHighlightRange();
216 Segment segment= (Segment) ((IStructuredSelection) selection).getFirstElement();
217 int start= segment.position.getOffset();
218 int length= segment.position.getLength();
220 fTextEditor.setHighlightRange(start, length, true);
221 } catch (IllegalArgumentException x) {
222 fTextEditor.resetHighlightRange();
228 * Sets the input of the outline page
230 public void setInput(Object input) {
236 * Updates the outline page.
238 public void update() {
239 TreeViewer viewer= getTreeViewer();
241 if (viewer != null) {
242 Control control= viewer.getControl();
243 if (control != null && !control.isDisposed()) {
244 control.setRedraw(false);
245 viewer.setInput(fInput);
247 control.setRedraw(true);