Java tutorial
/** * 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.List; import co.edu.icesi.academ.bo.EvaluacionBO; import co.edu.icesi.academ.bo.RolBO; import co.edu.icesi.academ.bo.UsuarioBO; 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.TabSheet.SelectedTabChangeEvent; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.Label; import com.vaadin.ui.ListSelect; import com.vaadin.ui.TabSheet; import com.vaadin.ui.TabSheet.SelectedTabChangeListener; public class PanelAsignarRolEvaluador extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private AbsoluteLayout mainLayout; @AutoGenerated private ListSelect lstEvaluadores; @AutoGenerated private TabSheet tstRoles; @AutoGenerated private Label lblRoles; @AutoGenerated private Button btnRemover; @AutoGenerated private Button btnAgregar; @AutoGenerated private Label lblEvaluadores; @AutoGenerated private Button btnGuardar; /** * 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 PanelAsignarRolEvaluador() { buildMainLayout(); setCompositionRoot(mainLayout); btnGuardar.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { ControladorPropietario.getInstance().guardarRolesAsignados((EvaluacionBO) ControladorPropietario .getInstance().getAcademUI().getHTTPSession().getAttribute("evaluacion")); } }); btnAgregar.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { EvaluacionBO evaluacionBO = (EvaluacionBO) ControladorPropietario.getInstance().getAcademUI() .getHTTPSession().getAttribute("evaluacion"); RolBO rol = new RolBO(); rol.setEvaluacion(evaluacionBO.getId()); rol.setNombre(tstRoles.getSelectedTab().getCaption()); System.out.println(rol.getEvaluacion() + " " + rol.getNombre()); UsuarioBO usuario = (UsuarioBO) lstEvaluadores.getValue(); usuario.getRoles().add(rol); lstEvaluadores.removeItem(usuario); ((ListSelect) tstRoles.getSelectedTab()).addItem(usuario); } }); btnRemover.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { EvaluacionBO evaluacionBO = (EvaluacionBO) ControladorPropietario.getInstance().getAcademUI() .getHTTPSession().getAttribute("evaluacion"); RolBO rol = new RolBO(); rol.setEvaluacion(evaluacionBO.getId()); rol.setNombre(tstRoles.getSelectedTab().getCaption()); UsuarioBO usuario = (UsuarioBO) ((ListSelect) tstRoles.getSelectedTab()).getValue(); usuario.getRoles().remove(rol); ((ListSelect) tstRoles.getSelectedTab()).removeItem(usuario); lstEvaluadores.addItem(usuario); } }); tstRoles.addSelectedTabChangeListener(new SelectedTabChangeListener() { /** * */ private static final long serialVersionUID = 1L; @Override public void selectedTabChange(SelectedTabChangeEvent event) { mostrarEvaluadoresDisponibles((ListSelect) tstRoles.getSelectedTab()); } }); } public void actualizar(List<RolBO> roles) { for (RolBO rol : roles) { ListSelect l = new ListSelect(rol.getNombre()); l.setWidth("100%"); l.setHeight("100%"); tstRoles.addTab(l); } mostrarEvaluadoresDisponibles((ListSelect) tstRoles.getSelectedTab()); } private void mostrarEvaluadoresDisponibles(ListSelect tab) { lstEvaluadores.removeAllItems(); tab.removeAllItems(); EvaluacionBO evaluacionBO = (EvaluacionBO) ControladorPropietario.getInstance().getAcademUI() .getHTTPSession().getAttribute("evaluacion"); RolBO rol = new RolBO(); rol.setEvaluacion(evaluacionBO.getId()); rol.setNombre(tab.getCaption()); for (UsuarioBO e : evaluacionBO.getEvaluadores()) { if (e.getRoles().contains(rol)) tab.addItem(e); else lstEvaluadores.addItem(e); } } @AutoGenerated private AbsoluteLayout buildMainLayout() { // common part: create layout mainLayout = new AbsoluteLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("100%"); mainLayout.setHeight("100%"); // top-level component properties setWidth("100.0%"); setHeight("100.0%"); // btnGuardar btnGuardar = new Button(); btnGuardar.setCaption("Guardar"); btnGuardar.setImmediate(true); btnGuardar.setWidth("-1px"); btnGuardar.setHeight("-1px"); mainLayout.addComponent(btnGuardar, "top:20.0px;left:10.0px;"); // lblEvaluadores lblEvaluadores = new Label(); lblEvaluadores.setImmediate(false); lblEvaluadores.setWidth("-1px"); lblEvaluadores.setHeight("-1px"); lblEvaluadores.setValue("Evaluadores:"); mainLayout.addComponent(lblEvaluadores, "top:80.0px;left:30.0px;"); // btnAgregar btnAgregar = new Button(); btnAgregar.setCaption(">"); btnAgregar.setImmediate(true); btnAgregar.setWidth("-1px"); btnAgregar.setHeight("-1px"); mainLayout.addComponent(btnAgregar, "top:160.0px;left:204.0px;"); // btnRemover btnRemover = new Button(); btnRemover.setCaption("<"); btnRemover.setImmediate(true); btnRemover.setWidth("-1px"); btnRemover.setHeight("-1px"); mainLayout.addComponent(btnRemover, "top:234.0px;left:204.0px;"); // lblRoles lblRoles = new Label(); lblRoles.setImmediate(false); lblRoles.setWidth("-1px"); lblRoles.setHeight("-1px"); lblRoles.setValue("Roles:"); mainLayout.addComponent(lblRoles, "top:80.0px;left:280.0px;"); // tstRoles tstRoles = new TabSheet(); tstRoles.setImmediate(false); tstRoles.setWidth("410px"); tstRoles.setHeight("246px"); mainLayout.addComponent(tstRoles, "top:114.0px;left:280.0px;"); // lstEvaluadores lstEvaluadores = new ListSelect(); lstEvaluadores.setImmediate(false); lstEvaluadores.setWidth("140px"); lstEvaluadores.setHeight("246px"); mainLayout.addComponent(lstEvaluadores, "top:114.0px;left:20.0px;"); return mainLayout; } }