<commit_before>package org.drools.eclipse.rulebuilder.ui;

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.guvnor.client.modeldriven.brl.SingleFieldConstraint;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
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 ConstraintValueEditor {

    private Composite              composite;

    private ISingleFieldConstraint constraint;

    private FormToolkit            toolkit;

    private RuleModeller           modeller;

    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();
    }

    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 linkExited(HyperlinkEvent e) {
                }
            } );

            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;
            }
        }

    }

    private void literalValueEditor(Composite parent,
                                    final ISingleFieldConstraint c,
                                    GridData gd) {

        String fieldName = ((SingleFieldConstraint) c).fieldName;
        DropDownData enums = null;
        boolean found = false;
        if ( ((SingleFieldConstraint) c).fieldType.equals( SuggestionCompletionEngine.TYPE_BOOLEAN )) {
            enums = DropDownData.create(new String[]{"true", "false"});
        }else
        {
            enums = modeller.getSuggestionCompletionEngine().getEnums( pattern,
                                                                       fieldName );
        }
        if ( enums != null && enums.fixedList.length > 0 ) {
            String[] list = enums.fixedList;
            final Combo combo = new Combo( parent,<commit_msg>JBIDE-5664: Unable to specify additional restriction in when part of Drools rule using Guided Rule Editor  - ConstraintValueEditor now also supports connective constraints<commit_after>package org.drools.eclipse.rulebuilder.ui;

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.ConnectiveConstraint;
import org.drools.guvnor.client.modeldriven.brl.FactPattern;
import org.drools.guvnor.client.modeldriven.brl.ISingleFieldConstraint;
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;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
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 ConstraintValueEditor {

    private Composite              composite;

    private ISingleFieldConstraint constraint;

    private FormToolkit            toolkit;

    private RuleModeller           modeller;

    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();
    }

    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 linkExited(HyperlinkEvent e) {
                }
            } );

            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;
            }
        }

    }

    private void literalValueEditor(Composite parent,
                                    final ISingleFieldConstraint c,
                                    GridData gd) {

        String fieldName = null;
        if (c instanceof SingleFieldConstraint)  {
        	fieldName = ((SingleFieldConstraint) c).fieldName;
        } else if (c instanceof ConnectiveConstraint) {
        	fieldName = ((ConnectiveConstraint) c).fieldName;
        }
        String fieldType = null;
        if (c instanceof SingleFieldConstraint)  {
        	fieldType = ((SingleFieldConstraint) c).fieldType;
        } else if (c instanceof ConnectiveConstraint) {
        	fieldType = ((ConnectiveConstraint) c).fieldType;
        }
        DropDownData enums = null;
        boolean found = false;
        if ( fieldType.equals( SuggestionCompletionEngine.TYPE_BOOLEAN )) {
            enums = DropDownData.create(new String[]{"true", "false"});
        }else
        {
            enums = modeller.getSuggestionCompletionEngine().getEnums( pattern,
                                                                       fieldName );
        }
        if ( enums != null && enums.fixedList.length > 0 ) {
            String[] list = enums.fixedList;
            final Combo combo = new Combo( parent,
{'fraction_non_alphanumeric': 0.06910432740691043, 'fraction_numerical': 0.0006709158000670916, 'mean_word_length': 1.4669011170872983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896991', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2741, 'n_words': 547}