KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > TextEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.editors.text;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IStatus;
15
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19
20 import org.eclipse.jface.text.source.ISourceViewer;
21 import org.eclipse.jface.text.source.ISourceViewerExtension2;
22
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
25 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditor;
26 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
27 import org.eclipse.ui.texteditor.IUpdate;
28 import org.eclipse.ui.texteditor.spelling.SpellingProblem;
29 import org.eclipse.ui.texteditor.spelling.SpellingService;
30
31
32 /**
33  * The standard text editor for file resources (<code>IFile</code>).
34  * <p>
35  * This editor has id <code>"org.eclipse.ui.DefaultTextEditor"</code>.
36  * The editor's context menu has id <code>#TextEditorContext</code>.
37  * The editor's ruler context menu has id <code>#TextRulerContext</code>.
38  * </p>
39  * <p>
40  * The workbench will automatically instantiate this class when the default
41  * editor is needed for a workbench window.
42  * </p>
43  */

44 public class TextEditor extends AbstractDecoratedTextEditor {
45
46     /**
47      * The encoding support for the editor.
48      * @since 2.0
49      */

50     protected DefaultEncodingSupport fEncodingSupport;
51
52
53     /**
54      * Creates a new text editor.
55      */

56     public TextEditor() {
57         super();
58         if (getSourceViewerConfiguration() == null) {
59             // configuration not yet set by subclass
60
setSourceViewerConfiguration(new TextSourceViewerConfiguration(getPreferenceStore()));
61         }
62
63     }
64
65     /**
66      * {@inheritDoc}
67      *
68      * This method configures the editor but does not define a
69      * <code>SourceViewerConfiguration</code>. When only interested in
70      * providing a custom source viewer configuration, subclasses may extend
71      * this method.
72      */

73     protected void initializeEditor() {
74         setEditorContextMenuId("#TextEditorContext"); //$NON-NLS-1$
75
setRulerContextMenuId("#TextRulerContext"); //$NON-NLS-1$
76
setHelpContextId(ITextEditorHelpContextIds.TEXT_EDITOR);
77         setPreferenceStore(EditorsPlugin.getDefault().getPreferenceStore());
78         configureInsertMode(SMART_INSERT, false);
79         setInsertMode(INSERT);
80     }
81
82     /*
83      * @see IWorkbenchPart#dispose()
84      * @since 2.0
85      */

86     public void dispose() {
87         if (fEncodingSupport != null) {
88             fEncodingSupport.dispose();
89             fEncodingSupport= null;
90         }
91
92         super.dispose();
93     }
94
95     /**
96      * Installs the encoding support on the given text editor.
97      * <p>
98      * Subclasses may override to install their own encoding
99      * support or to disable the default encoding support.
100      * </p>
101      * @since 2.1
102      */

103     protected void installEncodingSupport() {
104         fEncodingSupport= new DefaultEncodingSupport();
105         fEncodingSupport.initialize(this);
106     }
107
108     /*
109      * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
110      */

111     public boolean isSaveAsAllowed() {
112         return true;
113     }
114
115     /*
116      * @see AbstractTextEditor#createActions()
117      * @since 2.0
118      */

119     protected void createActions() {
120         installEncodingSupport();
121         super.createActions();
122     }
123
124     /*
125      * @see StatusTextEditor#getStatusHeader(IStatus)
126      * @since 2.0
127      */

128     protected String JavaDoc getStatusHeader(IStatus status) {
129         if (fEncodingSupport != null) {
130             String JavaDoc message= fEncodingSupport.getStatusHeader(status);
131             if (message != null)
132                 return message;
133         }
134         return super.getStatusHeader(status);
135     }
136
137     /*
138      * @see StatusTextEditor#getStatusBanner(IStatus)
139      * @since 2.0
140      */

141     protected String JavaDoc getStatusBanner(IStatus status) {
142         if (fEncodingSupport != null) {
143             String JavaDoc message= fEncodingSupport.getStatusBanner(status);
144             if (message != null)
145                 return message;
146         }
147         return super.getStatusBanner(status);
148     }
149
150     /*
151      * @see StatusTextEditor#getStatusMessage(IStatus)
152      * @since 2.0
153      */

154     protected String JavaDoc getStatusMessage(IStatus status) {
155         if (fEncodingSupport != null) {
156             String JavaDoc message= fEncodingSupport.getStatusMessage(status);
157             if (message != null)
158                 return message;
159         }
160         return super.getStatusMessage(status);
161     }
162
163     /*
164      * @see AbstractTextEditor#doSetInput(IEditorInput)
165      * @since 2.0
166      */

167     protected void doSetInput(IEditorInput input) throws CoreException {
168         super.doSetInput(input);
169         if (fEncodingSupport != null)
170             fEncodingSupport.reset();
171     }
172
173     /*
174      * @see IAdaptable#getAdapter(java.lang.Class)
175      * @since 2.0
176      */

177     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
178         if (IEncodingSupport.class.equals(adapter))
179             return fEncodingSupport;
180         return super.getAdapter(adapter);
181     }
182
183     /*
184      * @see org.eclipse.ui.texteditor.AbstractTextEditor#updatePropertyDependentActions()
185      * @since 2.0
186      */

187     protected void updatePropertyDependentActions() {
188         super.updatePropertyDependentActions();
189         if (fEncodingSupport != null)
190             fEncodingSupport.reset();
191     }
192     
193     /*
194      * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#handlePreferenceStoreChanged(org.eclipse.jface.util.PropertyChangeEvent)
195      * @since 3.3
196      */

197     protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
198         if (event.getProperty().equals(SpellingService.PREFERENCE_SPELLING_ENABLED)) {
199             ISourceViewer viewer= getSourceViewer();
200             
201             if (!(viewer instanceof ISourceViewerExtension2))
202                 return; // cannot unconfigure - do nothing
203

204             // XXX: this is pretty heavy-weight
205
((ISourceViewerExtension2)viewer).unconfigure();
206             viewer.configure(getSourceViewerConfiguration());
207             
208             if (Boolean.FALSE.equals(event.getNewValue()))
209                 SpellingProblem.removeAllInActiveEditor(this, null);
210             
211             IAction quickAssistAction= getAction(ITextEditorActionConstants.QUICK_ASSIST);
212             if (quickAssistAction instanceof IUpdate)
213                 ((IUpdate)quickAssistAction).update();
214             
215             return;
216         }
217         super.handlePreferenceStoreChanged(event);
218     }
219
220     /*
221      * @see org.eclipse.ui.texteditor.AbstractTextEditor#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
222      * @since 3.0
223      */

224     protected void editorContextMenuAboutToShow(IMenuManager menu) {
225         super.editorContextMenuAboutToShow(menu);
226         addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_RIGHT);
227         addAction(menu, ITextEditorActionConstants.GROUP_EDIT, ITextEditorActionConstants.SHIFT_LEFT);
228     }
229 }
230
Popular Tags