[jboss-svn-commits] JBL Code SVN: r25836 - labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Mar 26 02:20:00 EDT 2009
Author: ahtik
Date: 2009-03-26 02:20:00 -0400 (Thu, 26 Mar 2009)
New Revision: 25836
Added:
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFreeFormLineWidget.java
Modified:
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFactWidget.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewActionDialog.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewConditionDialog.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ConstraintValueEditor.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/DSLSentenceWidget.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/FactPatternWidget.java
labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/RuleModeller.java
Log:
JBRULES-2032: Free form DRL support added to eclipse guided editor
JBRULES-2033: Improved enums support for eclipse guided editor (support for advanced enums)
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFactWidget.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFactWidget.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFactWidget.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -1,15 +1,17 @@
package org.drools.eclipse.rulebuilder.ui;
+import org.drools.eclipse.rulebuilder.modeldriven.HumanReadable;
+import org.drools.guvnor.client.modeldriven.DropDownData;
import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.ActionFieldValue;
import org.drools.guvnor.client.modeldriven.brl.ActionInsertFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionFieldValue;
import org.drools.guvnor.client.modeldriven.brl.ActionInsertLogicalFact;
-import org.drools.eclipse.rulebuilder.modeldriven.HumanReadable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
@@ -139,30 +141,63 @@
private void valueEditor(Composite parent,
final ActionFieldValue val) {
- final Text box = toolkit.createText( parent,
- "" );
+ String fieldName = val.field;
+ DropDownData enums = modeller.getSuggestionCompletionEngine().getEnums( fact.factType,
+ fact.fieldValues,
+ val.field );
+ boolean found = false;
+ if ( enums != null && enums.fixedList.length > 0 ) {
+ String[] list = enums.fixedList;
+ final Combo combo = new Combo( parent,
+ SWT.DROP_DOWN | SWT.READ_ONLY );
+ for ( int i = 0; i < list.length; i++ ) {
+ String e = list[i];
+ combo.add( e );
+ if ( e.equals( val.value ) ) {
+ combo.select( i );
+ found = true;
+ }
+ }
+ if ( !found && val.value != null ) {
+ combo.add( val.value );
+ combo.select( combo.getItemCount() - 1 );
+ }
- if ( val.value != null ) {
- box.setText( val.value );
- }
+ combo.addModifyListener( new ModifyListener() {
- GridData gd = new GridData( GridData.FILL_HORIZONTAL );
- gd.grabExcessHorizontalSpace = true;
- gd.minimumWidth = 100;
- box.setLayoutData( gd );
+ public void modifyText(ModifyEvent e) {
+ val.value = combo.getItem( combo.getSelectionIndex() );
+ modeller.reloadRhs();
+ modeller.setDirty( true );
+ }
+ } );
- box.addModifyListener( new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- getModeller().setDirty( true );
- val.value = box.getText();
+ } else {
+
+ final Text box = toolkit.createText( parent,
+ "" );
+
+ if ( val.value != null ) {
+ box.setText( val.value );
}
- } );
- if (val.type.equals( SuggestionCompletionEngine.TYPE_NUMERIC )) {
- new NumericKeyFilter(box);
- }
-
-
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL );
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ box.setLayoutData( gd );
+
+ box.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ getModeller().setDirty( true );
+ val.value = box.getText();
+ }
+ } );
+
+ if ( val.type.equals( SuggestionCompletionEngine.TYPE_NUMERIC ) ) {
+ new NumericKeyFilter( box );
+ }
+ }
+
}
public SuggestionCompletionEngine getCompletion() {
Added: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFreeFormLineWidget.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFreeFormLineWidget.java (rev 0)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ActionInsertFreeFormLineWidget.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -0,0 +1,93 @@
+package org.drools.eclipse.rulebuilder.ui;
+
+import org.drools.guvnor.client.modeldriven.brl.FreeFormLine;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.graphics.FontMetrics;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.MessageBox;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.forms.events.HyperlinkEvent;
+import org.eclipse.ui.forms.events.IHyperlinkListener;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.eclipse.ui.forms.widgets.ImageHyperlink;
+
+public class ActionInsertFreeFormLineWidget extends Widget {
+
+ private FreeFormLine action;
+
+ public ActionInsertFreeFormLineWidget(FormToolkit toolkit,
+ Composite comp,
+ RuleModeller ruleModeller,
+ final FreeFormLine action,
+ int i) {
+
+ super( comp,
+ toolkit,
+ ruleModeller,
+ i );
+
+ GridLayout l = new GridLayout();
+ l.numColumns = 2;
+ parent.setLayout( l );
+
+ createTextfield( comp,
+ action );
+ addRemoveFieldAction( parent,
+ i );
+ toolkit.paintBordersFor( parent );
+ this.action = action;
+ }
+
+ private void createTextfield(Composite comp,
+ final FreeFormLine action) {
+ final Text text = toolkit.createText( comp,
+ action.text );
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL );
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ text.setLayoutData( gd );
+
+ text.addModifyListener( new ModifyListener() {
+
+ public void modifyText(ModifyEvent e) {
+ getModeller().setDirty( true );
+ action.text = text.getText();
+ }
+ } );
+ }
+
+ private void addRemoveFieldAction(Composite constraintComposite,
+ final int row) {
+ ImageHyperlink delLink = addImage( constraintComposite,
+ "icons/delete_item_small.gif" );
+ delLink.setToolTipText( "Remove this field action" );
+
+ delLink.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
+ SWT.YES | SWT.NO | SWT.ICON_WARNING );
+ dialog.setMessage( "Remove this item?" );
+ dialog.setText( "Remove this item?" );
+ if ( dialog.open() == SWT.YES ) {
+ getModeller().getModel().removeRhsItem( row );
+ getModeller().setDirty( true );
+ getModeller().reloadRhs();
+ }
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewActionDialog.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewActionDialog.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewActionDialog.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -1,263 +1,296 @@
-package org.drools.eclipse.rulebuilder.ui;
-
-import java.util.Arrays;
-import java.util.List;
-
-import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
-import org.drools.guvnor.client.modeldriven.brl.ActionInsertFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionInsertLogicalFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionRetractFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionSetField;
-import org.drools.guvnor.client.modeldriven.brl.ActionUpdateField;
-import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Shell;
-
-/**
- * This provides a popup for new RHS action selection.
- *
- * @author Anton Arhipov
- * @author Ahti Kitsik
- */
-public class AddNewActionDialog extends RuleDialog {
-
- private RuleModeller modeller;
-
- public AddNewActionDialog(Shell parent,
- RuleModeller modeller) {
- super( parent,
- "Add a new action",
- "Pick the values from combos and confirm the selection." );
- this.modeller = modeller;
- }
-
- protected Control createDialogArea(final Composite parent) {
- Composite composite = (Composite) super.createDialogArea( parent );
- String heading = "Choose...";
-
- createValuesOfFieldPart( composite,
- heading );
-
- createModifyFieldPart( composite,
- heading );
-
- createRetractFieldPart(composite, heading);
-
-
- String[] facts = getCompletion().getFactTypes();
-
- createFactAssertionPart( composite,
- heading,
- facts );
-
- createFactLogicalAssertionPart( composite,
- heading,
- facts );
-
- createDslSentences( composite,
- heading );
-
- return composite;
- }
-
- private void createRetractFieldPart(Composite composite, String heading) {
- createLabel( composite, "Retract the fact" );
-
- final Combo factsCombo = new Combo( composite, SWT.READ_ONLY );
-
- factsCombo.add( heading );
-
- List boundFacts = modeller.getModel().getBoundFacts();
-
- for ( int i = 0; i < boundFacts.size(); i++ ) {
- factsCombo.add( (String) boundFacts.get( i ) );
- }
- factsCombo.select( 0 );
-
- factsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( factsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addRhsItem( new ActionRetractFact(factsCombo.getText()) );
-
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
-
- }
-
- private void createModifyFieldPart(Composite composite,
- String heading) {
- createLabel( composite,
- "Modify a field on a fact" );
- final Combo factsCombo = new Combo( composite,
- SWT.READ_ONLY );
- factsCombo.add( heading );
-
- List boundFacts = modeller.getModel().getBoundFacts();
-
- for ( int i = 0; i < boundFacts.size(); i++ ) {
- factsCombo.add( (String) boundFacts.get( i ) );
- }
- factsCombo.select( 0 );
-
- factsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( factsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addRhsItem(new ActionUpdateField(factsCombo.getText()));
-
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
-
- }
-
- private void createDslSentences(Composite composite,
- String heading) {
- if ( getCompletion().getDSLActions().length > 0 ) {
- createLabel( composite,
- "Actions" );
-
- final Combo dslCombo = new Combo( composite,
- SWT.READ_ONLY );
- dslCombo.add( heading );
- for ( int i = 0; i < getCompletion().getDSLActions().length; i++ ) {
- DSLSentence sen = getCompletion().getDSLActions()[i];
- dslCombo.add( sen.toString() );
- }
-
- dslCombo.select( 0 );
-
- dslCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( dslCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- DSLSentence sentence = getCompletion().getDSLActions()[dslCombo.getSelectionIndex() - 1];
- modeller.getModel().addRhsItem( sentence.copy() );
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
-
- }
- }
-
- private void createFactLogicalAssertionPart(Composite composite,
- String heading,
- String[] facts) {
- createLabel( composite,
- "Logically insert a new fact" );
- final Combo factsCombo = createFactsCombo( composite,
- heading,
- facts );
- factsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
-
- if ( factsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addRhsItem( new ActionInsertLogicalFact( factsCombo.getText() ) );
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
- }
-
- private void createFactAssertionPart(Composite composite,
- String heading,
- String[] facts) {
- createLabel( composite,
- "Assert a new fact" );
- final Combo factsCombo = createFactsCombo( composite,
- heading,
- facts );
- factsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( factsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addRhsItem( new ActionInsertFact( factsCombo.getText() ) );
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
- }
-
- private Combo createFactsCombo(Composite composite,
- String heading,
- String[] facts) {
- Combo factsCombo = new Combo( composite,
- SWT.READ_ONLY );
- factsCombo.add( heading );
- for ( int i = 0; i < facts.length; i++ ) {
- factsCombo.add( facts[i] );
- }
- factsCombo.select( 0 );
- return factsCombo;
- }
-
- private void createValuesOfFieldPart(Composite composite,
- String heading) {
- createLabel( composite,
- "Set the values of a field on" );
- final Combo globalVarsCombo = new Combo( composite,
- SWT.READ_ONLY );
- globalVarsCombo.add( heading );
-
- List boundFacts = modeller.getModel().getBoundFacts();
-
- //adding globals
- String[] globals = modeller.getSuggestionCompletionEngine().getGlobalVariables();
- boundFacts.addAll(Arrays.asList(globals));
-
- for ( int i = 0; i < boundFacts.size(); i++ ) {
- globalVarsCombo.add( (String) boundFacts.get( i ) );
- }
- globalVarsCombo.select( 0 );
-
- globalVarsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( globalVarsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addRhsItem( new ActionSetField( globalVarsCombo.getText() ) );
- modeller.setDirty( true );
- modeller.reloadRhs();
- close();
- }
- } );
- }
-
- public SuggestionCompletionEngine getCompletion() {
- return modeller.getSuggestionCompletionEngine();
- }
-
-}
+package org.drools.eclipse.rulebuilder.ui;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.ActionInsertFact;
+import org.drools.guvnor.client.modeldriven.brl.ActionInsertLogicalFact;
+import org.drools.guvnor.client.modeldriven.brl.ActionRetractFact;
+import org.drools.guvnor.client.modeldriven.brl.ActionSetField;
+import org.drools.guvnor.client.modeldriven.brl.ActionUpdateField;
+import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
+import org.drools.guvnor.client.modeldriven.brl.FreeFormLine;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * This provides a popup for new RHS action selection.
+ *
+ * @author Anton Arhipov
+ * @author Ahti Kitsik
+ */
+public class AddNewActionDialog extends RuleDialog {
+
+ private RuleModeller modeller;
+
+ public AddNewActionDialog(Shell parent,
+ RuleModeller modeller) {
+ super( parent,
+ "Add a new action",
+ "Pick the values from combos and confirm the selection." );
+ this.modeller = modeller;
+ }
+
+ protected Control createDialogArea(final Composite parent) {
+ Composite composite = (Composite) super.createDialogArea( parent );
+ GridLayout layout = new GridLayout( 2,
+ false );
+ composite.setLayout( layout );
+ String heading = "Choose...";
+
+ createValuesOfFieldPart( composite,
+ heading );
+
+ createModifyFieldPart( composite,
+ heading );
+
+ createRetractFieldPart( composite,
+ heading );
+
+ String[] facts = getCompletion().getFactTypes();
+
+ createFactAssertionPart( composite,
+ heading,
+ facts );
+
+ createFactLogicalAssertionPart( composite,
+ heading,
+ facts );
+
+ createDrlSentences( composite );
+
+ // createAddFreeFromDSL(composite,heading);
+
+ return composite;
+ }
+
+ private void createDrlSentences(Composite composite) {
+ createLabel( composite,
+ "Free form action" );
+
+ Button b = new Button( composite,
+ SWT.NONE );
+ b.setText( "Add free form drl" );
+ b.addSelectionListener( new SelectionListener() {
+
+ public void widgetDefaultSelected(SelectionEvent e) {
+ }
+
+ public void widgetSelected(SelectionEvent e) {
+ modeller.getModel().addRhsItem( new FreeFormLine() );
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+ }
+
+ private void createRetractFieldPart(Composite composite,
+ String heading) {
+ createLabel( composite,
+ "Retract the fact" );
+
+ final Combo factsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+
+ factsCombo.add( heading );
+
+ List boundFacts = modeller.getModel().getBoundFacts();
+
+ for ( int i = 0; i < boundFacts.size(); i++ ) {
+ factsCombo.add( (String) boundFacts.get( i ) );
+ }
+ factsCombo.select( 0 );
+
+ factsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( factsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addRhsItem( new ActionRetractFact( factsCombo.getText() ) );
+
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+
+ }
+
+ private void createModifyFieldPart(Composite composite,
+ String heading) {
+ createLabel( composite,
+ "Modify a field on a fact" );
+ final Combo factsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ factsCombo.add( heading );
+
+ List boundFacts = modeller.getModel().getBoundFacts();
+
+ for ( int i = 0; i < boundFacts.size(); i++ ) {
+ factsCombo.add( (String) boundFacts.get( i ) );
+ }
+ factsCombo.select( 0 );
+
+ factsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( factsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addRhsItem( new ActionUpdateField( factsCombo.getText() ) );
+
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+
+ }
+
+ private void createDslSentences(Composite composite,
+ String heading) {
+ if ( getCompletion().getDSLActions().length > 0 ) {
+ createLabel( composite,
+ "Actions" );
+
+ final Combo dslCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ dslCombo.add( heading );
+ for ( int i = 0; i < getCompletion().getDSLActions().length; i++ ) {
+ DSLSentence sen = getCompletion().getDSLActions()[i];
+ dslCombo.add( sen.toString() );
+ }
+
+ dslCombo.select( 0 );
+
+ dslCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( dslCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ DSLSentence sentence = getCompletion().getDSLActions()[dslCombo.getSelectionIndex() - 1];
+ modeller.getModel().addRhsItem( sentence.copy() );
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+
+ }
+ }
+
+ private void createFactLogicalAssertionPart(Composite composite,
+ String heading,
+ String[] facts) {
+ createLabel( composite,
+ "Logically insert a new fact" );
+ final Combo factsCombo = createFactsCombo( composite,
+ heading,
+ facts );
+ factsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+
+ if ( factsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addRhsItem( new ActionInsertLogicalFact( factsCombo.getText() ) );
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+ }
+
+ private void createFactAssertionPart(Composite composite,
+ String heading,
+ String[] facts) {
+ createLabel( composite,
+ "Assert a new fact" );
+ final Combo factsCombo = createFactsCombo( composite,
+ heading,
+ facts );
+ factsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( factsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addRhsItem( new ActionInsertFact( factsCombo.getText() ) );
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+ }
+
+ private Combo createFactsCombo(Composite composite,
+ String heading,
+ String[] facts) {
+ Combo factsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ factsCombo.add( heading );
+ for ( int i = 0; i < facts.length; i++ ) {
+ factsCombo.add( facts[i] );
+ }
+ factsCombo.select( 0 );
+ return factsCombo;
+ }
+
+ private void createValuesOfFieldPart(Composite composite,
+ String heading) {
+ createLabel( composite,
+ "Set the values of a field on" );
+ final Combo globalVarsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ globalVarsCombo.add( heading );
+
+ List boundFacts = modeller.getModel().getBoundFacts();
+
+ //adding globals
+ String[] globals = modeller.getSuggestionCompletionEngine().getGlobalVariables();
+ boundFacts.addAll( Arrays.asList( globals ) );
+
+ for ( int i = 0; i < boundFacts.size(); i++ ) {
+ globalVarsCombo.add( (String) boundFacts.get( i ) );
+ }
+ globalVarsCombo.select( 0 );
+
+ globalVarsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( globalVarsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addRhsItem( new ActionSetField( globalVarsCombo.getText() ) );
+ modeller.setDirty( true );
+ modeller.reloadRhs();
+ close();
+ }
+ } );
+ }
+
+ public SuggestionCompletionEngine getCompletion() {
+ return modeller.getSuggestionCompletionEngine();
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewConditionDialog.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewConditionDialog.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/AddNewConditionDialog.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -1,154 +1,159 @@
-package org.drools.eclipse.rulebuilder.ui;
-
-import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
-import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
-import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
-import org.drools.guvnor.client.modeldriven.brl.FactPattern;
-import org.drools.guvnor.client.modeldriven.brl.IPattern;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Shell;
-
-/**
- * This provides a popup for new LHS condition selection. (add new if-condition)
- *
- * @author Anton Arhipov
- * @author Ahti Kitsik
- */
-public class AddNewConditionDialog extends RuleDialog {
-
- private IPattern pattern;
-
- private RuleModeller modeller;
-
- public AddNewConditionDialog(Shell parent,
- RuleModeller modeller) {
-
- super( parent,
- "Add new condition to the rule",
- "Pick the values from combos and confirm the selection." );
-
- this.modeller = modeller;
- }
-
- protected Control createDialogArea(final Composite parent) {
- Control dialog = super.createDialogArea( parent );
-
- Composite composite = (Composite) dialog;
-
- addFacts( composite );
-
- addConditionType( composite );
-
- addDSLSentences( composite );
-
- return composite;
- }
-
- private void addFacts(Composite composite) {
- createLabel( composite,
- "Fact" );
-
- String[] factTypes = getCompletion().getFactTypes();
- final Combo factsCombo = new Combo( composite,
- SWT.READ_ONLY );
- factsCombo.add( "Choose fact type..." );
- for ( int i = 0; i < factTypes.length; i++ ) {
- factsCombo.add( factTypes[i] );
- }
- factsCombo.select( 0 );
-
- factsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( factsCombo.getSelectionIndex() == 0 ) {
- return;
- }
- modeller.getModel().addLhsItem( new FactPattern( factsCombo.getText() ) );
- modeller.reloadLhs();
- modeller.setDirty( true );
- close();
- }
- } );
- }
-
- private void addConditionType(Composite composite) {
- createLabel( composite,
- "Condition type" );
-
- final Combo conditionalsCombo = new Combo( composite,
- SWT.READ_ONLY );
- String[] conditionalElements = getCompletion().getConditionalElements();
- conditionalsCombo.add( "Choose condition type..." );
- for ( int i = 0; i < conditionalElements.length; i++ ) {
- conditionalsCombo.add( conditionalElements[i] );
- }
- conditionalsCombo.select( 0 );
-
- conditionalsCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( conditionalsCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- modeller.getModel().addLhsItem( new CompositeFactPattern( conditionalsCombo.getText() ) );
- modeller.reloadLhs();
- modeller.setDirty( true );
- close();
- }
- } );
- }
-
- //
- // The list of DSL sentences
- //
- private void addDSLSentences(Composite composite) {
- if ( getCompletion().getDSLConditions().length > 0 ) {
- createLabel( composite,
- "Condition sentences" );
-
- final Combo dslCombo = new Combo( composite,
- SWT.READ_ONLY );
- dslCombo.add( "Choose..." );
-
- for ( int i = 0; i < getCompletion().getDSLConditions().length; i++ ) {
- DSLSentence sen = getCompletion().getDSLConditions()[i];
- dslCombo.add( sen.toString() );
- }
-
- dslCombo.select( 0 );
-
- dslCombo.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- if ( dslCombo.getSelectionIndex() == 0 ) {
- return;
- }
-
- DSLSentence sentence = getCompletion().getDSLConditions()[dslCombo.getSelectionIndex() - 1];
- //TODO Handle this kind of situations with care - add* can throw runtime exceptions
- modeller.getModel().addLhsItem( sentence.copy() );
-
- modeller.reloadLhs();
- modeller.setDirty( true );
- close();
- }
- } );
-
- }
- }
-
- public IPattern getPattern() {
- return pattern;
- }
-
- private SuggestionCompletionEngine getCompletion() {
- return modeller.getSuggestionCompletionEngine();
- }
-
-}
+package org.drools.eclipse.rulebuilder.ui;
+
+import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
+import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
+import org.drools.guvnor.client.modeldriven.brl.FactPattern;
+import org.drools.guvnor.client.modeldriven.brl.IPattern;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * This provides a popup for new LHS condition selection. (add new if-condition)
+ *
+ * @author Anton Arhipov
+ * @author Ahti Kitsik
+ */
+public class AddNewConditionDialog extends RuleDialog {
+
+ private IPattern pattern;
+
+ private RuleModeller modeller;
+
+ public AddNewConditionDialog(Shell parent,
+ RuleModeller modeller) {
+
+ super( parent,
+ "Add new condition to the rule",
+ "Pick the values from combos and confirm the selection." );
+
+ this.modeller = modeller;
+ }
+
+ protected Control createDialogArea(final Composite parent) {
+ Control dialog = super.createDialogArea( parent );
+
+ Composite composite = (Composite) dialog;
+ GridLayout layout = new GridLayout( 2,
+ false );
+ composite.setLayout( layout );
+
+ addFacts( composite );
+
+ addConditionType( composite );
+
+ addDSLSentences( composite );
+
+ return composite;
+ }
+
+ private void addFacts(Composite composite) {
+ createLabel( composite,
+ "Fact" );
+
+ String[] factTypes = getCompletion().getFactTypes();
+ final Combo factsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ factsCombo.add( "Choose fact type..." );
+ for ( int i = 0; i < factTypes.length; i++ ) {
+ factsCombo.add( factTypes[i] );
+ }
+ factsCombo.select( 0 );
+
+ factsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( factsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+ modeller.getModel().addLhsItem( new FactPattern( factsCombo.getText() ) );
+ modeller.reloadLhs();
+ modeller.setDirty( true );
+ close();
+ }
+ } );
+ }
+
+ private void addConditionType(Composite composite) {
+ createLabel( composite,
+ "Condition type" );
+
+ final Combo conditionalsCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ String[] conditionalElements = getCompletion().getConditionalElements();
+ conditionalsCombo.add( "Choose condition type..." );
+ for ( int i = 0; i < conditionalElements.length; i++ ) {
+ conditionalsCombo.add( conditionalElements[i] );
+ }
+ conditionalsCombo.select( 0 );
+
+ conditionalsCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( conditionalsCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ modeller.getModel().addLhsItem( new CompositeFactPattern( conditionalsCombo.getText() ) );
+ modeller.reloadLhs();
+ modeller.setDirty( true );
+ close();
+ }
+ } );
+ }
+
+ //
+ // The list of DSL sentences
+ //
+ private void addDSLSentences(Composite composite) {
+ if ( getCompletion().getDSLConditions().length > 0 ) {
+ createLabel( composite,
+ "Condition sentences" );
+
+ final Combo dslCombo = new Combo( composite,
+ SWT.READ_ONLY );
+ dslCombo.add( "Choose..." );
+
+ for ( int i = 0; i < getCompletion().getDSLConditions().length; i++ ) {
+ DSLSentence sen = getCompletion().getDSLConditions()[i];
+ dslCombo.add( sen.toString() );
+ }
+
+ dslCombo.select( 0 );
+
+ dslCombo.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ if ( dslCombo.getSelectionIndex() == 0 ) {
+ return;
+ }
+
+ DSLSentence sentence = getCompletion().getDSLConditions()[dslCombo.getSelectionIndex() - 1];
+ // TODO Handle this kind of situations with care - add* can
+ // throw runtime exceptions
+ modeller.getModel().addLhsItem( sentence.copy() );
+
+ modeller.reloadLhs();
+ modeller.setDirty( true );
+ close();
+ }
+ } );
+
+ }
+ }
+
+ public IPattern getPattern() {
+ return pattern;
+ }
+
+ private SuggestionCompletionEngine getCompletion() {
+ return modeller.getSuggestionCompletionEngine();
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ConstraintValueEditor.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ConstraintValueEditor.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/ConstraintValueEditor.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -2,9 +2,12 @@
import java.util.List;
+import org.drools.eclipse.DroolsEclipsePlugin;
+import org.drools.guvnor.client.modeldriven.DropDownData;
import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.FactPattern;
import org.drools.guvnor.client.modeldriven.brl.ISingleFieldConstraint;
-import org.drools.eclipse.DroolsEclipsePlugin;
+import org.drools.guvnor.client.modeldriven.brl.SingleFieldConstraint;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
@@ -22,173 +25,243 @@
public class ConstraintValueEditor {
- private Composite composite;
+ private Composite composite;
- private ISingleFieldConstraint constraint;
+ private ISingleFieldConstraint constraint;
- private FormToolkit toolkit;
+ private FormToolkit toolkit;
- private RuleModeller modeller;
+ private RuleModeller modeller;
- private boolean numericValue;
-
- public ConstraintValueEditor(Composite composite,
- ISingleFieldConstraint constraint, FormToolkit toolkit,
- RuleModeller modeller, String numericType /*e.g. is "Numeric"*/) {
- this.composite = composite;
- this.constraint = constraint;
- this.toolkit = toolkit;
- this.modeller = modeller;
-
- if (SuggestionCompletionEngine.TYPE_NUMERIC.equals( numericType )) {
+ private boolean numericValue;
+
+ private FactPattern pattern;
+
+ public ConstraintValueEditor(Composite composite,
+ ISingleFieldConstraint constraint,
+ FormToolkit toolkit,
+ RuleModeller modeller,
+ String numericType /* e.g. is "Numeric" */) {
+ this( composite,
+ constraint,
+ toolkit,
+ modeller,
+ numericType,
+ null );
+ }
+
+ public ConstraintValueEditor(Composite parent,
+ ISingleFieldConstraint c,
+ FormToolkit toolkit,
+ RuleModeller modeller,
+ String type,
+ FactPattern pattern) {
+ this.pattern = pattern;
+ this.composite = parent;
+ this.constraint = c;
+ this.toolkit = toolkit;
+ this.modeller = modeller;
+
+ if ( SuggestionCompletionEngine.TYPE_NUMERIC.equals( type ) ) {
this.numericValue = true;
}
- create();
- }
+ create();
+ }
- private void create() {
- if (constraint.constraintValueType == ISingleFieldConstraint.TYPE_UNDEFINED) {
- ImageHyperlink link = addImage(composite, "icons/edit.gif");
- link.setToolTipText("Choose value editor type");
- link.addHyperlinkListener(new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- RuleDialog popup = new ValueEditorTypeSelectionDialog(
- composite.getShell(), toolkit, modeller, constraint);
- popup.open();
- }
+ private void create() {
+ if ( constraint.constraintValueType == ISingleFieldConstraint.TYPE_UNDEFINED ) {
+ ImageHyperlink link = addImage( composite,
+ "icons/edit.gif" );
+ link.setToolTipText( "Choose value editor type" );
+ link.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ RuleDialog popup = new ValueEditorTypeSelectionDialog( composite.getShell(),
+ toolkit,
+ modeller,
+ constraint );
+ popup.open();
+ }
- public void linkEntered(HyperlinkEvent e) {
- }
+ public void linkEntered(HyperlinkEvent e) {
+ }
- public void linkExited(HyperlinkEvent e) {
- }
- });
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
- GridData gd = new GridData(GridData.FILL_HORIZONTAL
- | GridData.GRAB_HORIZONTAL
- | GridData.HORIZONTAL_ALIGN_BEGINNING);
- gd.horizontalSpan = 2;
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING );
+ gd.horizontalSpan = 2;
- link.setLayoutData(gd);
- } else {
- switch (constraint.constraintValueType) {
- case ISingleFieldConstraint.TYPE_LITERAL:
- literalValueEditor(composite, constraint, new GridData(
- GridData.FILL_HORIZONTAL));
- break;
- case ISingleFieldConstraint.TYPE_RET_VALUE:
- addImage(composite, "icons/function_assets.gif");
- formulaValueEditor(composite, constraint, new GridData(
- GridData.FILL_HORIZONTAL));
- break;
- case ISingleFieldConstraint.TYPE_VARIABLE:
- variableEditor(composite, constraint, new GridData(
- GridData.FILL_HORIZONTAL));
- break;
- default:
- break;
- }
- }
+ link.setLayoutData( gd );
+ } else {
+ switch ( constraint.constraintValueType ) {
+ case ISingleFieldConstraint.TYPE_LITERAL :
+ literalValueEditor( composite,
+ constraint,
+ new GridData( GridData.FILL_HORIZONTAL ) );
+ break;
+ case ISingleFieldConstraint.TYPE_RET_VALUE :
+ addImage( composite,
+ "icons/function_assets.gif" );
+ formulaValueEditor( composite,
+ constraint,
+ new GridData( GridData.FILL_HORIZONTAL ) );
+ break;
+ case ISingleFieldConstraint.TYPE_VARIABLE :
+ variableEditor( composite,
+ constraint,
+ new GridData( GridData.FILL_HORIZONTAL ) );
+ break;
+ default :
+ break;
+ }
+ }
- }
+ }
- private void literalValueEditor(Composite parent,
- final ISingleFieldConstraint c, GridData gd) {
- final Text box = toolkit.createText(parent, "");
+ private void literalValueEditor(Composite parent,
+ final ISingleFieldConstraint c,
+ GridData gd) {
- if (c.value != null) {
- box.setText(c.value);
- }
+ String fieldName = ((SingleFieldConstraint) c).fieldName;
+ DropDownData enums = modeller.getSuggestionCompletionEngine().getEnums( pattern,
+ fieldName );
+ boolean found = false;
+ if ( enums != null && enums.fixedList.length > 0 ) {
+ String[] list = enums.fixedList;
+ final Combo combo = new Combo( parent,
+ SWT.DROP_DOWN | SWT.READ_ONLY );
+ for ( int i = 0; i < list.length; i++ ) {
+ String e = list[i];
+ combo.add( e );
+ if ( e.equals( c.value ) ) {
+ combo.select( i );
+ found = true;
+ }
+ }
+ if ( !found && c.value != null ) {
+ combo.add( c.value );
+ combo.select( combo.getItemCount() - 1 );
+ }
- gd.horizontalSpan = 2;
- gd.grabExcessHorizontalSpace = true;
- gd.minimumWidth = 100;
- box.setLayoutData(gd);
+ combo.addModifyListener( new ModifyListener() {
- box.addModifyListener(new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- c.value = box.getText();
- modeller.setDirty(true);
- }
- });
-
- if (this.numericValue) {
- box.addKeyListener(new KeyListener(){
+ public void modifyText(ModifyEvent e) {
+ c.value = combo.getItem( combo.getSelectionIndex() );
+ modeller.reloadRhs();
+ modeller.setDirty( true );
+ }
+ } );
- public void keyPressed(KeyEvent e) {
- if(Character.isLetter(e.character)){
- e.doit = false;
- }
- }
+ gd.horizontalSpan = 2;
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ combo.setLayoutData( gd );
- public void keyReleased(KeyEvent e) {
-
- }
-
- });
- }
- }
+ } else {
- private void formulaValueEditor(Composite parent,
- final ISingleFieldConstraint c, GridData gd) {
+ final Text box = toolkit.createText( parent,
+ "" );
- final Text box = toolkit.createText(parent, "");
+ if ( c.value != null ) {
+ box.setText( c.value );
+ }
- if (c.value != null) {
- box.setText(c.value);
- }
+ gd.horizontalSpan = 2;
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ box.setLayoutData( gd );
- gd.grabExcessHorizontalSpace = true;
- gd.minimumWidth = 100;
- box.setLayoutData(gd);
+ box.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ c.value = box.getText();
+ modeller.setDirty( true );
+ }
+ } );
- box.addModifyListener(new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- c.value = box.getText();
- modeller.setDirty(true);
- }
- });
- }
+ if ( this.numericValue ) {
+ box.addKeyListener( new KeyListener() {
- private void variableEditor(Composite composite,
- final ISingleFieldConstraint c, GridData gd) {
- List vars = modeller.getModel().getBoundVariablesInScope(c);
+ public void keyPressed(KeyEvent e) {
+ if ( Character.isLetter( e.character ) ) {
+ e.doit = false;
+ }
+ }
- final Combo combo = new Combo(composite, SWT.READ_ONLY);
+ public void keyReleased(KeyEvent e) {
- gd.horizontalSpan = 2;
- combo.setLayoutData(gd);
- if (c.value == null) {
- combo.add("Choose ...");
- }
+ }
- int idx = 0;
+ } );
+ }
+ }
+ }
- for (int i = 0; i < vars.size(); i++) {
- String var = (String) vars.get(i);
+ private void formulaValueEditor(Composite parent,
+ final ISingleFieldConstraint c,
+ GridData gd) {
- if (c.value != null && c.value.equals(var)) {
- idx = i;
- }
- combo.add(var);
- }
+ final Text box = toolkit.createText( parent,
+ "" );
- combo.select(idx);
+ if ( c.value != null ) {
+ box.setText( c.value );
+ }
- combo.addModifyListener(new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- c.value = combo.getText();
- }
- });
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ box.setLayoutData( gd );
- }
+ box.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ c.value = box.getText();
+ modeller.setDirty( true );
+ }
+ } );
+ }
- public ImageHyperlink addImage(Composite parent, String fileName) {
- ImageHyperlink imageHyperlink = toolkit.createImageHyperlink(parent, 0);
- ImageDescriptor imageDescriptor = DroolsEclipsePlugin
- .getImageDescriptor(fileName);
- imageHyperlink.setImage(imageDescriptor.createImage());
- return imageHyperlink;
- }
+ private void variableEditor(Composite composite,
+ final ISingleFieldConstraint c,
+ GridData gd) {
+ List vars = modeller.getModel().getBoundVariablesInScope( c );
+ final Combo combo = new Combo( composite,
+ SWT.READ_ONLY );
+
+ gd.horizontalSpan = 2;
+ combo.setLayoutData( gd );
+ if ( c.value == null ) {
+ combo.add( "Choose ..." );
+ }
+
+ int idx = 0;
+
+ for ( int i = 0; i < vars.size(); i++ ) {
+ String var = (String) vars.get( i );
+
+ if ( c.value != null && c.value.equals( var ) ) {
+ idx = i;
+ }
+ combo.add( var );
+ }
+
+ combo.select( idx );
+
+ combo.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ c.value = combo.getText();
+ }
+ } );
+
+ }
+
+ public ImageHyperlink addImage(Composite parent,
+ String fileName) {
+ ImageHyperlink imageHyperlink = toolkit.createImageHyperlink( parent,
+ 0 );
+ ImageDescriptor imageDescriptor = DroolsEclipsePlugin.getImageDescriptor( fileName );
+ imageHyperlink.setImage( imageDescriptor.createImage() );
+ return imageHyperlink;
+ }
+
}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/DSLSentenceWidget.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/DSLSentenceWidget.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/DSLSentenceWidget.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -7,9 +7,9 @@
import java.util.Iterator;
import java.util.List;
-import org.drools.guvnor.client.modeldriven.ui.ConstraintValueEditorHelper;
import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
+import org.drools.guvnor.client.modeldriven.ui.ConstraintValueEditorHelper;
import org.drools.util.DateUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
@@ -36,7 +36,6 @@
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
-
/**
* This displays a widget to edit a DSL sentence.
*
@@ -45,18 +44,18 @@
*/
public abstract class DSLSentenceWidget extends Widget {
- private static final String ENUM_TAG = "ENUM";
- private static final String DATE_TAG = "DATE";
- private static final String BOOLEAN_TAG = "BOOLEAN";
+ private static final String ENUM_TAG = "ENUM";
+ private static final String DATE_TAG = "DATE";
+ private static final String BOOLEAN_TAG = "BOOLEAN";
- private static final String ITEM_ = "ITEM_";
+ private static final String ITEM_ = "ITEM_";
- private final DSLSentence sentence;
-
+ private final DSLSentence sentence;
+
protected SuggestionCompletionEngine completions;
-
- private List<ModelWidget> widgets = new ArrayList<ModelWidget>();
+ private List<ModelWidget> widgets = new ArrayList<ModelWidget>();
+
public DSLSentenceWidget(FormToolkit toolkit,
Composite parent,
DSLSentence sentence,
@@ -69,13 +68,12 @@
this.sentence = sentence;
completions = modeller.getSuggestionCompletionEngine();
-
+
Composite lastRow = makeWidgets( this.sentence.sentence );
- addDeleteAction(lastRow);
- toolkit.paintBordersFor( parent);
+ addDeleteAction( lastRow );
+ toolkit.paintBordersFor( parent );
}
-
protected abstract void updateModel();
private void addDeleteAction(Composite parent) {
@@ -103,7 +101,6 @@
delLink.setToolTipText( "Remove this condition." );
}
-
/**
* This will take a DSL line item, and split it into widget thingamies for
* displaying. One day, if this is too complex, this will have to be done on
@@ -111,9 +108,9 @@
* @return
*/
public Composite makeWidgets(String dslLine) {
-
+
List<ModelWidget> lineWidgets = new ArrayList<ModelWidget>();
-
+
int startVariable = dslLine.indexOf( "{" );
boolean firstOneIsBracket = (dslLine.indexOf( "{" ) == 0);
@@ -128,23 +125,25 @@
startLabel = dslLine;
}
- parent.setLayout( new GridLayout(1, true) );
-
- Composite row = toolkit.createComposite( parent);
+ parent.setLayout( new GridLayout( 1,
+ true ) );
+
+ Composite row = toolkit.createComposite( parent );
RowLayout rl = new RowLayout();
- rl.marginBottom=0;
- rl.marginHeight=0;
- rl.marginLeft=0;
- rl.marginRight=0;
- rl.pack=true;
- rl.spacing=0;
- rl.center=true;
+ rl.marginBottom = 0;
+ rl.marginHeight = 0;
+ rl.marginLeft = 0;
+ rl.marginRight = 0;
+ rl.pack = true;
+ rl.spacing = 0;
+ rl.center = true;
//rl.fill=true;
-
+
row.setLayout( rl );
-
- lineWidgets.add( new LabelWidget( row, startLabel ));
-
+
+ lineWidgets.add( new LabelWidget( row,
+ startLabel ) );
+
while ( startVariable > 0 || firstOneIsBracket ) {
firstOneIsBracket = false;
@@ -153,7 +152,8 @@
String currVariable = dslLine.substring( startVariable + 1,
endVariable );
- lineWidgets.add( addVariable(row, currVariable));
+ lineWidgets.add( addVariable( row,
+ currVariable ) );
// Parse out the next label between variables
startVariable = dslLine.indexOf( "{",
@@ -172,11 +172,13 @@
for ( int i = 0; i < lines.length; i++ ) {
row = toolkit.createComposite( parent );
row.setLayout( rl );
- lineWidgets.add( new DSLSentenceWidget.NewLine(row) );
- lineWidgets.add( new LabelWidget(row, lines[i] ) );
+ lineWidgets.add( new DSLSentenceWidget.NewLine( row ) );
+ lineWidgets.add( new LabelWidget( row,
+ lines[i] ) );
}
} else {
- lineWidgets.add( new LabelWidget(row, lbl));
+ lineWidgets.add( new LabelWidget( row,
+ lbl ) );
}
}
@@ -184,14 +186,14 @@
for ( ModelWidget widg : lineWidgets ) {
widgets.add( widg );
}
-
+
updateSentence();
return row;
}
+ public ModelWidget addVariable(Composite parent,
+ String currVariable) {
- public ModelWidget addVariable(Composite parent, String currVariable) {
-
// Formats are: <varName>:ENUM:<Field.type>
// <varName>:DATE:<dateFormat>
// <varName>:BOOLEAN:[checked | unchecked] <-initial value
@@ -208,33 +210,40 @@
String type = currVariable.substring( colonIndex + 1,
colonIndex + secondColonIndex + 1 );
if ( type.equalsIgnoreCase( ENUM_TAG ) ) {
- return addEnumDropdown( parent, currVariable );
+ return addEnumDropdown( parent,
+ currVariable );
} else if ( type.equalsIgnoreCase( DATE_TAG ) ) {
- return addDateSelector( parent, currVariable );
+ return addDateSelector( parent,
+ currVariable );
} else if ( type.equalsIgnoreCase( BOOLEAN_TAG ) ) {
- return addCheckbox( parent, currVariable );
+ return addCheckbox( parent,
+ currVariable );
}
} else {
String regex = currVariable.substring( colonIndex + 1,
currVariable.length() );
- return addBox( parent, currVariable,
- regex );
+ return addBox( parent,
+ currVariable,
+ regex );
}
}
- return addBox( parent, currVariable, "" );
+ return addBox( parent,
+ currVariable,
+ "" );
}
-
- public ModelWidget addBox(Composite parent, String variableDef,
- String regex) {
+ public ModelWidget addBox(Composite parent,
+ String variableDef,
+ String regex) {
+
int colonIndex = variableDef.indexOf( ":" );
if ( colonIndex > 0 ) {
variableDef = variableDef.substring( 0,
colonIndex );
}
- FieldEditor currentBox = new FieldEditor(parent);
+ FieldEditor currentBox = new FieldEditor( parent );
currentBox.setVisibleLength( variableDef.length() + 1 );
currentBox.setText( variableDef );
currentBox.setRestriction( regex );
@@ -242,27 +251,29 @@
}
-
- public ModelWidget addCheckbox(Composite parent, String variableDef) {
- return new DSLCheckBox( parent, variableDef );
+ public ModelWidget addCheckbox(Composite parent,
+ String variableDef) {
+ return new DSLCheckBox( parent,
+ variableDef );
}
-
- public ModelWidget addDateSelector(Composite parent, String variableDef) {
- return new DSLDateSelector( parent, variableDef );
+ public ModelWidget addDateSelector(Composite parent,
+ String variableDef) {
+ return new DSLDateSelector( parent,
+ variableDef );
}
-
- private ModelWidget addEnumDropdown(Composite parent, String variableDef) {
- return new DSLDropDown( parent, variableDef );
+ private ModelWidget addEnumDropdown(Composite parent,
+ String variableDef) {
+ return new DSLDropDown( parent,
+ variableDef );
}
-
protected void updateSentence() {
String newSentence = "";
for ( Iterator iter = widgets.iterator(); iter.hasNext(); ) {
ModelWidget wid = (ModelWidget) iter.next();
- if ( wid instanceof LabelWidget) {
+ if ( wid instanceof LabelWidget ) {
newSentence = newSentence + ((LabelWidget) wid).getText();
} else if ( wid instanceof FieldEditor ) {
FieldEditor editor = (FieldEditor) wid;
@@ -283,10 +294,10 @@
String type = drop.getType();
String factAndField = drop.getFactAndField();
- String key = ITEM_+(box.getSelectionIndex()+1);
-
- Object keyval = box.getData(key );
-
+ String key = ITEM_ + (box.getSelectionIndex() + 1);
+
+ Object keyval = box.getData( key );
+
newSentence = newSentence + "{" + keyval + ":" + type + ":" + factAndField + "} ";
} else if ( wid instanceof DSLCheckBox ) {
@@ -305,29 +316,35 @@
}
this.sentence.sentence = newSentence.trim();
}
-
- class LabelWidget implements ModelWidget {
-
- private Label control;
+
+ class LabelWidget
+ implements
+ ModelWidget {
+
+ private Label control;
private String val;
-
- public LabelWidget(Composite parent, String text) {
- val = text;
- control = toolkit.createLabel( parent, text);
+
+ public LabelWidget(Composite parent,
+ String text) {
+ val = text;
+ control = toolkit.createLabel( parent,
+ text );
}
-
+
public Control getControl() {
return control;
}
-
+
public String getText() {
return val;
}
-
+
}
-
- class NewLine implements ModelWidget {
-
+
+ class NewLine
+ implements
+ ModelWidget {
+
private Composite control;
public NewLine(Composite parent) {
@@ -338,18 +355,20 @@
return control;
}
}
-
- class FieldEditor implements ModelWidget {
- private Text control;
+ class FieldEditor
+ implements
+ ModelWidget {
- private String oldValue = "";
- private String regex = "";
+ private Text control;
+ private String oldValue = "";
+ private String regex = "";
public FieldEditor(Composite parent) {
-
- control = toolkit.createText( parent, "" );
+
+ control = toolkit.createText( parent,
+ "" );
// box.setStyleName( "dsl-field-TextBox" );
control.addModifyListener( new ModifyListener() {
@@ -357,7 +376,7 @@
Text otherBox = control;
if ( !regex.equals( "" ) && !otherBox.getText().matches( regex ) ) {
- System.err.println("VALUE IS NOT VALID: "+otherBox.getText());
+ System.err.println( "VALUE IS NOT VALID: " + otherBox.getText() );
control.setText( oldValue );
} else {
oldValue = otherBox.getText();
@@ -381,7 +400,7 @@
public Control getControl() {
return control;
}
-
+
public String getText() {
return control.getText();
}
@@ -402,19 +421,22 @@
}
}
- class DSLDropDown implements ModelWidget {
+ class DSLDropDown
+ implements
+ ModelWidget {
- Combo resultWidget = null;
+ Combo resultWidget = null;
// Format for the dropdown def is <varName>:<type>:<Fact.field>
private String varName = "";
private String type = "";
private String factAndField = "";
- public DSLDropDown(Composite parent, String variableDef) {
+ public DSLDropDown(Composite parent,
+ String variableDef) {
Composite comp = toolkit.createComposite( parent );
-
+
comp.setLayout( new FillLayout() );
-
+
int firstIndex = variableDef.indexOf( ":" );
int lastIndex = variableDef.lastIndexOf( ":" );
varName = variableDef.substring( 0,
@@ -432,13 +454,14 @@
String[] data = completions.getEnumValues( type,
field );
-
- Combo list = new Combo(comp,SWT.DROP_DOWN|SWT.READ_ONLY);
-
+
+ Combo list = new Combo( comp,
+ SWT.DROP_DOWN | SWT.READ_ONLY );
+
if ( data != null ) {
-
+
int selected = 0; // Select first item for now by default. Null values are not allowed.
-
+
for ( int i = 0; i < data.length; i++ ) {
String realValue = data[i];
String display = data[i];
@@ -450,25 +473,26 @@
if ( varName.equals( realValue ) ) {
selected = i;
}
-
- list.add( display);
-
- String key = ITEM_+list.getItemCount();
- list.setData(key, realValue);
-
+
+ list.add( display );
+
+ String key = ITEM_ + list.getItemCount();
+ list.setData( key,
+ realValue );
+
}
if ( selected >= 0 ) list.select( selected );
}
-
- list.addModifyListener(new ModifyListener() {
-
+
+ list.addModifyListener( new ModifyListener() {
+
public void modifyText(ModifyEvent e) {
updateSentence();
getModeller().setDirty( true );
}
-
- });
+ } );
+
resultWidget = list;
comp.layout();
}
@@ -494,17 +518,20 @@
}
}
- class DSLCheckBox implements ModelWidget {
- Combo resultWidget = null;
+ class DSLCheckBox
+ implements
+ ModelWidget {
+ Combo resultWidget = null;
// Format for the dropdown def is <varName>:<type>:<Fact.field>
- private String varName = "";
+ private String varName = "";
private Composite control;
- public DSLCheckBox(Composite parent, String variableDef) {
-
+ public DSLCheckBox(Composite parent,
+ String variableDef) {
+
control = toolkit.createComposite( parent );
control.setLayout( new RowLayout() );
-
+
int firstIndex = variableDef.indexOf( ":" );
int lastIndex = variableDef.lastIndexOf( ":" );
varName = variableDef.substring( 0,
@@ -512,7 +539,8 @@
String checkedUnchecked = variableDef.substring( lastIndex + 1,
variableDef.length() );
- resultWidget = new Combo(control, SWT.READ_ONLY);
+ resultWidget = new Combo( control,
+ SWT.READ_ONLY );
resultWidget.add( "true" );
resultWidget.add( "false" );
@@ -522,24 +550,24 @@
resultWidget.select( 1 );
}
- resultWidget.addModifyListener(new ModifyListener() {
+ resultWidget.addModifyListener( new ModifyListener() {
public void modifyText(ModifyEvent e) {
updateSentence();
modeller.setDirty( true );
}
-
- });
-
+
+ } );
+
resultWidget.setVisible( true );
- control.layout();
-
+ control.layout();
+
}
-
+
public Control getControl() {
return control;
}
-
+
public Combo getListBox() {
return resultWidget;
}
@@ -561,31 +589,32 @@
}
public String getCheckedValue() {
- return this.resultWidget.getSelectionIndex() == 0?"checked":"checked";
+ return this.resultWidget.getSelectionIndex() == 0 ? "checked" : "checked";
}
}
- class DSLDateSelector implements ModelWidget {
-
+ class DSLDateSelector
+ implements
+ ModelWidget {
+
//DateTime resultWidget = null;
-
+
// Format for the dropdown def is <varName>:<type>:<Fact.field>
- private String varName = "";
- private String javascriptFormat = "";
- private final String defaultJavascriptFormat = "d-M-y";
- private final String javaFormat = DateUtils.getDateFormatMask();
+ private String varName = "";
+ private String javascriptFormat = "";
+ private final String defaultJavascriptFormat = "d-M-y";
+ private final String javaFormat = DateUtils.getDateFormatMask();
private SimpleDateFormat formatter = null;
- private Composite control;
- private Text field;
-
- public DSLDateSelector(final Composite parent, String variableDef) {
-
+ private Composite control;
+ private Text field;
+
+ public DSLDateSelector(final Composite parent,
+ String variableDef) {
+
control = toolkit.createComposite( parent );
control.setLayout( new RowLayout() );
-
-
-
+
int firstIndex = variableDef.indexOf( ":" );
int lastIndex = variableDef.lastIndexOf( ":" );
varName = variableDef.substring( 0,
@@ -610,66 +639,81 @@
}
}
- field = toolkit.createText( control, "" );
- final Button open = new Button (control, SWT.ARROW | SWT.DOWN);
+ field = toolkit.createText( control,
+ "" );
+ final Button open = new Button( control,
+ SWT.ARROW | SWT.DOWN );
//open.setText ("Set");
- if ( origDate != null ) field.setText( formatter.format(origDate));
+ if ( origDate != null ) field.setText( formatter.format( origDate ) );
field.addModifyListener( new ModifyListener() {
public void modifyText(ModifyEvent e) {
updateSentence();
- modeller.setDirty( true );
+ modeller.setDirty( true );
}
} );
-
- open.addSelectionListener (new SelectionAdapter () {
- public void widgetSelected (SelectionEvent e) {
- final Shell dialog = new Shell (open.getShell(), SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
+
+ open.addSelectionListener( new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ final Shell dialog = new Shell( open.getShell(),
+ SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL );
dialog.setText( "Set date:" );
- dialog.setLayout (new GridLayout (1, false));
- final DateTime calendar = new DateTime (dialog, SWT.CALENDAR);
-
+ dialog.setLayout( new GridLayout( 1,
+ false ) );
+ final DateTime calendar = new DateTime( dialog,
+ SWT.CALENDAR );
+
Date date = new Date();
try {
String txt = field.getText();
- if (txt!=null && txt.length()>0) {
- date = formatter.parse(txt);
+ if ( txt != null && txt.length() > 0 ) {
+ date = formatter.parse( txt );
}
} catch ( ParseException e1 ) {
e1.printStackTrace();
}
- calendar.setDate(date.getYear()+1900, date.getMonth(), date.getDate());
-
- Point p2 = open.toDisplay( 0, 0 );
-
+ calendar.setDate( date.getYear() + 1900,
+ date.getMonth(),
+ date.getDate() );
+
+ Point p2 = open.toDisplay( 0,
+ 0 );
+
int x = p2.x;
- int y = p2.y+20;
-
- dialog.setLocation(x,y);
-
- Button ok = new Button (dialog, SWT.PUSH);
- ok.setText ("OK");
- ok.setLayoutData(new GridData (SWT.FILL, SWT.CENTER, false, false));
- ok.addSelectionListener (new SelectionAdapter () {
- public void widgetSelected (SelectionEvent e) {
- field.setText( formatter.format( new Date(calendar.getYear()-1900, calendar.getMonth(), calendar.getDay())));
+ int y = p2.y + 20;
+
+ dialog.setLocation( x,
+ y );
+
+ Button ok = new Button( dialog,
+ SWT.PUSH );
+ ok.setText( "OK" );
+ ok.setLayoutData( new GridData( SWT.FILL,
+ SWT.CENTER,
+ false,
+ false ) );
+ ok.addSelectionListener( new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ field.setText( formatter.format( new Date( calendar.getYear() - 1900,
+ calendar.getMonth(),
+ calendar.getDay() ) ) );
updateSentence();
modeller.setDirty( true );
- dialog.close ();
+ dialog.close();
}
- });
- dialog.setDefaultButton (ok);
- dialog.pack ();
- dialog.open ();
+ } );
+ dialog.setDefaultButton( ok );
+ dialog.pack();
+ dialog.open();
}
- });
-
+ } );
+
control.layout();
-
+
}
-
+
public Control getControl() {
return control;
}
@@ -686,9 +730,10 @@
Date value = new Date();
try {
String txt = field.getText();
- if (txt!=null && txt.length()>0) {
- value = formatter.parse(txt);
- } } catch ( ParseException e ) {
+ if ( txt != null && txt.length() > 0 ) {
+ value = formatter.parse( txt );
+ }
+ } catch ( ParseException e ) {
e.printStackTrace();
return "";
}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/FactPatternWidget.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/FactPatternWidget.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/FactPatternWidget.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -1,644 +1,653 @@
-package org.drools.eclipse.rulebuilder.ui;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
-import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
-import org.drools.guvnor.client.modeldriven.brl.CompositeFieldConstraint;
-import org.drools.guvnor.client.modeldriven.brl.ConnectiveConstraint;
-import org.drools.guvnor.client.modeldriven.brl.FactPattern;
-import org.drools.guvnor.client.modeldriven.brl.FieldConstraint;
-import org.drools.guvnor.client.modeldriven.brl.ISingleFieldConstraint;
-import org.drools.guvnor.client.modeldriven.brl.SingleFieldConstraint;
-import org.drools.eclipse.rulebuilder.modeldriven.HumanReadable;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.ModifyEvent;
-import org.eclipse.swt.events.ModifyListener;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.MessageBox;
-import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.forms.events.HyperlinkEvent;
-import org.eclipse.ui.forms.events.IHyperlinkListener;
-import org.eclipse.ui.forms.widgets.FormToolkit;
-import org.eclipse.ui.forms.widgets.ImageHyperlink;
-
-/**
- * This is the new smart widget that works off the model.
- *
- * @author Michael Neale
- * @author Ahti Kitsik
- * @author Anton Arhipov
- *
- */
-public class FactPatternWidget extends Widget {
-
- private final CompositeFactPattern parentPattern;
-
- private final FactPattern pattern;
-
- private boolean bindable;
-
- public FactPatternWidget(FormToolkit toolkit,
- Composite parent,
- RuleModeller mod,
- FactPattern factPattern,
- CompositeFactPattern parentPattern,
- int idx,
- boolean canBind) {
-
- super( parent,
- toolkit,
- mod,
- idx );
-
- this.pattern = factPattern;
- this.parentPattern = parentPattern;
- this.bindable = canBind;
-
- GridLayout l = new GridLayout();
- l.numColumns = 4;
- l.marginBottom = 0;
- l.marginHeight = 0;
- l.marginLeft = 0;
- l.marginRight = 0;
- l.marginTop = 0;
- l.marginWidth = 0;
- l.verticalSpacing = 0;
- parent.setLayout( l );
-
- create();
- }
-
- private void create() {
- Label l = toolkit.createLabel( parent,
- getPatternLabel() );
-
- GridData labelGD = new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL);
- labelGD.horizontalSpan = 2;
- //labelGD.verticalAlignment = SWT.CENTER;
- //labelGD.horizontalAlignment = SWT.CENTER;
- l.setLayoutData(labelGD);
- l.setBackground(new Color(parent.getShell().getDisplay(),240,240,240));
-
- addDeleteAction();
- addMoreOptionsAction();
-
- Composite constraintComposite = toolkit.createComposite( parent );
- GridLayout constraintLayout = new GridLayout();
- constraintLayout.numColumns = 8;
- constraintComposite.setLayout( constraintLayout );
-
- for ( int row = 0; row < pattern.getFieldConstraints().length; row++ ) {
- renderFieldConstraints( constraintComposite,
- pattern.getFieldConstraints()[row],
- null,
- row,
- true,
- false );
- }
-
- toolkit.paintBordersFor( constraintComposite );
- }
-
- private void addMoreOptionsAction() {
- ImageHyperlink link = addImage( parent,
- "icons/new_item.gif" );
-
- link.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- RuleDialog popup = new AddNewFieldConstraintDialog( parent.getShell(),
- toolkit,
- getModeller(),
- pattern,
- parentPattern != null );
- popup.open();
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
- link.setToolTipText( "Add a field to this condition, or bind a varible to this fact." );
- }
-
- private void addDeleteAction() {
- ImageHyperlink delWholeLink = addImage( parent,
- "icons/delete_obj.gif" );
- delWholeLink.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
- SWT.YES | SWT.NO | SWT.ICON_WARNING );
- dialog.setMessage( "Remove this ENTIRE condition, " + "and all the field constraints that belong to it." );
- dialog.setText( "Remove this entire condition?" );
- if ( dialog.open() == SWT.YES ) {
- if ( parentPattern == null ) {
- if ( getModeller().getModel().removeLhsItem( index ) ) {
- getModeller().reloadLhs();
- } else {
- showMessage( "Can't remove that item as it is used in the action part of the rule." );
- }
- } else {
- deleteBindedFact();
- }
- getModeller().setDirty( true );
- }
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
- delWholeLink.setToolTipText( "Remove this condition." );
- }
-
- private void renderFieldConstraints(Composite constraintComposite,
- FieldConstraint constraint,
- final CompositeFieldConstraint parentConstraint,
- int row,
- boolean showBinding,
- boolean nested) {
- if ( constraint instanceof SingleFieldConstraint ) {
- renderSingleFieldConstraint( constraintComposite,
- row,
- constraint,
- parentConstraint,
- showBinding,
- nested );
- } else if ( constraint instanceof CompositeFieldConstraint ) {
- compositeFieldConstraintEditor( constraintComposite,
- (CompositeFieldConstraint) constraint,
- parentConstraint,
- row,
- nested );
- }
- }
-
- private void compositeFieldConstraintEditor(Composite constraintComposite,
- final CompositeFieldConstraint constraint,
- final CompositeFieldConstraint parentConstraint,
- final int row,
- boolean nested) {
-
- // Label
- if ( constraint.compositeJunctionType.equals( CompositeFieldConstraint.COMPOSITE_TYPE_AND ) ) {
- toolkit.createLabel( constraintComposite,
- "All of:" );
- } else {
- toolkit.createLabel( constraintComposite,
- "Any of:" );
- }
-
- addRemoveButton( constraintComposite,
- parentConstraint,
- row,
- "icons/delete_obj.gif",
- nested );
-
- // button "add"
- ImageHyperlink link = addImage( constraintComposite,
- "icons/new_item.gif" );
- link.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- RuleDialog popup = new AddCompositeConstraintOptionDialog( parent.getShell(),
- getModeller(),
- constraint,
- pattern );
- popup.open();
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
-
- link.setToolTipText("Add fields to this constriant.");
-
- addNestedElements( constraintComposite,
- constraint );
- }
-
- private void addNestedElements(Composite constraintComposite,
- final CompositeFieldConstraint constraint) {
- // Nested elementss
- FieldConstraint[] nestedConstraints = constraint.constraints;
- if ( nestedConstraints != null ) {
- Composite nestedComposite = toolkit.createComposite( constraintComposite );
- GridData gd = new GridData( GridData.FILL_HORIZONTAL );
- gd.horizontalSpan = 5;
- nestedComposite.setLayoutData( gd );
-
- GridLayout l = new GridLayout();
- l.numColumns = 8;
- l.marginBottom = 0;
- l.marginHeight = 0;
- l.marginLeft = 0;
- l.marginRight = 0;
- l.marginTop = 0;
- l.marginWidth = 0;
- l.verticalSpacing = 0;
- nestedComposite.setLayout( l );
-
- for ( int i = 0; i < nestedConstraints.length; i++ ) {
- renderFieldConstraints( nestedComposite,
- nestedConstraints[i],
- constraint,
- i,
- false,
- true );
- toolkit.paintBordersFor( nestedComposite );
- }
- } else {
- GridData gd = new GridData( GridData.FILL_HORIZONTAL );
- gd.horizontalSpan = 5;
- Label dummyLabel = toolkit.createLabel( constraintComposite,
- "" ); // dummy
- dummyLabel.setLayoutData( gd );
- }
- }
-
- private void renderSingleFieldConstraint(Composite constraintComposite,
- int row,
- FieldConstraint constraint,
- CompositeFieldConstraint parentConstraint,
- boolean showBinding,
- boolean nested) {
- final SingleFieldConstraint c = (SingleFieldConstraint) constraint;
- if ( c.constraintValueType != ISingleFieldConstraint.TYPE_PREDICATE ) {
- createConstraintRow( constraintComposite,
- parentConstraint,
- row,
- c,
- showBinding,
- nested );
- } else {
- createPredicateConstraintRow( constraintComposite,
- row,
- c );
- }
- }
-
- private void createConstraintRow(Composite constraintComposite,
- CompositeFieldConstraint parentConstraint,
- int row,
- final SingleFieldConstraint c,
- boolean showBinding,
- boolean nested) {
- addBindingField( constraintComposite,
- c,
- showBinding );
- toolkit.createLabel( constraintComposite,
- c.fieldName );
- if ( c.connectives == null || c.connectives.length == 0 ) {
- addRemoveButton( constraintComposite,
- parentConstraint,
- row,
- "icons/delete_item_small.gif",
- nested );
- } else {
- toolkit.createLabel( constraintComposite,
- "" );
- }
- operatorDropDown( constraintComposite,
- c );
-
- constraintValueEditor(constraintComposite, c, c.fieldName);
-
- createConnectives( constraintComposite,
- c );
- addConnectiveAction( constraintComposite,
- c );
- }
-
- private void addBindingField(Composite constraintComposite,
- final SingleFieldConstraint c,
- boolean showBinding) {
- if ( !c.isBound() ) {
- if ( bindable && showBinding ) {
- ImageHyperlink link = addImage( constraintComposite,
- "icons/new_item.gif" );
- link.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- RuleDialog popup = new AssignFieldVariableDialog( parent.getShell(),
- toolkit,
- getModeller(),
- c );
- popup.open();
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
-
- link.setToolTipText("Bind the field called [" + c.fieldName + "] to a variable.");
- } else {
- toolkit.createLabel( constraintComposite,
- "" );
- }
- } else {
- toolkit.createLabel( constraintComposite,
- "[" + c.fieldBinding + "]" );
- }
-
- }
-
- private void createPredicateConstraintRow(Composite constraintComposite,
- int row,
- final SingleFieldConstraint c) {
- GridData gd = new GridData( GridData.FILL_HORIZONTAL );
- gd.horizontalSpan = 6;
- addImage( constraintComposite,
- "icons/function_assets.gif" );
- formulaValueEditor( constraintComposite,
- c,
- gd );
- addRemoveButton( constraintComposite,
- null,
- row,
- "icons/delete_item_small.gif",
- false );
- }
-
- private void createConnectives(Composite parent,
- SingleFieldConstraint c) {
- if ( c.connectives != null && c.connectives.length > 0 ) {
- for ( int i = 0; i < c.connectives.length; i++ ) {
- toolkit.createLabel( parent,
- "" ); // dummy
- toolkit.createLabel( parent,
- "" ); // dummy
- toolkit.createLabel( parent,
- "" ); // dummy
- ConnectiveConstraint con = c.connectives[i];
- addRemoveConstraintAction( parent,
- c,
- con );
- connectiveOperatorDropDown( parent,
- con,
- c.fieldName );
- constraintValueEditor( parent,
- con, c.fieldName );
-
- }
- }
- }
-
- private void constraintValueEditor(Composite parent, ISingleFieldConstraint c, String name ){
- String type = this.modeller.getSuggestionCompletionEngine().getFieldType( pattern.factType, name );
- new ConstraintValueEditor (parent, c, toolkit, modeller, type);
- }
-
-
- private void addConnectiveAction(Composite constraintComposite,
- final SingleFieldConstraint c) {
- ImageHyperlink link = addImage( constraintComposite,
- "icons/add_connective.gif" );
- link.setToolTipText( "Add more options to this fields values." );
- link.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- c.addNewConnective();
- getModeller().reloadLhs();
- getModeller().setDirty( true );
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
-
- link.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING ) );
- }
-
- private void addRemoveButton(Composite constraintComposite,
- final CompositeFieldConstraint parentConstraint,
- final int row,
- String iconRef,
- boolean nested) {
- if ( nested ) {
- addNestedConstraintDeleteAction( constraintComposite,
- parentConstraint,
- row,
- iconRef );
- } else {
- addRemoveFieldAction( constraintComposite,
- row,
- iconRef );
- }
-
- }
-
- private void addNestedConstraintDeleteAction(Composite constraintComposite,
- final CompositeFieldConstraint parentConstraint,
- final int row,
- String iconRef) {
- ImageHyperlink delLink = addImage( constraintComposite,
- iconRef );
- // "icons/delete_obj.gif");
- delLink.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
- SWT.YES | SWT.NO | SWT.ICON_WARNING );
- dialog.setMessage( "Remove this (nested) restriction." );
- dialog.setText( "Remove this item from nested constraint?" );
- if ( dialog.open() == SWT.YES ) {
- parentConstraint.removeConstraint( row );
- getModeller().reloadLhs();
- getModeller().setDirty( true );
- }
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
- }
-
- private void addRemoveFieldAction(Composite constraintComposite,
- final int currentRow,
- String iconRef) {
- ImageHyperlink delLink = addImage( constraintComposite,
- iconRef );
- delLink.setToolTipText( "Remove this fieldconstraint" );
- delLink.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
- SWT.YES | SWT.NO | SWT.ICON_WARNING );
- dialog.setMessage( "Remove this item?" );
- dialog.setText( "Remove this item?" );
- if ( dialog.open() == SWT.YES ) {
- pattern.removeConstraint( currentRow );
- getModeller().reloadLhs();
- getModeller().setDirty( true );
- }
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
- delLink.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING ) );
- }
-
- private void addRemoveConstraintAction(Composite composite,
- final SingleFieldConstraint constraint,
- final ConnectiveConstraint connConstraint) {
- ImageHyperlink delLink = addImage( composite,
- "icons/delete_item_small.gif" );
- delLink.setToolTipText( "Remove this field constraint" );
- delLink.addHyperlinkListener( new IHyperlinkListener() {
- public void linkActivated(HyperlinkEvent e) {
- MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
- SWT.YES | SWT.NO | SWT.ICON_WARNING );
- dialog.setMessage( "Remove this item?" );
- dialog.setText( "Remove this item?" );
- if ( dialog.open() == SWT.YES ) {
- ConnectiveConstraint[] connectives = constraint.connectives;
- List nConnectives = new ArrayList();
- for ( int i = 0; i < connectives.length; i++ ) {
- if ( connectives[i] != connConstraint ) {
- nConnectives.add( connectives[i] );
- }
- }
- constraint.connectives = (ConnectiveConstraint[]) nConnectives.toArray( new ConnectiveConstraint[nConnectives.size()] );
-
- getModeller().reloadLhs();
- getModeller().setDirty( true );
- }
- }
-
- public void linkEntered(HyperlinkEvent e) {
- }
-
- public void linkExited(HyperlinkEvent e) {
- }
- } );
- delLink.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END ) );
- }
-
- /**
- * This returns the pattern label.
- */
- private String getPatternLabel() {
- if ( pattern.boundName != null ) {
- return pattern.factType + " [" + pattern.boundName + "]";
- }
- return pattern.factType;
- }
-
- private void operatorDropDown(Composite parent,
- final SingleFieldConstraint c) {
- String[] ops = getCompletions().getOperatorCompletions( pattern.factType,
- c.fieldName );
- final Combo box = new Combo( parent,
- SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY );
- for ( int i = 0; i < ops.length; i++ ) {
- String op = ops[i];
- box.add( HumanReadable.getOperatorDisplayName( op ) );
- if ( op.equals( c.operator ) ) {
- box.select( i );
- }
- }
- GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
- gridData.horizontalSpan = 2;
- box.setLayoutData( gridData );
- box.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- c.operator = HumanReadable.getOperatorName( box.getText() );
- getModeller().setDirty( true );
- }
- } );
- }
-
- private void connectiveOperatorDropDown(Composite parent,
- final ConnectiveConstraint con,
- String fieldName) {
- String[] ops = getCompletions().getConnectiveOperatorCompletions( pattern.factType,
- fieldName );
- final Combo box = new Combo( parent,
- SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY );
- for ( int i = 0; i < ops.length; i++ ) {
- String op = ops[i];
- box.add( HumanReadable.getOperatorDisplayName( op ) );
- if ( op.equals( con.operator ) ) {
- box.select( i );
- }
- }
- GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
- gridData.horizontalSpan = 2;
- box.setLayoutData( gridData );
- box.addModifyListener( new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- con.operator = HumanReadable.getOperatorName( box.getText() );
- getModeller().setDirty( true );
-
-
- }
- } );
- }
-
- private void formulaValueEditor(Composite parent,
- final ISingleFieldConstraint c,
- GridData gd) {
-
- final Text box = toolkit.createText( parent,
- "" );
-
- if ( c.value != null ) {
- box.setText( c.value );
- }
-
- gd.grabExcessHorizontalSpace = true;
- gd.minimumWidth = 100;
- box.setLayoutData( gd );
-
- box.addModifyListener( new ModifyListener() {
- public void modifyText(ModifyEvent e) {
- c.value = box.getText();
- getModeller().setDirty( true );
- }
- } );
- }
-
- private void deleteBindedFact() {
- List newPatterns = new ArrayList();
- for ( int i = 0; i < parentPattern.patterns.length; i++ ) {
- if ( parentPattern.patterns[i] != pattern ) {
- newPatterns.add( parentPattern.patterns[i] );
- }
- }
- parentPattern.patterns = (FactPattern[]) newPatterns.toArray( new FactPattern[newPatterns.size()] );
- getModeller().reloadLhs();
- }
-
- private SuggestionCompletionEngine getCompletions() {
- return getModeller().getSuggestionCompletionEngine();
- }
-
-
-
-
-}
+package org.drools.eclipse.rulebuilder.ui;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
+import org.drools.guvnor.client.modeldriven.brl.CompositeFieldConstraint;
+import org.drools.guvnor.client.modeldriven.brl.ConnectiveConstraint;
+import org.drools.guvnor.client.modeldriven.brl.FactPattern;
+import org.drools.guvnor.client.modeldriven.brl.FieldConstraint;
+import org.drools.guvnor.client.modeldriven.brl.ISingleFieldConstraint;
+import org.drools.guvnor.client.modeldriven.brl.SingleFieldConstraint;
+import org.drools.eclipse.rulebuilder.modeldriven.HumanReadable;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.MessageBox;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.forms.events.HyperlinkEvent;
+import org.eclipse.ui.forms.events.IHyperlinkListener;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.eclipse.ui.forms.widgets.ImageHyperlink;
+
+/**
+ * This is the new smart widget that works off the model.
+ *
+ * @author Michael Neale
+ * @author Ahti Kitsik
+ * @author Anton Arhipov
+ *
+ */
+public class FactPatternWidget extends Widget {
+
+ private final CompositeFactPattern parentPattern;
+
+ private final FactPattern pattern;
+
+ private boolean bindable;
+
+ public FactPatternWidget(FormToolkit toolkit,
+ Composite parent,
+ RuleModeller mod,
+ FactPattern factPattern,
+ CompositeFactPattern parentPattern,
+ int idx,
+ boolean canBind) {
+
+ super( parent,
+ toolkit,
+ mod,
+ idx );
+
+ this.pattern = factPattern;
+ this.parentPattern = parentPattern;
+ this.bindable = canBind;
+
+ GridLayout l = new GridLayout();
+ l.numColumns = 4;
+ l.marginBottom = 0;
+ l.marginHeight = 0;
+ l.marginLeft = 0;
+ l.marginRight = 0;
+ l.marginTop = 0;
+ l.marginWidth = 0;
+ l.verticalSpacing = 0;
+ parent.setLayout( l );
+
+ create();
+ }
+
+ private void create() {
+ Label l = toolkit.createLabel( parent,
+ getPatternLabel() );
+
+ GridData labelGD = new GridData( GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL );
+ labelGD.horizontalSpan = 2;
+ //labelGD.verticalAlignment = SWT.CENTER;
+ //labelGD.horizontalAlignment = SWT.CENTER;
+ l.setLayoutData( labelGD );
+ l.setBackground( new Color( parent.getShell().getDisplay(),
+ 240,
+ 240,
+ 240 ) );
+
+ addDeleteAction();
+ addMoreOptionsAction();
+
+ Composite constraintComposite = toolkit.createComposite( parent );
+ GridLayout constraintLayout = new GridLayout();
+ constraintLayout.numColumns = 8;
+ constraintComposite.setLayout( constraintLayout );
+
+ for ( int row = 0; row < pattern.getFieldConstraints().length; row++ ) {
+ renderFieldConstraints( constraintComposite,
+ pattern.getFieldConstraints()[row],
+ null,
+ row,
+ true,
+ false );
+ }
+
+ toolkit.paintBordersFor( constraintComposite );
+ }
+
+ private void addMoreOptionsAction() {
+ ImageHyperlink link = addImage( parent,
+ "icons/new_item.gif" );
+
+ link.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ RuleDialog popup = new AddNewFieldConstraintDialog( parent.getShell(),
+ toolkit,
+ getModeller(),
+ pattern,
+ parentPattern != null );
+ popup.open();
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ link.setToolTipText( "Add a field to this condition, or bind a varible to this fact." );
+ }
+
+ private void addDeleteAction() {
+ ImageHyperlink delWholeLink = addImage( parent,
+ "icons/delete_obj.gif" );
+ delWholeLink.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
+ SWT.YES | SWT.NO | SWT.ICON_WARNING );
+ dialog.setMessage( "Remove this ENTIRE condition, " + "and all the field constraints that belong to it." );
+ dialog.setText( "Remove this entire condition?" );
+ if ( dialog.open() == SWT.YES ) {
+ if ( parentPattern == null ) {
+ if ( getModeller().getModel().removeLhsItem( index ) ) {
+ getModeller().reloadLhs();
+ } else {
+ showMessage( "Can't remove that item as it is used in the action part of the rule." );
+ }
+ } else {
+ deleteBindedFact();
+ }
+ getModeller().setDirty( true );
+ }
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ delWholeLink.setToolTipText( "Remove this condition." );
+ }
+
+ private void renderFieldConstraints(Composite constraintComposite,
+ FieldConstraint constraint,
+ final CompositeFieldConstraint parentConstraint,
+ int row,
+ boolean showBinding,
+ boolean nested) {
+ if ( constraint instanceof SingleFieldConstraint ) {
+ renderSingleFieldConstraint( constraintComposite,
+ row,
+ constraint,
+ parentConstraint,
+ showBinding,
+ nested );
+ } else if ( constraint instanceof CompositeFieldConstraint ) {
+ compositeFieldConstraintEditor( constraintComposite,
+ (CompositeFieldConstraint) constraint,
+ parentConstraint,
+ row,
+ nested );
+ }
+ }
+
+ private void compositeFieldConstraintEditor(Composite constraintComposite,
+ final CompositeFieldConstraint constraint,
+ final CompositeFieldConstraint parentConstraint,
+ final int row,
+ boolean nested) {
+
+ // Label
+ if ( constraint.compositeJunctionType.equals( CompositeFieldConstraint.COMPOSITE_TYPE_AND ) ) {
+ toolkit.createLabel( constraintComposite,
+ "All of:" );
+ } else {
+ toolkit.createLabel( constraintComposite,
+ "Any of:" );
+ }
+
+ addRemoveButton( constraintComposite,
+ parentConstraint,
+ row,
+ "icons/delete_obj.gif",
+ nested );
+
+ // button "add"
+ ImageHyperlink link = addImage( constraintComposite,
+ "icons/new_item.gif" );
+ link.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ RuleDialog popup = new AddCompositeConstraintOptionDialog( parent.getShell(),
+ getModeller(),
+ constraint,
+ pattern );
+ popup.open();
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+
+ link.setToolTipText( "Add fields to this constriant." );
+
+ addNestedElements( constraintComposite,
+ constraint );
+ }
+
+ private void addNestedElements(Composite constraintComposite,
+ final CompositeFieldConstraint constraint) {
+ // Nested elementss
+ FieldConstraint[] nestedConstraints = constraint.constraints;
+ if ( nestedConstraints != null ) {
+ Composite nestedComposite = toolkit.createComposite( constraintComposite );
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL );
+ gd.horizontalSpan = 5;
+ nestedComposite.setLayoutData( gd );
+
+ GridLayout l = new GridLayout();
+ l.numColumns = 8;
+ l.marginBottom = 0;
+ l.marginHeight = 0;
+ l.marginLeft = 0;
+ l.marginRight = 0;
+ l.marginTop = 0;
+ l.marginWidth = 0;
+ l.verticalSpacing = 0;
+ nestedComposite.setLayout( l );
+
+ for ( int i = 0; i < nestedConstraints.length; i++ ) {
+ renderFieldConstraints( nestedComposite,
+ nestedConstraints[i],
+ constraint,
+ i,
+ false,
+ true );
+ toolkit.paintBordersFor( nestedComposite );
+ }
+ } else {
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL );
+ gd.horizontalSpan = 5;
+ Label dummyLabel = toolkit.createLabel( constraintComposite,
+ "" ); // dummy
+ dummyLabel.setLayoutData( gd );
+ }
+ }
+
+ private void renderSingleFieldConstraint(Composite constraintComposite,
+ int row,
+ FieldConstraint constraint,
+ CompositeFieldConstraint parentConstraint,
+ boolean showBinding,
+ boolean nested) {
+ final SingleFieldConstraint c = (SingleFieldConstraint) constraint;
+ if ( c.constraintValueType != ISingleFieldConstraint.TYPE_PREDICATE ) {
+ createConstraintRow( constraintComposite,
+ parentConstraint,
+ row,
+ c,
+ showBinding,
+ nested );
+ } else {
+ createPredicateConstraintRow( constraintComposite,
+ row,
+ c );
+ }
+ }
+
+ private void createConstraintRow(Composite constraintComposite,
+ CompositeFieldConstraint parentConstraint,
+ int row,
+ final SingleFieldConstraint c,
+ boolean showBinding,
+ boolean nested) {
+ addBindingField( constraintComposite,
+ c,
+ showBinding );
+ toolkit.createLabel( constraintComposite,
+ c.fieldName );
+ if ( c.connectives == null || c.connectives.length == 0 ) {
+ addRemoveButton( constraintComposite,
+ parentConstraint,
+ row,
+ "icons/delete_item_small.gif",
+ nested );
+ } else {
+ toolkit.createLabel( constraintComposite,
+ "" );
+ }
+ operatorDropDown( constraintComposite,
+ c );
+
+ constraintValueEditor( constraintComposite,
+ c,
+ c.fieldName );
+
+ createConnectives( constraintComposite,
+ c );
+ addConnectiveAction( constraintComposite,
+ c );
+ }
+
+ private void addBindingField(Composite constraintComposite,
+ final SingleFieldConstraint c,
+ boolean showBinding) {
+ if ( !c.isBound() ) {
+ if ( bindable && showBinding ) {
+ ImageHyperlink link = addImage( constraintComposite,
+ "icons/new_item.gif" );
+ link.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ RuleDialog popup = new AssignFieldVariableDialog( parent.getShell(),
+ toolkit,
+ getModeller(),
+ c );
+ popup.open();
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+
+ link.setToolTipText( "Bind the field called [" + c.fieldName + "] to a variable." );
+ } else {
+ toolkit.createLabel( constraintComposite,
+ "" );
+ }
+ } else {
+ toolkit.createLabel( constraintComposite,
+ "[" + c.fieldBinding + "]" );
+ }
+
+ }
+
+ private void createPredicateConstraintRow(Composite constraintComposite,
+ int row,
+ final SingleFieldConstraint c) {
+ GridData gd = new GridData( GridData.FILL_HORIZONTAL );
+ gd.horizontalSpan = 6;
+ addImage( constraintComposite,
+ "icons/function_assets.gif" );
+ formulaValueEditor( constraintComposite,
+ c,
+ gd );
+ addRemoveButton( constraintComposite,
+ null,
+ row,
+ "icons/delete_item_small.gif",
+ false );
+ }
+
+ private void createConnectives(Composite parent,
+ SingleFieldConstraint c) {
+ if ( c.connectives != null && c.connectives.length > 0 ) {
+ for ( int i = 0; i < c.connectives.length; i++ ) {
+ toolkit.createLabel( parent,
+ "" ); // dummy
+ toolkit.createLabel( parent,
+ "" ); // dummy
+ toolkit.createLabel( parent,
+ "" ); // dummy
+ ConnectiveConstraint con = c.connectives[i];
+ addRemoveConstraintAction( parent,
+ c,
+ con );
+ connectiveOperatorDropDown( parent,
+ con,
+ c.fieldName );
+ constraintValueEditor( parent,
+ con,
+ c.fieldName );
+
+ }
+ }
+ }
+
+ private void constraintValueEditor(Composite parent,
+ ISingleFieldConstraint c,
+ String name) {
+ String type = this.modeller.getSuggestionCompletionEngine().getFieldType( pattern.factType,
+ name );
+ new ConstraintValueEditor( parent,
+ c,
+ toolkit,
+ modeller,
+ type,
+ pattern );
+ }
+
+ private void addConnectiveAction(Composite constraintComposite,
+ final SingleFieldConstraint c) {
+ ImageHyperlink link = addImage( constraintComposite,
+ "icons/add_connective.gif" );
+ link.setToolTipText( "Add more options to this fields values." );
+ link.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ c.addNewConnective();
+ getModeller().reloadLhs();
+ getModeller().setDirty( true );
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+
+ link.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING ) );
+ }
+
+ private void addRemoveButton(Composite constraintComposite,
+ final CompositeFieldConstraint parentConstraint,
+ final int row,
+ String iconRef,
+ boolean nested) {
+ if ( nested ) {
+ addNestedConstraintDeleteAction( constraintComposite,
+ parentConstraint,
+ row,
+ iconRef );
+ } else {
+ addRemoveFieldAction( constraintComposite,
+ row,
+ iconRef );
+ }
+
+ }
+
+ private void addNestedConstraintDeleteAction(Composite constraintComposite,
+ final CompositeFieldConstraint parentConstraint,
+ final int row,
+ String iconRef) {
+ ImageHyperlink delLink = addImage( constraintComposite,
+ iconRef );
+ // "icons/delete_obj.gif");
+ delLink.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
+ SWT.YES | SWT.NO | SWT.ICON_WARNING );
+ dialog.setMessage( "Remove this (nested) restriction." );
+ dialog.setText( "Remove this item from nested constraint?" );
+ if ( dialog.open() == SWT.YES ) {
+ parentConstraint.removeConstraint( row );
+ getModeller().reloadLhs();
+ getModeller().setDirty( true );
+ }
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ }
+
+ private void addRemoveFieldAction(Composite constraintComposite,
+ final int currentRow,
+ String iconRef) {
+ ImageHyperlink delLink = addImage( constraintComposite,
+ iconRef );
+ delLink.setToolTipText( "Remove this fieldconstraint" );
+ delLink.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
+ SWT.YES | SWT.NO | SWT.ICON_WARNING );
+ dialog.setMessage( "Remove this item?" );
+ dialog.setText( "Remove this item?" );
+ if ( dialog.open() == SWT.YES ) {
+ pattern.removeConstraint( currentRow );
+ getModeller().reloadLhs();
+ getModeller().setDirty( true );
+ }
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ delLink.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_BEGINNING ) );
+ }
+
+ private void addRemoveConstraintAction(Composite composite,
+ final SingleFieldConstraint constraint,
+ final ConnectiveConstraint connConstraint) {
+ ImageHyperlink delLink = addImage( composite,
+ "icons/delete_item_small.gif" );
+ delLink.setToolTipText( "Remove this field constraint" );
+ delLink.addHyperlinkListener( new IHyperlinkListener() {
+ public void linkActivated(HyperlinkEvent e) {
+ MessageBox dialog = new MessageBox( Display.getCurrent().getActiveShell(),
+ SWT.YES | SWT.NO | SWT.ICON_WARNING );
+ dialog.setMessage( "Remove this item?" );
+ dialog.setText( "Remove this item?" );
+ if ( dialog.open() == SWT.YES ) {
+ ConnectiveConstraint[] connectives = constraint.connectives;
+ List nConnectives = new ArrayList();
+ for ( int i = 0; i < connectives.length; i++ ) {
+ if ( connectives[i] != connConstraint ) {
+ nConnectives.add( connectives[i] );
+ }
+ }
+ constraint.connectives = (ConnectiveConstraint[]) nConnectives.toArray( new ConnectiveConstraint[nConnectives.size()] );
+
+ getModeller().reloadLhs();
+ getModeller().setDirty( true );
+ }
+ }
+
+ public void linkEntered(HyperlinkEvent e) {
+ }
+
+ public void linkExited(HyperlinkEvent e) {
+ }
+ } );
+ delLink.setLayoutData( new GridData( GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END ) );
+ }
+
+ /**
+ * This returns the pattern label.
+ */
+ private String getPatternLabel() {
+ if ( pattern.boundName != null ) {
+ return pattern.factType + " [" + pattern.boundName + "]";
+ }
+ return pattern.factType;
+ }
+
+ private void operatorDropDown(Composite parent,
+ final SingleFieldConstraint c) {
+ String[] ops = getCompletions().getOperatorCompletions( pattern.factType,
+ c.fieldName );
+ final Combo box = new Combo( parent,
+ SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY );
+ for ( int i = 0; i < ops.length; i++ ) {
+ String op = ops[i];
+ box.add( HumanReadable.getOperatorDisplayName( op ) );
+ if ( op.equals( c.operator ) ) {
+ box.select( i );
+ }
+ }
+ GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
+ gridData.horizontalSpan = 2;
+ box.setLayoutData( gridData );
+ box.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ c.operator = HumanReadable.getOperatorName( box.getText() );
+ getModeller().setDirty( true );
+ }
+ } );
+ }
+
+ private void connectiveOperatorDropDown(Composite parent,
+ final ConnectiveConstraint con,
+ String fieldName) {
+ String[] ops = getCompletions().getConnectiveOperatorCompletions( pattern.factType,
+ fieldName );
+ final Combo box = new Combo( parent,
+ SWT.SIMPLE | SWT.DROP_DOWN | SWT.READ_ONLY );
+ for ( int i = 0; i < ops.length; i++ ) {
+ String op = ops[i];
+ box.add( HumanReadable.getOperatorDisplayName( op ) );
+ if ( op.equals( con.operator ) ) {
+ box.select( i );
+ }
+ }
+ GridData gridData = new GridData( GridData.FILL_HORIZONTAL );
+ gridData.horizontalSpan = 2;
+ box.setLayoutData( gridData );
+ box.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ con.operator = HumanReadable.getOperatorName( box.getText() );
+ getModeller().setDirty( true );
+
+ }
+ } );
+ }
+
+ private void formulaValueEditor(Composite parent,
+ final ISingleFieldConstraint c,
+ GridData gd) {
+
+ final Text box = toolkit.createText( parent,
+ "" );
+
+ if ( c.value != null ) {
+ box.setText( c.value );
+ }
+
+ gd.grabExcessHorizontalSpace = true;
+ gd.minimumWidth = 100;
+ box.setLayoutData( gd );
+
+ box.addModifyListener( new ModifyListener() {
+ public void modifyText(ModifyEvent e) {
+ c.value = box.getText();
+ getModeller().setDirty( true );
+ }
+ } );
+ }
+
+ private void deleteBindedFact() {
+ List newPatterns = new ArrayList();
+ for ( int i = 0; i < parentPattern.patterns.length; i++ ) {
+ if ( parentPattern.patterns[i] != pattern ) {
+ newPatterns.add( parentPattern.patterns[i] );
+ }
+ }
+ parentPattern.patterns = (FactPattern[]) newPatterns.toArray( new FactPattern[newPatterns.size()] );
+ getModeller().reloadLhs();
+ }
+
+ private SuggestionCompletionEngine getCompletions() {
+ return getModeller().getSuggestionCompletionEngine();
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/RuleModeller.java
===================================================================
--- labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/RuleModeller.java 2009-03-26 04:13:02 UTC (rev 25835)
+++ labs/jbossrules/trunk/drools-eclipse/org.drools.eclipse/src/main/java/org/drools/eclipse/rulebuilder/ui/RuleModeller.java 2009-03-26 06:20:00 UTC (rev 25836)
@@ -1,387 +1,401 @@
-package org.drools.eclipse.rulebuilder.ui;
-
-import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
-import org.drools.guvnor.client.modeldriven.brl.ActionInsertFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionRetractFact;
-import org.drools.guvnor.client.modeldriven.brl.ActionSetField;
-import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
-import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
-import org.drools.guvnor.client.modeldriven.brl.FactPattern;
-import org.drools.guvnor.client.modeldriven.brl.IAction;
-import org.drools.guvnor.client.modeldriven.brl.IPattern;
-import org.drools.guvnor.client.modeldriven.brl.RuleModel;
-import org.drools.eclipse.rulebuilder.editors.RuleEditor;
-import org.eclipse.jface.dialogs.Dialog;
-import org.eclipse.jface.window.Window;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.Font;
-import org.eclipse.swt.graphics.FontData;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.ToolBar;
-import org.eclipse.swt.widgets.ToolItem;
-import org.eclipse.ui.ISharedImages;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.forms.FormColors;
-import org.eclipse.ui.forms.widgets.ColumnLayout;
-import org.eclipse.ui.forms.widgets.ExpandableComposite;
-import org.eclipse.ui.forms.widgets.FormToolkit;
-import org.eclipse.ui.forms.widgets.ScrolledForm;
-import org.eclipse.ui.forms.widgets.Section;
-
-/**
- * Main modeling class responsible for Eclipse Forms-based rule builder widget
- * rendering
- *
- * @author Anton Arhipov
- * @author Ahti Kitsik
- *
- */
-public class RuleModeller {
-
- private Composite ifComposite;
-
- private Composite thenComposite;
-
- private Composite optionsComposite;
-
- private final ScrolledForm form;
-
- private final FormToolkit toolkit;
-
- private RuleModel model;
-
- private boolean dirty;
-
- private RuleEditor editor;
-
- public boolean isDirty() {
- return dirty;
- }
-
- public void setDirty(boolean dirty) {
- this.dirty = dirty;
- editor.dirtyPropertyChanged();
- }
-
- public RuleModeller(ScrolledForm form,
- FormToolkit toolkit,
- RuleModel model,
- RuleEditor editor) {
-
- this.form = form;
- this.toolkit = toolkit;
- this.model = model;
- this.editor = editor;
-
- setTitleAndFont(form);
-
- ColumnLayout colLayout = new ColumnLayout();
- colLayout.minNumColumns = 1;
- colLayout.maxNumColumns = 1;
-
- form.getBody().setLayout( colLayout );
-
- // addToolBar(toolkit, form);
-
- Shell shell = new Shell( Display.getCurrent() );
- Window conditionPopup = new AddNewConditionDialog( shell,
- this );
- Window actionPopup = new AddNewActionDialog( shell,
- this );
-
- Window optionsPopup = new RuleAttributesDialog( shell,
- this );
-
- Section ifSection = createMainSection( form,
- toolkit,
- "WHEN",
- conditionPopup );
- Section thenSection = createMainSection( form,
- toolkit,
- "THEN",
- actionPopup );
- Section optionsSection = createMainSection( form,
- toolkit,
- "(options)",
- optionsPopup );
-
- ColumnLayout layout = new ColumnLayout();
- layout.minNumColumns = 1;
- layout.maxNumColumns = 1;
- // layout.verticalSpacing = 0;
-
- ((Composite) (ifSection.getClient())).setLayout( layout );
- ((Composite) (thenSection.getClient())).setLayout( layout );
- ((Composite) (optionsSection.getClient())).setLayout( layout );
- ifSection.setLayout( layout );
- thenSection.setLayout( layout );
- optionsSection.setLayout( layout );
-
- ifComposite = (Composite) ifSection.getClient();
- thenComposite = (Composite) thenSection.getClient();
- optionsComposite = (Composite) optionsSection.getClient();
-
- }
-
- private void setTitleAndFont(ScrolledForm form) {
- form.setText( "Guided rule editor" );
-
- Font systemFont = form.getDisplay().getSystemFont();
- FontData[] exfds = systemFont.getFontData();
- if ( exfds.length > 0 ) {
- FontData fd = exfds[0];
- fd.setHeight( fd.getHeight() + 2 );
- fd.setStyle(SWT.BOLD);
- Font f = new Font( systemFont.getDevice(),
- fd );
- form.setFont(f);
- }
- }
-
- public SuggestionCompletionEngine getSuggestionCompletionEngine() {
- return editor.getCompletionEngine();
- }
-
- public RuleModel getModel() {
- return model;
- }
-
- public void setModel(RuleModel model) {
- this.model = model;
- }
-
- private void clearComposite(Composite composite) {
- if ( composite != null ) {
- Control[] c = composite.getChildren();
- for ( int i = 0; i < c.length; i++ ) {
- Control c2 = c[i];
- c2.dispose();
- }
- }
- }
-
- private void reloadCommon() {
- toolkit.paintBordersFor( form.getBody() );
- form.redraw();
- Dialog.applyDialogFont( form.getBody() );
- form.reflow( true );
- }
-
- public void reloadRhs() {
- clearComposite( thenComposite );
- redrawRhs();
- reloadCommon();
- }
-
- public void reloadLhs() {
- clearComposite( ifComposite );
- redrawLhs();
- reloadCommon();
- }
-
- public void reloadOptions() {
- clearComposite( optionsComposite );
- redrawOptions();
- reloadCommon();
- }
-
- public void reloadWidgets() {
- reloadLhs();
- reloadRhs();
- reloadOptions();
- }
-
- private void redrawOptions() {
- Composite comp = toolkit.createComposite( optionsComposite );
- new RuleAttributeWidget( toolkit,
- comp,
- this );
- }
-
- private void redrawRhs() {
- for ( int i = 0; i < model.rhs.length; i++ ) {
- IAction action = model.rhs[i];
-
- if ( action instanceof ActionSetField ) {
- addActionSetFieldWidget( action,
- i );
- } else if ( action instanceof ActionInsertFact ) {
- addActionInsertFactWidget( action,
- i );
- } else if ( action instanceof ActionRetractFact ) {
- addActionRetractFactWidget( action,
- i );
- } else if ( action instanceof DSLSentence ) {
- addRHSDSLSentenceWidget( i,
- (DSLSentence) action );
- }
-
- }
- }
-
- private void addActionInsertFactWidget(IAction action,
- int i) {
- Composite comp = toolkit.createComposite( thenComposite );
- new ActionInsertFactWidget( toolkit,
- comp,
- this,
- (ActionInsertFact) action,
- i );
- }
-
- private void redrawLhs() {
- for ( int i = 0; i < model.lhs.length; i++ ) {
- IPattern pattern = model.lhs[i];
-
- if ( pattern instanceof FactPattern ) {
- addFactPatternWidget( i,
- (FactPattern) pattern );
- }
- if ( pattern instanceof CompositeFactPattern ) {
- addCompositeFactPatternWidget( i,
- (CompositeFactPattern) pattern );
- } else if ( pattern instanceof DSLSentence ) {
- // skip for now
- } else {
- // dont' care
- }
- }
-
- for ( int i = 0; i < model.lhs.length; i++ ) {
- IPattern pattern = model.lhs[i];
- if ( pattern instanceof DSLSentence ) {
- addLHSDSLSentenceWidget( i,
- (DSLSentence) pattern );
- }
- }
- }
-
- private void addActionRetractFactWidget(IAction action,
- int i) {
- Composite comp = toolkit.createComposite( thenComposite );
- new ActionRetractFactWidget( toolkit,
- comp,
- this,
- (ActionRetractFact) action,
- i );
- }
-
- /*private void addActionAssertFactWidget(IAction action,
- int i) {
- Composite comp = toolkit.createComposite( thenComposite );
- new ActionInsertFactWidget( toolkit,
- comp,
- this,
- (ActionInsertFact) action,
- i );
-
- }*/
-
- private void addActionSetFieldWidget(IAction action,
- int i) {
- Composite comp = toolkit.createComposite( thenComposite );
- new ActionSetFieldWidget( toolkit,
- comp,
- this,
- model,
- (ActionSetField) action,
- i );
- }
-
- private void addRHSDSLSentenceWidget(int idx,
- DSLSentence pattern) {
- Composite comp = toolkit.createComposite( thenComposite );
- new RHSDSLSentenceWidget( toolkit,
- comp,
- pattern,
- this,
- idx);
- }
-
- private void addLHSDSLSentenceWidget(int idx,
- DSLSentence pattern) {
- Composite comp = toolkit.createComposite( ifComposite );
- new LHSDSLSentenceWidget( toolkit,
- comp,
- pattern,
- this,
- idx );
- }
-
- private void addCompositeFactPatternWidget(int idx,
- CompositeFactPattern pattern) {
- Composite comp = toolkit.createComposite( ifComposite );
- new CompositeFactPatternWidget( toolkit,
- comp,
- this,
- pattern,
- idx );
- }
-
- private void addFactPatternWidget(int idx,
- FactPattern pattern) {
- Composite comp = toolkit.createComposite( ifComposite );
- new FactPatternWidget( toolkit,
- comp,
- this,
- pattern,
- null,
- idx,
- true );
- }
-
- private Section createMainSection(final ScrolledForm form,
- FormToolkit toolkit,
- String title,
- Window popup) {
- ColumnLayout layout = new ColumnLayout();
- layout.minNumColumns = 1;
- layout.maxNumColumns = 1;
- Section l1Sect = toolkit.createSection( form.getBody(),
- ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED );
- l1Sect.setActiveToggleColor( toolkit.getHyperlinkGroup().getActiveForeground() );
- l1Sect.setToggleColor( toolkit.getColors().getColor( FormColors.SEPARATOR ) );
- l1Sect.setText( title );
- createAddToolItem( l1Sect,
- popup );
- Composite comp = toolkit.createComposite( l1Sect );
- l1Sect.setClient( comp );
- return l1Sect;
- }
-
- private void createAddToolItem(Section sect,
- final Window popup) {
- ToolBar tbar = new ToolBar( sect,
- SWT.FLAT | SWT.HORIZONTAL );
- ToolItem titem = new ToolItem( tbar,
- SWT.SEPARATOR );
- titem = new ToolItem( tbar,
- SWT.PUSH );
- titem.setImage( PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_TOOL_NEW_WIZARD ) );
-
- titem.addListener( SWT.Selection,
- new Listener() {
- public void handleEvent(Event event) {
- popup.open();
- }
- } );
- sect.setTextClient( tbar );
- }
-
- public void refresh() {
- ifComposite.layout();
- ifComposite.redraw();
-
- thenComposite.layout();
- thenComposite.redraw();
-
- optionsComposite.layout();
- optionsComposite.redraw();
- }
-
-}
+package org.drools.eclipse.rulebuilder.ui;
+
+import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.guvnor.client.modeldriven.brl.ActionInsertFact;
+import org.drools.guvnor.client.modeldriven.brl.ActionRetractFact;
+import org.drools.guvnor.client.modeldriven.brl.ActionSetField;
+import org.drools.guvnor.client.modeldriven.brl.CompositeFactPattern;
+import org.drools.guvnor.client.modeldriven.brl.DSLSentence;
+import org.drools.guvnor.client.modeldriven.brl.FactPattern;
+import org.drools.guvnor.client.modeldriven.brl.FreeFormLine;
+import org.drools.guvnor.client.modeldriven.brl.IAction;
+import org.drools.guvnor.client.modeldriven.brl.IPattern;
+import org.drools.guvnor.client.modeldriven.brl.RuleModel;
+import org.drools.eclipse.rulebuilder.editors.RuleEditor;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
+import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.forms.FormColors;
+import org.eclipse.ui.forms.widgets.ColumnLayout;
+import org.eclipse.ui.forms.widgets.ExpandableComposite;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+import org.eclipse.ui.forms.widgets.ScrolledForm;
+import org.eclipse.ui.forms.widgets.Section;
+
+/**
+ * Main modeling class responsible for Eclipse Forms-based rule builder widget
+ * rendering
+ *
+ * @author Anton Arhipov
+ * @author Ahti Kitsik
+ *
+ */
+public class RuleModeller {
+
+ private Composite ifComposite;
+
+ private Composite thenComposite;
+
+ private Composite optionsComposite;
+
+ private final ScrolledForm form;
+
+ private final FormToolkit toolkit;
+
+ private RuleModel model;
+
+ private boolean dirty;
+
+ private RuleEditor editor;
+
+ public boolean isDirty() {
+ return dirty;
+ }
+
+ public void setDirty(boolean dirty) {
+ this.dirty = dirty;
+ editor.dirtyPropertyChanged();
+ }
+
+ public RuleModeller(ScrolledForm form,
+ FormToolkit toolkit,
+ RuleModel model,
+ RuleEditor editor) {
+
+ this.form = form;
+ this.toolkit = toolkit;
+ this.model = model;
+ this.editor = editor;
+
+ setTitleAndFont( form );
+
+ ColumnLayout colLayout = new ColumnLayout();
+ colLayout.minNumColumns = 1;
+ colLayout.maxNumColumns = 1;
+
+ form.getBody().setLayout( colLayout );
+
+ // addToolBar(toolkit, form);
+
+ Shell shell = new Shell( Display.getCurrent() );
+ Window conditionPopup = new AddNewConditionDialog( shell,
+ this );
+ Window actionPopup = new AddNewActionDialog( shell,
+ this );
+
+ Window optionsPopup = new RuleAttributesDialog( shell,
+ this );
+
+ Section ifSection = createMainSection( form,
+ toolkit,
+ "WHEN",
+ conditionPopup );
+ Section thenSection = createMainSection( form,
+ toolkit,
+ "THEN",
+ actionPopup );
+ Section optionsSection = createMainSection( form,
+ toolkit,
+ "(options)",
+ optionsPopup );
+
+ ColumnLayout layout = new ColumnLayout();
+ layout.minNumColumns = 1;
+ layout.maxNumColumns = 1;
+ // layout.verticalSpacing = 0;
+
+ ((Composite) (ifSection.getClient())).setLayout( layout );
+ ((Composite) (thenSection.getClient())).setLayout( layout );
+ ((Composite) (optionsSection.getClient())).setLayout( layout );
+ ifSection.setLayout( layout );
+ thenSection.setLayout( layout );
+ optionsSection.setLayout( layout );
+
+ ifComposite = (Composite) ifSection.getClient();
+ thenComposite = (Composite) thenSection.getClient();
+ optionsComposite = (Composite) optionsSection.getClient();
+
+ }
+
+ private void setTitleAndFont(ScrolledForm form) {
+ form.setText( "Guided rule editor" );
+
+ Font systemFont = form.getDisplay().getSystemFont();
+ FontData[] exfds = systemFont.getFontData();
+ if ( exfds.length > 0 ) {
+ FontData fd = exfds[0];
+ fd.setHeight( fd.getHeight() + 2 );
+ fd.setStyle( SWT.BOLD );
+ Font f = new Font( systemFont.getDevice(),
+ fd );
+ form.setFont( f );
+ }
+ }
+
+ public SuggestionCompletionEngine getSuggestionCompletionEngine() {
+ return editor.getCompletionEngine();
+ }
+
+ public RuleModel getModel() {
+ return model;
+ }
+
+ public void setModel(RuleModel model) {
+ this.model = model;
+ }
+
+ private void clearComposite(Composite composite) {
+ if ( composite != null ) {
+ Control[] c = composite.getChildren();
+ for ( int i = 0; i < c.length; i++ ) {
+ Control c2 = c[i];
+ c2.dispose();
+ }
+ }
+ }
+
+ private void reloadCommon() {
+ toolkit.paintBordersFor( form.getBody() );
+ form.redraw();
+ Dialog.applyDialogFont( form.getBody() );
+ form.reflow( true );
+ }
+
+ public void reloadRhs() {
+ clearComposite( thenComposite );
+ redrawRhs();
+ reloadCommon();
+ }
+
+ public void reloadLhs() {
+ clearComposite( ifComposite );
+ redrawLhs();
+ reloadCommon();
+ }
+
+ public void reloadOptions() {
+ clearComposite( optionsComposite );
+ redrawOptions();
+ reloadCommon();
+ }
+
+ public void reloadWidgets() {
+ reloadLhs();
+ reloadRhs();
+ reloadOptions();
+ }
+
+ private void redrawOptions() {
+ Composite comp = toolkit.createComposite( optionsComposite );
+ new RuleAttributeWidget( toolkit,
+ comp,
+ this );
+ }
+
+ private void redrawRhs() {
+ for ( int i = 0; i < model.rhs.length; i++ ) {
+ IAction action = model.rhs[i];
+
+ if ( action instanceof ActionSetField ) {
+ addActionSetFieldWidget( action,
+ i );
+ } else if ( action instanceof ActionInsertFact ) {
+ addActionInsertFactWidget( action,
+ i );
+ } else if ( action instanceof ActionRetractFact ) {
+ addActionRetractFactWidget( action,
+ i );
+ } else if ( action instanceof DSLSentence ) {
+ addRHSDSLSentenceWidget( i,
+ (DSLSentence) action );
+ } else if ( action instanceof FreeFormLine ) {
+ addRHSDSLFreeFormLine( (FreeFormLine) action,
+ i );
+ }
+
+ }
+ }
+
+ private void addRHSDSLFreeFormLine(FreeFormLine action,
+ int i) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new ActionInsertFreeFormLineWidget( toolkit,
+ comp,
+ this,
+ (FreeFormLine) action,
+ i );
+ }
+
+ private void addActionInsertFactWidget(IAction action,
+ int i) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new ActionInsertFactWidget( toolkit,
+ comp,
+ this,
+ (ActionInsertFact) action,
+ i );
+ }
+
+ private void redrawLhs() {
+ for ( int i = 0; i < model.lhs.length; i++ ) {
+ IPattern pattern = model.lhs[i];
+
+ if ( pattern instanceof FactPattern ) {
+ addFactPatternWidget( i,
+ (FactPattern) pattern );
+ }
+ if ( pattern instanceof CompositeFactPattern ) {
+ addCompositeFactPatternWidget( i,
+ (CompositeFactPattern) pattern );
+ } else if ( pattern instanceof DSLSentence ) {
+ // skip for now
+ } else {
+ // dont' care
+ }
+ }
+
+ for ( int i = 0; i < model.lhs.length; i++ ) {
+ IPattern pattern = model.lhs[i];
+ if ( pattern instanceof DSLSentence ) {
+ addLHSDSLSentenceWidget( i,
+ (DSLSentence) pattern );
+ }
+ }
+ }
+
+ private void addActionRetractFactWidget(IAction action,
+ int i) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new ActionRetractFactWidget( toolkit,
+ comp,
+ this,
+ (ActionRetractFact) action,
+ i );
+ }
+
+ /*private void addActionAssertFactWidget(IAction action,
+ int i) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new ActionInsertFactWidget( toolkit,
+ comp,
+ this,
+ (ActionInsertFact) action,
+ i );
+
+ }*/
+
+ private void addActionSetFieldWidget(IAction action,
+ int i) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new ActionSetFieldWidget( toolkit,
+ comp,
+ this,
+ model,
+ (ActionSetField) action,
+ i );
+ }
+
+ private void addRHSDSLSentenceWidget(int idx,
+ DSLSentence pattern) {
+ Composite comp = toolkit.createComposite( thenComposite );
+ new RHSDSLSentenceWidget( toolkit,
+ comp,
+ pattern,
+ this,
+ idx );
+ }
+
+ private void addLHSDSLSentenceWidget(int idx,
+ DSLSentence pattern) {
+ Composite comp = toolkit.createComposite( ifComposite );
+ new LHSDSLSentenceWidget( toolkit,
+ comp,
+ pattern,
+ this,
+ idx );
+ }
+
+ private void addCompositeFactPatternWidget(int idx,
+ CompositeFactPattern pattern) {
+ Composite comp = toolkit.createComposite( ifComposite );
+ new CompositeFactPatternWidget( toolkit,
+ comp,
+ this,
+ pattern,
+ idx );
+ }
+
+ private void addFactPatternWidget(int idx,
+ FactPattern pattern) {
+ Composite comp = toolkit.createComposite( ifComposite );
+ new FactPatternWidget( toolkit,
+ comp,
+ this,
+ pattern,
+ null,
+ idx,
+ true );
+ }
+
+ private Section createMainSection(final ScrolledForm form,
+ FormToolkit toolkit,
+ String title,
+ Window popup) {
+ ColumnLayout layout = new ColumnLayout();
+ layout.minNumColumns = 1;
+ layout.maxNumColumns = 1;
+ Section l1Sect = toolkit.createSection( form.getBody(),
+ ExpandableComposite.TWISTIE | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED );
+ l1Sect.setActiveToggleColor( toolkit.getHyperlinkGroup().getActiveForeground() );
+ l1Sect.setToggleColor( toolkit.getColors().getColor( FormColors.SEPARATOR ) );
+ l1Sect.setText( title );
+ createAddToolItem( l1Sect,
+ popup );
+ Composite comp = toolkit.createComposite( l1Sect );
+ l1Sect.setClient( comp );
+ return l1Sect;
+ }
+
+ private void createAddToolItem(Section sect,
+ final Window popup) {
+ ToolBar tbar = new ToolBar( sect,
+ SWT.FLAT | SWT.HORIZONTAL );
+ ToolItem titem = new ToolItem( tbar,
+ SWT.SEPARATOR );
+ titem = new ToolItem( tbar,
+ SWT.PUSH );
+ titem.setImage( PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_TOOL_NEW_WIZARD ) );
+
+ titem.addListener( SWT.Selection,
+ new Listener() {
+ public void handleEvent(Event event) {
+ popup.open();
+ }
+ } );
+ sect.setTextClient( tbar );
+ }
+
+ public void refresh() {
+ ifComposite.layout();
+ ifComposite.redraw();
+
+ thenComposite.layout();
+ thenComposite.redraw();
+
+ optionsComposite.layout();
+ optionsComposite.redraw();
+ }
+
+}
More information about the jboss-svn-commits
mailing list