intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.js.ui / src / net / sourceforge / phpeclipse / js / ui / editors / JSEditor.java
1 /*
2  * $RCSfile: JSEditor.java,v $
3  *
4  * Copyright 2002
5  * CH-1700 Fribourg, Switzerland
6  * All rights reserved.
7  *
8  *========================================================================
9  * Modifications history
10  *========================================================================
11  * $Log: not supported by cvs2svn $
12  * Revision 1.4  2004/02/27 17:12:48  cell
13  * Use the new outline page
14  *
15  * Revision 1.3  2004/02/26 02:25:57  agfitzp
16  * renamed packages to match xml & css
17  *
18  * Revision 1.2  2004/02/14 18:36:30  ayashi
19  * More UI-Core refactoring... still doesn't work though...
20  *
21  * Revision 1.1  2004/02/05 03:13:28  agfitzp
22  * Initial submission, outline view is broken due to refactoring
23  *
24  * Revision 1.5.2.1  2003/12/12 21:37:24  agfitzp
25  * Experimental work for Classes view
26  *
27  * Revision 1.5  2003/08/14 15:14:15  agfitzp
28  * Removed thread hack from automatic update
29  *
30  * Revision 1.4  2003/07/04 17:26:56  agfitzp
31  * New hack, update in a new thread only if we're not already in the middle of updating
32  *
33  * Revision 1.3  2003/06/21 03:48:51  agfitzp
34  * fixed global variables as functions bug
35  * fixed length calculation of instance variables
36  * Automatic outlining is now a preference
37  *
38  * Revision 1.2  2003/05/28 20:47:58  agfitzp
39  * Outline the document, not the file.
40  *
41  * Revision 1.1  2003/05/28 15:17:12  agfitzp
42  * net.sourceforge.phpeclipse.js.ui 0.0.1 code base
43  *
44  *========================================================================
45 */
46
47 package net.sourceforge.phpeclipse.js.ui.editors;
48
49 import net.sourceforge.phpeclipse.js.core.model.JSElement;
50 import net.sourceforge.phpeclipse.js.ui.internal.outline.JSOutlinePage;
51
52 import org.eclipse.core.runtime.IProgressMonitor;
53 import org.eclipse.jface.action.IAction;
54 import org.eclipse.jface.viewers.ISelectionChangedListener;
55 import org.eclipse.jface.viewers.IStructuredSelection;
56 import org.eclipse.jface.viewers.SelectionChangedEvent;
57 import org.eclipse.ui.editors.text.TextEditor;
58 import org.eclipse.ui.texteditor.ContentAssistAction;
59 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
60 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
61
62 /**
63  * DOCUMENT ME!
64  * 
65  * @version $Revision: 1.1 $
66  * @author $Author: jsurfer $, $Date: 2004-09-02 18:23:49 $
67  */
68 public class JSEditor extends TextEditor implements ISelectionChangedListener
69 {
70         protected JSColorManager colorManager = new JSColorManager();
71         protected JSOutlinePage outlinePage;
72         protected JSConfiguration configuration;
73         
74         protected boolean updating = false;
75
76         /**
77          * Constructor for SampleEditor.
78          */
79         public JSEditor()
80         {
81                 super();
82                 configuration = new JSConfiguration(colorManager);
83                 
84                 setSourceViewerConfiguration(configuration);
85                 setDocumentProvider(new JSDocumentProvider());
86         }
87         protected void createActions() {
88                 super.createActions();
89
90                 IAction action = new ContentAssistAction(JSEditorMessages.getResourceBundle(),
91                 "ContentAssistProposal.", this); //$NON-NLS-1$
92             action
93                 .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
94             setAction("ContentAssistProposal", action); //$NON-NLS-1$
95             markAsStateDependentAction("ContentAssistProposal", true); //$NON-NLS-1$
96             
97 //              IAction action= new TextOperationAction(
98 //                              TemplateMessages.getResourceBundle(),
99 //                              "Editor." + TEMPLATE_PROPOSALS + ".", //$NON-NLS-1$ //$NON-NLS-2$
100 //                              this,
101 //                              ISourceViewer.CONTENTASSIST_PROPOSALS);
102 //              action.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
103 //              setAction(TEMPLATE_PROPOSALS, action);
104 //              markAsStateDependentAction(TEMPLATE_PROPOSALS, true);
105         }
106         /**
107          * Method declared on IEditorPart
108          * @param monitor
109          */
110         public void doSave(IProgressMonitor monitor)
111         {
112                 super.doSave(monitor);
113
114                 if (outlinePage != null)
115                 {
116                         outlinePage.update();
117                 }
118         }
119
120         /**
121          *
122          */
123         public void dispose()
124         {
125                 colorManager.dispose();
126                 super.dispose();
127         }
128
129         /*
130          * @see org.eclipse.ui.texteditor.AbstractTextEditor#getAdapter(Class)
131          */
132         public Object getAdapter(Class key) {
133                 if (key.equals(IContentOutlinePage.class)) {
134                         outlinePage = new JSOutlinePage(this);
135                         outlinePage.addSelectionChangedListener(this);
136                         return outlinePage;
137                 }
138                 return super.getAdapter(key);
139         }
140
141         /**
142          * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(SelectionChangedEvent)
143          */
144         public void selectionChanged(SelectionChangedEvent event)
145         {
146                 if (null != event)
147                 {
148                         if (event.getSelection() instanceof IStructuredSelection)
149                         {
150                                 IStructuredSelection sel = (IStructuredSelection) event.getSelection();
151                                 if (null != sel)
152                                 {
153                                         JSElement fe = (JSElement) sel.getFirstElement();
154                                         if (null != fe)
155                                         {
156                                                 selectAndReveal(fe.getStart(), fe.getLength());
157                                         }
158                                 }
159                         }
160                 }
161         }
162
163         /**
164          * Updates all content dependent actions.
165          * 
166          * This might be a hack: We're trapping this update to ensure that the 
167          * outline is always up to date.
168          */
169         protected void updateContentDependentActions()
170         {
171                 super.updateContentDependentActions();
172                 
173                 if(!updating)
174                 {
175                         if (configuration.getAutomaticOutliningPreference())
176                         {
177                                 if (outlinePage != null)
178                                 {
179                                         updating = true;
180         
181                                         outlinePage.update();
182                                         updating = false;
183                                 }
184                         }
185                 }
186         }
187 }