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 org.eclipse.swt.graphics.RGB;
013:
014: import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy;
015: import org.eclipse.jface.text.IAutoEditStrategy;
016: import org.eclipse.jface.text.IDocument;
017: import org.eclipse.jface.text.ITextDoubleClickStrategy;
018: import org.eclipse.jface.text.ITextHover;
019: import org.eclipse.jface.text.TextAttribute;
020: import org.eclipse.jface.text.contentassist.ContentAssistant;
021: import org.eclipse.jface.text.contentassist.IContentAssistant;
022: import org.eclipse.jface.text.presentation.IPresentationReconciler;
023: import org.eclipse.jface.text.presentation.PresentationReconciler;
024: import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
025: import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
026: import org.eclipse.jface.text.rules.Token;
027: import org.eclipse.jface.text.source.IAnnotationHover;
028: import org.eclipse.jface.text.source.ISourceViewer;
029: import org.eclipse.jface.text.source.SourceViewerConfiguration;
030:
031: import org.eclipse.ui.examples.javaeditor.java.JavaAutoIndentStrategy;
032: import org.eclipse.ui.examples.javaeditor.java.JavaCompletionProcessor;
033: import org.eclipse.ui.examples.javaeditor.java.JavaDoubleClickSelector;
034: import org.eclipse.ui.examples.javaeditor.javadoc.JavaDocCompletionProcessor;
035: import org.eclipse.ui.examples.javaeditor.util.JavaColorProvider;
036:
037: /**
038: * Example configuration for an <code>SourceViewer</code> which shows Java code.
039: */
040: public class JavaSourceViewerConfiguration extends
041: SourceViewerConfiguration {
042:
043: /**
044: * Single token scanner.
045: */
046: static class SingleTokenScanner extends BufferedRuleBasedScanner {
047: public SingleTokenScanner(TextAttribute attribute) {
048: setDefaultReturnToken(new Token(attribute));
049: }
050: }
051:
052: /**
053: * Default constructor.
054: */
055: public JavaSourceViewerConfiguration() {
056: }
057:
058: /* (non-Javadoc)
059: * Method declared on SourceViewerConfiguration
060: */
061: public IAnnotationHover getAnnotationHover(
062: ISourceViewer sourceViewer) {
063: return new JavaAnnotationHover();
064: }
065:
066: /*
067: * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
068: */
069: public IAutoEditStrategy[] getAutoEditStrategies(
070: ISourceViewer sourceViewer, String contentType) {
071: IAutoEditStrategy strategy = (IDocument.DEFAULT_CONTENT_TYPE
072: .equals(contentType) ? new JavaAutoIndentStrategy()
073: : new DefaultIndentLineAutoEditStrategy());
074: return new IAutoEditStrategy[] { strategy };
075: }
076:
077: /*
078: * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
079: */
080: public String getConfiguredDocumentPartitioning(
081: ISourceViewer sourceViewer) {
082: return JavaEditorExamplePlugin.JAVA_PARTITIONING;
083: }
084:
085: /* (non-Javadoc)
086: * Method declared on SourceViewerConfiguration
087: */
088: public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
089: return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
090: JavaPartitionScanner.JAVA_DOC,
091: JavaPartitionScanner.JAVA_MULTILINE_COMMENT };
092: }
093:
094: /* (non-Javadoc)
095: * Method declared on SourceViewerConfiguration
096: */
097: public IContentAssistant getContentAssistant(
098: ISourceViewer sourceViewer) {
099:
100: ContentAssistant assistant = new ContentAssistant();
101: assistant
102: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
103: assistant.setContentAssistProcessor(
104: new JavaCompletionProcessor(),
105: IDocument.DEFAULT_CONTENT_TYPE);
106: assistant.setContentAssistProcessor(
107: new JavaDocCompletionProcessor(),
108: JavaPartitionScanner.JAVA_DOC);
109:
110: assistant.enableAutoActivation(true);
111: assistant.setAutoActivationDelay(500);
112: assistant
113: .setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
114: assistant
115: .setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
116: assistant
117: .setContextInformationPopupBackground(JavaEditorExamplePlugin
118: .getDefault().getJavaColorProvider().getColor(
119: new RGB(150, 150, 0)));
120:
121: return assistant;
122: }
123:
124: /* (non-Javadoc)
125: * Method declared on SourceViewerConfiguration
126: */
127: public String getDefaultPrefix(ISourceViewer sourceViewer,
128: String contentType) {
129: return (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) ? "//" : null); //$NON-NLS-1$
130: }
131:
132: /* (non-Javadoc)
133: * Method declared on SourceViewerConfiguration
134: */
135: public ITextDoubleClickStrategy getDoubleClickStrategy(
136: ISourceViewer sourceViewer, String contentType) {
137: return new JavaDoubleClickSelector();
138: }
139:
140: /* (non-Javadoc)
141: * Method declared on SourceViewerConfiguration
142: */
143: public String[] getIndentPrefixes(ISourceViewer sourceViewer,
144: String contentType) {
145: return new String[] { "\t", " " }; //$NON-NLS-1$ //$NON-NLS-2$
146: }
147:
148: /* (non-Javadoc)
149: * Method declared on SourceViewerConfiguration
150: */
151: public IPresentationReconciler getPresentationReconciler(
152: ISourceViewer sourceViewer) {
153:
154: JavaColorProvider provider = JavaEditorExamplePlugin
155: .getDefault().getJavaColorProvider();
156: PresentationReconciler reconciler = new PresentationReconciler();
157: reconciler
158: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
159:
160: DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
161: JavaEditorExamplePlugin.getDefault()
162: .getJavaCodeScanner());
163: reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
164: reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
165:
166: dr = new DefaultDamagerRepairer(JavaEditorExamplePlugin
167: .getDefault().getJavaDocScanner());
168: reconciler.setDamager(dr, JavaPartitionScanner.JAVA_DOC);
169: reconciler.setRepairer(dr, JavaPartitionScanner.JAVA_DOC);
170:
171: dr = new DefaultDamagerRepairer(
172: new SingleTokenScanner(
173: new TextAttribute(
174: provider
175: .getColor(JavaColorProvider.MULTI_LINE_COMMENT))));
176: reconciler.setDamager(dr,
177: JavaPartitionScanner.JAVA_MULTILINE_COMMENT);
178: reconciler.setRepairer(dr,
179: JavaPartitionScanner.JAVA_MULTILINE_COMMENT);
180:
181: return reconciler;
182: }
183:
184: /* (non-Javadoc)
185: * Method declared on SourceViewerConfiguration
186: */
187: public int getTabWidth(ISourceViewer sourceViewer) {
188: return 4;
189: }
190:
191: /* (non-Javadoc)
192: * Method declared on SourceViewerConfiguration
193: */
194: public ITextHover getTextHover(ISourceViewer sourceViewer,
195: String contentType) {
196: return new JavaTextHover();
197: }
198: }
|