co.edu.icesi.academ.client.perfiles.administrador.PanelProgramasBloques.java Source code

Java tutorial

Introduction

Here is the source code for co.edu.icesi.academ.client.perfiles.administrador.PanelProgramasBloques.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.administrador;

import java.util.List;

import co.edu.icesi.academ.bo.BloqueBO;
import co.edu.icesi.academ.bo.ProgramaBO;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
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.Notification;
import com.vaadin.ui.Table;

public class PanelProgramasBloques extends CustomComponent {

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

    @AutoGenerated
    private AbsoluteLayout mainLayout;
    @AutoGenerated
    private Table tableBloquesAsociados;
    @AutoGenerated
    private Table tableBloquesNoAsociados;
    @AutoGenerated
    private Table tableProgramas;
    @AutoGenerated
    private Button btnDesasociar;
    @AutoGenerated
    private Button btnAsociar;
    @AutoGenerated
    private Button btnEditarPrograma;
    @AutoGenerated
    private Button btnCrearPrograma;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private List<ProgramaBO> listProgramas;
    private List<BloqueBO> listBloqAsoc, listBloqNoAsoc;

    /**
     * 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 PanelProgramasBloques() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        btnEditarPrograma.setVisible(false);

        btnEditarPrograma.setEnabled(false);
        btnAsociar.setEnabled(false);
        btnDesasociar.setEnabled(false);

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

            @Override
            public void buttonClick(ClickEvent event) {
                int p = Integer.parseInt(tableProgramas.getValue().toString());
                ProgramaBO bo = listProgramas.get(p);
                PanelEditarPrograma panelEditarPrograma = new PanelEditarPrograma(bo);
                ControladorAdministrador.getInstance().getAcademUI().mostrarVentanaEmergente(panelEditarPrograma,
                        "Editar programa");
            }
        });

        tableProgramas.addContainerProperty("Cdigo", Integer.class, null);
        tableProgramas.addContainerProperty("Programa", ProgramaBO.class, null);
        tableProgramas.addContainerProperty("Descripcin", String.class, null);
        tableProgramas.setImmediate(true);
        tableProgramas.setNullSelectionAllowed(false);
        tableProgramas.setSelectable(true);

        tableProgramas.addValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                try {
                    btnAsociar.setEnabled(false);
                    btnDesasociar.setEnabled(false);
                    btnEditarPrograma.setEnabled(true);
                    int i = Integer.parseInt(tableProgramas.getValue().toString());
                    ControladorAdministrador.getInstance().cargarBloquesAsociadosPrograma(listProgramas.get(i));
                    ControladorAdministrador.getInstance().cargarBloquesNoAsociadosPrograma(listProgramas.get(i));
                } catch (Exception e) {
                    cargarProgramas(ControladorAdministrador.getInstance().darListaProgramas());
                    Notification.show(e.getMessage() + " panelBloqueProgr");
                }
            }
        });

        tableBloquesAsociados.addContainerProperty("Bloques asociados", BloqueBO.class, null);
        tableBloquesAsociados.setImmediate(true);
        tableBloquesAsociados.setNullSelectionAllowed(false);
        tableBloquesAsociados.setSelectable(true);

        tableBloquesNoAsociados.addContainerProperty("Bloques no asociados", BloqueBO.class, null);
        tableBloquesNoAsociados.setImmediate(true);
        tableBloquesNoAsociados.setNullSelectionAllowed(false);
        tableBloquesNoAsociados.setSelectable(true);

        tableBloquesAsociados.addValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                try {
                    Integer.parseInt(tableBloquesAsociados.getValue().toString());
                    btnDesasociar.setEnabled(true);
                } catch (Exception e) {
                    btnDesasociar.setEnabled(false);
                }
            }
        });

        tableBloquesNoAsociados.addValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                try {
                    Integer.parseInt(tableBloquesNoAsociados.getValue().toString());
                    btnAsociar.setEnabled(true);
                } catch (Exception e) {
                    btnAsociar.setEnabled(false);
                }
            }
        });

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

            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    int b = Integer.parseInt(tableBloquesNoAsociados.getValue().toString());
                    BloqueBO bloque = listBloqNoAsoc.get(b);
                    int p = Integer.parseInt(tableProgramas.getValue().toString());
                    ProgramaBO programa = listProgramas.get(p);

                    ControladorAdministrador.getInstance().asociarBloquesAPrograma(bloque, programa);
                    ControladorAdministrador.getInstance().cargarBloquesAsociadosPrograma(programa);
                    ControladorAdministrador.getInstance().cargarBloquesNoAsociadosPrograma(programa);

                } catch (Exception e) {
                }
            }
        });

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

            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    int b = Integer.parseInt(tableBloquesAsociados.getValue().toString());
                    BloqueBO bloque = listBloqAsoc.get(b);
                    int p = Integer.parseInt(tableProgramas.getValue().toString());
                    ProgramaBO programa = listProgramas.get(p);

                    ControladorAdministrador.getInstance().desAsociarBloquesAPrograma(bloque, programa);
                    ControladorAdministrador.getInstance().cargarBloquesAsociadosPrograma(programa);
                    ControladorAdministrador.getInstance().cargarBloquesNoAsociadosPrograma(programa);

                } catch (Exception e) {
                }
            }
        });

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

            @Override
            public void buttonClick(ClickEvent event) {
                ControladorAdministrador.getInstance().getAcademUI()
                        .mostrarVentanaEmergente(new PanelCrearPrograma(), "Crear programa");
            }
        });

    }

    public void cargarBloquesAsociados(List<BloqueBO> list) {
        if (list.size() == 0)
            btnDesasociar.setEnabled(false);
        tableBloquesAsociados.removeAllItems();
        listBloqAsoc = list;
        for (int i = 0; i < list.size(); i++) {
            BloqueBO bo = list.get(i);
            tableBloquesAsociados.addItem(new Object[] { bo }, new Integer(i));
        }
    }

    public void cargarBloquesNoAsociados(List<BloqueBO> list) {
        if (list.size() == 0)
            btnAsociar.setEnabled(false);
        tableBloquesNoAsociados.removeAllItems();
        listBloqNoAsoc = list;
        for (int i = 0; i < list.size(); i++) {
            BloqueBO bo = list.get(i);
            tableBloquesNoAsociados.addItem(new Object[] { bo }, new Integer(i));
        }
    }

    public void cargarProgramas(List<ProgramaBO> list) {
        listProgramas = list;
        tableProgramas.removeAllItems();
        for (int i = 0; i < listProgramas.size(); i++) {
            ProgramaBO programa = listProgramas.get(i);
            tableProgramas.addItem(
                    new Object[] { new Integer(programa.getCodigo()), programa, programa.getDescripcion() },
                    new Integer(i));
        }
    }

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

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

        // btnCrearPrograma
        btnCrearPrograma = new Button();
        btnCrearPrograma.setCaption("Crear Programa");
        btnCrearPrograma.setImmediate(true);
        btnCrearPrograma.setWidth("160px");
        btnCrearPrograma.setHeight("-1px");
        mainLayout.addComponent(btnCrearPrograma, "top:20.0px;left:20.0px;");

        // btnEditarPrograma
        btnEditarPrograma = new Button();
        btnEditarPrograma.setCaption("Editar Programa");
        btnEditarPrograma.setImmediate(true);
        btnEditarPrograma.setWidth("160px");
        btnEditarPrograma.setHeight("-1px");
        mainLayout.addComponent(btnEditarPrograma, "top:20.0px;left:220.0px;");

        // btnAsociar
        btnAsociar = new Button();
        btnAsociar.setCaption(">");
        btnAsociar.setImmediate(true);
        btnAsociar.setWidth("100px");
        btnAsociar.setHeight("26px");
        mainLayout.addComponent(btnAsociar, "top:269.0px;left:360.0px;");

        // btnDesasociar
        btnDesasociar = new Button();
        btnDesasociar.setCaption("<");
        btnDesasociar.setImmediate(true);
        btnDesasociar.setWidth("100px");
        btnDesasociar.setHeight("-1px");
        mainLayout.addComponent(btnDesasociar, "top:295.0px;left:360.0px;");

        // tableProgramas
        tableProgramas = new Table();
        tableProgramas.setImmediate(false);
        tableProgramas.setWidth("780px");
        tableProgramas.setHeight("143px");
        mainLayout.addComponent(tableProgramas, "top:60.0px;left:20.0px;");

        // tableBloquesNoAsociados
        tableBloquesNoAsociados = new Table();
        tableBloquesNoAsociados.setImmediate(false);
        tableBloquesNoAsociados.setWidth("340px");
        tableBloquesNoAsociados.setHeight("160px");
        mainLayout.addComponent(tableBloquesNoAsociados, "top:240.0px;left:20.0px;");

        // tableBloquesAsociados
        tableBloquesAsociados = new Table();
        tableBloquesAsociados.setImmediate(false);
        tableBloquesAsociados.setWidth("340px");
        tableBloquesAsociados.setHeight("160px");
        mainLayout.addComponent(tableBloquesAsociados, "top:240.0px;left:460.0px;");

        return mainLayout;
    }
}