1 11 package org.eclipse.jdt.ui.text; 12 13 import java.util.Arrays ; 14 import java.util.Map ; 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 116 public class JavaSourceViewerConfiguration extends TextSourceViewerConfiguration { 117 118 124 public final static String PREFERENCE_TAB_WIDTH= PreferenceConstants.EDITOR_TAB_WIDTH; 125 126 132 public final static String SPACES_FOR_TABS= PreferenceConstants.EDITOR_SPACES_FOR_TABS; 133 134 135 private JavaTextTools fJavaTextTools; 136 private ITextEditor fTextEditor; 137 141 private String fDocumentPartitioning; 142 146 private AbstractJavaScanner fCodeScanner; 147 151 private AbstractJavaScanner fMultilineCommentScanner; 152 156 private AbstractJavaScanner fSinglelineCommentScanner; 157 161 private AbstractJavaScanner fStringScanner; 162 166 private AbstractJavaScanner fJavaDocScanner; 167 171 private IColorManager fColorManager; 172 176 private JavaDoubleClickSelector fJavaDoubleClickSelector; 177 178 179 195 public JavaSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String partitioning) { 196 super(preferenceStore); 197 fColorManager= colorManager; 198 fTextEditor= editor; 199 fDocumentPartitioning= partitioning; 200 initializeScanners(); 201 } 202 203 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 229 protected RuleBasedScanner getCodeScanner() { 230 return fCodeScanner; 231 } 232 233 239 protected RuleBasedScanner getMultilineCommentScanner() { 240 return fMultilineCommentScanner; 241 } 242 243 249 protected RuleBasedScanner getSinglelineCommentScanner() { 250 return fSinglelineCommentScanner; 251 } 252 253 259 protected RuleBasedScanner getStringScanner() { 260 return fStringScanner; 261 } 262 263 268 protected RuleBasedScanner getJavaDocScanner() { 269 return fJavaDocScanner; 270 } 271 272 277 protected IColorManager getColorManager() { 278 return fColorManager; 279 } 280 281 286 protected ITextEditor getEditor() { 287 return fTextEditor; 288 } 289 290 302 protected IPreferenceStore getPreferenceStore() { 303 Assert.isTrue(!isNewSetup()); 304 return fJavaTextTools.getPreferenceStore(); 305 } 306 307 312 private boolean isNewSetup() { 313 return fJavaTextTools == null; 314 } 315 316 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 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 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 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")); 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 425 public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) { 426 if (getEditor() != null) 427 return new JavaCorrectionAssistant(getEditor()); 428 return null; 429 } 430 431 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 454 public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) { 455 String 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 469 public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String 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 489 public String [] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) { 490 return new String [] { "//", "" }; } 492 493 496 public String [] getIndentPrefixes(ISourceViewer sourceViewer, String 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 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 [] { new String (spaces), "" }; } else if (!useSpaces) 517 return getIndentPrefixesForTab(tabWidth); 518 else 519 return getIndentPrefixesForSpaces(tabWidth); 520 } 521 522 531 private String [] getIndentPrefixesForSpaces(int tabWidth) { 532 String [] indentPrefixes= new String [tabWidth + 2]; 533 indentPrefixes[0]= getStringWithSpaces(tabWidth); 534 535 for (int i= 0; i < tabWidth; i++) { 536 String spaces= getStringWithSpaces(i); 537 if (i < tabWidth) 538 indentPrefixes[i+1]= spaces + '\t'; 539 else 540 indentPrefixes[i+1]= new String (spaces); 541 } 542 543 indentPrefixes[tabWidth + 1]= ""; 545 return indentPrefixes; 546 } 547 548 555 private String getStringWithSpaces(int count) { 556 char[] spaceChars= new char[count]; 557 Arrays.fill(spaceChars, ' '); 558 return new String (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 586 public int getTabWidth(ISourceViewer sourceViewer) { 587 return CodeFormatterUtil.getTabWidth(getProject()); 588 } 589 590 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 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 617 public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String 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 646 public ITextHover getTextHover(ISourceViewer sourceViewer, String 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 661 public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) { 662 return getTextHover(sourceViewer, contentType, ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK); 663 } 664 665 668 public String [] getConfiguredContentTypes(ISourceViewer sourceViewer) { 669 return new String [] { 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 683 public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) { 684 if (fDocumentPartitioning != null) 685 return fDocumentPartitioning; 686 return super.getConfiguredDocumentPartitioning(sourceViewer); 687 } 688 689 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 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 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 744 private IInformationControlCreator getOutlinePresenterControlCreator(ISourceViewer sourceViewer, final String 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 768 public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) { 769 InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator(sourceViewer)); 770 presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer)); 771 772 IInformationProvider provider= new JavaInformationProvider(getEditor()); 774 String [] 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 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 817 private IDialogSettings getSettings(String 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 834 public IInformationPresenter getHierarchyPresenter(ISourceViewer sourceViewer, boolean doCodeResolve) { 835 836 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 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 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 ) 896 fJavaDoubleClickSelector.setSourceVersion((String ) event.getNewValue()); 897 } 898 899 903 protected Map getHyperlinkDetectorTargets(ISourceViewer sourceViewer) { 904 Map targets= super.getHyperlinkDetectorTargets(sourceViewer); 905 targets.put("org.eclipse.jdt.ui.javaCode", fTextEditor); return targets; 907 } 908 909 } 910 | Popular Tags |