Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 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.Map;
 
 
 public class FactDataWidgetFactory {
 
     private final DirtyableFlexTable widget;
     private final Scenario scenario;
     private final PackageDataModelOracle dmo;
     private final FixtureList definitionList;
     private final ExecutionTrace executionTrace;
 
     private final RowIndexByFieldName rowIndexByFieldName = new RowIndexByFieldName();
     private int col = 0;
     private final ScenarioParentWidget parent;
 
     public FactDataWidgetFactory(Scenario scenario,
                                  PackageDataModelOracle dmo,
                                  FixtureList definitionList,
                                  ExecutionTrace executionTrace,
                                  ScenarioParentWidget parent,
                                  DirtyableFlexTable widget) {
         this. = scenario;
         this. = dmo;
         this. = definitionList;
         this. = executionTrace;
         this. = parent;
         this. = widget;
     }
 
 
     public void build(String headerText,
                       Fact fact) {
 
         if (fact instanceof FactData ) {
             FactData factData = (FactDatafact;
             .setWidget(0,
                     ++,
                     new SmallLabel("[" + factData.getName() + "]"));
         } else {
             ++;
         }
 
         .setWidget(
                 0,
                 0,
                 new ClickableLabel(headerText,
                         createAddFieldButton(fact)));
 
         Map<FieldDataFieldDataConstraintEditorenumEditorMap
                 = new HashMap<FieldDataFieldDataConstraintEditor>();
         // Sets row name and delete button.
         for (final Field field : fact.getFieldData()) {
             // Avoid duplicate field rows, only one for each name.
             if (.doesNotContain(field.getName())) {
                 newRow(factfield.getName());
             }
            // Sets row data
            int fieldRowIndex = .getRowIndex(field.getName());
            IsWidget editableCell = editableCell(
                    field,
                    fact,
                    fact.getType());
            .setWidget(fieldRowIndex,
                    ,
                    editableCell);
            if (field instanceof FieldData) {
                FieldData fieldData = (FieldDatafield;
                if (fieldData.getNature() == .) {
                    enumEditorMap.put(fieldData, (FieldDataConstraintEditoreditableCell);
                }
            }
        }
        for (FieldDataConstraintEditor outerEnumEditor : enumEditorMap.values()) {
            for (FieldDataConstraintEditor innerEnumEditor : enumEditorMap.values()) {
                if (outerEnumEditor != innerEnumEditor) {
                    outerEnumEditor.addIfDependentEnumEditor(innerEnumEditor);
                }
            }
        }
        if (fact instanceof FactData) {
            DeleteFactColumnButton deleteFactColumnButton = new DeleteFactColumnButton((FactDatafact);
            .setWidget(
                    .amountOrRows() + 1,
                    ,
                    deleteFactColumnButton);
        }
    }
    private ClickHandler createAddFieldButton(Fact fact) {
        if (fact instanceof FactData) {
            return new AddFieldToFactDataClickHandler(
                    ,
                    ,
                    );
        } else {
            return new AddFieldToFactClickHandler(
                    fact,
                    ,
                    );
        }
    }
    private void newRow(final Fact fact,
                        final String fieldName) {
        .addRow(fieldName);
        int rowIndex = .getRowIndex(fieldName);
        .setWidget(rowIndex,
                0,
                createFieldNameWidget(fieldName));
        .setWidget(rowIndex,
                .size() + 1,
                new DeleteFieldRowButton(fact,
                        fieldName));
                0,
                .);
    }

    
This will provide a cell editor. It will filter non numerics, show choices etc as appropriate.

Parameters:
field
factType
Returns:
    private IsWidget editableCell(final Field field,
                                  Fact fact,
                                  String factType) {
        if (field instanceof FieldData) {
            FieldDataConstraintEditor fieldDataConstraintEditor = new FieldDataConstraintEditor(
                    factType,
                    (FieldData)field,
                    fact,
                    ,
                    ,
                    );
            fieldDataConstraintEditor.addValueChangeHandler(new ValueChangeHandler<String>() {
                @Override
                public void onValueChange(ValueChangeEvent<StringstringValueChangeEvent) {
                    ((FieldDatafield).setValue(stringValueChangeEvent.getValue());
                }
            });
            return fieldDataConstraintEditor;
        } else if (field instanceof CollectionFieldData ) {
            return new CollectionFieldDataConstraintEditor(
                    factType,
                    (CollectionFieldData)field,
                    fact,
                    ,
                    ,
                    );
        } else if (field instanceof FactAssignmentField ) {
            return new FactAssignmentFieldWidget(
                    (FactAssignmentFieldfield,
                    ,
                    ,
                    ,
                    ,
                    );
        } else if (field instanceof FieldPlaceHolder) {
            return new FieldSelectorWidget(
                    field,
                    new FieldConstraintHelper(
                            ,
                            ,
                            ,
                            factType,
                            field,
                            fact),
                    );
        }
        throw new IllegalArgumentException("Unknown field type: " + field.getClass());
    }
    private IsWidget createFieldNameWidget(String fieldName) {
        return new FieldNameWidgetImpl(fieldName);
    }
    public int amountOrRows() {
        return .amountOrRows();
    }
    class DeleteFactColumnButton extends ImageButton {
        public DeleteFactColumnButton(final FactData fact) {
            super(..DeleteItemSmall(),
                    ..RemoveTheColumnForScenario(fact.getName()));
            addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    if (.isFactDataReferenced(fact)) {
                        Window.alert(..CanTRemoveThisColumnAsTheName0IsBeingUsed(fact.getName()));
                    } else if (Window.confirm(..AreYouSureYouWantToRemoveColumn0(fact.getName()))) {
                        .removeFixture(fact);
                        .remove(fact);
                        .renderEditor();
                    }
                }
            });
        }
    }
    class DeleteFieldRowButton extends ImageButton {
        public DeleteFieldRowButton(final Fact fact,
                                    final String fieldName) {
            super(..DeleteItemSmall(),
                    ..RemoveThisRow());
            addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    if (fact instanceof FactData) {
                        if (Window.confirm(..AreYouSureYouWantToRemoveRow0(fieldName))) {
                            ScenarioHelper.removeFields(,
                                    fieldName);
                        }
                    } else if (fact instanceof Fact) {
                        if (Window.confirm(..AreYouSureYouWantToRemoveRow0(fieldName))) {
                            fact.removeField(fieldName);
                        }
                    }
                    .renderEditor();
                }
            });
        }
    }
    class RowIndexByFieldName {
        private Map<StringIntegerrows = new HashMap<StringInteger>();
        public void addRow(String fieldName) {
            .put(fieldName,
                    .size() + 1);
        }
        public boolean doesNotContain(String fieldName) {
            return !.containsKey(fieldName);
        }
        public Integer getRowIndex(String fieldName) {
            return .get(fieldName);
        }
        public int amountOrRows() {
            return .size();
        }
    }
New to GrepCode? Check out our FAQ X