[jboss-svn-commits] JBL Code SVN: r19582 - in labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse: editors/completion and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Apr 15 18:44:12 EDT 2008


Author: KrisVerlaenen
Date: 2008-04-15 18:44:12 -0400 (Tue, 15 Apr 2008)
New Revision: 19582

Added:
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionCompletionProcessor.java
Modified:
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/DroolsEclipsePlugin.java
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/completion/RuleCompletionProcessor.java
   labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionDialog.java
Log:
JBRULES-1561: Code completion for ruleflow action editor
 - Added code completion to action dialog
JBRULES-1402: Inserting facts within "Action Node" (IDE RULEFLOW EDITOR)
 - support for drools.* and update/insert/... methods (for both Java and MVEL

Modified: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/DroolsEclipsePlugin.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/DroolsEclipsePlugin.java	2008-04-15 22:44:05 UTC (rev 19581)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/DroolsEclipsePlugin.java	2008-04-15 22:44:12 UTC (rev 19582)
@@ -312,7 +312,7 @@
 		return null;
 	}
 
-	private DRLInfo generateParsedResource(String content, IResource resource, boolean useCache, boolean compile) throws DroolsParserException {
+	public DRLInfo generateParsedResource(String content, IResource resource, boolean useCache, boolean compile) throws DroolsParserException {
 		useCache = useCache && useCachePreference;
         DrlParser parser = new DrlParser();
         try {

Modified: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/completion/RuleCompletionProcessor.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/completion/RuleCompletionProcessor.java	2008-04-15 22:44:05 UTC (rev 19581)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/editors/completion/RuleCompletionProcessor.java	2008-04-15 22:44:12 UTC (rev 19582)
@@ -35,6 +35,7 @@
 import org.drools.rule.builder.dialect.mvel.MVELDialect;
 import org.drools.spi.KnowledgeHelper;
 import org.drools.util.asm.ClassFieldInspector;
+import org.eclipse.core.resources.IResource;
 import org.eclipse.jdt.core.CompletionProposal;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal;
@@ -44,6 +45,7 @@
 import org.eclipse.jface.text.ITextViewer;
 import org.eclipse.jface.text.contentassist.ICompletionProposal;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.ui.IFileEditorInput;
 import org.mvel.compiler.CompiledExpression;
 import org.mvel.compiler.ExpressionCompiler;
 import org.mvel.ParserContext;
@@ -955,14 +957,16 @@
 
         final Set proposals = new HashSet();
 
-        if ( !(getEditor() instanceof DRLRuleEditor) ) {
+        if (!(getEditor().getEditorInput() instanceof IFileEditorInput)) {
             return proposals;
         }
 
         try {
-            DRLInfo drlInfo = DroolsEclipsePlugin.getDefault().parseResource( (DRLRuleEditor) getEditor(),
-                                                                              true,
-                                                                              true );
+            DRLInfo drlInfo = DroolsEclipsePlugin.getDefault().generateParsedResource(
+                ruleBackText,
+                ((IFileEditorInput) getEditor().getEditorInput()).getFile(),
+                false,
+                true );
 
             String textWithoutPrefix = CompletionUtil.getTextWithoutPrefix( consequenceBackText,
                                                                             prefix );
@@ -1086,7 +1090,7 @@
                             proposals );
         return uniqueProposals;
     }
-
+    
     private Map getResolvedMvelInputs(Map params) {
         ClassTypeResolver resolver = new ClassTypeResolver( getUniqueImports(),
                                                             ProjectClassLoader.getProjectClassLoader( getEditor() ) );
@@ -1211,9 +1215,11 @@
                                                          null,
                                                          qualifiedName );
 
-        for ( Iterator it = dialect.getPackgeImports().values().iterator(); it.hasNext(); ) {
-            String packageImport = (String) it.next();
-            context.addPackageImport( packageImport );
+        if (dialect.getPackgeImports() != null) {
+            for ( Iterator it = dialect.getPackgeImports().values().iterator(); it.hasNext(); ) {
+                String packageImport = (String) it.next();
+                context.addPackageImport( packageImport );
+            }
         }
         context.setStrictTypeEnforcement( false );
 
@@ -1260,7 +1266,7 @@
         //ideally the variable name should be inferred from the last member of the expression
         final String syntheticVarName = "mvdrlofc";
 
-        String javaText = "\n" + CompletionUtil.getSimpleClassName( lastType ) + " " + syntheticVarName + ";\n" + syntheticVarName + ".";
+        String javaText = "\n" + lastType.getPackage().getName() + "." + CompletionUtil.getSimpleClassName( lastType ) + " " + syntheticVarName + ";\n" + syntheticVarName + ".";
         final List list1 = new ArrayList();
         requestJavaCompletionProposals( javaText,
                                         prefix,

Added: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionCompletionProcessor.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionCompletionProcessor.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionCompletionProcessor.java	2008-04-15 22:44:12 UTC (rev 19582)
@@ -0,0 +1,162 @@
+package org.drools.eclipse.flow.ruleflow.view.property.action;
+
+/*
+ * Copyright 2005 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.eclipse.editors.DRLRuleEditor;
+import org.drools.eclipse.editors.completion.RuleCompletionProcessor;
+import org.drools.workflow.core.WorkflowProcess;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * Completion for ruleflow constraints. 
+ * 
+ * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
+ */
+public class ActionCompletionProcessor extends RuleCompletionProcessor {
+
+	private WorkflowProcess process;
+	private List imports;
+	private List globals;
+	private Map attributes;
+	private String dialect;
+	
+	public ActionCompletionProcessor(WorkflowProcess process) {
+		super(null);
+		this.process = process;
+	}
+	
+	public void setDialect(String dialect) {
+	    this.dialect = dialect;
+	    this.attributes = null;
+	}
+	
+	public IEditorPart getEditor() {
+		IWorkbench workbench = PlatformUI.getWorkbench();
+		if (workbench != null) { 
+			IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
+			if (workbenchWindow != null) {
+				IWorkbenchPage workbenchPage = workbenchWindow.getActivePage(); 
+				if (workbenchPage != null) {
+					return workbenchPage.getActiveEditor();
+				}
+			}
+		}
+		return null;
+	}
+
+    protected String readBackwards(int documentOffset, IDocument doc) throws BadLocationException {
+        int startPart = doc.getPartition(documentOffset).getOffset();
+        String prefix = doc.get(startPart, documentOffset - startPart);
+        return "package dummy.package \n rule dummy "
+            + (dialect == null ? "" : " dialect \"" + dialect + "\" ")
+            + "\n when \n then \n " + prefix;
+    }
+    
+    public List getImports() {
+    	if (imports == null) {
+    		loadImports();
+    	}
+    	return imports;
+    }
+    
+    private void loadImports() {
+    	this.imports = new ArrayList();
+    	List imports = process.getImports();
+    	if (imports != null) {
+	    	Iterator iterator = imports.iterator();
+	        while (iterator.hasNext()) {
+	            String importName = (String) iterator.next();
+	            if (importName.endsWith(".*")) {
+	            	IJavaProject javaProject = getJavaProject();
+	            	if (javaProject != null) {
+		                String packageName = importName.substring(0, importName.length() - 2);
+		                this.imports.addAll(DRLRuleEditor.getAllClassesInPackage(packageName, javaProject));
+	            	}
+	            } else {
+	            	this.imports.add(importName);
+	            }
+	        }
+    	}
+    }
+    
+    public List getGlobals() {
+    	if (globals == null) {
+    		loadGlobals();
+    	}
+    	return globals;
+    }
+    
+    private void loadGlobals() {
+    	this.globals = Arrays.asList(process.getGlobalNames());
+    }
+    
+    private void loadAttributes() {
+        if (this.dialect == null) {
+            attributes = Collections.EMPTY_MAP;
+        } else {
+            Map result = new HashMap();
+            result.put("dialect", dialect);
+            attributes = result;
+        }
+    }
+    
+    protected Map getAttributes() {
+        if (attributes == null) {
+            loadAttributes();
+        }
+        return attributes;
+    }
+    
+    private IJavaProject getJavaProject() {
+    	IEditorPart editor = getEditor();
+    	if (editor != null && editor.getEditorInput() instanceof IFileEditorInput) {
+			IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
+	    	try {
+	    		if (file.getProject().getNature("org.eclipse.jdt.core.javanature") != null) {
+	    			return JavaCore.create(file.getProject());
+	    		}
+	    	} catch (CoreException e) {
+	    		// do nothing
+	    	}
+		}
+    	return null;
+    }
+    
+    public void reset() {
+    	this.imports = null;
+    	this.globals = null;
+    }
+}

Modified: labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionDialog.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionDialog.java	2008-04-15 22:44:05 UTC (rev 19581)
+++ labs/jbossrules/trunk/drools-eclipse/drools-eclipse-plugin/src/main/java/org/drools/eclipse/flow/ruleflow/view/property/action/ActionDialog.java	2008-04-15 22:44:12 UTC (rev 19582)
@@ -18,6 +18,7 @@
 import java.util.List;
 import java.util.Map;
 
+import org.drools.eclipse.editors.DRLSourceViewerConfig;
 import org.drools.eclipse.editors.scanners.DRLPartionScanner;
 import org.drools.eclipse.flow.common.view.property.EditBeanDialog;
 import org.drools.eclipse.flow.ruleflow.view.property.constraint.RuleFlowGlobalsDialog;
@@ -30,6 +31,9 @@
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IDocumentPartitioner;
+import org.eclipse.jface.text.contentassist.ContentAssistant;
+import org.eclipse.jface.text.contentassist.IContentAssistant;
+import org.eclipse.jface.text.reconciler.IReconciler;
 import org.eclipse.jface.text.rules.FastPartitioner;
 import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.SourceViewer;
@@ -38,6 +42,7 @@
 import org.eclipse.swt.events.KeyListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
@@ -63,7 +68,7 @@
 	private TabFolder tabFolder;
 	private SourceViewer actionViewer;
 	private Combo dialectCombo;
-	//private ActionCompletionProcessor completionProcessor;
+	private ActionCompletionProcessor completionProcessor;
 
 	public ActionDialog(Shell parentShell, WorkflowProcess process, ActionNode actionNode) {
 		super(parentShell, "Action editor");
@@ -89,19 +94,21 @@
 
 	private Control createTextualEditor(Composite parent) {
 		actionViewer = new SourceViewer(parent, null, SWT.BORDER);
-//		actionViewer.configure(new DRLSourceViewerConfig(null) {
-//			public IReconciler getReconciler(ISourceViewer sourceViewer) {
-//				return null;
-//			}
-//			public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
-//				ContentAssistant assistant = new ContentAssistant();
-//				completionProcessor = new ActionCompletionProcessor(process);
-//				assistant.setContentAssistProcessor(
-//					completionProcessor, IDocument.DEFAULT_CONTENT_TYPE);
-//				assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
-//				return assistant;
-//			}
-//		});
+		actionViewer.configure(new DRLSourceViewerConfig(null) {
+			public IReconciler getReconciler(ISourceViewer sourceViewer) {
+				return null;
+			}
+			public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
+				ContentAssistant assistant = new ContentAssistant();
+				completionProcessor = new ActionCompletionProcessor(process);
+				assistant.setContentAssistProcessor(
+					completionProcessor, IDocument.DEFAULT_CONTENT_TYPE);
+				assistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
+				return assistant;
+			}
+		});
+		completionProcessor.setDialect(
+            dialectCombo.getItem(dialectCombo.getSelectionIndex()));
 		Object action = getValue();
 		String value = null;
 		if (action instanceof DroolsConsequenceAction) {
@@ -143,7 +150,17 @@
             }
         }
         dialectCombo.select(index);
-	    return dialectCombo;
+        dialectCombo.addSelectionListener(new SelectionListener() {
+            public void widgetDefaultSelected(SelectionEvent e) {
+                completionProcessor.setDialect(
+                    dialectCombo.getItem(dialectCombo.getSelectionIndex()));
+            }
+            public void widgetSelected(SelectionEvent e) {
+                completionProcessor.setDialect(
+                    dialectCombo.getItem(dialectCombo.getSelectionIndex()));
+            }
+        });
+        return dialectCombo;
 	}
 	
 	private Object getAction() {
@@ -214,7 +231,7 @@
 				if (code != CANCEL) {
 					List imports = dialog.getImports();
 					process.setImports(imports);
-//					completionProcessor.reset();
+					completionProcessor.reset();
 				}
 			}
 		};
@@ -231,7 +248,7 @@
 				if (code != CANCEL) {
 					Map globals = dialog.getGlobals();
 					process.setGlobals(globals);
-//					completionProcessor.reset();
+					completionProcessor.reset();
 				}
 			}
 		};




More information about the jboss-svn-commits mailing list