Package org.gwtoolbox.widget.client.form.layout.simple

Source Code of org.gwtoolbox.widget.client.form.layout.simple.SimpleFormLayout

package org.gwtoolbox.widget.client.form.layout.simple;

import com.google.gwt.user.client.ui.*;
import org.gwtoolbox.widget.client.form.Form;
import org.gwtoolbox.widget.client.form.FormFields;
import org.gwtoolbox.widget.client.form.field.FormField;
import org.gwtoolbox.widget.client.form.layout.FormLayout;
import org.gwtoolbox.widget.client.panel.fieldset.FieldSet;

import java.util.*;

/**
* @author Uri Boness
*/
public class SimpleFormLayout implements FormLayout<FlowPanel> {

    private List<FieldGroup> groups;

    private Map<String, FieldRow> fieldRowsByKey = new HashMap<String, FieldRow>();

    public FlowPanel getLayoutContainer() {
        return new FlowPanel();
    }

    public void layout(FlowPanel container, FormFields fields) {
        Widget widget = createUngroupedFieldsWidget(fields);
        container.add(widget);
        for (FieldGroup group : groups) {
            widget = createGroupWidget(fields, group);
            container.add(widget);
        }
    }

    public void handleValidationResult(Form.FormValidationResult result) {
        Collection<String> invalidKeys = result.getInvalidFieldsKeys();
        for (String key : fieldRowsByKey.keySet()) {
            FieldRow row = fieldRowsByKey.get(key);
            if (invalidKeys.contains(key)) {
                row.markInvalid(result.getErrorMessages(key));
            } else {
                row.markValid();
            }
        }
    }

    public void addGroup(FieldGroup group) {
        if (groups == null) {
            groups = new ArrayList<FieldGroup>();
        }
        groups.add(group);
    }

    //================================================ Helper Methods ==================================================

    private Widget createUngroupedFieldsWidget(FormFields fields) {
        List<String> keys = new ArrayList<String>();
        for (String key : fields.getFieldsKeys()) {
            if (!isInGroup(key)) {
                keys.add(key);
            }
        }
        Grid grid = new Grid(keys.size(), 3);
        int row = 0;
        for (String key : keys) {
            addFieldRow(fields.getField(key), grid, row++);
        }
        return grid;
    }

    private Widget createGroupWidget(FormFields fields, FieldGroup group) {
        Grid grid = new Grid(3, group.getKeys().size());
        int row = 0;
        for (String key : group.getKeys()) {
            addFieldRow(fields.getField(key), grid, row++);
        }
        return new FieldSet(group.getName(), group.isOptional(), grid);
    }

    private void addFieldRow(FormField field, Grid grid, int row) {
        grid.setWidget(row, 0, new Label(field.getLabel()));
        grid.setWidget(row, 1, field.getEditor().getWidget());
        Image image = new Image();
        image.setStyleName("ErrorImage");
        image.setVisible(false);
        grid.setWidget(row, 2, image);
        FieldRow fieldRow = new FieldRow(field, image);
        fieldRowsByKey.put(field.getKey(), fieldRow);
    }

    private boolean isInGroup(String key) {
        for (FieldGroup group : groups) {
            if (group.contains(key)) {
                return true;
            }
        }
        return false;
    }

    //================================================= Inner Classes ==================================================

    private class FieldRow {

        private final FormField field;
        private final Image errorImage;

        private FieldRow(FormField field, Image errorImage) {
            this.field = field;
            this.errorImage = errorImage;
        }

        public void markInvalid(List<String> messages) {
            errorImage.setVisible(true);
            errorImage.setTitle(messages.get(0));
        }

        public void markValid() {
            errorImage.setTitle("");
            errorImage.setVisible(false);
        }
    }
}
TOP

Related Classes of org.gwtoolbox.widget.client.form.layout.simple.SimpleFormLayout

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.