KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > JavaSourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.ui.text;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Shell;
21
22 import org.eclipse.jface.dialogs.IDialogSettings;
23 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26
27 import org.eclipse.jface.text.AbstractInformationControlManager;
28 import org.eclipse.jface.text.DefaultInformationControl;
29 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy;
30 import org.eclipse.jface.text.IAutoEditStrategy;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.IInformationControl;
33 import org.eclipse.jface.text.IInformationControlCreator;
34 import org.eclipse.jface.text.ITextDoubleClickStrategy;
35 import org.eclipse.jface.text.ITextHover;
36 import org.eclipse.jface.text.ITextViewerExtension2;
37 import org.eclipse.jface.text.contentassist.ContentAssistant;
38 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
39 import org.eclipse.jface.text.contentassist.IContentAssistant;
40 import org.eclipse.jface.text.formatter.IContentFormatter;
41 import org.eclipse.jface.text.formatter.MultiPassContentFormatter;
42 import org.eclipse.jface.text.information.IInformationPresenter;
43 import org.eclipse.jface.text.information.IInformationProvider;
44 import org.eclipse.jface.text.information.InformationPresenter;
45 import org.eclipse.jface.text.presentation.IPresentationReconciler;
46 import org.eclipse.jface.text.presentation.PresentationReconciler;
47 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
48 import org.eclipse.jface.text.reconciler.IReconciler;
49 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
50 import org.eclipse.jface.text.rules.RuleBasedScanner;
51 import org.eclipse.jface.text.source.Annotation;
52 import org.eclipse.jface.text.source.IAnnotationHover;
53 import org.eclipse.jface.text.source.ISourceViewer;
54
55 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;
60
61 import org.eclipse.ui.editors.text.EditorsUI;
62 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
63
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;
68
69 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
70
71 import org.eclipse.jdt.ui.JavaUI;
72 import org.eclipse.jdt.ui.PreferenceConstants;
73 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
74
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.comment.CommentFormattingStrategy;
90 import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionAssistant;
91 import org.eclipse.jdt.internal.ui.text.java.ContentAssistProcessor;
92 import org.eclipse.jdt.internal.ui.text.java.JavaAutoIndentStrategy;
93 import org.eclipse.jdt.internal.ui.text.java.JavaCodeScanner;
94 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor;
95 import org.eclipse.jdt.internal.ui.text.java.JavaDoubleClickSelector;
96 import org.eclipse.jdt.internal.ui.text.java.JavaFormattingStrategy;
97 import org.eclipse.jdt.internal.ui.text.java.JavaStringAutoIndentStrategy;
98 import org.eclipse.jdt.internal.ui.text.java.JavaStringDoubleClickSelector;
99 import org.eclipse.jdt.internal.ui.text.java.JavadocDoubleClickStrategy;
100 import org.eclipse.jdt.internal.ui.text.java.SmartSemicolonAutoEditStrategy;
101 import org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor;
102 import org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy;
103 import org.eclipse.jdt.internal.ui.text.java.hover.JavaInformationProvider;
104 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocAutoIndentStrategy;
105 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocScanner;
106 import org.eclipse.jdt.internal.ui.text.javadoc.JavadocCompletionProcessor;
107 import org.eclipse.jdt.internal.ui.typehierarchy.HierarchyInformationControl;
108
109
110 /**
111  * Configuration for a source viewer which shows Java code.
112  * <p>
113  * This class may be instantiated; it is not intended to be subclassed.
114  * </p>
115  */

116 public class JavaSourceViewerConfiguration extends TextSourceViewerConfiguration {
117
118     /**
119      * Preference key used to look up display tab width.
120      *
121      * @since 2.0
122      * @deprecated As of 3.0, replaced by {@link AbstractDecoratedTextEditorPreferenceConstants#EDITOR_TAB_WIDTH}
123      */

124     public final static String JavaDoc PREFERENCE_TAB_WIDTH= PreferenceConstants.EDITOR_TAB_WIDTH;
125
126     /**
127      * Preference key for inserting spaces rather than tabs.
128      *
129      * @since 2.0
130      * @deprecated as of 3.1 use {@link org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants#FORMATTER_TAB_CHAR}
131      */

132     public final static String JavaDoc SPACES_FOR_TABS= PreferenceConstants.EDITOR_SPACES_FOR_TABS;
133
134
135     private JavaTextTools fJavaTextTools;
136     private ITextEditor fTextEditor;
137     /**
138      * The document partitioning.
139      * @since 3.0
140      */

141     private String JavaDoc fDocumentPartitioning;
142     /**
143      * The Java source code scanner.
144      * @since 3.0
145      */

146     private AbstractJavaScanner fCodeScanner;
147     /**
148      * The Java multi-line comment scanner.
149      * @since 3.0
150      */

151     private AbstractJavaScanner fMultilineCommentScanner;
152     /**
153      * The Java single-line comment scanner.
154      * @since 3.0
155      */

156     private AbstractJavaScanner fSinglelineCommentScanner;
157     /**
158      * The Java string scanner.
159      * @since 3.0
160      */

161     private AbstractJavaScanner fStringScanner;
162     /**
163      * The Javadoc scanner.
164      * @since 3.0
165      */

166     private AbstractJavaScanner fJavaDocScanner;
167     /**
168      * The color manager.
169      * @since 3.0
170      */

171     private IColorManager fColorManager;
172     /**
173      * The double click strategy.
174      * @since 3.1
175      */

176     private JavaDoubleClickSelector fJavaDoubleClickSelector;
177
178
179     /**
180      * Creates a new Java source viewer configuration for viewers in the given editor
181      * using the given preference store, the color manager and the specified document partitioning.
182      * <p>
183      * Creates a Java source viewer configuration in the new setup without text tools. Clients are
184      * allowed to call {@link JavaSourceViewerConfiguration#handlePropertyChangeEvent(PropertyChangeEvent)}
185      * and disallowed to call {@link JavaSourceViewerConfiguration#getPreferenceStore()} on the resulting
186      * Java source viewer configuration.
187      * </p>
188      *
189      * @param colorManager the color manager
190      * @param preferenceStore the preference store, can be read-only
191      * @param editor the editor in which the configured viewer(s) will reside, or <code>null</code> if none
192      * @param partitioning the document partitioning for this configuration, or <code>null</code> for the default partitioning
193      * @since 3.0
194      */

195     public JavaSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String JavaDoc partitioning) {
196         super(preferenceStore);
197         fColorManager= colorManager;
198         fTextEditor= editor;
199         fDocumentPartitioning= partitioning;
200         initializeScanners();
201     }
202
203     /**
204      * Creates a new Java source viewer configuration for viewers in the given editor
205      * using the given Java tools.
206      *
207      * @param tools the Java text tools to be used
208      * @param editor the editor in which the configured viewer(s) will reside, or <code>null</code> if none
209      * @see JavaTextTools
210      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)}
211      */

212     public JavaSourceViewerConfiguration(JavaTextTools tools, ITextEditor editor) {
213         super(createPreferenceStore(tools));
214         fJavaTextTools= tools;
215         fColorManager= tools.getColorManager();
216         fCodeScanner= (AbstractJavaScanner) fJavaTextTools.getCodeScanner();
217         fMultilineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getMultilineCommentScanner();
218         fSinglelineCommentScanner= (AbstractJavaScanner) fJavaTextTools.getSinglelineCommentScanner();
219         fStringScanner= (AbstractJavaScanner) fJavaTextTools.getStringScanner();
220         fJavaDocScanner= (AbstractJavaScanner) fJavaTextTools.getJavaDocScanner();
221         fTextEditor= editor;
222     }
223
224     /**
225      * Returns the Java source code scanner for this configuration.
226      *
227      * @return the Java source code scanner
228      */

229     protected RuleBasedScanner getCodeScanner() {
230         return fCodeScanner;
231     }
232
233     /**
234      * Returns the Java multi-line comment scanner for this configuration.
235      *
236      * @return the Java multi-line comment scanner
237      * @since 2.0
238      */

239     protected RuleBasedScanner getMultilineCommentScanner() {
240         return fMultilineCommentScanner;
241     }
242
243     /**
244      * Returns the Java single-line comment scanner for this configuration.
245      *
246      * @return the Java single-line comment scanner
247      * @since 2.0
248      */

249     protected RuleBasedScanner getSinglelineCommentScanner() {
250         return fSinglelineCommentScanner;
251     }
252
253     /**
254      * Returns the Java string scanner for this configuration.
255      *
256      * @return the Java string scanner
257      * @since 2.0
258      */

259     protected RuleBasedScanner getStringScanner() {
260         return fStringScanner;
261     }
262
263     /**
264      * Returns the JavaDoc scanner for this configuration.
265      *
266      * @return the JavaDoc scanner
267      */

268     protected RuleBasedScanner getJavaDocScanner() {
269         return fJavaDocScanner;
270     }
271
272     /**
273      * Returns the color manager for this configuration.
274      *
275      * @return the color manager
276      */

277     protected IColorManager getColorManager() {
278         return fColorManager;
279     }
280
281     /**
282      * Returns the editor in which the configured viewer(s) will reside.
283      *
284      * @return the enclosing editor
285      */

286     protected ITextEditor getEditor() {
287         return fTextEditor;
288     }
289
290     /**
291      * Returns the preference store used by this configuration to initialize
292      * the individual bits and pieces.
293      * <p>
294      * Clients are not allowed to call this method if the new setup without
295      * text tools is in use.</p>
296      *
297      * @return the preference store used to initialize this configuration
298      * @see JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
299      * @since 2.0
300      * @deprecated As of 3.0
301      */

302     protected IPreferenceStore getPreferenceStore() {
303         Assert.isTrue(!isNewSetup());
304         return fJavaTextTools.getPreferenceStore();
305     }
306
307     /**
308      * @return <code>true</code> iff the new setup without text tools is in use.
309      *
310      * @since 3.0
311      */

312     private boolean isNewSetup() {
313         return fJavaTextTools == null;
314     }
315
316     /**
317      * Creates and returns a preference store which combines the preference
318      * stores from the text tools and which is read-only.
319      *
320      * @param javaTextTools the Java text tools
321      * @return the combined read-only preference store
322      * @since 3.0
323      */

324     private static final IPreferenceStore createPreferenceStore(JavaTextTools javaTextTools) {
325         Assert.isNotNull(javaTextTools);
326         IPreferenceStore generalTextStore= EditorsUI.getPreferenceStore();
327         if (javaTextTools.getCorePreferenceStore() == null)
328             return new ChainedPreferenceStore(new IPreferenceStore[] { javaTextTools.getPreferenceStore(), generalTextStore});
329
330         return new ChainedPreferenceStore(new IPreferenceStore[] { javaTextTools.getPreferenceStore(), new PreferencesAdapter(javaTextTools.getCorePreferenceStore()), generalTextStore });
331     }
332
333     /**
334      * Initializes the scanners.
335      *
336      * @since 3.0
337      */

338     private void initializeScanners() {
339         Assert.isTrue(isNewSetup());
340         fCodeScanner= new JavaCodeScanner(getColorManager(), fPreferenceStore);
341         fMultilineCommentScanner= new JavaCommentScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_MULTI_LINE_COMMENT);
342         fSinglelineCommentScanner= new JavaCommentScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT);
343         fStringScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, IJavaColorConstants.JAVA_STRING);
344         fJavaDocScanner= new JavaDocScanner(getColorManager(), fPreferenceStore);
345     }
346
347     /*
348      * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
349      */

350     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
351
352         PresentationReconciler reconciler= new JavaPresentationReconciler();
353         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
354
355         DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getCodeScanner());
356         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
357         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
358
359         dr= new DefaultDamagerRepairer(getJavaDocScanner());
360         reconciler.setDamager(dr, IJavaPartitions.JAVA_DOC);
361         reconciler.setRepairer(dr, IJavaPartitions.JAVA_DOC);
362
363         dr= new DefaultDamagerRepairer(getMultilineCommentScanner());
364         reconciler.setDamager(dr, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
365         reconciler.setRepairer(dr, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
366
367         dr= new DefaultDamagerRepairer(getSinglelineCommentScanner());
368         reconciler.setDamager(dr, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
369         reconciler.setRepairer(dr, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
370
371         dr= new DefaultDamagerRepairer(getStringScanner());
372         reconciler.setDamager(dr, IJavaPartitions.JAVA_STRING);
373         reconciler.setRepairer(dr, IJavaPartitions.JAVA_STRING);
374
375         dr= new DefaultDamagerRepairer(getStringScanner());
376         reconciler.setDamager(dr, IJavaPartitions.JAVA_CHARACTER);
377         reconciler.setRepairer(dr, IJavaPartitions.JAVA_CHARACTER);
378
379
380         return reconciler;
381     }
382
383     /*
384      * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
385      */

386     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
387
388         if (getEditor() != null) {
389
390             ContentAssistant assistant= new ContentAssistant();
391             assistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
392
393             assistant.setRestoreCompletionProposalSize(getSettings("completion_proposal_size")); //$NON-NLS-1$
394

395             IContentAssistProcessor javaProcessor= new JavaCompletionProcessor(getEditor(), assistant, IDocument.DEFAULT_CONTENT_TYPE);
396             assistant.setContentAssistProcessor(javaProcessor, IDocument.DEFAULT_CONTENT_TYPE);
397
398             ContentAssistProcessor singleLineProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
399             assistant.setContentAssistProcessor(singleLineProcessor, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
400
401             ContentAssistProcessor stringProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_STRING);
402             assistant.setContentAssistProcessor(stringProcessor, IJavaPartitions.JAVA_STRING);
403             
404             ContentAssistProcessor multiLineProcessor= new JavaCompletionProcessor(getEditor(), assistant, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
405             assistant.setContentAssistProcessor(multiLineProcessor, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
406
407             ContentAssistProcessor javadocProcessor= new JavadocCompletionProcessor(getEditor(), assistant);
408             assistant.setContentAssistProcessor(javadocProcessor, IJavaPartitions.JAVA_DOC);
409
410             ContentAssistPreference.configure(assistant, fPreferenceStore);
411
412             assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
413             assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
414             
415             return assistant;
416         }
417
418         return null;
419     }
420
421     /*
422      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getQuickAssistAssistant(org.eclipse.jface.text.source.ISourceViewer)
423      * @since 3.2
424      */

425     public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
426         if (getEditor() != null)
427             return new JavaCorrectionAssistant(getEditor());
428         return null;
429     }
430
431     /*
432      * @see SourceViewerConfiguration#getReconciler(ISourceViewer)
433      */

434     public IReconciler getReconciler(ISourceViewer sourceViewer) {
435
436         final ITextEditor editor= getEditor();
437         if (editor != null && editor.isEditable()) {
438
439             JavaCompositeReconcilingStrategy strategy= new JavaCompositeReconcilingStrategy(sourceViewer, editor, getConfiguredDocumentPartitioning(sourceViewer));
440             JavaReconciler reconciler= new JavaReconciler(editor, strategy, false);
441             reconciler.setIsIncrementalReconciler(false);
442             reconciler.setIsAllowedToModifyDocument(false);
443             reconciler.setProgressMonitor(new NullProgressMonitor());
444             reconciler.setDelay(500);
445
446             return reconciler;
447         }
448         return null;
449     }
450
451     /*
452      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
453      */

454     public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String JavaDoc contentType) {
455         String JavaDoc partitioning= getConfiguredDocumentPartitioning(sourceViewer);
456         if (IJavaPartitions.JAVA_DOC.equals(contentType) || IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(contentType))
457             return new IAutoEditStrategy[] { new JavaDocAutoIndentStrategy(partitioning) };
458         else if (IJavaPartitions.JAVA_STRING.equals(contentType))
459             return new IAutoEditStrategy[] { new SmartSemicolonAutoEditStrategy(partitioning), new JavaStringAutoIndentStrategy(partitioning) };
460         else if (IJavaPartitions.JAVA_CHARACTER.equals(contentType) || IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
461             return new IAutoEditStrategy[] { new SmartSemicolonAutoEditStrategy(partitioning), new JavaAutoIndentStrategy(partitioning, getProject()) };
462         else
463             return new IAutoEditStrategy[] { new JavaAutoIndentStrategy(partitioning, getProject()) };
464     }
465
466     /*
467      * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
468      */

469     public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String JavaDoc contentType) {
470         if (IJavaPartitions.JAVA_DOC.equals(contentType))
471             return new JavadocDoubleClickStrategy();
472         if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(contentType) ||
473                 IJavaPartitions.JAVA_SINGLE_LINE_COMMENT.equals(contentType))
474             return new DefaultTextDoubleClickStrategy();
475         else if (IJavaPartitions.JAVA_STRING.equals(contentType) ||
476                 IJavaPartitions.JAVA_CHARACTER.equals(contentType))
477             return new JavaStringDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer));
478         if (fJavaDoubleClickSelector == null) {
479             fJavaDoubleClickSelector= new JavaDoubleClickSelector();
480             fJavaDoubleClickSelector.setSourceVersion(fPreferenceStore.getString(JavaCore.COMPILER_SOURCE));
481         }
482         return fJavaDoubleClickSelector;
483     }
484
485     /*
486      * @see SourceViewerConfiguration#getDefaultPrefixes(ISourceViewer, String)
487      * @since 2.0
488      */

489     public String JavaDoc[] getDefaultPrefixes(ISourceViewer sourceViewer, String JavaDoc contentType) {
490         return new String JavaDoc[] { "//", "" }; //$NON-NLS-1$ //$NON-NLS-2$
491
}
492
493     /*
494      * @see SourceViewerConfiguration#getIndentPrefixes(ISourceViewer, String)
495      */

496     public String JavaDoc[] getIndentPrefixes(ISourceViewer sourceViewer, String JavaDoc contentType) {
497         IJavaProject project= getProject();
498         final int tabWidth= CodeFormatterUtil.getTabWidth(project);
499         final int indentWidth= CodeFormatterUtil.getIndentWidth(project);
500         boolean allowTabs= tabWidth <= indentWidth;
501         
502         String JavaDoc indentMode;
503         if (project == null)
504             indentMode= JavaCore.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
505         else
506             indentMode= project.getOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, true);
507
508         boolean useSpaces= JavaCore.SPACE.equals(indentMode) || DefaultCodeFormatterConstants.MIXED.equals(indentMode);
509         
510         Assert.isLegal(allowTabs || useSpaces);
511         
512         if (!allowTabs) {
513             char[] spaces= new char[indentWidth];
514             Arrays.fill(spaces, ' ');
515             return new String JavaDoc[] { new String JavaDoc(spaces), "" }; //$NON-NLS-1$
516
} else if (!useSpaces)
517             return getIndentPrefixesForTab(tabWidth);
518         else
519             return getIndentPrefixesForSpaces(tabWidth);
520     }
521
522     /**
523      * Computes and returns the indent prefixes for space indentation
524      * and the given <code>tabWidth</code>.
525      *
526      * @param tabWidth the display tab width
527      * @return the indent prefixes
528      * @see #getIndentPrefixes(ISourceViewer, String)
529      * @since 3.3
530      */

531     private String JavaDoc[] getIndentPrefixesForSpaces(int tabWidth) {
532         String JavaDoc[] indentPrefixes= new String JavaDoc[tabWidth + 2];
533         indentPrefixes[0]= getStringWithSpaces(tabWidth);
534         
535         for (int i= 0; i < tabWidth; i++) {
536             String JavaDoc spaces= getStringWithSpaces(i);
537             if (i < tabWidth)
538                 indentPrefixes[i+1]= spaces + '\t';
539             else
540                 indentPrefixes[i+1]= new String JavaDoc(spaces);
541         }
542         
543         indentPrefixes[tabWidth + 1]= ""; //$NON-NLS-1$
544

545         return indentPrefixes;
546     }
547
548     /**
549      * Creates and returns a String with <code>count</code> spaces.
550      *
551      * @param count the space count
552      * @return the string with the spaces
553      * @since 3.3
554      */

555     private String JavaDoc getStringWithSpaces(int count) {
556         char[] spaceChars= new char[count];
557         Arrays.fill(spaceChars, ' ');
558         return new String JavaDoc(spaceChars);
559     }
560
561     private IJavaProject getProject() {
562         ITextEditor editor= getEditor();
563         if (editor == null)
564             return null;
565
566         IJavaElement element= null;
567         IEditorInput input= editor.getEditorInput();
568         IDocumentProvider provider= editor.getDocumentProvider();
569         if (provider instanceof ICompilationUnitDocumentProvider) {
570             ICompilationUnitDocumentProvider cudp= (ICompilationUnitDocumentProvider) provider;
571             element= cudp.getWorkingCopy(input);
572         } else if (input instanceof IClassFileEditorInput) {
573             IClassFileEditorInput cfei= (IClassFileEditorInput) input;
574             element= cfei.getClassFile();
575         }
576
577         if (element == null)
578             return null;
579
580         return element.getJavaProject();
581     }
582
583     /*
584      * @see SourceViewerConfiguration#getTabWidth(ISourceViewer)
585      */

586     public int getTabWidth(ISourceViewer sourceViewer) {
587         return CodeFormatterUtil.getTabWidth(getProject());
588     }
589
590     /*
591      * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
592      */

593     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
594         return new HTMLAnnotationHover() {
595             protected boolean isIncluded(Annotation annotation) {
596                 return isShowInVerticalRuler(annotation);
597             }
598         };
599     }
600
601     /*
602      * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
603      * @since 3.0
604      */

605     public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
606         return new HTMLAnnotationHover() {
607             protected boolean isIncluded(Annotation annotation) {
608                 return isShowInOverviewRuler(annotation);
609             }
610         };
611     }
612
613     /*
614      * @see SourceViewerConfiguration#getConfiguredTextHoverStateMasks(ISourceViewer, String)
615      * @since 2.1
616      */

617     public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String JavaDoc contentType) {
618         JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
619         int stateMasks[]= new int[hoverDescs.length];
620         int stateMasksLength= 0;
621         for (int i= 0; i < hoverDescs.length; i++) {
622             if (hoverDescs[i].isEnabled()) {
623                 int j= 0;
624                 int stateMask= hoverDescs[i].getStateMask();
625                 while (j < stateMasksLength) {
626                     if (stateMasks[j] == stateMask)
627                         break;
628                     j++;
629                 }
630                 if (j == stateMasksLength)
631                     stateMasks[stateMasksLength++]= stateMask;
632             }
633         }
634         if (stateMasksLength == hoverDescs.length)
635             return stateMasks;
636
637         int[] shortenedStateMasks= new int[stateMasksLength];
638         System.arraycopy(stateMasks, 0, shortenedStateMasks, 0, stateMasksLength);
639         return shortenedStateMasks;
640     }
641
642     /*
643      * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String, int)
644      * @since 2.1
645      */

646     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType, int stateMask) {
647         JavaEditorTextHoverDescriptor[] hoverDescs= JavaPlugin.getDefault().getJavaEditorTextHoverDescriptors();
648         int i= 0;
649         while (i < hoverDescs.length) {
650             if (hoverDescs[i].isEnabled() && hoverDescs[i].getStateMask() == stateMask)
651                 return new JavaEditorTextHoverProxy(hoverDescs[i], getEditor());
652             i++;
653         }
654
655         return null;
656     }
657
658     /*
659      * @see SourceViewerConfiguration#getTextHover(ISourceViewer, String)
660      */

661     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType) {
662         return getTextHover(sourceViewer, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK);
663     }
664
665     /*
666      * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
667      */

668     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
669         return new String JavaDoc[] {
670             IDocument.DEFAULT_CONTENT_TYPE,
671             IJavaPartitions.JAVA_DOC,
672             IJavaPartitions.JAVA_MULTI_LINE_COMMENT,
673             IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
674             IJavaPartitions.JAVA_STRING,
675             IJavaPartitions.JAVA_CHARACTER
676         };
677     }
678
679     /*
680      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
681      * @since 3.0
682      */

683     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
684         if (fDocumentPartitioning != null)
685             return fDocumentPartitioning;
686         return super.getConfiguredDocumentPartitioning(sourceViewer);
687     }
688
689     /*
690      * @see SourceViewerConfiguration#getContentFormatter(ISourceViewer)
691      */

692     public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
693         final MultiPassContentFormatter formatter= new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE);
694
695         formatter.setMasterStrategy(new JavaFormattingStrategy());
696         formatter.setSlaveStrategy(new CommentFormattingStrategy(), IJavaPartitions.JAVA_DOC);
697         formatter.setSlaveStrategy(new CommentFormattingStrategy(), IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
698         formatter.setSlaveStrategy(new CommentFormattingStrategy(), IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
699
700         return formatter;
701     }
702
703     /*
704      * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
705      * @since 2.0
706      */

707     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
708         return new IInformationControlCreator() {
709             public IInformationControl createInformationControl(Shell parent) {
710                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
711             }
712         };
713     }
714
715     /**
716      * Returns the information presenter control creator. The creator is a factory creating the
717      * presenter controls for the given source viewer. This implementation always returns a creator
718      * for <code>DefaultInformationControl</code> instances.
719      *
720      * @param sourceViewer the source viewer to be configured by this configuration
721      * @return an information control creator
722      * @since 2.1
723      */

724     private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
725         return new IInformationControlCreator() {
726             public IInformationControl createInformationControl(Shell parent) {
727                 int shellStyle= SWT.RESIZE | SWT.TOOL;
728                 int style= SWT.V_SCROLL | SWT.H_SCROLL;
729                 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
730             }
731         };
732     }
733
734     /**
735      * Returns the outline presenter control creator. The creator is a factory creating outline
736      * presenter controls for the given source viewer. This implementation always returns a creator
737      * for <code>JavaOutlineInformationControl</code> instances.
738      *
739      * @param sourceViewer the source viewer to be configured by this configuration
740      * @param commandId the ID of the command that opens this control
741      * @return an information control creator
742      * @since 2.1
743      */

744     private IInformationControlCreator getOutlinePresenterControlCreator(ISourceViewer sourceViewer, final String JavaDoc commandId) {
745         return new IInformationControlCreator() {
746             public IInformationControl createInformationControl(Shell parent) {
747                 int shellStyle= SWT.RESIZE;
748                 int treeStyle= SWT.V_SCROLL | SWT.H_SCROLL;
749                 return new JavaOutlineInformationControl(parent, shellStyle, treeStyle, commandId);
750             }
751         };
752     }
753
754     private IInformationControlCreator getHierarchyPresenterControlCreator(ISourceViewer sourceViewer) {
755         return new IInformationControlCreator() {
756             public IInformationControl createInformationControl(Shell parent) {
757                 int shellStyle= SWT.RESIZE;
758                 int treeStyle= SWT.V_SCROLL | SWT.H_SCROLL;
759                 return new HierarchyInformationControl(parent, shellStyle, treeStyle);
760             }
761         };
762     }
763
764     /*
765      * @see SourceViewerConfiguration#getInformationPresenter(ISourceViewer)
766      * @since 2.0
767      */

768     public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
769         InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));
770         presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
771         
772         // Register information provider
773
IInformationProvider provider= new JavaInformationProvider(getEditor());
774         String JavaDoc[] contentTypes= getConfiguredContentTypes(sourceViewer);
775         for (int i= 0; i < contentTypes.length; i++)
776             presenter.setInformationProvider(provider, contentTypes[i]);
777         
778         presenter.setSizeConstraints(60, 10, true, true);
779         return presenter;
780     }
781
782     /**
783      * Returns the outline presenter which will determine and shown
784      * information requested for the current cursor position.
785      *
786      * @param sourceViewer the source viewer to be configured by this configuration
787      * @param doCodeResolve a boolean which specifies whether code resolve should be used to compute the Java element
788      * @return an information presenter
789      * @since 2.1
790      */

791     public IInformationPresenter getOutlinePresenter(ISourceViewer sourceViewer, boolean doCodeResolve) {
792         InformationPresenter presenter;
793         if (doCodeResolve)
794             presenter= new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer, IJavaEditorActionDefinitionIds.OPEN_STRUCTURE));
795         else
796             presenter= new InformationPresenter(getOutlinePresenterControlCreator(sourceViewer, IJavaEditorActionDefinitionIds.SHOW_OUTLINE));
797         presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
798         presenter.setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
799         IInformationProvider provider= new JavaElementProvider(getEditor(), doCodeResolve);
800         presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
801         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
802         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
803         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
804         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_STRING);
805         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_CHARACTER);
806         presenter.setSizeConstraints(50, 20, true, false);
807         return presenter;
808     }
809
810     /**
811      * Returns the settings for the given section.
812      *
813      * @param sectionName the section name
814      * @return the settings
815      * @since 3.0
816      */

817     private IDialogSettings getSettings(String JavaDoc sectionName) {
818         IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings().getSection(sectionName);
819         if (settings == null)
820             settings= JavaPlugin.getDefault().getDialogSettings().addNewSection(sectionName);
821
822         return settings;
823     }
824
825     /**
826      * Returns the hierarchy presenter which will determine and shown type hierarchy
827      * information requested for the current cursor position.
828      *
829      * @param sourceViewer the source viewer to be configured by this configuration
830      * @param doCodeResolve a boolean which specifies whether code resolve should be used to compute the Java element
831      * @return an information presenter
832      * @since 3.0
833      */

834     public IInformationPresenter getHierarchyPresenter(ISourceViewer sourceViewer, boolean doCodeResolve) {
835         
836         // Do not create hierarchy presenter if there's no CU.
837
if (getEditor() != null && getEditor().getEditorInput() != null && JavaUI.getEditorInputJavaElement(getEditor().getEditorInput()) == null)
838             return null;
839         
840         InformationPresenter presenter= new InformationPresenter(getHierarchyPresenterControlCreator(sourceViewer));
841         presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
842         presenter.setAnchor(AbstractInformationControlManager.ANCHOR_GLOBAL);
843         IInformationProvider provider= new JavaElementProvider(getEditor(), doCodeResolve);
844         presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
845         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_DOC);
846         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
847         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
848         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_STRING);
849         presenter.setInformationProvider(provider, IJavaPartitions.JAVA_CHARACTER);
850         presenter.setSizeConstraints(50, 20, true, false);
851         return presenter;
852     }
853
854     /**
855      * Determines whether the preference change encoded by the given event
856      * changes the behavior of one of its contained components.
857      *
858      * @param event the event to be investigated
859      * @return <code>true</code> if event causes a behavioral change
860      * @since 3.0
861      */

862     public boolean affectsTextPresentation(PropertyChangeEvent event) {
863         return fCodeScanner.affectsBehavior(event)
864             || fMultilineCommentScanner.affectsBehavior(event)
865             || fSinglelineCommentScanner.affectsBehavior(event)
866             || fStringScanner.affectsBehavior(event)
867             || fJavaDocScanner.affectsBehavior(event);
868     }
869
870     /**
871      * Adapts the behavior of the contained components to the change
872      * encoded in the given event.
873      * <p>
874      * Clients are not allowed to call this method if the old setup with
875      * text tools is in use.
876      * </p>
877      *
878      * @param event the event to which to adapt
879      * @see JavaSourceViewerConfiguration#JavaSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
880      * @since 3.0
881      */

882     public void handlePropertyChangeEvent(PropertyChangeEvent event) {
883         Assert.isTrue(isNewSetup());
884         if (fCodeScanner.affectsBehavior(event))
885             fCodeScanner.adaptToPreferenceChange(event);
886         if (fMultilineCommentScanner.affectsBehavior(event))
887             fMultilineCommentScanner.adaptToPreferenceChange(event);
888         if (fSinglelineCommentScanner.affectsBehavior(event))
889             fSinglelineCommentScanner.adaptToPreferenceChange(event);
890         if (fStringScanner.affectsBehavior(event))
891             fStringScanner.adaptToPreferenceChange(event);
892         if (fJavaDocScanner.affectsBehavior(event))
893             fJavaDocScanner.adaptToPreferenceChange(event);
894         if (fJavaDoubleClickSelector != null && JavaCore.COMPILER_SOURCE.equals(event.getProperty()))
895             if (event.getNewValue() instanceof String JavaDoc)
896                 fJavaDoubleClickSelector.setSourceVersion((String JavaDoc) event.getNewValue());
897     }
898
899     /*
900      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
901      * @since 3.3
902      */

903     protected Map JavaDoc getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
904         Map JavaDoc targets= super.getHyperlinkDetectorTargets(sourceViewer);
905         targets.put("org.eclipse.jdt.ui.javaCode", fTextEditor); //$NON-NLS-1$
906
return targets;
907     }
908
909 }
910
Popular Tags