org.balisunrise.spilumba.layout.GridLayout.java Source code

Java tutorial

Introduction

Here is the source code for org.balisunrise.spilumba.layout.GridLayout.java

Source

/*
 * Copyright (C) 2016 Glauco Knihs. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Please contact Glauco Knihs, Rua 10 de Junho, N469, Centro, 
 * Guabiruba-SC, CEP 88360-000, BRAZIL, eglauko@hotmail.com, 
 * if you need additional information or have any questions.
 */
package org.balisunrise.spilumba.layout;

import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

/**
 *
 * @author Glauco
 */
public class GridLayout extends FieldLayout<com.vaadin.ui.GridLayout> {

    @Override
    public com.vaadin.ui.GridLayout createLayout() {
        int row = 0;
        int col = 0;
        int mc = width.toGridColumns();

        com.vaadin.ui.GridLayout grid = new com.vaadin.ui.GridLayout(mc, 1);
        grid.setWidth("100%");
        grid.setHeightUndefined();
        grid.setSpacing(true);

        for (Property prop : properties) {
            Field field = createField(prop);
            field.setWidth("100%");
            fieldGroup.bind(field, prop.getFieldName());

            int pc = prop.getWidth().toGridColumns();
            int ri, rf, ci, cf;
            ci = col;
            cf = pc + ci - 1;
            // checar se no cabe na linha
            if (cf >= mc) {
                // pula linha
                if (ci > 0) {
                    row++;
                    grid.setRows(row + 1);
                }
                ci = 0;
                // se colunas do componente maior que colunas do grid
                if (pc > mc) {
                    // cria componente 100%
                    cf = mc - 1;
                } else {
                    // novo final de componente.
                    cf = pc - 1;
                }
            }
            ri = row;
            rf = ri + prop.getHeight().toGridColumns() - 1;
            grid.addComponent(field, ci, ri, cf, rf);
            col = cf + 1;
        }

        return grid;
    }

    private Field createField(Property prop) {

        switch (prop.getType()) {
        case TEXT:
            TextField tf = new TextField();
            tf.setCaption(prop.getLabel());
            tf.setMaxLength(prop.getMaxLength());
            return tf;
        default:
            throw new AssertionError();
        }
    }
}