Wednesday, June 26, 2013

Jsf Richfaces3.3.3 final Picklist example

JSP Code





Move control buttons without labels
<rich:pickList id="pickListId"  valueChangeListener="#{rolePermissionsBean.selectionChanged}" label="source List" title="pickList"
            immediate="true"
            value="#{rolePermissionsBean.selectedModuleList}" showButtonsLabel="false" listsHeight="150; width:239px;">
            <a4j:support event="picklistChanged" ajaxSingle="true" actionListener="#{rolePermissionsBean.listChanged}" >
                </a4j:support>
                <f:selectItems value="#{rolePermissionsBean.sourceModuleList}"/>
</rich:pickList>

Description Here,
1 : <rich:pickList id="pickListId"  valueChangeListener="#{rolePermissionsBean.selectionChanged}"
    value change listener will called when there is change in the pick list in left and right panel.

2 : value="#{rolePermissionsBean.selectedModuleList}" this indicate the pre selection of data in right panel

3 : <f:selectItems value="#{rolePermissionsBean.sourceModuleList}"/>  this indicates the Source list at left panel which can be move to right side .

4:   <a4j:support event="picklistChanged" ajaxSingle="true" actionListener="#{rolePermissionsBean.listChanged}" >  event to be called on selection.

5: Most important the Source and target (left and right)should be single list u should not define two list.

Backing Bean


    private List<Object> selectedModuleList;
    private List<ModuleVO> masterModuleList;
    private List<RoleVO> masterRoleList;
    private Map<Integer, ModuleVO>  masterServiceMap;
    private Map<Integer, RoleVO>  masterRoleMap;
    private AdminManager adminManager;
    private List<SelectItem> sourceModuleList;
    private int roleId = 0;
    private List<SelectItem> roleNameList;
    private String infoMessages;
    private String infoMessageImg="display:none";
   
    /**
     * Get the infoMessages.
     *
     * @return the infoMessages
     */
    public String getInfoMessages() {
        return infoMessages;
    }

    /**
     * Set the infoMessages to infoMessages.
     *
     * @param infoMessages the infoMessages to set
     */
    public void setInfoMessages(String infoMessages) {
        this.infoMessages = infoMessages;
    }

    /**
     * Get the infoMessageImg.
     *
     * @return the infoMessageImg
     */
    public String getInfoMessageImg() {
        return infoMessageImg;
    }

    /**
     * Set the infoMessageImg to infoMessageImg.
     *
     * @param infoMessageImg the infoMessageImg to set
     */
    public void setInfoMessageImg(String infoMessageImg) {
        this.infoMessageImg = infoMessageImg;
    }
    /**
     * Get the roleNameList.
     *
     * @return the roleNameList
     */
    public List<SelectItem> getRoleNameList() {
        //initRoles();
        return roleNameList;
    }

    /**
     * Set the roleNameList to roleNameList.
     *
     * @param roleNameList the roleNameList to set
     */
    public void setRoleNameList(List<SelectItem> roleNameList) {
        this.roleNameList = roleNameList;
    }
    /**
     * Get the masterServiceMap.
     *
     * @return the masterServiceMap
     */
    public Map<Integer, ModuleVO> getMasterServiceMap() {
        return masterServiceMap;
    }

    /**
     * Set the masterServiceMap to masterServiceMap.
     *
     * @param masterServiceMap the masterServiceMap to set
     */
    public void setMasterServiceMap(Map<Integer, ModuleVO> masterServiceMap) {
        this.masterServiceMap = masterServiceMap;
    }

    /**
     * Get the masterRoleMap.
     *
     * @return the masterRoleMap
     */
    public Map<Integer, RoleVO> getMasterRoleMap() {
        return masterRoleMap;
    }

    /**
     * Set the masterRoleMap to masterRoleMap.
     *
     * @param masterRoleMap the masterRoleMap to set
     */
    public void setMasterRoleMap(Map<Integer, RoleVO> masterRoleMap) {
        this.masterRoleMap = masterRoleMap;
    }
    @PostConstruct
    public void  initSourceModule() {
        try {
            System.out.println("Start initSourceService");
            ArgumentVO argumentVO = new ArgumentVO();
            argumentVO.setAll(true);
            masterModuleList = adminManager.getModuleList(argumentVO);
            if (masterModuleList != null && masterModuleList.size() > 0) {
                masterServiceMap = new HashMap<Integer,ModuleVO>();
                sourceModuleList = new ArrayList<SelectItem>();
                for (ModuleVO modulesVO : masterModuleList) {
                    masterServiceMap.put(modulesVO.getId(), modulesVO);
                    SelectItem sItem = new SelectItem(modulesVO.getId(), modulesVO.getName());
                    sourceModuleList.add(sItem);
                }
            }
            System.out.println("End initSourceService");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    @PostConstruct
    public void initRoles() {
        try {
            HttpSessionUtils httpSessionUtils = new HttpSessionUtils();
            int branchId = (Integer)httpSessionUtils.
            getAttribute(ApplicationConstants.USER_BRANCH_ID);
            ArgumentVO argumentVO = new ArgumentVO();
            argumentVO.setAll(false);
            //argumentVO.setBranchId(branchId);
            SelectItem selectItem = null;
            masterRoleList = adminManager.getRoleList(argumentVO);
           
            roleNameList = new ArrayList<SelectItem>();
            if(CommonUtils.isValidObject(masterRoleList)){
                masterRoleMap = new HashMap<Integer, RoleVO>();
                System.out.println("Size of roles "+masterRoleList.size());
                for (RoleVO role : masterRoleList) {
                    selectItem = new SelectItem();
                    selectItem.setLabel(""+role.getId());
                    selectItem.setValue(role.getName());
                   
                    roleNameList.add(selectItem);
                    masterRoleMap.put(role.getId(), role);
                }
            } else{
                System.out.println("No roles are exist");
            }
            System.out.println("ENd of initRoles ");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    @PostConstruct
    public void  initTargetModules() {
        try {
            System.out.println("Role Id in initTargetModules:" + this.roleId);
            RoleVO  roleVO = masterRoleMap.get(roleId);
             if (roleVO != null && roleVO.getModuleIdList() != null
                     && roleVO.getModuleIdList().size() > 0) {
                 selectedModuleList  = new ArrayList<Object>();
                for (Integer moduleId : roleVO.getModuleIdList()) {
                    System.out.println("Selected Module :"
                            + moduleId);
                    selectedModuleList.add(moduleId);
                }
                System.out.println("selected Module List size is:>>>>>>"+selectedModuleList.size());
             } else {
                 selectedModuleList  = new ArrayList<Object>();
                 System.out.println("No data selected!");
             }
             System.out.println("End of initTargetModules");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    private String selectedInfo;
   
     /**
     * Get the selectedInfo.
     *
     * @return the selectedInfo
     */
    public String getSelectedInfo() {
        return selectedInfo;
    }

    /**
     * Set the selectedInfo to selectedInfo.
     *
     * @param selectedInfo the selectedInfo to set
     */
    public void setSelectedInfo(String selectedInfo) {
        this.selectedInfo = selectedInfo;
    }
    public void selectionChanged(ValueChangeEvent evt){
        try {
           
            System.out.println("Calling Selection Changed Listener");
            System.out.println("New value is:"+evt.getNewValue());
            System.out.println("Old value is:"+evt.getOldValue());
            Object newObj = evt.getNewValue();
            if (newObj instanceof List) {
                System.out.println("List Object");
                List list = (List)newObj;
                if (list != null && list.size() > 0) {/*
                    System.out.println("Size of List:" + list.size());
                    System.out.println("contents :" + list);
                    for (Object object : list) {
                        System.out.println("obj : " + object);
                        System.out.println("hash code: " + object.hashCode());
                        if (object instanceof ServicesVO) {
                            ServicesVO ss= (ServicesVO)object;
                            System.out.println("Service :" + ss.getName());
                        } else if (object instanceof Integer) {
                            System.out.println("Integer vo");
                        } else if (object instanceof String){
                            String st = (String)object;
                            System.out.println("String value: "+st);
                        } else {
                            System.out.println("Not object");
                        }   
                    }
                */} else {
                    System.out.println("List is null!");
                }
            }
            } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    /**
     * Get the sourceModuleList.
     *
     * @return the sourceModuleList
     */
    public List<SelectItem> getSourceModuleList() {
        return sourceModuleList;
    }

    /**
     * Set the sourceModuleList to sourceModuleList.
     *
     * @param sourceModuleList the sourceModuleList to set
     */
    public void setSourceModuleList(List<SelectItem> sourceModuleList) {
        this.sourceModuleList = sourceModuleList;
    }
    /**
     * Get the selectedModuleList.
     *
     * @return the selectedModuleList
     */
    public List<Object> getSelectedModuleList() {
        return selectedModuleList;
    }

    /**
     * Set the selectedModuleList to selectedModuleList.
     *
     * @param selectedModuleList the selectedModuleList to set
     */
    public void setSelectedModuleList(List<Object> selectedModuleList) {
        this.selectedModuleList = selectedModuleList;
    }
    /**
     * Get the masterModuleList.
     *
     * @return the masterModuleList
     */
    public List<ModuleVO> getMasterModuleList() {
        return masterModuleList;
    }

    /**
     * Set the masterModuleList to masterModuleList.
     *
     * @param masterModuleList the masterModuleList to set
     */
    public void setMasterModuleList(List<ModuleVO> masterModuleList) {
        this.masterModuleList = masterModuleList;
    }

    /**
     * Get the masterRoleList.
     *
     * @return the masterRoleList
     */
    public List<RoleVO> getMasterRoleList() {
        return masterRoleList;
    }

    /**
     * Set the masterRoleList to masterRoleList.
     *
     * @param masterRoleList the masterRoleList to set
     */
    public void setMasterRoleList(List<RoleVO> masterRoleList) {
        this.masterRoleList = masterRoleList;
    }
    /**
     * Get the adminManager.
     *
     * @return the adminManager
     */
    public AdminManager getAdminManager() {
        return adminManager;
    }
    /**
     * Set the adminManager to adminManager.
     *
     * @param adminManager the adminManager to set
     */
    public void setAdminManager(AdminManager adminManager) {
        this.adminManager = adminManager;
    }
   
    /**
     * Method to capture the valueChange Event for counters
     * @param event
     *   An instance of <tt>javax.faces.event.ValueChangeEvent</tt>
     */
    public void valueChangeListener(javax.faces.event.ValueChangeEvent event){
        System.out.println("Inside of value change evnet interface");
        try {
            System.out.println("valueChangeListener():");
            //String counterIdStr = (String) event.getNewValue();
            int roleId = 0;
            UIComboBox comboBox = (UIComboBox)event.getComponent();
            if ( (event.getComponent().getId().equalsIgnoreCase("roleComboId")
                    )    && CommonUtils.isValidObject(comboBox.getValue()) ) {
                roleId = Integer.parseInt(getValue(roleNameList,
                                        ""+comboBox.getValue()));
            }
            System.out.println("Role ID  :  " + roleId);
            selectedModuleList = null;
            sourceModuleList = null;
            this.roleId = roleId;
            initTargetModules();
            initSourceModule();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public void actionListener(ActionEvent event){
        System.out.println("Inside of action Listener event");
        try {
           
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public void listChanged(ActionEvent actionEvent) {
        try {
            System.out.println("List changes Listener event");
            int sid = 0;
            HtmlPickList pickList = (HtmlPickList)actionEvent.getComponent();
            if ( (actionEvent.getComponent().getId().equalsIgnoreCase("pickListId")
                    )    && CommonUtils.isValidObject(pickList.getValue()) ) {
                sid = Integer.parseInt(getValue(sourceModuleList,
                                        ""+pickList.getValue()));
            }
            System.out.println("pickList Item ID  :  " + sid);
        //    actionEvent.get     
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    public String getValue(List<SelectItem> itemlist,String label){
        String id ="";
        try{
            if ( CommonUtils.isValidObject(itemlist)
                    && StringUtils.isValidString(label)
                    && itemlist.size() >0 ){
                if(CommonUtils.isValidObject(itemlist)){
                    for(SelectItem item : itemlist){
                        if(item.getValue().equals(label)){
                            id = item.getLabel();
                            break;
                        }
                    }
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return id;
    }
    public boolean saveSelectedRoleModuleList() {
        RoleVO roleVO = null;
        List<Integer> moduleIdArray = null;
        boolean isAddEdit = false;
        try {
            System.out.println(" Inside saveSelectedRoleModuleList");
            if (this.roleId > 0) {
                roleVO = masterRoleMap.get(this.roleId);
                roleVO.setModuleIdList(null);
                roleVO.setModuleList(null);
                if (CommonUtils.isValidObject(selectedModuleList) && selectedModuleList.size()>0 ) {
                    moduleIdArray = new ArrayList<Integer>();
                    //int count = 0;
                    System.out.println("Size of Selected Module:" + selectedModuleList.size());
                    for (Object modulesVO : selectedModuleList) {
                        System.out.println("Final ID :" +  modulesVO);
                        moduleIdArray.add(Integer.parseInt(modulesVO.toString()));
                    }
                } else {
                    System.out.println("selected Role Module list is null");
                }
                roleVO.setModuleIdList(moduleIdArray);
                isAddEdit = adminManager.saveOrUpdateRoles(roleVO);
                MessageProperties properties = MessageProperties.getInstance();
                if(isAddEdit) {
                    infoMessages = properties.getValueByKey(
                            MessageConstants.ADMIN_ROLEPERMISSION_ADD_SUCCESS);
                    infoMessageImg = "../images/successicon.png";
                } else {
                    infoMessages = properties.getValueByKey(
                            MessageConstants.ADMIN_ROLEPERMISSION_ADD_FAILURE);
                    infoMessageImg = "../images/erroricon.png";
                }
            }
            //sourceServiceList.clear();
            this.roleId = 0;
            initRoles();
            initTargetModules();
            initSourceModule();
            FacesContext.getCurrentInstance().getExternalContext()
            .redirect(URLConstants.MANAGE_ROLE_PERMISSION_PAGE);
        } catch (ServiceException exception) {
            exception.printStackTrace();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return isAddEdit;
       
    }
   




Where : adminManager Object is to get the Data list for Module and Role List from database.

12 comments:

  1. Could you tell me how can I add the HtmlPickList.class to my project?

    ReplyDelete
  2. Great post. Needed to write simple word that Thanks for suggestions. Keep it up! best sap simple finance online training institute in hyderabad

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I loved this blog so much very informative
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. The motto of the Sanjary kids is to provide good atmosphere to the kids.Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and provides Teacher Training Program.We have the both indoor and outdoor activities for your children.We build a strong value foundation for your child on Psychology and Personality development.
    Preschool in hyderabad

    ReplyDelete
  5. Excellent blog I was inspired and liked it
    Best QA / QC Course in India, Hyderabad. sanjaryacademy is a well-known institute. We have offer professional Engineering Course like Piping Design Course, QA / QC Course,document Controller course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course.
    QA / QC Course

    ReplyDelete
  6. Good Post
    Sanjary kids is the best playschool, preschool in Hyderabad, India. Start your play school,preschool in Hyderabad with sanjary kids. Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and Teacher Training Program.
    play school in hyderabad, India
    Preschool in hyderabad, India
    Preschool teacher training course in hyderabad, India
    pre and primary teacher training course in hyderabad,India
    early childhood teacher training course in hyderabad, India

    ReplyDelete
  7. Thanks for sharing information
    "Yaaron media is one of the rapidly growing digital marketing company in Hyderabad,india.Grow your business or brand name with best online, digital marketing companies in ameerpet, Hyderabad. Our Services digitalmarketing, SEO, SEM, SMO, SMM, e-mail marketing, webdesigning & development, mobile appilcation.
    "
    Best web designing companies in Hyderabad
    Best web designing & development companies in Hyderabad
    Best web development companies in Hyderabad

    ReplyDelete
  8. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    Rajasthan University BCOM 1st, 2nd & Final Year Time Table 2020

    ReplyDelete
  9. Hi there colleagues, good paragraph and good urging commented here, I am genuinely enjoying by these. Kumaun University BA Part 1 Result

    ReplyDelete
  10. Regarding Java 11, I am not sure of any books. Either you can go through OCJP6/8 and then skip unwanted chapters and then learn Java 11 features from online.
    React Training in Gurgaon
    Java Framawork Training in Gurgaon

    ReplyDelete