Defined a limit for code completion list entries PHPeclipsePlugin.MAX_PROPOSALS
[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.util.ArrayList;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
19 import java.util.TreeSet;
20
21 import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
22 import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
23 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
24 import net.sourceforge.phpdt.internal.ui.viewsupport.ImageDescriptorRegistry;
25 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
26
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.ui.texteditor.IDocumentProvider;
42 import org.eclipse.ui.texteditor.ITextEditor;
43
44 import test.PHPParserManager;
45 import test.PHPParserSuperclass;
46
47 /**
48  * A content outline page which always represents the functions of the
49  * connected PHPEditor.
50  */
51 public class PHPContentOutlinePage extends AbstractContentOutlinePage {
52   private static final String ERROR = "error"; //$NON-NLS-1$
53   private static final String WARNING = "warning"; //$NON-NLS-1$
54
55   protected static class SegmentComparator implements Comparator {
56     public int compare(Object o1, Object o2) {
57       if (o1 instanceof OutlineableWithChildren && !(o2 instanceof OutlineableWithChildren)) {
58         return 1;
59       }
60       if (o2 instanceof OutlineableWithChildren && !(o1 instanceof OutlineableWithChildren)) {
61         return -1;
62       }
63       return ((Outlineable) o1).toString().compareToIgnoreCase(((Outlineable) o2).toString());
64     }
65   }
66
67   /**
68    * Divides the editor's document into ten segments and provides elements for them.
69    */
70   protected class ContentProvider implements ITreeContentProvider {
71
72     protected final static String SEGMENTS = "__php_segments"; //$NON-NLS-1$
73     protected IPositionUpdater fPositionUpdater = new DefaultPositionUpdater(SEGMENTS);
74     protected List fContent = new ArrayList(10);
75     protected TreeSet fVariables = new TreeSet();
76
77     //    private String getIdentifier(String text, int firstIndex) {
78     //      int i = firstIndex;
79     //      char c;
80     //      int textLength = text.length();
81     //      StringBuffer identifier = new StringBuffer();
82     //      while (i < textLength) {
83     //        c = text.charAt(i++);
84     //        if (Scanner.isPHPIdentifierPart(c) || (c == '$')) {
85     //          identifier.append(c);
86     //        } else if ((i == firstIndex + 1) && (c == '$')) {
87     //          identifier.append(c);
88     //        } else {
89     //          return identifier.toString();
90     //        }
91     //      }
92     //      return null;
93     //    }
94
95     protected void parse(IDocument document) {
96
97       //      int lines = document.getNumberOfLines();
98       //      int increment = Math.max(Math.round((float) (lines / 10)), 10);
99
100       String name;
101       int index;
102       String text = document.get();
103       PHPParserSuperclass parser = PHPParserManager.getParser(null);
104
105       PHPOutlineInfo outlineInfo = parser.parseInfo(fInput, text);
106       fVariables = outlineInfo.getVariables();
107
108       OutlineableWithChildren declarations = outlineInfo.getDeclarations();
109       Outlineable temp;
110       for (int i = 0; i < declarations.size(); i++) {
111         temp = declarations.get(i);
112         fContent.add(temp);
113       }
114       Collections.sort(fContent, new SegmentComparator());
115     }
116
117     /*
118      * @see IContentProvider#inputChanged(Viewer, Object, Object)
119      */
120     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
121       if (oldInput != null) {
122         IDocument document = fDocumentProvider.getDocument(oldInput);
123         if (document != null) {
124           try {
125             document.removePositionCategory(SEGMENTS);
126           } catch (BadPositionCategoryException x) {
127           }
128           document.removePositionUpdater(fPositionUpdater);
129         }
130       }
131
132       fContent.clear();
133       fVariables.clear();
134
135       if (newInput != null) {
136         IDocument document = fDocumentProvider.getDocument(newInput);
137         if (document != null) {
138           document.addPositionCategory(SEGMENTS);
139           document.addPositionUpdater(fPositionUpdater);
140
141           parse(document);
142         }
143       }
144     }
145
146     /*
147      * @see IContentProvider#dispose
148      */
149     public void dispose() {
150       if (fContent != null) {
151         fContent.clear();
152         fContent = null;
153       }
154       if (fVariables != null) {
155         fVariables.clear();
156         fVariables = null;
157       }
158     }
159
160     /*
161      * @see IContentProvider#isDeleted(Object)
162      */
163     public boolean isDeleted(Object element) {
164       return false;
165     }
166
167     /**
168      * returns all PHP variables
169      */
170     public Object[] getVariables() {
171       return fVariables.toArray();
172     }
173
174     /*
175      * @see IStructuredContentProvider#getElements(Object)
176      */
177     public Object[] getElements(Object element) {
178       return fContent.toArray();
179     }
180
181     /*
182      * @see ITreeContentProvider#hasChildren(Object)
183      */
184     public boolean hasChildren(Object element) {
185       if (element instanceof OutlineableWithChildren) {
186         return !((OutlineableWithChildren) element).getList().isEmpty();
187       }
188       return element == fInput;
189     }
190
191     /*
192      * @see ITreeContentProvider#getParent(Object)
193      */
194     public Object getParent(Object element) {
195       if (element instanceof Outlineable) {
196         return ((Outlineable) element).getParent();
197       }
198       return null;
199     }
200
201     /*
202      * @see ITreeContentProvider#getChildren(Object)
203      */
204     public Object[] getChildren(Object element) {
205       if (element == fInput)
206         return fContent.toArray();
207       if (element instanceof OutlineableWithChildren)
208         return ((OutlineableWithChildren) element).getList().toArray();
209       return new Object[0];
210     }
211   };
212
213   protected class OutlineLabelProvider extends LabelProvider {
214     private ImageDescriptorRegistry fRegistry;
215
216     public OutlineLabelProvider() {
217       fRegistry = PHPeclipsePlugin.getImageDescriptorRegistry();
218     }
219     /**
220     * The <code>LabelProvider</code> implementation of this 
221     * <code>ILabelProvider</code> method returns <code>null</code>. Subclasses may 
222     * override.
223     */
224     public Image getImage(Object element) {
225       if (element instanceof Outlineable) {
226         ImageDescriptor descriptor = ((Outlineable) element).getImage();
227         return fRegistry.get(descriptor);
228       }
229       return null;
230     }
231   }
232
233   protected IDocumentProvider fDocumentProvider;
234   protected ITextEditor fTextEditor;
235   protected PHPEditor fEditor;
236   protected ContentProvider fContentProvider;
237
238   /**
239    * Creates a content outline page using the given provider and the given editor.
240    */
241   public PHPContentOutlinePage(IDocumentProvider provider, ITextEditor editor) {
242     super();
243     fContentProvider = null;
244     fDocumentProvider = provider;
245     fTextEditor = editor;
246     if (editor instanceof PHPEditor)
247       fEditor = (PHPEditor) editor;
248   }
249
250   /* (non-Javadoc)
251    * Method declared on ContentOutlinePage
252    */
253   public void createControl(Composite parent) {
254
255     super.createControl(parent);
256
257     TreeViewer viewer = getTreeViewer();
258
259     fContentProvider = new ContentProvider();
260     viewer.setContentProvider(fContentProvider);
261     viewer.setLabelProvider(new OutlineLabelProvider());
262
263     viewer.addSelectionChangedListener(this);
264
265     if (fInput != null)
266       viewer.setInput(fInput);
267   }
268
269   /* (non-Javadoc)
270    * Method declared on ContentOutlinePage
271    */
272   public void selectionChanged(SelectionChangedEvent event) {
273
274     super.selectionChanged(event);
275
276     ISelection selection = event.getSelection();
277     if (selection.isEmpty())
278       fTextEditor.resetHighlightRange();
279     else {
280       Outlineable segment = (Outlineable) ((IStructuredSelection) selection).getFirstElement();
281       int start = segment.getPosition().getOffset();
282       int length = segment.getPosition().getLength();
283       try {
284         fTextEditor.setHighlightRange(start, length, true);
285       } catch (IllegalArgumentException x) {
286         fTextEditor.resetHighlightRange();
287       }
288     }
289   }
290   
291   public Object[] getVariables() {
292     if (fContentProvider != null) {
293       return fContentProvider.getVariables();
294     }
295     return null;
296   }
297   //  public ContentProvider getContentProvider() {
298   //    return contentProvider;
299   //  }
300
301 }