001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.javaeditor;
011:
012: import java.util.ResourceBundle;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016:
017: import org.eclipse.swt.widgets.Composite;
018:
019: import org.eclipse.jface.action.IAction;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.viewers.ISelection;
022:
023: import org.eclipse.jface.text.BadLocationException;
024: import org.eclipse.jface.text.IDocument;
025: import org.eclipse.jface.text.ITextSelection;
026: import org.eclipse.jface.text.ITextViewerExtension5;
027: import org.eclipse.jface.text.Position;
028: import org.eclipse.jface.text.Region;
029: import org.eclipse.jface.text.source.IAnnotationModel;
030: import org.eclipse.jface.text.source.ISourceViewer;
031: import org.eclipse.jface.text.source.IVerticalRuler;
032: import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
033: import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
034: import org.eclipse.jface.text.source.projection.ProjectionSupport;
035: import org.eclipse.jface.text.source.projection.ProjectionViewer;
036:
037: import org.eclipse.ui.IEditorInput;
038: import org.eclipse.ui.editors.text.TextEditor;
039: import org.eclipse.ui.texteditor.ITextEditor;
040: import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
041: import org.eclipse.ui.texteditor.TextEditorAction;
042: import org.eclipse.ui.texteditor.TextOperationAction;
043: import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
044:
045: /**
046: * Java specific text editor.
047: */
048: public class JavaEditor extends TextEditor {
049:
050: private class DefineFoldingRegionAction extends TextEditorAction {
051:
052: public DefineFoldingRegionAction(ResourceBundle bundle,
053: String prefix, ITextEditor editor) {
054: super (bundle, prefix, editor);
055: }
056:
057: private IAnnotationModel getAnnotationModel(ITextEditor editor) {
058: return (IAnnotationModel) editor
059: .getAdapter(ProjectionAnnotationModel.class);
060: }
061:
062: /*
063: * @see org.eclipse.jface.action.Action#run()
064: */
065: public void run() {
066: ITextEditor editor = getTextEditor();
067: ISelection selection = editor.getSelectionProvider()
068: .getSelection();
069: if (selection instanceof ITextSelection) {
070: ITextSelection textSelection = (ITextSelection) selection;
071: if (!textSelection.isEmpty()) {
072: IAnnotationModel model = getAnnotationModel(editor);
073: if (model != null) {
074:
075: int start = textSelection.getStartLine();
076: int end = textSelection.getEndLine();
077:
078: try {
079: IDocument document = editor
080: .getDocumentProvider().getDocument(
081: editor.getEditorInput());
082: int offset = document.getLineOffset(start);
083: int endOffset = document
084: .getLineOffset(end + 1);
085: Position position = new Position(offset,
086: endOffset - offset);
087: model.addAnnotation(
088: new ProjectionAnnotation(),
089: position);
090: } catch (BadLocationException x) {
091: // ignore
092: }
093: }
094: }
095: }
096: }
097: }
098:
099: /** The outline page */
100: private JavaContentOutlinePage fOutlinePage;
101: /** The projection support */
102: private ProjectionSupport fProjectionSupport;
103:
104: /**
105: * Default constructor.
106: */
107: public JavaEditor() {
108: super ();
109: }
110:
111: /** The <code>JavaEditor</code> implementation of this
112: * <code>AbstractTextEditor</code> method extend the
113: * actions to add those specific to the receiver
114: */
115: protected void createActions() {
116: super .createActions();
117:
118: IAction a = new TextOperationAction(
119: JavaEditorMessages.getResourceBundle(),
120: "ContentAssistProposal.", this , ISourceViewer.CONTENTASSIST_PROPOSALS); //$NON-NLS-1$
121: a
122: .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS);
123: setAction("ContentAssistProposal", a); //$NON-NLS-1$
124:
125: a = new TextOperationAction(
126: JavaEditorMessages.getResourceBundle(),
127: "ContentAssistTip.", this , ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION); //$NON-NLS-1$
128: a
129: .setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_CONTEXT_INFORMATION);
130: setAction("ContentAssistTip", a); //$NON-NLS-1$
131:
132: a = new DefineFoldingRegionAction(JavaEditorMessages
133: .getResourceBundle(), "DefineFoldingRegion.", this ); //$NON-NLS-1$
134: setAction("DefineFoldingRegion", a); //$NON-NLS-1$
135: }
136:
137: /** The <code>JavaEditor</code> implementation of this
138: * <code>AbstractTextEditor</code> method performs any extra
139: * disposal actions required by the java editor.
140: */
141: public void dispose() {
142: if (fOutlinePage != null)
143: fOutlinePage.setInput(null);
144: super .dispose();
145: }
146:
147: /** The <code>JavaEditor</code> implementation of this
148: * <code>AbstractTextEditor</code> method performs any extra
149: * revert behavior required by the java editor.
150: */
151: public void doRevertToSaved() {
152: super .doRevertToSaved();
153: if (fOutlinePage != null)
154: fOutlinePage.update();
155: }
156:
157: /** The <code>JavaEditor</code> implementation of this
158: * <code>AbstractTextEditor</code> method performs any extra
159: * save behavior required by the java editor.
160: *
161: * @param monitor the progress monitor
162: */
163: public void doSave(IProgressMonitor monitor) {
164: super .doSave(monitor);
165: if (fOutlinePage != null)
166: fOutlinePage.update();
167: }
168:
169: /** The <code>JavaEditor</code> implementation of this
170: * <code>AbstractTextEditor</code> method performs any extra
171: * save as behavior required by the java editor.
172: */
173: public void doSaveAs() {
174: super .doSaveAs();
175: if (fOutlinePage != null)
176: fOutlinePage.update();
177: }
178:
179: /** The <code>JavaEditor</code> implementation of this
180: * <code>AbstractTextEditor</code> method performs sets the
181: * input of the outline page after AbstractTextEditor has set input.
182: *
183: * @param input the editor input
184: * @throws CoreException in case the input can not be set
185: */
186: public void doSetInput(IEditorInput input) throws CoreException {
187: super .doSetInput(input);
188: if (fOutlinePage != null)
189: fOutlinePage.setInput(input);
190: }
191:
192: /*
193: * @see org.eclipse.ui.texteditor.ExtendedTextEditor#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
194: */
195: protected void editorContextMenuAboutToShow(IMenuManager menu) {
196: super .editorContextMenuAboutToShow(menu);
197: addAction(menu, "ContentAssistProposal"); //$NON-NLS-1$
198: addAction(menu, "ContentAssistTip"); //$NON-NLS-1$
199: addAction(menu, "DefineFoldingRegion"); //$NON-NLS-1$
200: }
201:
202: /** The <code>JavaEditor</code> implementation of this
203: * <code>AbstractTextEditor</code> method performs gets
204: * the java content outline page if request is for a an
205: * outline page.
206: *
207: * @param required the required type
208: * @return an adapter for the required type or <code>null</code>
209: */
210: public Object getAdapter(Class required) {
211: if (IContentOutlinePage.class.equals(required)) {
212: if (fOutlinePage == null) {
213: fOutlinePage = new JavaContentOutlinePage(
214: getDocumentProvider(), this );
215: if (getEditorInput() != null)
216: fOutlinePage.setInput(getEditorInput());
217: }
218: return fOutlinePage;
219: }
220:
221: if (fProjectionSupport != null) {
222: Object adapter = fProjectionSupport.getAdapter(
223: getSourceViewer(), required);
224: if (adapter != null)
225: return adapter;
226: }
227:
228: return super .getAdapter(required);
229: }
230:
231: /* (non-Javadoc)
232: * Method declared on AbstractTextEditor
233: */
234: protected void initializeEditor() {
235: super .initializeEditor();
236: setSourceViewerConfiguration(new JavaSourceViewerConfiguration());
237: }
238:
239: /*
240: * @see org.eclipse.ui.texteditor.ExtendedTextEditor#createSourceViewer(org.eclipse.swt.widgets.Composite, org.eclipse.jface.text.source.IVerticalRuler, int)
241: */
242: protected ISourceViewer createSourceViewer(Composite parent,
243: IVerticalRuler ruler, int styles) {
244:
245: fAnnotationAccess = createAnnotationAccess();
246: fOverviewRuler = createOverviewRuler(getSharedColors());
247:
248: ISourceViewer viewer = new ProjectionViewer(parent, ruler,
249: getOverviewRuler(), isOverviewRulerVisible(), styles);
250: // ensure decoration support has been created and configured.
251: getSourceViewerDecorationSupport(viewer);
252:
253: return viewer;
254: }
255:
256: /*
257: * @see org.eclipse.ui.texteditor.ExtendedTextEditor#createPartControl(org.eclipse.swt.widgets.Composite)
258: */
259: public void createPartControl(Composite parent) {
260: super .createPartControl(parent);
261: ProjectionViewer viewer = (ProjectionViewer) getSourceViewer();
262: fProjectionSupport = new ProjectionSupport(viewer,
263: getAnnotationAccess(), getSharedColors());
264: fProjectionSupport
265: .addSummarizableAnnotationType("org.eclipse.ui.workbench.texteditor.error"); //$NON-NLS-1$
266: fProjectionSupport
267: .addSummarizableAnnotationType("org.eclipse.ui.workbench.texteditor.warning"); //$NON-NLS-1$
268: fProjectionSupport.install();
269: viewer.doOperation(ProjectionViewer.TOGGLE);
270: }
271:
272: /*
273: * @see org.eclipse.ui.texteditor.AbstractTextEditor#adjustHighlightRange(int, int)
274: */
275: protected void adjustHighlightRange(int offset, int length) {
276: ISourceViewer viewer = getSourceViewer();
277: if (viewer instanceof ITextViewerExtension5) {
278: ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
279: extension.exposeModelRange(new Region(offset, length));
280: }
281: }
282: }
|