3 Replies Latest reply on Jun 14, 2013 3:00 AM by ktulu

    picklist update target list

    ktulu

      Hi all,

      I'm facing a problem updating the target side of the picklist (richefaces 4.3).

      Here is my code :

       

      xhtml:

                         

                          <h:selectOneMenu value="#{myCtrl.option}" >

                              <f:selectItems value="#{userBean.options}" />

                          </h:selectOneMenu>

                          <h:commandButton value="load" action="#{myCtrl.updateList}"/>

       

                                      <rich:pickList value="#{listBean.selection}"

                                                                       var="item"

                                                                       listWidth="300px"

                                                                       listHeight="300px"

                                                                       id="my-pl"

                                                                       >

                                          <f:selectItems value="#{listBean.toSelect}" />

                                          <f:converter converterId="itemConverter"  />

                                          <rich:column>

                                              <f:facet name="header">Column1</f:facet>

                                              #{item.col1}

                                          </rich:column>

                                          <rich:column>

                                              <f:facet name="header">Column2</f:facet>

                                              #{item.col2}

                                          </rich:column>

                                      </rich:pickList>

                          <h:commandButton value="OK" action="#{myCtrl.save}"/>

       

      converter:

      @FacesConverter("itemConverter")

      public class ItemConverter implements Converter {

       

                @ManagedProperty(value="#{userBean}")

                private UserBean userBean;

       

                public MarkConverter() {

                          // TODO Auto-generated constructor stub

                }

       

       

                @Override

                public Object getAsObject(FacesContext fc, UIComponent arg1, String s) {

       

                               for (Item m : getUserBean().getItems()) {

                  if (m.getCol1().equals(s)) {

                      return m;

                  }

              }

              return null;

                }

       

                @Override

                public String getAsString(FacesContext arg0, UIComponent arg1, Object o) {

       

                               if (o == null) return null;

              return ((Item) o).getCol1();

                }

       

       

                public UserBean getUserBean() {

                               return userBean;

                }

       

       

                public void setUserBean(UserBean userBean) {

                               this.userBean = userBean;

                }

      }

       

      listBean:

       

      @ManagedBean(name="listBean")

      @SessionScoped

      public class ListBean {

       

                private List<Item> toSelect;

                private List<Item> selection;

       

                public List<Item> getToSelect() {

                               return toSelect;

                }

       

                public void setToSelect(List<Item> toSelect) {

                               this.toSelect = toSelect;

                }

       

                public List<Item> getSelection() {

                               return selection;

                }

       

                public void setSelection(List<Item> selection) {

                               this.selection = selection;

                }

       

                public ListBean() {

                }

       

      }

       

      myCtrl:

       

      @ManagedBean(name="myCtrl")

      public class MyController implements Serializable {

       

                private Logger log = Logger.getLogger(MyController.class);

       

                @ManagedProperty(value="#{userBean}")

                private UserBean userbean;

       

                @ManagedProperty(value="#{listBean}")

                private ListBean listBean;

       

                private String option;

       

                public String initItems() {

       

                               log.debug("initializing items");

                               if (listBean.getToSelect() == null || listBean.getToSelect().isEmpty()) {

       

                                              log.debug("initializing toselect");

       

                  List<Item> list = new ArrayList<Item>();

                  list.addAll(userbean.getItems());

                  listBean.setToSelect(list);

                               }

       

                               return "myPage";

                }

       

                /**

         *

                 */

                public String updateItems() {

       

                               List<Item> toselect = new ArrayList<Item>();

                               List<Item> selection = new ArrayList<Item>();

       

             // process to fill the lists //

       

             listBean.setSelection(selection);

             listBean.setToSelect(toselect);

       

             log.debug("list selection size=" + listBean.getSelection().size());

             log.debug("list toselect size=" + listBean.getToSelect().size());

       

             return "myPage";

                }

       

                public MyController() {

                }

       

       

                public String save() {

             log.debug("saving itemss");

                               for (Item i : listBean.getSelection()) {

                                              log.debug("item selected : " + m.getCol1());

                               }

                               return "myPage";

                }

       

       

                public UserBean getUserbean() {

                               return userbean;

                }

       

       

                public void setUserbean(UserBean userbean) {

                               this.userbean = userbean;

                }

       

                public ListBean getListBean() {

                               return listBean;

                }

       

       

                public void setDelegationList(ListBean listBean) {

                               this.listBean = listBean;

                }

       

                public String getOption() {

                               return option;

                }

       

       

                public void setOption(String option) {

                               this.option = option;

                }

       

      }

       

      the problem :

      When I click on the "OK" button, the save action is launch, I'm getting the selection and when I'm back on the page, the picklist is filled properly on the both side. But when I click on the "load" button, I'm properly setting the lists in the listBean (according to the logs) but when I'm back on the page, only the left side of the picklist is updated, the target list remains empty.

      I've tried a lot of workaround by I don't have any more idea...

        • 1. Re: picklist update target list
          bleathem

          In your Item Object, do you have you overridden both equals and hashCode?  It's unfortunate that overriding hashCode is required, we plan to correct this in a future release.

           

          See this thread for details:

          https://community.jboss.org/message/822827

          • 2. Re: picklist update target list
            sivaprasad9394

            Do override the below methods in your entity class to avoid problems(same object) in moving data from lefe to right and vice versa,

             

            @Override

                public boolean equals(Object obj) {

                    if (!((obj != null) && obj instanceof EntityObj)) {

                        return false;

                    }

             

                    EntityObj  rSubType = (EntityObj) obj;

             

                    if (this.getPrimarykeyId()!=null && this.getprimaryKeyId().equals(rSubType.getPrimarykeyId())) {

                        return true;

                    }

             

                    return false;

                }

             

                @Override

                public int hashCode() {

                    return (this.primarykeyId== null) ? 0 : primaryKeyId.hashCode();

                }

            • 3. Re: picklist update target list
              ktulu

              Thanks for you reply.

               

              I've seen these equals and hashCode issues but the problem was somewhere else.

               

              I've finally fixed the problem which was the way I was initializing the lists.

              Under // process to fill the lists // in myCtrl,  I was filling the toSelect list with all the available items but the selected ones, which were placed in the selection list.

              I've changed it to keep all the available items in the toSelect list regardless what was in the selection list.

               

              Thanks again...