/machinelearning/5.0/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/completion/AbstractCompletionProcessor.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 113 lines · 63 code · 15 blank · 35 comment · 7 complexity · e68f5592d75f51b3d86663d853c37b0c MD5 · raw file

  1. package org.drools.eclipse.editors.completion;
  2. import java.util.Collections;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.eclipse.jface.text.BadLocationException;
  6. import org.eclipse.jface.text.IDocument;
  7. import org.eclipse.jface.text.ITextViewer;
  8. import org.eclipse.jface.text.contentassist.ICompletionProposal;
  9. import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
  10. import org.eclipse.jface.text.contentassist.IContextInformation;
  11. import org.eclipse.jface.text.contentassist.IContextInformationValidator;
  12. import org.eclipse.ui.IEditorPart;
  13. /**
  14. *
  15. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  16. */
  17. public abstract class AbstractCompletionProcessor implements IContentAssistProcessor {
  18. private IEditorPart editor;
  19. public AbstractCompletionProcessor(IEditorPart editor) {
  20. this.editor = editor;
  21. }
  22. protected IEditorPart getEditor() {
  23. return editor;
  24. }
  25. public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
  26. List proposals = getCompletionProposals(viewer, documentOffset);
  27. if (proposals == null) {
  28. return new ICompletionProposal[0];
  29. }
  30. Collections.sort(proposals, new RuleCompletionProposal.RuleCompletionProposalComparator());
  31. return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
  32. }
  33. /**
  34. * Returns a list of RuleCompletionProposals.
  35. *
  36. * @param viewer
  37. * @param documentOffset
  38. * @return
  39. */
  40. protected abstract List getCompletionProposals(ITextViewer viewer, int documentOffset);
  41. /**
  42. * Filter out the proposals whose content does not start with the given prefix.
  43. */
  44. protected static void filterProposalsOnPrefix(String prefix, List props) {
  45. if ( prefix != null && prefix.trim().length() > 0 ) {
  46. Iterator iterator = props.iterator();
  47. String prefixLc = prefix.toLowerCase();
  48. while ( iterator.hasNext() ) {
  49. ICompletionProposal item = (ICompletionProposal) iterator.next();
  50. String content = item.getDisplayString().toLowerCase();
  51. if ( !content.toLowerCase().startsWith( prefixLc ) ) {
  52. iterator.remove();
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * Read some text from behind the cursor position.
  59. * This provides context to both filter what is shown based
  60. * on what the user has typed in, and also to provide more information for the
  61. * list of suggestions based on context.
  62. */
  63. protected String readBackwards(int documentOffset, IDocument doc) throws BadLocationException {
  64. int startPart = doc.getPartition(documentOffset).getOffset();
  65. String prefix = doc.get(startPart, documentOffset - startPart);
  66. return prefix;
  67. }
  68. /*
  69. * @see IContentAssistProcessor
  70. */
  71. public char[] getCompletionProposalAutoActivationCharacters() {
  72. return null;
  73. }
  74. /*
  75. * @see IContentAssistProcessor
  76. */
  77. public char[] getContextInformationAutoActivationCharacters() {
  78. return null;
  79. }
  80. /*
  81. * @see IContentAssistProcessor
  82. */
  83. public IContextInformationValidator getContextInformationValidator() {
  84. return null;
  85. }
  86. /*
  87. * @see IContentAssistProcessor
  88. */
  89. public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
  90. return null;
  91. }
  92. /*
  93. * @see IContentAssistProcessor
  94. */
  95. public String getErrorMessage() {
  96. return null;
  97. }
  98. }