/machinelearning/4.0.x/drools-eclipse3.3/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/DRLSourceViewerConfig.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 125 lines · 85 code · 18 blank · 22 comment · 4 complexity · 81de8947c0b7422a025fcb9023594fc3 MD5 · raw file

  1. package org.drools.eclipse.editors;
  2. import org.drools.eclipse.editors.completion.DefaultCompletionProcessor;
  3. import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
  4. import org.drools.eclipse.editors.scanners.DRLPartionScanner;
  5. import org.drools.eclipse.editors.scanners.DRLScanner;
  6. import org.eclipse.core.runtime.NullProgressMonitor;
  7. import org.eclipse.jface.text.IDocument;
  8. import org.eclipse.jface.text.TextAttribute;
  9. import org.eclipse.jface.text.contentassist.ContentAssistant;
  10. import org.eclipse.jface.text.contentassist.IContentAssistant;
  11. import org.eclipse.jface.text.presentation.IPresentationReconciler;
  12. import org.eclipse.jface.text.presentation.PresentationReconciler;
  13. import org.eclipse.jface.text.reconciler.IReconciler;
  14. import org.eclipse.jface.text.reconciler.MonoReconciler;
  15. import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
  16. import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
  17. import org.eclipse.jface.text.rules.Token;
  18. import org.eclipse.jface.text.source.DefaultAnnotationHover;
  19. import org.eclipse.jface.text.source.IAnnotationHover;
  20. import org.eclipse.jface.text.source.ISourceViewer;
  21. import org.eclipse.jface.text.source.SourceViewerConfiguration;
  22. /**
  23. * Source viewer config wires up the syntax highlighting, partitioning
  24. * and content assistance.
  25. *
  26. * @author Michael Neale
  27. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  28. */
  29. public class DRLSourceViewerConfig extends SourceViewerConfiguration {
  30. private DRLScanner scanner;
  31. private AbstractRuleEditor editor;
  32. public DRLSourceViewerConfig(AbstractRuleEditor editor) {
  33. this.editor = editor;
  34. }
  35. protected AbstractRuleEditor getEditor() {
  36. return editor;
  37. }
  38. protected DRLScanner getScanner() {
  39. if (scanner == null) {
  40. scanner = new DRLScanner();
  41. }
  42. return scanner;
  43. }
  44. /**
  45. * Define reconciler - this has to be done for each partition.
  46. * Currently there are 3 partitions, Inside rule, outside rule and inside comment.
  47. */
  48. public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
  49. PresentationReconciler reconciler = new PresentationReconciler();
  50. //bucket partition... (everything else outside a rule)
  51. DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getScanner());
  52. reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
  53. reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
  54. //inside a rule partition
  55. dr = new DefaultDamagerRepairer(getScanner());
  56. reconciler.setDamager(dr, DRLPartionScanner.RULE_PART_CONTENT);
  57. reconciler.setRepairer(dr, DRLPartionScanner.RULE_PART_CONTENT);
  58. //finally, inside a multi line comment.
  59. dr = new DefaultDamagerRepairer(new SingleTokenScanner(
  60. new TextAttribute(ColorManager.getInstance().getColor(
  61. ColorManager.SINGLE_LINE_COMMENT))));
  62. reconciler.setDamager(dr, DRLPartionScanner.RULE_COMMENT);
  63. reconciler.setRepairer(dr, DRLPartionScanner.RULE_COMMENT);
  64. return reconciler;
  65. }
  66. /**
  67. * Single token scanner, used for scanning for multiline comments mainly.
  68. */
  69. static class SingleTokenScanner extends BufferedRuleBasedScanner {
  70. public SingleTokenScanner(TextAttribute attribute) {
  71. setDefaultReturnToken(new Token(attribute));
  72. }
  73. }
  74. /**
  75. * Get the appropriate content assistance, for each partition.
  76. */
  77. public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
  78. ContentAssistant assistant = new ContentAssistant();
  79. //setup the content assistance, which is
  80. //sensitive to the partition that it is in.
  81. assistant.setContentAssistProcessor(
  82. new DefaultCompletionProcessor(editor), IDocument.DEFAULT_CONTENT_TYPE);
  83. assistant.setContentAssistProcessor(
  84. new RuleCompletionProcessor(editor), DRLPartionScanner.RULE_PART_CONTENT);
  85. assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
  86. return assistant;
  87. }
  88. public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
  89. return DRLPartionScanner.LEGAL_CONTENT_TYPES;
  90. }
  91. public IReconciler getReconciler(ISourceViewer sourceViewer) {
  92. MonoReconciler reconciler = null;
  93. if (sourceViewer != null) {
  94. reconciler = new MonoReconciler(
  95. new DRLReconcilingStrategy(sourceViewer, editor), false);
  96. reconciler.setDelay(500);
  97. reconciler.setProgressMonitor(new NullProgressMonitor());
  98. }
  99. return reconciler;
  100. }
  101. public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
  102. return new DefaultAnnotationHover();
  103. }
  104. public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
  105. return new DefaultAnnotationHover();
  106. }
  107. }