<commit_before>package org.drools.brms.client.qa;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.drools.brms.client.common.DirtyableFlexTable;
import org.drools.brms.client.common.ErrorPopup;
import org.drools.brms.client.common.FormStylePopup;
import org.drools.brms.client.common.ImageButton;
import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
import org.drools.brms.client.modeldriven.testing.ExecutionTrace;
import org.drools.brms.client.modeldriven.testing.FactData;
import org.drools.brms.client.modeldriven.testing.FieldData;
import org.drools.brms.client.modeldriven.testing.Fixture;
import org.drools.brms.client.modeldriven.testing.RetractFact;
import org.drools.brms.client.modeldriven.testing.Scenario;
import org.drools.brms.client.modeldriven.testing.VerifyFact;
import org.drools.brms.client.modeldriven.testing.VerifyField;
import org.drools.brms.client.modeldriven.testing.VerifyRuleFired;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ScenarioWidget extends Composite {

    private DirtyableFlexTable layout;
	private String[] availableRules;
	private Scenario scenario;
	private SuggestionCompletionEngine sce;




	public ScenarioWidget(Scenario scenario, String[] ruleList, SuggestionCompletionEngine eng) {
    	layout = new DirtyableFlexTable();
    	this.availableRules = ruleList;
    	this.scenario = scenario;
    	this.sce = eng;
        render();

        layout.setStyleName("model-builder-Background");


        initWidget(layout);
        setWidth("100%");
        setHeight("100%");

    }


	void render() {
		layout.clear();
		ScenarioHelper hlp = new ScenarioHelper();
		List fixtures = hlp.lumpyMap(scenario.fixtures);



        int layoutRow = 0;

        for (int i = 0; i < fixtures.size(); i++) {
			Object f = fixtures.get(i);
			if (f instanceof ExecutionTrace) {
				layout.setWidget(layoutRow, 0, new Label("EXPECT"));
				ExecutionTrace ex = (ExecutionTrace) f;

				layout.setWidget(layoutRow, 1, new ExecutionWidget(ex));
				layout.setWidget(layoutRow, 2, getNewExpectationButton(ex, scenario, availableRules));
				layout.getFlexCellFormatter().setHorizontalAlignment(layoutRow, 2, HasHorizontalAlignment.ALIGN_LEFT);
			} else if (f instanceof Map) {
				layout.setWidget(layoutRow, 0, new Label("GIVEN"));
				layoutRow++;
				Map facts = (Map) f;
				VerticalPanel vert = new VerticalPanel();
		        for (Iterator iterator = facts.entrySet().iterator(); iterator.hasNext();) {
		            Map.Entry e = (Map.Entry) iterator.next();
		            List factList = (List) facts.get(e.getKey());
		            vert.add(new DataInputWidget((String)e.getKey(), factList, false, scenario, sce, this));
		        }
		        layout.setWidget(layoutRow, 1, vert);
			} else {
				List l = (List) f;
				Fixture first = (Fixture) l.get(0);
				if (first instanceof VerifyFact) {
					doVerifyFacts(l, layout, layoutRow);
				} else if (first instanceof VerifyRuleFired) {
					layout.setWidget(layoutRow, 1, new VerifyRulesFiredWidget(l, availableRules, scenario));
				} else {
					layout.setWidget(layoutRow, 1, new RetractWidget(l));
				}

			}
			layoutRow++;
		}


        layout.setWidget(layoutRow, 0, new Label("Configuration"));
        layoutRow++;
        //config section
        ConfigWidget conf = new ConfigWidget(scenario, availableRules);
        layout.setWidget(layoutRow, 1, conf);

        //global section
        Map globals = hlp.lumpyMapGlobals(scenario.globals);
        VerticalPanel globalPanel = new VerticalPanel();
        for (Iterator iterator = globals.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry e = (Map.Entry) iterator.next();
            globalPanel.add(new DataInputWidget((String)e.getKey(), (List) globals.get(e.getKey()), true, scenario, sce, this));
        }
        layout.setWidget(layoutRow, 0, new Label("Globals"));
        layoutRow++;
        layout.setWidget(layoutRow, 1, globalPanel);
	}


	private Widget getNewExpectationButton(final ExecutionTrace ex,
			final Scenario sc, final String[] ruleList) {

		Image add = new ImageButton("images/new_item.gif", "Add a new expectation.", new ClickListener() {
			public void onClick(Widget w) {
				final FormStylePopup pop = new FormStylePopup("images/rule_asset.gif", "New expectation");

		        final ListBox rules = new ListBox();
		        for (int i = 0; i < ruleList.length; i++) {
		            rules.addItem(ruleList[i]);
		        }
		        pop.addRow(rules);
		        Button ok = new Button("Add");

		        ok.addClickListener(new ClickListener() {
		            public void onClick(Widget w) {
		                String r = rules.getItemText(rules.getSelectedIndex());
		                VerifyRuleFired vr = new VerifyRuleFired(r, null, new Boolean(true));
		                sc.insertAfter(ex, vr);
		                render();
		                pop.hide();
		            }<commit_msg>JBRULES-1271 test scenario editing<commit_after>package org.drools.brms.client.qa;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.drools.brms.client.common.DirtyableFlexTable;
import org.drools.brms.client.common.ErrorPopup;
import org.drools.brms.client.common.FormStylePopup;
import org.drools.brms.client.common.ImageButton;
import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
import org.drools.brms.client.modeldriven.testing.ExecutionTrace;
import org.drools.brms.client.modeldriven.testing.FactData;
import org.drools.brms.client.modeldriven.testing.FieldData;
import org.drools.brms.client.modeldriven.testing.Fixture;
import org.drools.brms.client.modeldriven.testing.RetractFact;
import org.drools.brms.client.modeldriven.testing.Scenario;
import org.drools.brms.client.modeldriven.testing.VerifyFact;
import org.drools.brms.client.modeldriven.testing.VerifyField;
import org.drools.brms.client.modeldriven.testing.VerifyRuleFired;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ScenarioWidget extends Composite {

    private DirtyableFlexTable layout;
	private String[] availableRules;
	private Scenario scenario;
	private SuggestionCompletionEngine sce;




	public ScenarioWidget(Scenario scenario, String[] ruleList, SuggestionCompletionEngine eng) {
    	layout = new DirtyableFlexTable();
    	this.availableRules = ruleList;
    	this.scenario = scenario;
    	this.sce = eng;
        render();

        layout.setStyleName("model-builder-Background");


        initWidget(layout);
        setWidth("100%");
        setHeight("100%");

    }


	void render() {
		layout.clear();
		ScenarioHelper hlp = new ScenarioHelper();
		List fixtures = hlp.lumpyMap(scenario.fixtures);



        int layoutRow = 0;

        for (int i = 0; i < fixtures.size(); i++) {
			Object f = fixtures.get(i);
			if (f instanceof ExecutionTrace) {
				layout.setWidget(layoutRow, 0, new Label("EXPECT"));
				ExecutionTrace ex = (ExecutionTrace) f;

				layout.setWidget(layoutRow, 1, new ExecutionWidget(ex));
				layout.setWidget(layoutRow, 2, getNewExpectationButton(ex, scenario, availableRules));
				layout.getFlexCellFormatter().setHorizontalAlignment(layoutRow, 2, HasHorizontalAlignment.ALIGN_LEFT);
			} else if (f instanceof Map) {
				layout.setWidget(layoutRow, 0, new Label("GIVEN"));
				layoutRow++;
				Map facts = (Map) f;
				VerticalPanel vert = new VerticalPanel();
		        for (Iterator iterator = facts.entrySet().iterator(); iterator.hasNext();) {
		            Map.Entry e = (Map.Entry) iterator.next();
		            List factList = (List) facts.get(e.getKey());
		            vert.add(new DataInputWidget((String)e.getKey(), factList, false, scenario, sce, this));
		        }
		        layout.setWidget(layoutRow, 1, vert);
			} else {
				List l = (List) f;
				Fixture first = (Fixture) l.get(0);
				if (first instanceof VerifyFact) {
					doVerifyFacts(l, layout, layoutRow);
				} else if (first instanceof VerifyRuleFired) {
					layout.setWidget(layoutRow, 1, new VerifyRulesFiredWidget(l, availableRules, scenario));
				} else {
					layout.setWidget(layoutRow, 1, new RetractWidget(l));
				}

			}
			layoutRow++;
		}


        layout.setWidget(layoutRow, 0, new Label("(configuration)"));
        layoutRow++;
        //config section
        ConfigWidget conf = new ConfigWidget(scenario, availableRules);
        layout.setWidget(layoutRow, 1, conf);

        layoutRow++;

        //global section
        Map globals = hlp.lumpyMapGlobals(scenario.globals);
        VerticalPanel globalPanel = new VerticalPanel();
        for (Iterator iterator = globals.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry e = (Map.Entry) iterator.next();
            globalPanel.add(new DataInputWidget((String)e.getKey(), (List) globals.get(e.getKey()), true, scenario, sce, this));
        }
        layout.setWidget(layoutRow, 0, new Label("(globals)"));
        layoutRow++;
        layout.setWidget(layoutRow, 1, globalPanel);
	}


	private Widget getNewExpectationButton(final ExecutionTrace ex,
			final Scenario sc, final String[] ruleList) {

		Image add = new ImageButton("images/new_item.gif", "Add a new expectation.", new ClickListener() {
			public void onClick(Widget w) {
				final FormStylePopup pop = new FormStylePopup("images/rule_asset.gif", "New expectation");

		        final ListBox rules = new ListBox();
		        for (int i = 0; i < ruleList.length; i++) {
		            rules.addItem(ruleList[i]);
		        }
		        pop.addRow(rules);
		        Button ok = new Button("Add");

		        ok.addClickListener(new ClickListener() {
		            public void onClick(Widget w) {
		                String r = rules.getItemText(rules.getSelectedIndex());
		                VerifyRuleFired vr = new VerifyRuleFired(r, null, new Boolean(true));
		                sc.insertAfter(ex, vr);
		                render();
		                pop.hide();
		            }
{'fraction_non_alphanumeric': 0.1230216442809479, 'fraction_numerical': 0.004106424843870305, 'mean_word_length': 3.4669468857470385, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393409', 'n_tokens_mistral': 3865, 'n_tokens_neox': 3546, 'n_words': 712}