co.edu.icesi.academ.client.perfiles.propietario.DialogoEditarRubrica.java Source code

Java tutorial

Introduction

Here is the source code for co.edu.icesi.academ.client.perfiles.propietario.DialogoEditarRubrica.java

Source

/**
* Copyright  2013 Universidad Icesi
* 
* This file is part of ACADEM.
* 
* ACADEM 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.
* 
* ACADEM 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 ACADEM.  If not, see <http://www.gnu.org/licenses/>.
**/

package co.edu.icesi.academ.client.perfiles.propietario;

import java.util.LinkedList;

import co.edu.icesi.academ.bo.CriterioBO;
import co.edu.icesi.academ.bo.NivelCriterioBO;
import co.edu.icesi.academ.bo.RubricaBO;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;

public class DialogoEditarRubrica extends CustomComponent {

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    @AutoGenerated
    private AbsoluteLayout mainLayout;
    @AutoGenerated
    private Table tblCriterios;
    @AutoGenerated
    private Button btnGuardar;
    @AutoGenerated
    private Table tblNiveles;
    @AutoGenerated
    private Button btnCrearNivel;
    @AutoGenerated
    private Button btnCrearCriterio;
    @AutoGenerated
    private Label lblNiveles;
    @AutoGenerated
    private Label lblCriterios;

    private RubricaBO rubrica;

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public DialogoEditarRubrica() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        tblCriterios.addContainerProperty("Posicion", TextField.class, null);
        tblCriterios.addContainerProperty("Descripcion", TextField.class, null);

        tblNiveles.addContainerProperty("Puntos", TextField.class, null);
        tblNiveles.addContainerProperty("Descripcion", TextField.class, null);

        btnCrearCriterio.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                CriterioBO criterio = new CriterioBO();
                criterio.setId(0);
                criterio.setDescripcion("");

                TextField txtId = new TextField("", criterio.getId() + "");

                TextField txtDescripcion = new TextField("", criterio.getDescripcion());

                tblCriterios.addItem(new Object[] { txtId, txtDescripcion }, criterio);
            }
        });

        btnCrearNivel.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                NivelCriterioBO nivel = new NivelCriterioBO();
                nivel.setId(0);
                nivel.setNivel(0);
                nivel.setDescripcion("");

                TextField txtNivel = new TextField("", nivel.getNivel() + "");

                TextField txtDescripcion = new TextField("", nivel.getDescripcion());

                tblNiveles.addItem(new Object[] { txtNivel, txtDescripcion }, nivel);
            }
        });

        btnGuardar.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {

                rubrica.getCriterios().clear();

                while (tblCriterios.size() != 0) {
                    for (Object o : tblCriterios.getItemIds().toArray()) {
                        CriterioBO criterio = (CriterioBO) o;

                        criterio.setId(Integer.parseInt(
                                tblCriterios.getItem(criterio).getItemProperty("Posicion").getValue().toString()));
                        criterio.setDescripcion(tblCriterios.getItem(criterio).getItemProperty("Descripcion")
                                .getValue().toString());

                        tblCriterios.removeItem(criterio);

                        if (!rubrica.getCriterios().contains(criterio))
                            rubrica.getCriterios().add(criterio);
                    }
                }

                LinkedList<NivelCriterioBO> niveles = new LinkedList<NivelCriterioBO>();

                while (tblNiveles.size() != 0) {
                    for (Object o : tblNiveles.getItemIds().toArray()) {
                        NivelCriterioBO nivel = (NivelCriterioBO) o;

                        nivel.setId(Integer.parseInt(
                                tblNiveles.getItem(nivel).getItemProperty("Puntos").getValue().toString()));
                        nivel.setNivel(nivel.getId());
                        nivel.setDescripcion(
                                tblNiveles.getItem(nivel).getItemProperty("Descripcion").getValue().toString());

                        tblNiveles.removeItem(nivel);

                        if (!niveles.contains(nivel))
                            niveles.add(nivel);
                    }
                }

                for (CriterioBO criterio : rubrica.getCriterios())
                    criterio.setNiveles(niveles);

                ControladorPropietario.getInstance().guardarRubrica(rubrica);

                actualizar(rubrica);
            }
        });
    }

    public void actualizar(RubricaBO rubricaBO) {
        rubrica = rubricaBO;

        tblCriterios.removeAllItems();

        for (CriterioBO criterio : rubrica.getCriterios()) {
            TextField txtId = new TextField("", criterio.getId() + "");
            txtId.setId("id");

            TextField txtDescripcion = new TextField("", criterio.getDescripcion());
            txtDescripcion.setId("descripcion");

            tblCriterios.addItem(new Object[] { txtId, txtDescripcion }, criterio);
        }

        tblNiveles.removeAllItems();

        if (!rubrica.getCriterios().isEmpty()) {
            for (NivelCriterioBO nivel : rubrica.getCriterios().get(0).getNiveles()) {
                TextField txtNivel = new TextField("", nivel.getNivel() + "");
                txtNivel.setId("nivel");

                TextField txtDescripcion = new TextField("", nivel.getDescripcion());
                txtDescripcion.setId("descripcion");

                tblNiveles.addItem(new Object[] { txtNivel, txtDescripcion }, nivel);
            }
        }
    }

    @AutoGenerated
    private AbsoluteLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new AbsoluteLayout();
        mainLayout.setImmediate(false);
        mainLayout.setWidth("440px");
        mainLayout.setHeight("561px");

        // top-level component properties
        setWidth("440px");
        setHeight("561px");

        // lblCriterios
        lblCriterios = new Label();
        lblCriterios.setImmediate(false);
        lblCriterios.setWidth("-1px");
        lblCriterios.setHeight("-1px");
        lblCriterios.setValue("Criterios:");
        mainLayout.addComponent(lblCriterios, "top:20.0px;left:20.0px;");

        // lblNiveles
        lblNiveles = new Label();
        lblNiveles.setImmediate(false);
        lblNiveles.setWidth("-1px");
        lblNiveles.setHeight("-1px");
        lblNiveles.setValue("Niveles:");
        mainLayout.addComponent(lblNiveles, "top:264.0px;left:20.0px;");

        // btnCrearCriterio
        btnCrearCriterio = new Button();
        btnCrearCriterio.setCaption("Crear Criterio");
        btnCrearCriterio.setImmediate(true);
        btnCrearCriterio.setWidth("-1px");
        btnCrearCriterio.setHeight("-1px");
        mainLayout.addComponent(btnCrearCriterio, "top:54.0px;left:19.0px;");

        // btnCrearNivel
        btnCrearNivel = new Button();
        btnCrearNivel.setCaption("Crear Nivel");
        btnCrearNivel.setImmediate(true);
        btnCrearNivel.setWidth("-1px");
        btnCrearNivel.setHeight("-1px");
        mainLayout.addComponent(btnCrearNivel, "top:300.0px;left:19.0px;");

        // tblNiveles
        tblNiveles = new Table();
        tblNiveles.setImmediate(false);
        tblNiveles.setWidth("401px");
        tblNiveles.setHeight("140px");
        mainLayout.addComponent(tblNiveles, "top:340.0px;left:19.0px;");

        // btnGuardar
        btnGuardar = new Button();
        btnGuardar.setCaption("Guardar");
        btnGuardar.setImmediate(true);
        btnGuardar.setWidth("-1px");
        btnGuardar.setHeight("-1px");
        mainLayout.addComponent(btnGuardar, "top:514.0px;left:350.0px;");

        // tblCriterios
        tblCriterios = new Table();
        tblCriterios.setImmediate(false);
        tblCriterios.setWidth("400px");
        tblCriterios.setHeight("140px");
        mainLayout.addComponent(tblCriterios, "top:100.0px;left:20.0px;");

        return mainLayout;
    }

}