Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.workbench.screens.testscenario.client;
 
 import java.util.List;
 
Constraint editor for the FieldData in the Given Section
 
         extends DirtyableComposite
         implements
         HasValueChangeHandlers<String>,
         ScenarioParentWidget {
 
     private FieldData field;
     private IsWidget valueEditorWidget;
     private final Panel panel = new SimplePanel();
     private final FieldConstraintHelper helper;
 
 
     public FieldDataConstraintEditor(String factType,
                                      FieldData field,
                                      Fact givenFact,
                                      PackageDataModelOracle dmo,
                                      Scenario scenario,
                                      ExecutionTrace executionTrace) {
         this. = field;
         this. = new FieldConstraintHelper(scenario,
                 executionTrace,
                 dmo,
                 factType,
                 field,
                 givenFact);
         renderEditor();
         initWidget();
     }
 
     @Override
     public void renderEditor() {
         final String flType = .getFieldType();
         .clear();
 
         if (flType != null && flType.equals(.)) {
             addBooleanEditor();
 
         } else if (flType != null && flType.equals(.)) {
             addDateEditor();
 
         } else {
             final DropDownData dropDownData = .getEnums();
 
             if (dropDownData != null) {
                 addDropDownEditor(dropDownData);
             } else {
 
                setFieldNatureIfItIsWasNotSetBefore();
                if (.getNature() == . && (.isThereABoundVariableToSet() == true || .isItAList() == true)) {
                    addFieldSelectorWidget();
                } else if (isFieldVariable()) {
                    addVariableEditor();
                } else {
                    addDefaultTextBoxWidget(flType);
                }
            }
        }
    }
    private void addDateEditor() {
         = dateEditor();
        .add();
    }
    private void addBooleanEditor() {
         = booleanEditor();
        .add();
    }
    private void addDropDownEditor(DropDownData dropDownData) {
         = dropDownEditor(dropDownData);
        .add();
    }
    private void addDefaultTextBoxWidget(String flType) {
            @Override
            public void onValueChange(ValueChangeEvent<StringnewValue) {
                valueHasChanged(newValue.getValue());
            }
        },
                flType,
                .getName(),
                .getValue());
        .add();
    }
    private void setFieldNatureIfItIsWasNotSetBefore() {
        if (.getValue() != null && .getValue().length() > 0 && .getNature() == .) {
            if (.getValue().length() > 1 && .getValue().charAt(1) == '[' && .getValue().charAt(0) == '=') {
                .setNature(.);
            } else if (.getValue().charAt(0) == '=') {
                .setNature(.);
            } else {
                .setNature(.);
            }
        }
    }
    private void addVariableEditor() {
         = variableEditor();
        .add();
    }
    private void addFieldSelectorWidget() {
        .setParentIsAList(true);
                ,
                this);
        .add();
    }
    private boolean isFieldVariable() {
        return .getNature() == .;
    }
    private EnumDropDown booleanEditor() {
        return new EnumDropDown(.getValue(),
                new DropDownValueChanged() {
                    public void valueChanged(String newText,
                                             String newValue) {
                        valueHasChanged(newValue);
                    }
                },
                DropDownData.create(new String[]{"true""false"}));
    }
    private EnumDropDown dropDownEditor(final DropDownData dropDownData) {
        return new EnumDropDown(.getValue(),
                new DropDownValueChanged() {
                    public void valueChanged(String newText,
                                             String newValue) {
                        valueHasChanged(newValue);
                        for (FieldDataConstraintEditor dependentEnumEditor : ) {
                            dependentEnumEditor.refreshDropDownData();
                        }
                    }
                },
                dropDownData);
    }
    private DatePickerTextBox dateEditor() {
        DatePickerTextBox editor = new DatePickerTextBox(.getValue());
        editor.addValueChanged(new ValueChanged() {
            public void valueChanged(String newValue) {
                .setValue(newValue);
            }
        });
        return editor;
    }
    private TextBox textBoxEditor(final ValueChangeHandler<StringvalueChangeHandler,
                                  final String dataType,
                                  String fieldName,
                                  String initialValue) {
        final TextBox tb = TextBoxFactory.getTextBox(dataType);
        tb.setText(initialValue);
        tb.setTitle(..ValueFor0(fieldName));
        tb.addValueChangeHandler(valueChangeHandler);
        return tb;
    }
    private Widget variableEditor() {
        List<Stringvars = .getFactNamesInScope();
        final ListBox box = new ListBox();
        if (this..getValue() == null) {
            box.addItem(..Choose());
        }
        int j = 0;
        for (String var : vars) {
            if (.getFactTypeByVariableName(var).getType().equals(.resolveFieldType())) {
                if (box.getItemCount() == 0) {
                    box.addItem("...");
                    j++;
                }
                box.addItem("=" + var);
                if (this..getValue() != null && this..getValue().equals("=" + var)) {
                    box.setSelectedIndex(j);
                }
                j++;
            }
        }
        box.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                .setValue(box.getItemText(box.getSelectedIndex()));
                valueHasChanged(.getValue());
            }
        });
        return box;
    }
    private void valueHasChanged(String newValue) {
        ValueChangeEvent.fire(this,
                newValue);
    }
    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<StringstringValueChangeHandler) {
        return addHandler(stringValueChangeHandler,
                ValueChangeEvent.getType());
    }
    public void addIfDependentEnumEditor(FieldDataConstraintEditor candidateDependentEnumEditor) {
        if (.isDependentEnum(candidateDependentEnumEditor.helper)) {
            .add(candidateDependentEnumEditor);
        }
    }
    private void refreshDropDownData() {
        if (this. instanceof EnumDropDown) {
            final EnumDropDown dropdown = (EnumDropDownthis.;
            dropdown.setDropDownData(.getValue(),
                    .getEnums());
        }
    }
New to GrepCode? Check out our FAQ X