intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.ui / src / net / sourceforge / phpeclipse / js / ui / internal / outline / JSOutlinePage.java
1 /*
2  * Copyright (c) 2002-2004 Adrian Dinu and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Adrian Dinu - initial implementation
10  *     Alex Fitzpatrick - update outline while editing
11  *     Christopher Lenz - use custom content and label providers
12  * 
13  * $Id: JSOutlinePage.java,v 1.1 2004-09-02 18:23:49 jsurfer Exp $
14  */
15
16 package net.sourceforge.phpeclipse.js.ui.internal.outline;
17
18 import net.sourceforge.phpeclipse.js.core.model.JSElementList;
19 import net.sourceforge.phpeclipse.js.core.parser.JSSyntaxModelFactory;
20 import net.sourceforge.phpeclipse.js.ui.model.JSNameSorter;
21
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.ui.model.WorkbenchContentProvider;
27 import org.eclipse.ui.model.WorkbenchLabelProvider;
28 import org.eclipse.ui.texteditor.IDocumentProvider;
29 import org.eclipse.ui.texteditor.ITextEditor;
30 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
31
32 /**
33  * Implements an outline page for JavaScript files.
34  */
35 public class JSOutlinePage extends ContentOutlinePage {
36
37         // Instance Variables ------------------------------------------------------
38
39         /** The associated text editor. **/
40         protected ITextEditor editor;
41
42         // Constructors ------------------------------------------------------------
43
44         /**
45          * Creates a new JSOutlinePage.
46          * 
47          * @param editor the associated text editor
48          */
49         public JSOutlinePage(ITextEditor editor) {
50                 this.editor = editor;
51         }
52
53         // ContentOutlinePage Implementation ---------------------------------------
54
55         /* 
56          * @see org.eclipse.ui.part.IPage#createControl(Composite)
57          */
58         public void createControl(Composite parent) {
59                 super.createControl(parent);
60                 TreeViewer viewer = getTreeViewer();
61                 viewer.setContentProvider(new WorkbenchContentProvider());
62                 viewer.setLabelProvider(new WorkbenchLabelProvider());
63                 // TODO Make sorting optional
64                 viewer.setSorter(new JSNameSorter());
65         }
66
67         // Public Methods ----------------------------------------------------------
68
69         /**
70          * Forces the outline page to update its contents.
71          */
72         public void update() {
73                 IDocument document = getDocument();
74                 JSSyntaxModelFactory factory = JSSyntaxModelFactory.getInstance();
75                 JSElementList model = factory.getContentOutline(document);
76                 if (model != null) {
77                         TreeViewer viewer = getTreeViewer();
78                         if (viewer != null) {
79                                 Control control = viewer.getControl();
80                                 if ((control != null) && !control.isDisposed()) {
81                                         control.setRedraw(false);
82                                         viewer.setInput(model);
83                                         viewer.expandAll();
84                                         control.setRedraw(true);
85                                 }
86                         }
87                 }
88         }
89
90         // Private Methods ---------------------------------------------------------
91
92         /**
93          * Returns the document that is open in the associated text editor.
94          * 
95          * @return the document being edited
96          */
97         private IDocument getDocument() {
98                 IDocumentProvider provider = editor.getDocumentProvider();
99                 return provider.getDocument(editor.getEditorInput());
100         }
101
102 }