org.balisunrise.vaadin.components.user.UserPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.balisunrise.vaadin.components.user.UserPanel.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.vaadin.components.user;

import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import org.balisunrise.users.models.CreateUserModel;
import org.balisunrise.vaadin.components.Panel;

/**
 *
 * @author Glauco
 */
public class UserPanel extends Panel {

    private CreateUserModel model;

    private TextField nome;

    private TextField login;

    private PasswordField senha;

    private PasswordField repita;

    public UserPanel() {
        init();
    }

    private void init() {

        model = new CreateUserModel();
        BeanItem<CreateUserModel> item = new BeanItem<>(model);
        FieldGroup fg = new FieldGroup(item);

        nome = new TextField("Nome");
        nome.setWidth("100%");
        nome.setMaxLength(50);
        fg.bind(nome, "nome");

        login = new TextField("Login");
        login.setWidth("100%");
        login.setMaxLength(32);
        fg.bind(login, "login");

        senha = new PasswordField("Senha");
        senha.setWidth("100%");
        senha.setMaxLength(32);
        fg.bind(senha, "senha");

        repita = new PasswordField("Repita a senha");
        repita.setWidth("100%");
        repita.setMaxLength(32);
        fg.bind(repita, "repita");

        GridLayout grid = new GridLayout(20, 1);
        grid.setWidth("100%");
        grid.setHeightUndefined();
        grid.setSpacing(true);

        grid.addComponent(nome, 0, 0, 9, 0);
        grid.addComponent(login, 10, 0, 19, 0);
        grid.setRows(2);
        grid.addComponent(senha, 0, 1, 9, 1);
        grid.addComponent(repita, 10, 1, 19, 1);

        addComponent(grid);
        setSpacing(true);

        repita.addBlurListener((e) -> {
            try {
                fg.commit();
                Notification.show("Changes committed!\n" + model.toString(), Notification.Type.TRAY_NOTIFICATION);
            } catch (final FieldGroup.CommitException ex) {
                Notification.show("Commit failed: " + ex.getCause().getMessage(),
                        Notification.Type.TRAY_NOTIFICATION);
            }
        });
    }
}