Added: PreferencePage; External Browser startup; extended syntax highlighting
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPContentOutlinePage.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
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
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
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;
35
36 import org.eclipse.ui.texteditor.IDocumentProvider;
37 import org.eclipse.ui.texteditor.ITextEditor;
38 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
39
40 /**
41  * A content outline page which always represents the content of the
42  * connected editor in 10 segments.
43  */
44 public class PHPContentOutlinePage extends ContentOutlinePage {
45
46         /**
47          * A segment element.
48          */
49         protected static class Segment {
50                 public String name;
51                 public Position position;
52
53                 public Segment(String name, Position position) {
54                         this.name= name;
55                         this.position= position;
56                 }
57
58                 public String toString() {
59                         return name;
60                 }
61         };
62
63         /**
64          * Divides the editor's document into ten segments and provides elements for them.
65          */
66         protected class ContentProvider implements ITreeContentProvider {
67
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);
71
72                 protected void parse(IDocument document) {
73
74                         int lines= document.getNumberOfLines();
75                         int increment= Math.max(Math.round((float) (lines / 10)), 10);
76
77                         for (int line= 0; line < lines; line += increment) {
78
79                                 int length= increment;
80                                 if (line + increment > lines)
81                                         length= lines - line;
82
83                                 try {
84
85                                         int offset= document.getLineOffset(line);
86                                         int end= document.getLineOffset(line + length);
87                                         length= end - offset;
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$
91
92                                 } catch (BadPositionCategoryException x) {
93                                 } catch (BadLocationException x) {
94                                 }
95                         }
96                 }
97
98                 /*
99                  * @see IContentProvider#inputChanged(Viewer, Object, Object)
100                  */
101                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
102                         if (oldInput != null) {
103                                 IDocument document= fDocumentProvider.getDocument(oldInput);
104                                 if (document != null) {
105                                         try {
106                                                 document.removePositionCategory(SEGMENTS);
107                                         } catch (BadPositionCategoryException x) {
108                                         }
109                                         document.removePositionUpdater(fPositionUpdater);
110                                 }
111                         }
112
113                         fContent.clear();
114
115                         if (newInput != null) {
116                                 IDocument document= fDocumentProvider.getDocument(newInput);
117                                 if (document != null) {
118                                         document.addPositionCategory(SEGMENTS);
119                                         document.addPositionUpdater(fPositionUpdater);
120
121                                         parse(document);
122                                 }
123                         }
124                 }
125
126                 /*
127                  * @see IContentProvider#dispose
128                  */
129                 public void dispose() {
130                         if (fContent != null) {
131                                 fContent.clear();
132                                 fContent= null;
133                         }
134                 }
135
136                 /*
137                  * @see IContentProvider#isDeleted(Object)
138                  */
139                 public boolean isDeleted(Object element) {
140                         return false;
141                 }
142
143                 /*
144                  * @see IStructuredContentProvider#getElements(Object)
145                  */
146                 public Object[] getElements(Object element) {
147                         return fContent.toArray();
148                 }
149
150                 /*
151                  * @see ITreeContentProvider#hasChildren(Object)
152                  */
153                 public boolean hasChildren(Object element) {
154                         return element == fInput;
155                 }
156
157                 /*
158                  * @see ITreeContentProvider#getParent(Object)
159                  */
160                 public Object getParent(Object element) {
161                         if (element instanceof Segment)
162                                 return fInput;
163                         return null;
164                 }
165
166                 /*
167                  * @see ITreeContentProvider#getChildren(Object)
168                  */
169                 public Object[] getChildren(Object element) {
170                         if (element == fInput)
171                                 return fContent.toArray();
172                         return new Object[0];
173                 }
174         };
175
176         protected Object fInput;
177         protected IDocumentProvider fDocumentProvider;
178         protected ITextEditor fTextEditor;
179
180         /**
181          * Creates a content outline page using the given provider and the given editor.
182          */
183         public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
184                 super();
185                 fDocumentProvider= provider;
186                 fTextEditor= editor;
187         }
188         
189         /* (non-Javadoc)
190          * Method declared on ContentOutlinePage
191          */
192         public void createControl(Composite parent) {
193
194                 super.createControl(parent);
195
196                 TreeViewer viewer= getTreeViewer();
197                 viewer.setContentProvider(new ContentProvider());
198                 viewer.setLabelProvider(new LabelProvider());
199                 viewer.addSelectionChangedListener(this);
200
201                 if (fInput != null)
202                         viewer.setInput(fInput);
203         }
204         
205         /* (non-Javadoc)
206          * Method declared on ContentOutlinePage
207          */
208         public void selectionChanged(SelectionChangedEvent event) {
209
210                 super.selectionChanged(event);
211
212                 ISelection selection= event.getSelection();
213                 if (selection.isEmpty())
214                         fTextEditor.resetHighlightRange();
215                 else {
216                         Segment segment= (Segment) ((IStructuredSelection) selection).getFirstElement();
217                         int start= segment.position.getOffset();
218                         int length= segment.position.getLength();
219                         try {
220                                 fTextEditor.setHighlightRange(start, length, true);
221                         } catch (IllegalArgumentException x) {
222                                 fTextEditor.resetHighlightRange();
223                         }
224                 }
225         }
226         
227         /**
228          * Sets the input of the outline page
229          */
230         public void setInput(Object input) {
231                 fInput= input;
232                 update();
233         }
234         
235         /**
236          * Updates the outline page.
237          */
238         public void update() {
239                 TreeViewer viewer= getTreeViewer();
240
241                 if (viewer != null) {
242                         Control control= viewer.getControl();
243                         if (control != null && !control.isDisposed()) {
244                                 control.setRedraw(false);
245                                 viewer.setInput(fInput);
246                                 viewer.expandAll();
247                                 control.setRedraw(true);
248                         }
249                 }
250         }
251 }