1 2 3 4 5 Previous Next 61 Replies Latest reply on Sep 19, 2006 11:39 AM by cptnkirk Go to original post
      • 30. Re: @SelectItems and @SelectItemsSelection annotations?

        Sry, isDirty() == false, then no binding? Binary dyslexia. :)

        • 31. Re: @SelectItems and @SelectItemsSelection annotations?

          JBSEAM-241 and JBSEAM-242 are necessary for some of the more complex data bindings. Any chance they'll make it into 1.0 final? Because this request changes method sigs on the DataBinder/DataSelector interfaces it's important to get the changes in now before ppl start developing against the existing interfaces. If it's a matter of man power let me know and I'll work on a patch.

          An example of a complex data binding that needs access to the annotation would be a generic SelectItems binder able to do arbitrary property binding. Without access to the annotation one would need to develop binders per type, UserSelectItems, BooksSelectItems, etc. Not impossible but not desirable.

          @SelectItems(labelMethod="getName", disabledMethod="isDisabled")
          List<Users> ...
          
          @SelectItems(labelMethod="getTitle")
          List<Books> ...


          • 32. Re: @SelectItems and @SelectItemsSelection annotations?
            gavin.king

            Its done in CVS

            • 33. Re: @SelectItems and @SelectItemsSelection annotations?

              Looks good, my DataBinder works fine. Is getSelection() supposed to be on both DataBinder and DataSelector interfaces? What is role of this method on DataBinder?

              • 34. Re: @SelectItems and @SelectItemsSelection annotations?

                Does anyone have an example of this ? I'm trying to create my own @SelectItems and @SelectItemsSelection and I can't get the DataSelector implementation to fire.

                So I have a couple of questions:

                1) What do I put in the value attribute of a <h:selectOneMenu/> tag ? Should it be just a regular bijected field of the same type as the Entity that is used in the List that I have marked up with @SelectItemsSelection ?

                2) Do I need - as Peter Muir has suggested above - to re-read the List from the EntityManager ? This seems wasteful to me. Should I get the List from the Seam context ? Essentially - how do I convert the value from the <h:selectOneMenu/> in JSF into the selected Entity value ?

                Thanks in advance,

                • 35. Re: @SelectItems and @SelectItemsSelection annotations?

                  SelectItems are a pain in that they are a model that does not store selected state. The Seam DataBinding framework was designed for binding and selecting of model objects that do store extra selected state, like DataModel.

                  If you want to go the easiest route you can simply not have a DataSelector for your SelectItems. Simply use the <h:selectOneMenu/> value attribute and couple this to a property on your controller. Where selector is the controller and items are the bijected SelectItems list.

                  <h:selectOneListbox size="10" value="#{selector.selected}">
                   <f:selectItems value="#{items}"/>
                  </h:selectOneListbox>
                  


                   private String selected = "Mark";
                  
                   public String getSelected()
                   {
                   return selected;
                   }
                  
                   public void setSelected(String selected)
                   {
                   this.selected = selected;
                   }
                  
                   @SelectItems(valueStrategy=SelectItems.Strategy.STRING)
                   public List items;
                  
                   @Factory("items")
                   public List getItems()
                   {
                   items = new ArrayList();
                   items.add("Jim");
                   items.add("Dan");
                   items.add("Mark");
                   ArrayList<String> subgroup = new ArrayList<String>();
                   subgroup.add("Other Developers");
                   subgroup.add("Steve");
                   subgroup.add("Ananda");
                   items.add(subgroup);
                   return items;
                   }
                  


                  To answer your question about the selector firing. Unless something is returned from DataBinder.getSelection() your selector won't fire. And since <h:selectOneMenu/> doesn't set the selected state on your #items, it's hard to do anything with the getSelection() method.

                  I've been happy with the solution above, however if you really needed SelectItemsSelection support like DataModelSelection, then instead of a DataSelector, I'd write another DataBinder for your <h:selectOneMenu/> value object.

                  Add a 'for' attribute to your SelectItemsSelection annotation so that you can get to it later. Then within your SelectItemsSelectionBinder's wrap/getWrappedData methods, do a lookup for your scoped SelectItems component and do the translation based on whatever value strategy you're using for your SelectItems (might need to include valueStrategy attribute in your SelectItemsSelection annotation).

                  This might be a fun project for the weekend. If I get to it I'll try and post my solution. Hope this helps.

                  -Jim

                  • 36. Re: @SelectItems and @SelectItemsSelection annotations?

                    Thanks Jim.

                    This helps a lot - it confirms what I understood, but thought that I didn't and you provided me with a critical bit of information (SelectOneMenu doesn't actually set a selection on the SelectItems).

                    I thought I was 'missing a trick' - the first approach seems sufficient for most purposes. My only concern here is that it introduces 'binding and conversion' code into my action - which doesn't feel like business logic.

                    I can give the second approach a go this weekend also - I think that it will work and it gives me a chance to learn more about annotations.

                    I do wonder if there is a third attack on this problem, which is a not annotation based at all. A JSF component - essentially a UIDataModel based component that renders an HTML Select and sets the selected state on the appropriate item. It would be a single action element (tag) that takes DataModel EL expression and another EL expression that resolves the Label. It could do more than this (multi value binding ?). Maybe this is another item for my weekend.

                    This may obviate the need for @SelectItems and @SelectItemsSelection - allowing @DataModel and @DataModelSelection to work.

                    Thanks again,

                    • 37. Re: @SelectItems and @SelectItemsSelection annotations?
                      vlasov01

                      Hello,

                      Is it possible to use this approach to select multiple items?

                      Regards,

                      Sergey

                      • 38. Re: @SelectItems and @SelectItemsSelection annotations?

                        It should be, however your SelectItemsSelector will need to be smart enough to handle either the SelectOne or SelectMany case. Since the SelectMany's will allow different value types to be returned than SelectOne, this could be slightly more work, but should be possible with a little type checking.

                        • 39. Re: @SelectItems and @SelectItemsSelection annotations?
                          vlasov01

                          Jim,

                          Could you please post a complete example (WAR) at seam wiki. It will be very helpful.

                          Regards,

                          Sergey

                          • 40. Re: @SelectItems and @SelectItemsSelection annotations?

                            :) That's a little bit more time consuming than pointing you in the right direction. Unfortunately my time this weekend was more limited than I expected. My SelectItems binder is complete but my SelectedItems binder is not.

                            If you'd like to be able to use a @SelectItems annotation to bind a list of arbitrary objects to SelectItems, then I can post that. If you're looking for the selected translation you'll probably have to wait a few more days.

                            -Jim

                            • 41. Re: @SelectItems and @SelectItemsSelection annotations?

                              In response to many requests I've decided to post my @SelectItems DataBinder to the Seam Wiki.

                              http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossSeam

                              At present it's only a DataBinder and doesn't support selection (you can still use JSF EL to bind selected values to your controller).

                              Comments welcome.

                              -Jim

                              • 42. Re: @SelectItems and @SelectItemsSelection annotations?
                                gavin.king

                                 

                                In response to many requests I've decided to post my @SelectItems DataBinder to the Seam Wiki.


                                Thanks man :)

                                • 43. Re: @SelectItems and @SelectItemsSelection annotations?
                                  vlasov01

                                  Thank you a lot!

                                  • 44. Re: @SelectItems and @SelectItemsSelection annotations?

                                    I tweaked your wrap method to add some extra functionality.

                                    I added the following annotation property to SelectItems.

                                     /**
                                     * If specified along with Strategy.OBJECT, and new
                                     * object with the specified empty lable will be added.
                                     *
                                     * @return The method to call for a custom disabled value.
                                     */
                                     String emptyValueLabel() default "";
                                    


                                    I modified the wrap methond on SelectItemsBinder to add an empty object with the specified emptyValueLabe.

                                    I needed this because my users do not like when their drop downs are set to a value already and I wanted to control this where I load the collection.

                                    I also needed this for an Excel style autofilter that I implemented. I need a select option labled "Any" so I can conditionally include parts of a dynamically generated HQL query.

                                     public List<SelectItem> wrap(SelectItems ann, List list)
                                     {
                                     if (list != null)
                                     {
                                     WrappingArrayList<SelectItem> selectItems = new WrappingArrayList<SelectItem>();
                                     selectItems.setWrappedData(list);
                                    
                                     if (ann.emptyValueLabel().length() > 0 && ann.valueStrategy() == SelectItems.Strategy.OBJECT)
                                     {
                                     try
                                     {
                                     // Get an new instance of the type of the first object in the list.
                                     // If there is a problem do nothing.
                                     Object obj = list.get(0).getClass().newInstance();
                                     selectItems.add(new SelectItem(obj, ann.emptyValueLabel()));
                                     } catch (Exception e) { }
                                     }
                                     for (int i = 0; i < list.size(); i++)
                                     {
                                     selectItems.add(convertToSelectItem(String.valueOf(i), list.get(i), ann));
                                     }
                                     return selectItems;
                                     } else
                                     {
                                     return null;
                                     }
                                     }
                                    


                                    The annotaion in use looks like this.
                                     @SelectItems(valueStrategy = SelectItems.Strategy.OBJECT,
                                     labelMethod = "getGrade",
                                     emptyValueLabel = "Any")
                                     private List gradeSelectItems;
                                    


                                    I would like to hear others comments for improvement of this technique and expanding the funtionality of this in other ways.

                                    The autofilter is much appreciated by my users, but still takes too much effort to set up.

                                    I would like for it to me a pluggable configurable component.

                                    Has anyone developed something like this yet?