001: package org.drools.eclipse.editors.completion;
002:
003: import java.util.Collections;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.eclipse.jface.text.BadLocationException;
008: import org.eclipse.jface.text.IDocument;
009: import org.eclipse.jface.text.ITextViewer;
010: import org.eclipse.jface.text.contentassist.ICompletionProposal;
011: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
012: import org.eclipse.jface.text.contentassist.IContextInformation;
013: import org.eclipse.jface.text.contentassist.IContextInformationValidator;
014: import org.eclipse.ui.IEditorPart;
015:
016: /**
017: *
018: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
019: */
020: public abstract class AbstractCompletionProcessor implements
021: IContentAssistProcessor {
022:
023: private IEditorPart editor;
024:
025: public AbstractCompletionProcessor(IEditorPart editor) {
026: this .editor = editor;
027: }
028:
029: protected IEditorPart getEditor() {
030: return editor;
031: }
032:
033: public ICompletionProposal[] computeCompletionProposals(
034: ITextViewer viewer, int documentOffset) {
035: List proposals = getCompletionProposals(viewer, documentOffset);
036: if (proposals == null) {
037: return new ICompletionProposal[0];
038: }
039: Collections
040: .sort(
041: proposals,
042: new RuleCompletionProposal.RuleCompletionProposalComparator());
043: return (ICompletionProposal[]) proposals
044: .toArray(new ICompletionProposal[proposals.size()]);
045: }
046:
047: /**
048: * Returns a list of RuleCompletionProposals.
049: *
050: * @param viewer
051: * @param documentOffset
052: * @return
053: */
054: protected abstract List getCompletionProposals(ITextViewer viewer,
055: int documentOffset);
056:
057: /**
058: * Filter out the proposals whose content does not start with the given prefix.
059: */
060: protected static void filterProposalsOnPrefix(String prefix,
061: List props) {
062: if (prefix != null && prefix.trim().length() > 0) {
063: Iterator iterator = props.iterator();
064: String prefixLc = prefix.toLowerCase();
065: while (iterator.hasNext()) {
066: ICompletionProposal item = (ICompletionProposal) iterator
067: .next();
068: String content = item.getDisplayString().toLowerCase();
069: if (!content.toLowerCase().startsWith(prefixLc)) {
070: iterator.remove();
071: }
072: }
073: }
074: }
075:
076: /**
077: * Read some text from behind the cursor position.
078: * This provides context to both filter what is shown based
079: * on what the user has typed in, and also to provide more information for the
080: * list of suggestions based on context.
081: */
082: protected String readBackwards(int documentOffset, IDocument doc)
083: throws BadLocationException {
084: int startPart = doc.getPartition(documentOffset).getOffset();
085: String prefix = doc.get(startPart, documentOffset - startPart);
086: return prefix;
087: }
088:
089: /*
090: * @see IContentAssistProcessor
091: */
092: public char[] getCompletionProposalAutoActivationCharacters() {
093: return null;
094: }
095:
096: /*
097: * @see IContentAssistProcessor
098: */
099: public char[] getContextInformationAutoActivationCharacters() {
100: return null;
101: }
102:
103: /*
104: * @see IContentAssistProcessor
105: */
106: public IContextInformationValidator getContextInformationValidator() {
107: return null;
108: }
109:
110: /*
111: * @see IContentAssistProcessor
112: */
113: public IContextInformation[] computeContextInformation(
114: ITextViewer viewer, int documentOffset) {
115: return null;
116: }
117:
118: /*
119: * @see IContentAssistProcessor
120: */
121: public String getErrorMessage() {
122: return null;
123: }
124: }
|