001: package org.drools.eclipse.editors;
002:
003: import org.drools.eclipse.editors.completion.DefaultCompletionProcessor;
004: import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
005: import org.drools.eclipse.editors.scanners.DRLPartionScanner;
006: import org.drools.eclipse.editors.scanners.DRLScanner;
007: import org.eclipse.core.runtime.NullProgressMonitor;
008: import org.eclipse.jface.text.IDocument;
009: import org.eclipse.jface.text.TextAttribute;
010: import org.eclipse.jface.text.contentassist.ContentAssistant;
011: import org.eclipse.jface.text.contentassist.IContentAssistant;
012: import org.eclipse.jface.text.presentation.IPresentationReconciler;
013: import org.eclipse.jface.text.presentation.PresentationReconciler;
014: import org.eclipse.jface.text.reconciler.IReconciler;
015: import org.eclipse.jface.text.reconciler.MonoReconciler;
016: import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
017: import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
018: import org.eclipse.jface.text.rules.Token;
019: import org.eclipse.jface.text.source.DefaultAnnotationHover;
020: import org.eclipse.jface.text.source.IAnnotationHover;
021: import org.eclipse.jface.text.source.ISourceViewer;
022: import org.eclipse.jface.text.source.SourceViewerConfiguration;
023: import org.eclipse.swt.graphics.Color;
024:
025: /**
026: * Source viewer config wires up the syntax highlighting, partitioning
027: * and content assistance.
028: *
029: * @author Michael Neale
030: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
031: */
032: public class DRLSourceViewerConfig extends SourceViewerConfiguration {
033:
034: private DRLScanner scanner;
035:
036: private static Color DEFAULT_COLOR = ColorManager.getInstance()
037: .getColor(ColorManager.DEFAULT);
038:
039: private AbstractRuleEditor editor;
040:
041: public DRLSourceViewerConfig(AbstractRuleEditor editor) {
042: this .editor = editor;
043: }
044:
045: protected AbstractRuleEditor getEditor() {
046: return editor;
047: }
048:
049: protected DRLScanner getScanner() {
050: if (scanner == null) {
051: scanner = new DRLScanner();
052: scanner.setDefaultReturnToken(new Token(new TextAttribute(
053: DEFAULT_COLOR)));
054: }
055: return scanner;
056: }
057:
058: /**
059: * Define reconciler - this has to be done for each partition.
060: * Currently there are 3 partitions, Inside rule, outside rule and inside comment.
061: */
062: public IPresentationReconciler getPresentationReconciler(
063: ISourceViewer sourceViewer) {
064: PresentationReconciler reconciler = new PresentationReconciler();
065:
066: //bucket partition... (everything else outside a rule)
067: DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
068: getScanner());
069: reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
070: reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
071:
072: //inside a rule partition
073: dr = new DefaultDamagerRepairer(getScanner());
074: reconciler.setDamager(dr, DRLPartionScanner.RULE_PART_CONTENT);
075: reconciler.setRepairer(dr, DRLPartionScanner.RULE_PART_CONTENT);
076:
077: //finally, inside a multi line comment.
078: dr = new DefaultDamagerRepairer(new SingleTokenScanner(
079: new TextAttribute(ColorManager.getInstance().getColor(
080: ColorManager.SINGLE_LINE_COMMENT))));
081: reconciler.setDamager(dr, DRLPartionScanner.RULE_COMMENT);
082: reconciler.setRepairer(dr, DRLPartionScanner.RULE_COMMENT);
083:
084: return reconciler;
085: }
086:
087: /**
088: * Single token scanner, used for scanning for multiline comments mainly.
089: */
090: static class SingleTokenScanner extends BufferedRuleBasedScanner {
091: public SingleTokenScanner(TextAttribute attribute) {
092: setDefaultReturnToken(new Token(attribute));
093: }
094: }
095:
096: /**
097: * Get the appropriate content assistance, for each partition.
098: */
099: public IContentAssistant getContentAssistant(
100: ISourceViewer sourceViewer) {
101: ContentAssistant assistant = new ContentAssistant();
102: //setup the content assistance, which is
103: //sensitive to the partition that it is in.
104: assistant.setContentAssistProcessor(
105: new DefaultCompletionProcessor(editor),
106: IDocument.DEFAULT_CONTENT_TYPE);
107: assistant.setContentAssistProcessor(
108: new RuleCompletionProcessor(editor),
109: DRLPartionScanner.RULE_PART_CONTENT);
110: assistant
111: .setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
112: return assistant;
113: }
114:
115: public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
116: return DRLPartionScanner.LEGAL_CONTENT_TYPES;
117: }
118:
119: public IReconciler getReconciler(ISourceViewer sourceViewer) {
120: MonoReconciler reconciler = null;
121: if (sourceViewer != null) {
122: reconciler = new MonoReconciler(new DRLReconcilingStrategy(
123: sourceViewer, editor), false);
124: reconciler.setDelay(500);
125: reconciler.setProgressMonitor(new NullProgressMonitor());
126: }
127: return reconciler;
128: }
129:
130: public IAnnotationHover getOverviewRulerAnnotationHover(
131: ISourceViewer sourceViewer) {
132: return new DefaultAnnotationHover();
133: }
134:
135: public IAnnotationHover getAnnotationHover(
136: ISourceViewer sourceViewer) {
137: return new DefaultAnnotationHover();
138: }
139: }
|