Search in sources :

Example 1 with FieldGroup

use of org.apache.ofbiz.widget.model.ModelForm.FieldGroup in project ofbiz-framework by apache.

the class FormRenderer method renderSingleFormString.

private void renderSingleFormString(Appendable writer, Map<String, Object> context, int positions) throws IOException {
    List<ModelFormField> tempFieldList = new LinkedList<>();
    tempFieldList.addAll(modelForm.getFieldList());
    // Check to see if there is a field, same name and same use-when (could come from extended form)
    for (int j = 0; j < tempFieldList.size(); j++) {
        ModelFormField modelFormField = tempFieldList.get(j);
        if (modelForm.getUseWhenFields().contains(modelFormField.getName())) {
            boolean shouldUse1 = modelFormField.shouldUse(context);
            for (int i = j + 1; i < tempFieldList.size(); i++) {
                ModelFormField curField = tempFieldList.get(i);
                if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) {
                    boolean shouldUse2 = curField.shouldUse(context);
                    if (shouldUse1 == shouldUse2) {
                        tempFieldList.remove(i--);
                    }
                } else {
                    continue;
                }
            }
        }
    }
    Set<String> alreadyRendered = new TreeSet<>();
    FieldGroup lastFieldGroup = null;
    // render form open
    if (!modelForm.getSkipStart()) {
        formStringRenderer.renderFormOpen(writer, context, modelForm);
    }
    // render all hidden & ignored fields
    List<ModelFormField> hiddenIgnoredFieldList = this.getHiddenIgnoredFields(context, alreadyRendered, tempFieldList, -1);
    this.renderHiddenIgnoredFields(writer, context, formStringRenderer, hiddenIgnoredFieldList);
    // render formatting wrapper open
    // This should be covered by fieldGroup.renderStartString
    // formStringRenderer.renderFormatSingleWrapperOpen(writer, context, this);
    // render each field row, except hidden & ignored rows
    Iterator<ModelFormField> fieldIter = tempFieldList.iterator();
    ModelFormField lastFormField = null;
    ModelFormField currentFormField = null;
    ModelFormField nextFormField = null;
    if (fieldIter.hasNext()) {
        currentFormField = fieldIter.next();
    }
    if (fieldIter.hasNext()) {
        nextFormField = fieldIter.next();
    }
    FieldGroup currentFieldGroup = null;
    String currentFieldGroupName = null;
    String lastFieldGroupName = null;
    if (currentFormField != null) {
        currentFieldGroup = (FieldGroup) modelForm.getFieldGroupMap().get(currentFormField.getFieldName());
        if (currentFieldGroup == null) {
            currentFieldGroup = modelForm.getDefaultFieldGroup();
        }
        if (currentFieldGroup != null) {
            currentFieldGroupName = currentFieldGroup.getId();
        }
    }
    boolean isFirstPass = true;
    boolean haveRenderedOpenFieldRow = false;
    while (currentFormField != null) {
        // don't do it on the first pass though...
        if (isFirstPass) {
            isFirstPass = false;
            List<FieldGroupBase> inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup);
            for (FieldGroupBase obj : inbetweenList) {
                if (obj instanceof ModelForm.Banner) {
                    ((ModelForm.Banner) obj).renderString(writer, context, formStringRenderer);
                }
            }
            if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) {
                currentFieldGroup.renderStartString(writer, context, formStringRenderer);
                lastFieldGroup = currentFieldGroup;
            }
        } else {
            if (fieldIter.hasNext()) {
                // at least two loops left
                lastFormField = currentFormField;
                currentFormField = nextFormField;
                nextFormField = fieldIter.next();
            } else if (nextFormField != null) {
                // okay, just one loop left
                lastFormField = currentFormField;
                currentFormField = nextFormField;
                nextFormField = null;
            } else {
                // at the end...
                currentFormField = null;
                // nextFormField is already null
                break;
            }
            currentFieldGroup = null;
            if (currentFormField != null) {
                currentFieldGroup = (FieldGroup) modelForm.getFieldGroupMap().get(currentFormField.getName());
            }
            if (currentFieldGroup == null) {
                currentFieldGroup = modelForm.getDefaultFieldGroup();
            }
            currentFieldGroupName = currentFieldGroup.getId();
            if (lastFieldGroup != null) {
                lastFieldGroupName = lastFieldGroup.getId();
                if (!lastFieldGroupName.equals(currentFieldGroupName)) {
                    if (haveRenderedOpenFieldRow) {
                        formStringRenderer.renderFormatFieldRowClose(writer, context, modelForm);
                        haveRenderedOpenFieldRow = false;
                    }
                    lastFieldGroup.renderEndString(writer, context, formStringRenderer);
                    List<FieldGroupBase> inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup);
                    for (FieldGroupBase obj : inbetweenList) {
                        if (obj instanceof ModelForm.Banner) {
                            ((ModelForm.Banner) obj).renderString(writer, context, formStringRenderer);
                        }
                    }
                }
            }
            if (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName)) {
                currentFieldGroup.renderStartString(writer, context, formStringRenderer);
                lastFieldGroup = currentFieldGroup;
            }
        }
        FieldInfo fieldInfo = currentFormField.getFieldInfo();
        if (fieldInfo.getFieldType() == FieldInfo.HIDDEN || fieldInfo.getFieldType() == FieldInfo.IGNORED) {
            continue;
        }
        if (alreadyRendered.contains(currentFormField.getName())) {
            continue;
        }
        if (!currentFormField.shouldUse(context)) {
            if (UtilValidate.isNotEmpty(lastFormField)) {
                currentFormField = lastFormField;
            }
            continue;
        }
        alreadyRendered.add(currentFormField.getName());
        if (focusFieldName.isEmpty()) {
            if (fieldInfo.getFieldType() != FieldInfo.DISPLAY && fieldInfo.getFieldType() != FieldInfo.HIDDEN && fieldInfo.getFieldType() != FieldInfo.DISPLAY_ENTITY && fieldInfo.getFieldType() != FieldInfo.IGNORED && fieldInfo.getFieldType() != FieldInfo.IMAGE) {
                focusFieldName = currentFormField.getName();
                context.put(modelForm.getName().concat(".focusFieldName"), focusFieldName);
            }
        }
        boolean stayingOnRow = false;
        if (lastFormField != null) {
            if (lastFormField.getPosition() >= currentFormField.getPosition()) {
                // moving to next row
                stayingOnRow = false;
            } else {
                // staying on same row
                stayingOnRow = true;
            }
        }
        int positionSpan = 1;
        Integer nextPositionInRow = null;
        if (nextFormField != null) {
            if (nextFormField.getPosition() > currentFormField.getPosition()) {
                positionSpan = nextFormField.getPosition() - currentFormField.getPosition() - 1;
                nextPositionInRow = Integer.valueOf(nextFormField.getPosition());
            } else {
                positionSpan = positions - currentFormField.getPosition();
            }
        }
        if (stayingOnRow) {
        // no spacer cell, might add later though...
        // formStringRenderer.renderFormatFieldRowSpacerCell(writer, context, currentFormField);
        } else {
            if (haveRenderedOpenFieldRow) {
                // render row formatting close
                formStringRenderer.renderFormatFieldRowClose(writer, context, modelForm);
                haveRenderedOpenFieldRow = false;
            }
            // render row formatting open
            formStringRenderer.renderFormatFieldRowOpen(writer, context, modelForm);
            haveRenderedOpenFieldRow = true;
        }
        // 
        if (!haveRenderedOpenFieldRow) {
            formStringRenderer.renderFormatFieldRowOpen(writer, context, modelForm);
            haveRenderedOpenFieldRow = true;
        }
        // render title formatting open
        formStringRenderer.renderFormatFieldRowTitleCellOpen(writer, context, currentFormField);
        // render title (unless this is a submit or a reset field)
        if (fieldInfo.getFieldType() != FieldInfo.SUBMIT && fieldInfo.getFieldType() != FieldInfo.RESET) {
            formStringRenderer.renderFieldTitle(writer, context, currentFormField);
        } else {
            formStringRenderer.renderFormatEmptySpace(writer, context, modelForm);
        }
        // render title formatting close
        formStringRenderer.renderFormatFieldRowTitleCellClose(writer, context, currentFormField);
        // render separator
        formStringRenderer.renderFormatFieldRowSpacerCell(writer, context, currentFormField);
        // render widget formatting open
        formStringRenderer.renderFormatFieldRowWidgetCellOpen(writer, context, currentFormField, positions, positionSpan, nextPositionInRow);
        // render widget
        currentFormField.renderFieldString(writer, context, formStringRenderer);
        // render widget formatting close
        formStringRenderer.renderFormatFieldRowWidgetCellClose(writer, context, currentFormField, positions, positionSpan, nextPositionInRow);
    }
    // render row formatting close after the end if needed
    if (haveRenderedOpenFieldRow) {
        formStringRenderer.renderFormatFieldRowClose(writer, context, modelForm);
    }
    if (lastFieldGroup != null) {
        lastFieldGroup.renderEndString(writer, context, formStringRenderer);
    }
    // render form close
    if (!modelForm.getSkipEnd()) {
        formStringRenderer.renderFormClose(writer, context, modelForm);
    }
}
Also used : FieldGroup(org.apache.ofbiz.widget.model.ModelForm.FieldGroup) ModelFormField(org.apache.ofbiz.widget.model.ModelFormField) LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) FieldInfo(org.apache.ofbiz.widget.model.FieldInfo) FieldGroupBase(org.apache.ofbiz.widget.model.ModelForm.FieldGroupBase)

Example 2 with FieldGroup

use of org.apache.ofbiz.widget.model.ModelForm.FieldGroup in project ofbiz-framework by apache.

the class FormRenderer method getInbetweenList.

private List<FieldGroupBase> getInbetweenList(FieldGroup startFieldGroup, FieldGroup endFieldGroup) {
    List<FieldGroupBase> inbetweenList = new ArrayList<>();
    boolean firstFound = false;
    String startFieldGroupId = null;
    String endFieldGroupId = null;
    if (endFieldGroup != null) {
        endFieldGroupId = endFieldGroup.getId();
    }
    if (startFieldGroup == null) {
        firstFound = true;
    } else {
        startFieldGroupId = startFieldGroup.getId();
    }
    Iterator<FieldGroupBase> iter = modelForm.getFieldGroupList().iterator();
    while (iter.hasNext()) {
        FieldGroupBase obj = iter.next();
        if (obj instanceof ModelForm.Banner) {
            if (firstFound) {
                inbetweenList.add(obj);
            }
        } else {
            FieldGroup fieldGroup = (FieldGroup) obj;
            String fieldGroupId = fieldGroup.getId();
            if (!firstFound) {
                if (fieldGroupId.equals(startFieldGroupId)) {
                    firstFound = true;
                    continue;
                }
            }
            if (firstFound) {
                if (fieldGroupId.equals(endFieldGroupId)) {
                    break;
                }
                inbetweenList.add(fieldGroup);
            }
        }
    }
    return inbetweenList;
}
Also used : FieldGroup(org.apache.ofbiz.widget.model.ModelForm.FieldGroup) ArrayList(java.util.ArrayList) FieldGroupBase(org.apache.ofbiz.widget.model.ModelForm.FieldGroupBase)

Aggregations

FieldGroup (org.apache.ofbiz.widget.model.ModelForm.FieldGroup)2 FieldGroupBase (org.apache.ofbiz.widget.model.ModelForm.FieldGroupBase)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 TreeSet (java.util.TreeSet)1 FieldInfo (org.apache.ofbiz.widget.model.FieldInfo)1 ModelFormField (org.apache.ofbiz.widget.model.ModelFormField)1