Tuesday, July 16, 2013

Utilizar <p:pictList> con DataClassG y JSFGenerator

Al presionar el botón Dialogo se muestra un dialogo que invocara el proceso

Al dar clic en Seleccionar mostrar invocara el proceso

Codigo fuente del Converter 

Crear el Converter
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.mavenproject1.beans.Usuarios;
import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

/**
*
* @author avbravo
*/
@FacesConverter(value = "primeFacesPickListConverter")
public class PrimeFacesPickListConverter implements Converter {
       @Override
       public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
           Object ret = null;
           if (arg1 instanceof PickList) {
               Object dualList = ((PickList) arg1).getValue();
               DualListModel dl = (DualListModel) dualList;
               for (Object o : dl.getSource()) {
                   String id = "";
                   if (o instanceof Usuarios) {
                       id += ((Usuarios) o).getUsername();
                   }

                   if (arg2.equals(id)) {
                       ret = o;
                       break;
                   }
               }
               if (ret == null)
                   for (Object o : dl.getTarget()) {
                           String id = "";
                           if (o instanceof Usuarios) {
                               id += ((Usuarios) o).getUsername();
                           }

                       if (arg2.equals(id)) {
                           ret = o;
                           break;
                       }
                   }
           }
           return ret;
       }

       @Override
       public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
           String str = "";
           if (arg2 instanceof Usuarios) {
               str = "" + ((Usuarios) arg2).getUsername();
           }

           return str;
       }

   
}







EN EL MANAGED BEANS UsuariosData.java
  • Crear un List<> para source y target
   List<Usuarios> source = new ArrayList<>();
   List<Usuarios> target = new ArrayList<>();
  • Crear un DualListModel
private DualListModel<Usuarios> usuariosPick;

   public DualListModel<Usuarios> getUsuariosPick() {
       return usuariosPick;
   }

   public void setUsuariosPick(DualListModel<Usuarios> usuariosPick) {
       this.usuariosPick = usuariosPick;
   }



  • En el metodo init cargar la lista en source y en el usuariosPick
 @PostConstruct
   public void init() {
//           ---udpate with your filter;
       selectAll();

       source = usuarioslist;
       usuariosPick = new DualListModel<>(source, target);
   }



  • Crear el metodo procesarPickList()
public String procesarPickList() {
       try {
           for(Usuarios u:usuariosPick.getTarget()){
              JSFUtil.addSuccessMessage("u1 ==="+u.getNombre()+ " "+u.getPassword());
           }
       } catch (Exception e) {
           JSFUtil.addErrorMessage(e.getLocalizedMessage());
       }
       return "";
   }



EN EL XHTML
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:ui="http://java.sun.com/jsf/facelets"
     xmlns:p="http://primefaces.org/ui"
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:h="http://java.sun.com/jsf/html">
   <h:body>

       <ui:composition template="./../../templates.xhtml">
           <!--
            <ui:define name="toformRenderedp">
               top
            </ui:define>
           -->
           <!--
            <ui:define name="left">
               left
            </ui:define>
           -->
           <ui:define name="content">
               <h:form id="formsprintmeetingasistentesInsert"  rendered="#{menuBeans.sprintmeetingasistentesCrear}">
                   <p:growl id="growl"  />
              



                   <p:pickList id="pickList1" value="#{usuariosData.usuariosPick}" var="var"   
                               itemLabel="#{var.nombre}" itemValue="#{var}" effect="bounce"    
                               converter="primeFacesPickListConverter"
                               showSourceControls="true" showTargetControls="true"
                               showSourceFilter="true" showTargetFilter="true" filterMatchMode="contains">  
                       <f:facet name="sourceCaption">Available</f:facet>
                       <f:facet name="targetCaption">Selected</f:facet>



                       <p:column >
                           #{var.nombre}  
                       </p:column>
                       <p:column>
                           #{var.cedula}
                       </p:column>

                   </p:pickList>
                   <p:commandButton id="citySubmit" value="Dialogo" update=":formsprintmeetingasistentesInsert:displayCities" oncomplete="cityDialog.show()" style="margin-top:5px"/>  
                   <p:commandButton update=":formsprintmeetingasistentesInsert:growl" value="Seleccionar" action="#{usuariosData.procesarPickList()}"/>




                   <p:dialog header="Seleccionados" modal="true" closable="false" showEffect="fade" hideEffect="fade" widgetVar="cityDialog">  
                       <h:panelGrid id="displayCities" columns="2">  
                           <!--
                             <h:outputText value="Source: " style="font-weight:bold"/>  
                             <ui:repeat value="#{usuariosData.usuariosPick.source}" var="item">  
                                 <h:outputText value="#{item}" style="margin-right:5px" />  
                             </ui:repeat>  
                           -->
                           <h:outputText value="Target: " style="font-weight:bold"/>  
                        <!--
                           <ui:repeat value="#{usuariosData.usuariosPick.target}" var="item">  
                               <h:outputText value="#{item}" style="margin-right:5px" />  
                           </ui:repeat>  
                           -->
                          
                             <p:commandButton update=":formsprintmeetingasistentesInsert:growl" onclick="confirmation.hide()" value="Dialogo" action="#{usuariosData.procesarPickList()}"/>
                           
                       </h:panelGrid>  

                   </p:dialog>  
               </h:form>

               <h:form rendered="#{!menuBeans.sprintmeetingasistentesCrear}">
                   <p:panelGrid columns="1">
                       <h:outputText value="#{mensajes['login.accesodenegado']}"/>
                       <h:outputText value="#{mensajes['login.accesodenegadoDetalle']}"/>
                       <p:commandButton action="#{loginBean.irLogin}" value="#{mensajes['boton.regresar']}" ajax="false"/>
                   </p:panelGrid>
               </h:form>
           </ui:define>
           <!--
            <ui:define name="bottom">
               Bottom
            </ui:define>
           -->
       </ui:composition>

   </h:body>
</html>


No comments: