001: package org.drools.eclipse.flow.ruleflow.view.property.constraint;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.drools.eclipse.editors.DRLRuleEditor;
024: import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
025: import org.drools.ruleflow.core.RuleFlowProcess;
026: import org.eclipse.core.resources.IFile;
027: import org.eclipse.core.runtime.CoreException;
028: import org.eclipse.jdt.core.IJavaProject;
029: import org.eclipse.jdt.core.JavaCore;
030: import org.eclipse.jface.text.BadLocationException;
031: import org.eclipse.jface.text.IDocument;
032: import org.eclipse.ui.IEditorPart;
033: import org.eclipse.ui.IFileEditorInput;
034: import org.eclipse.ui.IWorkbench;
035: import org.eclipse.ui.IWorkbenchPage;
036: import org.eclipse.ui.IWorkbenchWindow;
037: import org.eclipse.ui.PlatformUI;
038:
039: /**
040: * Completion for ruleflow constraints.
041: *
042: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
043: */
044: public class ConstraintCompletionProcessor extends
045: RuleCompletionProcessor {
046:
047: private RuleFlowProcess process;
048: private List imports;
049:
050: public ConstraintCompletionProcessor(RuleFlowProcess process) {
051: super (null);
052: this .process = process;
053: }
054:
055: public IEditorPart getEditor() {
056: IWorkbench workbench = PlatformUI.getWorkbench();
057: if (workbench != null) {
058: IWorkbenchWindow workbenchWindow = workbench
059: .getActiveWorkbenchWindow();
060: if (workbenchWindow != null) {
061: IWorkbenchPage workbenchPage = workbenchWindow
062: .getActivePage();
063: if (workbenchPage != null) {
064: return workbenchPage.getActiveEditor();
065: }
066: }
067: }
068: return null;
069: }
070:
071: protected String readBackwards(int documentOffset, IDocument doc)
072: throws BadLocationException {
073: int startPart = doc.getPartition(documentOffset).getOffset();
074: String prefix = doc.get(startPart, documentOffset - startPart);
075: return "rule dummy \n when \n" + prefix;
076: }
077:
078: public List getImports() {
079: if (imports == null) {
080: loadImports();
081: }
082: return imports;
083: }
084:
085: private void loadImports() {
086: this .imports = new ArrayList();
087: List imports = process.getImports();
088: if (imports != null) {
089: Iterator iterator = imports.iterator();
090: while (iterator.hasNext()) {
091: String importName = (String) iterator.next();
092: if (importName.endsWith(".*")) {
093: IJavaProject javaProject = getJavaProject();
094: if (javaProject != null) {
095: String packageName = importName.substring(0,
096: importName.length() - 2);
097: this .imports.addAll(DRLRuleEditor
098: .getAllClassesInPackage(packageName,
099: javaProject));
100: }
101: } else {
102: this .imports.add(importName);
103: }
104: }
105: }
106: }
107:
108: private IJavaProject getJavaProject() {
109: IEditorPart editor = getEditor();
110: if (editor != null
111: && editor.getEditorInput() instanceof IFileEditorInput) {
112: IFile file = ((IFileEditorInput) editor.getEditorInput())
113: .getFile();
114: try {
115: if (file.getProject().getNature(
116: "org.eclipse.jdt.core.javanature") != null) {
117: return JavaCore.create(file.getProject());
118: }
119: } catch (CoreException e) {
120: // do nothing
121: }
122: }
123: return null;
124: }
125:
126: public void reset() {
127: this.imports = null;
128: }
129: }
|