1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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
9 * IBM Corporation - initial API and implementation
10 * Tom Eicher (Avaloq Evolution AG) - block selection mode
11 *******************************************************************************/
12 package org.eclipse.jdt.ui.text;
14 import java.util.Arrays;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.IAdaptable;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.util.PropertyChangeEvent;
27 import org.eclipse.jface.text.AbstractInformationControlManager;
28 import org.eclipse.jface.text.DefaultInformationControl;
29 import org.eclipse.jface.text.IAutoEditStrategy;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IInformationControl;
32 import org.eclipse.jface.text.IInformationControlCreator;
33 import org.eclipse.jface.text.ITextDoubleClickStrategy;
34 import org.eclipse.jface.text.ITextHover;
35 import org.eclipse.jface.text.ITextViewerExtension2;
36 import org.eclipse.jface.text.contentassist.ContentAssistant;
37 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
38 import org.eclipse.jface.text.contentassist.IContentAssistant;
39 import org.eclipse.jface.text.formatter.IContentFormatter;
40 import org.eclipse.jface.text.formatter.MultiPassContentFormatter;
41 import org.eclipse.jface.text.information.IInformationPresenter;
42 import org.eclipse.jface.text.information.IInformationProvider;
43 import org.eclipse.jface.text.information.InformationPresenter;
44 import org.eclipse.jface.text.presentation.IPresentationReconciler;
45 import org.eclipse.jface.text.presentation.PresentationReconciler;
46 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
47 import org.eclipse.jface.text.reconciler.IReconciler;
48 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
49 import org.eclipse.jface.text.rules.RuleBasedScanner;
50 import org.eclipse.jface.text.source.Annotation;
51 import org.eclipse.jface.text.source.IAnnotationHover;
52 import org.eclipse.jface.text.source.ISourceViewer;
54 import org.eclipse.ui.IEditorInput;
56 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
57 import org.eclipse.ui.texteditor.ChainedPreferenceStore;
58 import org.eclipse.ui.texteditor.IDocumentProvider;
59 import org.eclipse.ui.texteditor.ITextEditor;
61 import org.eclipse.ui.editors.text.EditorsUI;
62 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
64 import org.eclipse.jdt.core.IJavaElement;
65 import org.eclipse.jdt.core.IJavaProject;
66 import org.eclipse.jdt.core.JavaCore;
67 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
69 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
71 import org.eclipse.jdt.ui.JavaUI;
72 import org.eclipse.jdt.ui.PreferenceConstants;
73 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
75 import org.eclipse.jdt.internal.ui.JavaPlugin;
76 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
77 import org.eclipse.jdt.internal.ui.javaeditor.ICompilationUnitDocumentProvider;
78 import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
79 import org.eclipse.jdt.internal.ui.text.ContentAssistPreference;
80 import org.eclipse.jdt.internal.ui.text.HTMLAnnotationHover;
81 import org.eclipse.jdt.internal.ui.text.JavaCommentScanner;
82 import org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy;
83 import org.eclipse.jdt.internal.ui.text.JavaElementProvider;
84 import org.eclipse.jdt.internal.ui.text.JavaOutlineInformationControl;
85 import org.eclipse.jdt.internal.ui.text.JavaPresentationReconciler;
86 import org.eclipse.jdt.internal.ui.text.JavaReconciler;
87 import org.eclipse.jdt.internal.ui.text.PreferencesAdapter;
88 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
89 import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionAssistant;
90 import org.eclipse.jdt.internal.ui.text.java.ContentAssistProcessor;
91 import org.eclipse.jdt.internal.ui.text.java.JavaAutoIndentStrategy;
92 import org.eclipse.jdt.internal.ui.text.java.JavaCodeScanner;
93 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor;
94 import org.eclipse.jdt.internal.ui.text.java.JavaDoubleClickSelector;
95 import org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy;
96 import org.eclipse.jdt.internal.ui.text.java.JavaStringAutoIndentStrategy;
97 import org.eclipse.jdt.internal.ui.text.java.JavadocDoubleClickStrategy;
98 import org.eclipse.jdt.internal.ui.text.java.PartitionDoubleClickSelector;
99 import org.eclipse.jdt.internal.ui.text.java.SmartSemicolonAutoEditStrategy;
100 import org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
101 import org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy;
102 import org.eclipse.jdt.internal.ui.text.java.hover.JavaInformationProvider;
103 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocAutoIndentStrategy;
104 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocScanner;
105 import org.eclipse.jdt.internal.ui.text.javadoc.JavadocCompletionProcessor;
106 import org.eclipse.jdt.internal.ui.typehierarchy.HierarchyInformationControl;
110 * Configuration for a source viewer which shows Java code.
112 * This class may be instantiated; it is not intended to be subclassed.
115 * @noextend This class is not intended to be subclassed by clients.
117 public class JavaSourceViewerConfiguration extends TextSourceViewerConfiguration {
120 * Preference key used to look up display tab width.
123 * @deprecated As of 3.0, replaced by {@link AbstractDecoratedTextEditorPreferenceConstants#EDITOR_TAB_WIDTH}
125 public final static String PREFERENCE_TAB_WIDTH= PreferenceConstants.EDITOR_TAB_WIDTH;
128 * Preference key for inserting spaces rather than tabs.
131 * @deprecated as of 3.1 use {@link org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants#FORMATTER_TAB_CHAR}
133 public final static String SPACES_FOR_TABS= PreferenceConstants.EDITOR_SPACES_FOR_TABS;
136 private JavaTextTools fJavaTextTools;
137 private ITextEditor fTextEditor;
139 * The document partitioning.
142 private String fDocumentPartitioning;
144 * The Java source code scanner.
147 private AbstractJavaScanner fCodeScanner;
149 * The Java multi-line comment scanner.
152 private AbstractJavaScanner fMultilineCommentScanner;
154 * The Java single-line comment scanner.
157 private AbstractJavaScanner fSinglelineCommentScanner;
159 * The Java string scanner.
162 private AbstractJavaScanner fStringScanner;
164 * The Javadoc scanner.
167 private AbstractJavaScanner fJavaDocScanner;
172 private IColorManager fColorManager;
174 * The double click strategy.
177 private JavaDoubleClickSelector fJavaDoubleClickSelector;
181 * Creates a new Java source viewer configuration for viewers in the given editor
182 * using the given preference store, the color manager and the specified document partitioning.
184 * Creates a Java source viewer configuration in the new setup without text tools. Clients are
185 * allowed to call {@link JavaSourceViewerConfiguration#handlePropertyChangeEvent(PropertyChangeEvent)}
186 * and disallowed to call {@link JavaSourceViewerConfiguration#getPreferenceStore()} on the resulting
187 * Java source viewer configuration.
190 * @param colorManager the color manager
191 * @param preferenceStore the preference store, can be read-only
192 * @param editor the editor in which the configured viewer(s) will reside, or <code>null</code> if none
193 * @param partitioning the document partitioning for this configuration, or <code>null</code> for the default partitioning
196 public JavaSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String partitioning) {
197 super(preferenceStore);
198 fColorManager= colorManager;
200 fDocumentPartitioning= partitioning;
201 initializeScanners();
205 * Creates a new Java source viewer configuration for viewers in the given editor
206 * using the given Java tools.
208 * @param tools the Java text tools to be used
209 * @param editor the editor in which the configured viewer(s) will reside, or <code>null</code> if none
211 * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)}
213 public JavaSourceViewerConfiguration(JavaTextTools tools, ITextEditor editor) {
214 super(createPreferenceStore(tools));
215 fJavaTextTools= tools;
216 fColorManager= tools.getColorManager();
217 fCodeScanner= (AbstractJavaScanner) fJavaTextTools.getCodeScanner();
218 fMultilineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getMultilineCommentScanner();
219 fSinglelineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getSinglelineCommentScanner();
220 fStringScanner= (AbstractJavaScanner) fJavaTextTools.getStringScanner();
221 fJavaDocScanner= (AbstractJavaScanner) fJavaTextTools.getJavaDocScanner();
226 * Returns the Java source code scanner for this configuration.
228 * @return the Java source code scanner
230 protected RuleBasedScanner getCodeScanner() {
235 * Returns the Java multi-line comment scanner for this configuration.
237 * @return the Java multi-line comment scanner
240 protected RuleBasedScanner getMultilineCommentScanner() {
241 return fMultilineCommentScanner;
245 * Returns the Java single-line comment scanner for this configuration.
247 * @return the Java single-line comment scanner
250 protected RuleBasedScanner getSinglelineCommentScanner() {
251 return fSinglelineCommentScanner;
255 * Returns the Java string scanner for this configuration.
257 * @return the Java string scanner
260 protected RuleBasedScanner getStringScanner() {
261 return fStringScanner;
265 * Returns the JavaDoc scanner for this configuration.
267 * @return the JavaDoc scanner
269 protected RuleBasedScanner getJavaDocScanner() {
270 return fJavaDocScanner;
274 * Returns the color manager for this configuration.
276 * @return the color manager
278 protected IColorManager getColorManager() {
279 return fColorManager;
283 * Returns the editor in which the configured viewer(s) will reside.
285 * @return the enclosing editor
287 protected ITextEditor getEditor() {
292 * Returns the preference store used by this configuration to initialize
293 * the individual bits and pieces.
295 * Clients are not allowed to call this method if the new setup without
296 * text tools is in use.</p>
298 * @return the preference store used to initialize this configuration
299 * @see JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
301 * @deprecated As of 3.0
303 protected IPreferenceStore getPreferenceStore() {
304 Assert.isTrue(!isNewSetup());
305 return fJavaTextTools.getPreferenceStore();
309 * @return <code>true</code> iff the new setup without text tools is in use.
313 private boolean isNewSetup() {
314 return fJavaTextTools == null;
318 * Creates and returns a preference store which combines the preference
319 * stores from the text tools and which is read-only.
321 * @param javaTextTools the Java text tools
322 * @return the combined read-only preference store
325 private static final IPreferenceStore createPreferenceStore(JavaTextTools javaTextTools) {
326 Assert.isNotNull(javaTextTools);
327 IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore();
328 if (javaTextTools.getCorePreferenceStore() == null)
329 return new ChainedPreferenceStore(new IPreferenceStore[] { javaTextTools.getPreferenceStore(), generalTextStore});
331 return new ChainedPreferenceStore(new IPreferenceStore[] { javaTextTools.getPreferenceStore(), new PreferencesAdapter(javaTextTools.getCorePreferenceStore()), generalTextStore });
335 * Initializes the scanners.
339 private void initializeScanners() {
340 Assert.isTrue(isNewSetup());
341 fCodeScanner= new JavaCodeScanner(getColorManager(), fPreferenceStore);
342 fMultilineCommentScanner= new JavaCommentScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_MULTI_LINE_COMMENT);
343 fSinglelineCommentScanner= new JavaCommentScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT);
344 fStringScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_STRING);
345 fJavaDocScanner= new JavaDocScanner(getColorManager(), fPreferenceStore);
349 * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
352 public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
354 PresentationReconciler reconciler= new JavaPresentationReconciler();
355 reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
357 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getCodeScanner());
358 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
359 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
361 dr= new DefaultDamagerRepairer(getJavaDocScanner());
362 reconciler.setDamager(dr, IJavaPartitions.JAVA_DOC);
363 reconciler.setRepairer(dr, IJavaPartitions.JAVA_DOC);
365 dr= new DefaultDamagerRepairer(getMultilineCommentScanner());
366 reconciler.setDamager(dr, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
367 reconciler.setRepairer(dr, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
369 dr= new DefaultDamagerRepairer(getSinglelineCommentScanner());
370 reconciler.setDamager(dr, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
371 reconciler.setRepairer(dr, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
373 dr= new DefaultDamagerRepairer(getStringScanner());
374 reconciler.setDamager(dr, IJavaPartitions.JAVA_STRING);
375 reconciler.setRepairer(dr, IJavaPartitions.JAVA_STRING);
377 dr= new DefaultDamagerRepairer(getStringScanner());
378 reconciler.setDamager(dr, IJavaPartitions.JAVA_CHARACTER);
379 reconciler.setRepairer(dr, IJavaPartitions.JAVA_CHARACTER);
386 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
389 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
391 if (getEditor() != null) {
393 ContentAssistant assistant= new ContentAssistant();
394 assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
396 assistant.setRestoreCompletionProposalSize(getSettings("completion_proposal_size")); //$NON-NLS-1$
398 IContentAssistProcessor javaProcessor= new JavaCompletionProcessor(getEditor(), assistant, IDocument.DEFAULT_CONTENT_TYPE);
399 assistant.setContentAssistProcessor(javaProcessor, IDocument.DEFAULT_CONTENT_TYPE);
401 ContentAssistProcessor singleLineProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
402 assistant.setContentAssistProcessor(singleLineProcessor, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
404 ContentAssistProcessor stringProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_STRING);
405 assistant.setContentAssistProcessor(stringProcessor, IJavaPartitions.JAVA_STRING);
407 ContentAssistProcessor multiLineProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
408 assistant.setContentAssistProcessor(multiLineProcessor, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
410 ContentAssistProcessor javadocProcessor= new JavadocCompletionProcessor(getEditor(), assistant);
411 assistant.setContentAssistProcessor(javadocProcessor, IJavaPartitions.JAVA_DOC);
413 ContentAssistPreference.configure(assistant, fPreferenceStore);
415 assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
416 assistant.setInformationControlCreator(new IInformationControlCreator() {
417 public IInformationControl createInformationControl(Shell parent) {
418 return new DefaultInformationControl(parent, JavaPlugin.getAdditionalInfoAffordanceString());
429 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getQuickAssistAssistant(org.eclipse.jface.text.source.ISourceViewer)
433 public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
434 if (getEditor() != null) {
435 JavaCorrectionAssistant assistant= new JavaCorrectionAssistant(getEditor());
436 assistant.setRestoreCompletionProposalSize(getSettings("quick_assist_proposal_size")); //$NON-NLS-1$
443 * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
446 public IReconciler getReconciler(ISourceViewer sourceViewer) {
448 final ITextEditor editor= getEditor();
449 if (editor != null && editor.isEditable()) {
451 JavaCompositeReconcilingStrategy strategy= new JavaCompositeReconcilingStrategy(sourceViewer, editor, getConfiguredDocumentPartitioning(sourceViewer));
452 JavaReconciler reconciler= new JavaReconciler(editor, strategy, false);
453 reconciler.setIsAllowedToModifyDocument(false);
454 reconciler.setDelay(500);
462 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
465 public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
466 String partitioning= getConfiguredDocumentPartitioning(sourceViewer);
467 if (IJavaPartitions.JAVA_DOC.equals(contentType) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(contentType))
468 return new IAutoEditStrategy[] { new JavaDocAutoIndentStrategy(partitioning) };
469 else if (IJavaPartitions.JAVA_STRING.equals(contentType))
470 return new IAutoEditStrategy[] { new SmartSemicolonAutoEditStrategy(partitioning), new JavaStringAutoIndentStrategy(partitioning) };
471 else if (IJavaPartitions.JAVA_CHARACTER.equals(contentType) || IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
472 return new IAutoEditStrategy[] { new SmartSemicolonAutoEditStrategy(partitioning), new JavaAutoIndentStrategy(partitioning, getProject(), sourceViewer) };
474 return new IAutoEditStrategy[] { new JavaAutoIndentStrategy(partitioning, getProject(), sourceViewer) };
478 * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
481 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
482 if (IJavaPartitions.JAVA_DOC.equals(contentType))
483 return new JavadocDoubleClickStrategy(getConfiguredDocumentPartitioning(sourceViewer));
484 if (IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(contentType))
485 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0);
486 if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(contentType))
487 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0);
488 else if (IJavaPartitions.JAVA_STRING.equals(contentType) || IJavaPartitions.JAVA_CHARACTER.equals(contentType))
489 return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 1, 1);
490 if (fJavaDoubleClickSelector == null) {
491 fJavaDoubleClickSelector= new JavaDoubleClickSelector();
492 fJavaDoubleClickSelector.setSourceVersion(fPreferenceStore.getString(JavaCore.COMPILER_SOURCE));
494 return fJavaDoubleClickSelector;
498 * @see SourceViewerConfiguration#getDefaultPrefixes(ISourceViewer, String)
502 public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
503 return new String[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
507 * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
510 public String[] getIndentPrefixes(ISourceViewer sourceViewer, String contentType) {
511 IJavaProject project= getProject();
512 final int tabWidth= CodeFormatterUtil.getTabWidth(project);
513 final int indentWidth= CodeFormatterUtil.getIndentWidth(project);
514 boolean allowTabs= tabWidth <= indentWidth;
518 indentMode= JavaCore.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
520 indentMode= project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true);
522 boolean useSpaces= JavaCore.SPACE.equals(indentMode) || DefaultCodeFormatterConstants.MIXED.equals(indentMode);
524 Assert.isLegal(allowTabs || useSpaces);
527 char[] spaces= new char[indentWidth];
528 Arrays.fill(spaces, ' ');
529 return new String[] { new String(spaces), "" }; //$NON-NLS-1$
530 } else if (!useSpaces)
531 return getIndentPrefixesForTab(tabWidth);
533 return getIndentPrefixesForSpaces(tabWidth);
537 * Computes and returns the indent prefixes for space indentation
538 * and the given <code>tabWidth</code>.
540 * @param tabWidth the display tab width
541 * @return the indent prefixes
542 * @see #getIndentPrefixes(ISourceViewer, String)
545 private String[] getIndentPrefixesForSpaces(int tabWidth) {
546 String[] indentPrefixes= new String[tabWidth + 2];
547 indentPrefixes[0]= getStringWithSpaces(tabWidth);
549 for (int i= 0; i < tabWidth; i++) {
550 String spaces= getStringWithSpaces(i);
552 indentPrefixes[i+1]= spaces + '\t';
554 indentPrefixes[i+1]= new String(spaces);
557 indentPrefixes[tabWidth + 1]= ""; //$NON-NLS-1$
559 return indentPrefixes;
563 * Creates and returns a String with <code>count</code> spaces.
565 * @param count the space count
566 * @return the string with the spaces
569 private String getStringWithSpaces(int count) {
570 char[] spaceChars= new char[count];
571 Arrays.fill(spaceChars, ' ');
572 return new String(spaceChars);
575 private IJavaProject getProject() {
576 ITextEditor editor= getEditor();
580 IJavaElement element= null;
581 IEditorInput input= editor.getEditorInput();
582 IDocumentProvider provider= editor.getDocumentProvider();
583 if (provider instanceof ICompilationUnitDocumentProvider) {
584 ICompilationUnitDocumentProvider cudp= (ICompilationUnitDocumentProvider) provider;
585 element= cudp.getWorkingCopy(input);
586 } else if (input instanceof IClassFileEditorInput) {
587 IClassFileEditorInput cfei= (IClassFileEditorInput) input;
588 element= cfei.getClassFile();
594 return element.getJavaProject();
598 * @see SourceViewerConfiguration#getTabWidth(ISourceViewer)
601 public int getTabWidth(ISourceViewer sourceViewer) {
602 return CodeFormatterUtil.getTabWidth(getProject());
606 * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
609 public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
610 return new HTMLAnnotationHover(false) {
612 protected boolean isIncluded(Annotation annotation) {
613 return isShowInVerticalRuler(annotation);
619 * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
623 public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
624 return new HTMLAnnotationHover(true) {
626 protected boolean isIncluded(Annotation annotation) {
627 return isShowInOverviewRuler(annotation);
633 * @see SourceViewerConfiguration#getConfiguredTextHoverStateMasks(ISourceViewer, String)
637 public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String contentType) {
638 JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
639 int stateMasks[]= new int[hoverDescs.length];
640 int stateMasksLength= 0;
641 for (int i= 0; i < hoverDescs.length; i++) {
642 if (hoverDescs[i].isEnabled()) {
644 int stateMask= hoverDescs[i].getStateMask();
645 while (j < stateMasksLength) {
646 if (stateMasks[j] == stateMask)
650 if (j == stateMasksLength)
651 stateMasks[stateMasksLength++]= stateMask;
654 if (stateMasksLength == hoverDescs.length)
657 int[] shortenedStateMasks= new int[stateMasksLength];
658 System.arraycopy(stateMasks, 0, shortenedStateMasks, 0, stateMasksLength);
659 return shortenedStateMasks;
663 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
667 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
668 JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
670 while (i < hoverDescs.length) {
671 if (hoverDescs[i].isEnabled() && hoverDescs[i].getStateMask() == stateMask)
672 return new JavaEditorTextHoverProxy(hoverDescs[i], getEditor());
680 * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
683 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
684 return getTextHover(sourceViewer, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
688 * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
691 public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
692 return new String[] {
693 IDocument.DEFAULT_CONTENT_TYPE,
694 IJavaPartitions.JAVA_DOC,
695 IJavaPartitions.JAVA_MULTI_LINE_COMMENT,
696 IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
697 IJavaPartitions.JAVA_STRING,
698 IJavaPartitions.JAVA_CHARACTER
703 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
707 public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
708 if (fDocumentPartitioning != null)
709 return fDocumentPartitioning;
710 return super.getConfiguredDocumentPartitioning(sourceViewer);
714 * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
717 public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
718 final MultiPassContentFormatter formatter= new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE);
719 formatter.setMasterStrategy(new JavaFormattingStrategy());
724 * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
728 public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
729 return new IInformationControlCreator() {
730 public IInformationControl createInformationControl(Shell parent) {
731 return new DefaultInformationControl(parent, false);
737 * Returns the information presenter control creator. The creator is a factory creating the
738 * presenter controls for the given source viewer. This implementation always returns a creator
739 * for <code>DefaultInformationControl</code> instances.
741 * @param sourceViewer the source viewer to be configured by this configuration
742 * @return an information control creator
745 private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
746 return new IInformationControlCreator() {
747 public IInformationControl createInformationControl(Shell parent) {
748 return new DefaultInformationControl(parent, true);
754 * Returns the outline presenter control creator. The creator is a factory creating outline
755 * presenter controls for the given source viewer. This implementation always returns a creator
756 * for <code>JavaOutlineInformationControl</code> instances.
758 * @param sourceViewer the source viewer to be configured by this configuration
759 * @param commandId the ID of the command that opens this control
760 * @return an information control creator
763 private IInformationControlCreator getOutlinePresenterControlCreator(ISourceViewer sourceViewer, final String commandId) {
764 return new IInformationControlCreator() {
765 public IInformationControl createInformationControl(Shell parent) {
766 int shellStyle= SWT.RESIZE;
767 int treeStyle= SWT.V_SCROLL | SWT.H_SCROLL;
768 return new JavaOutlineInformationControl(parent, shellStyle, treeStyle, commandId);
773 private IInformationControlCreator getHierarchyPresenterControlCreator() {
774 return new IInformationControlCreator() {
775 public IInformationControl createInformationControl(Shell parent) {
776 int shellStyle= SWT.RESIZE;
777 int treeStyle= SWT.V_SCROLL | SWT.H_SCROLL;
778 return new HierarchyInformationControl(parent, shellStyle, treeStyle);
784 * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
788 public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
789 InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
790 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
792 // Register information provider
793 IInformationProvider provider= new JavaInformationProvider(getEditor());
794 String[] contentTypes= getConfiguredContentTypes(sourceViewer);
795 for (int i= 0; i < contentTypes.length; i++)
796 presenter.setInformationProvider(provider, contentTypes[i]);
798 // sizes: see org.eclipse.jface.text.TextViewer.TEXT_HOVER_*_CHARS
799 presenter.setSizeConstraints(100, 12, false, true);
804 * Returns the outline presenter which will determine and shown
805 * information requested for the current cursor position.
807 * @param sourceViewer the source viewer to be configured by this configuration
808 * @param doCodeResolve a boolean which specifies whether code resolve should be used to compute the Java element
809 * @return an information presenter
812 public IInformationPresenter getOutlinePresenter(ISourceViewer sourceViewer, boolean doCodeResolve) {
813 InformationPresenter presenter;
815 presenter= new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer, IJavaEditorActionDefinitionIds.OPEN_STRUCTURE));
817 presenter= new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer, IJavaEditorActionDefinitionIds.SHOW_OUTLINE));
818 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
819 presenter.setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
820 IInformationProvider provider= new JavaElementProvider(getEditor(), doCodeResolve);
821 presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
822 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
823 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
824 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
825 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_STRING);
826 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_CHARACTER);
827 presenter.setSizeConstraints(50, 20, true, false);
832 * Returns the settings for the given section.
834 * @param sectionName the section name
835 * @return the settings
838 private IDialogSettings getSettings(String sectionName) {
839 IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings().getSection(sectionName);
840 if (settings == null)
841 settings= JavaPlugin.getDefault().getDialogSettings().addNewSection(sectionName);
847 * Returns the hierarchy presenter which will determine and shown type hierarchy
848 * information requested for the current cursor position.
850 * @param sourceViewer the source viewer to be configured by this configuration
851 * @param doCodeResolve a boolean which specifies whether code resolve should be used to compute the Java element
852 * @return an information presenter
855 public IInformationPresenter getHierarchyPresenter(ISourceViewer sourceViewer, boolean doCodeResolve) {
857 // Do not create hierarchy presenter if there's no CU.
858 if (getEditor() != null && getEditor().getEditorInput() != null && JavaUI.getEditorInputJavaElement(getEditor().getEditorInput()) == null)
861 InformationPresenter presenter= new InformationPresenter(getHierarchyPresenterControlCreator());
862 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
863 presenter.setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
864 IInformationProvider provider= new JavaElementProvider(getEditor(), doCodeResolve);
865 presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
866 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
867 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
868 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
869 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_STRING);
870 presenter.setInformationProvider(provider, IJavaPartitions.JAVA_CHARACTER);
871 presenter.setSizeConstraints(50, 20, true, false);
876 * Determines whether the preference change encoded by the given event
877 * changes the behavior of one of its contained components.
879 * @param event the event to be investigated
880 * @return <code>true</code> if event causes a behavioral change
883 public boolean affectsTextPresentation(PropertyChangeEvent event) {
884 return fCodeScanner.affectsBehavior(event)
885 || fMultilineCommentScanner.affectsBehavior(event)
886 || fSinglelineCommentScanner.affectsBehavior(event)
887 || fStringScanner.affectsBehavior(event)
888 || fJavaDocScanner.affectsBehavior(event);
892 * Adapts the behavior of the contained components to the change
893 * encoded in the given event.
895 * Clients are not allowed to call this method if the old setup with
896 * text tools is in use.
899 * @param event the event to which to adapt
900 * @see JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
903 public void handlePropertyChangeEvent(PropertyChangeEvent event) {
904 Assert.isTrue(isNewSetup());
905 if (fCodeScanner.affectsBehavior(event))
906 fCodeScanner.adaptToPreferenceChange(event);
907 if (fMultilineCommentScanner.affectsBehavior(event))
908 fMultilineCommentScanner.adaptToPreferenceChange(event);
909 if (fSinglelineCommentScanner.affectsBehavior(event))
910 fSinglelineCommentScanner.adaptToPreferenceChange(event);
911 if (fStringScanner.affectsBehavior(event))
912 fStringScanner.adaptToPreferenceChange(event);
913 if (fJavaDocScanner.affectsBehavior(event))
914 fJavaDocScanner.adaptToPreferenceChange(event);
915 if (fJavaDoubleClickSelector != null && JavaCore.COMPILER_SOURCE.equals(event.getProperty()))
916 if (event.getNewValue() instanceof String)
917 fJavaDoubleClickSelector.setSourceVersion((String) event.getNewValue());
921 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
925 protected Map<String, IAdaptable> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
926 Map<String, IAdaptable> targets= super.getHyperlinkDetectorTargets(sourceViewer);
927 targets.put("org.eclipse.jdt.ui.javaCode", fTextEditor); //$NON-NLS-1$