/machinelearning/5.0.x/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/constraint/GlobalCompletionProcessor.java

https://github.com/etirelli/droolsjbpm-contributed-experiments · Java · 116 lines · 82 code · 13 blank · 21 comment · 11 complexity · e66d193f5a3fd1d304b70a03fdcd9770 MD5 · raw file

  1. package org.drools.eclipse.flow.ruleflow.view.property.constraint;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.regex.Matcher;
  23. import org.drools.eclipse.DroolsEclipsePlugin;
  24. import org.drools.eclipse.editors.completion.CompletionUtil;
  25. import org.drools.eclipse.editors.completion.DefaultCompletionProcessor;
  26. import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
  27. import org.drools.eclipse.editors.completion.RuleCompletionProposal;
  28. import org.eclipse.core.resources.IFile;
  29. import org.eclipse.core.runtime.CoreException;
  30. import org.eclipse.jdt.core.IJavaProject;
  31. import org.eclipse.jdt.core.JavaCore;
  32. import org.eclipse.jface.text.IDocument;
  33. import org.eclipse.jface.text.ITextViewer;
  34. import org.eclipse.ui.IEditorPart;
  35. import org.eclipse.ui.IFileEditorInput;
  36. import org.eclipse.ui.IWorkbench;
  37. import org.eclipse.ui.IWorkbenchPage;
  38. import org.eclipse.ui.IWorkbenchWindow;
  39. import org.eclipse.ui.PlatformUI;
  40. /**
  41. * Completion for ruleflow constraints.
  42. *
  43. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  44. */
  45. public class GlobalCompletionProcessor extends DefaultCompletionProcessor {
  46. public GlobalCompletionProcessor() {
  47. super(null);
  48. }
  49. public IEditorPart getEditor() {
  50. IWorkbench workbench = PlatformUI.getWorkbench();
  51. if (workbench != null) {
  52. IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
  53. if (workbenchWindow != null) {
  54. IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
  55. if (workbenchPage != null) {
  56. return workbenchPage.getActiveEditor();
  57. }
  58. }
  59. }
  60. return null;
  61. }
  62. protected List getCompletionProposals(ITextViewer viewer,
  63. int documentOffset) {
  64. try {
  65. IDocument doc = viewer.getDocument();
  66. String backText = readBackwards(documentOffset, doc);
  67. String prefix = CompletionUtil.stripLastWord(backText);
  68. return getPossibleProposals(viewer, documentOffset, backText, prefix);
  69. } catch (Throwable t) {
  70. DroolsEclipsePlugin.log(t);
  71. }
  72. return null;
  73. }
  74. public List getImports() {
  75. return Collections.EMPTY_LIST;
  76. }
  77. public List getGlobals() {
  78. return Collections.EMPTY_LIST;
  79. }
  80. protected IJavaProject getCurrentJavaProject() {
  81. IEditorPart editor = getEditor();
  82. if (editor != null && editor.getEditorInput() instanceof IFileEditorInput) {
  83. IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
  84. try {
  85. if (file.getProject().getNature("org.eclipse.jdt.core.javanature") != null) {
  86. return JavaCore.create(file.getProject());
  87. }
  88. } catch (CoreException e) {
  89. // do nothing
  90. }
  91. }
  92. return null;
  93. }
  94. protected List getPossibleProposals(ITextViewer viewer,
  95. int documentOffset,
  96. String backText,
  97. final String prefix) {
  98. List list = new ArrayList();
  99. list.add(new RuleCompletionProposal(documentOffset - prefix.length(), prefix.length(), "global", "global "));
  100. DefaultCompletionProcessor.filterProposalsOnPrefix(prefix, list);
  101. return list;
  102. }
  103. }