TwinColSelect: how to use it in a BeanItem based form

Hi all,

I’m creating a form which is based on a BeanItem. Now I want to add a
TwinColSelect
component. But I’m unsure how to implement this in a correct way.

The
TwinColSelect
is added to my form by field factory:


public Field createField(Item item, Object propertyId, Component uiContext) {
...
if ("my_field".equals(propertyId)) {
  TwinColSelect tcs = new TwinColSelect("My Caption");
  tcs.setRequired(false);
  return tcs;
}
...

My question now is how to build the bean? After reading the post
TwinColSelect exception on…
I tried it like this:


private Set<String> my_field = null;

public Set<String> getMy_field() {
  HashSet<String> hs = new HashSet<String>();
  hs.add("Entry 1");
  hs.add("Entry 2");
  return hs;
}

public void setMy_field(Set<String> my_field) {
  this.my_field = my_field;
}

The effect is that the
TwinColSelect
appears in the form, but there are no entries. I also tried
List
instead of
Set
: the same!

Could anybody give me a hint how to do this? Any examples?

Thanks,
Thorsten

••••••
Eclipse 3.6 • Vaadin 6.4.8 • Vaadin Eclipse Integration 1.3.0 • JRE 6
Windows XP • IE7 (7.0.5730) • Tomcat 5.5.28 • SQL Server 2005

Well, I am not an expert, but I have used TwinColSelect in my project and it works just fine. Logically, my code is pretty similar to yours. However, I hesitate that u have populated the possible variants for your TwinColSelect. In other words - filled the container with some possible data.


    public TeamSelect(String caption)
    {
      super(caption);
      setHeight("100%");
      setWidth("100%");
      setImmediate(true);
      ApplicationController.setCurrentNamespace("....");
      final List<Worker> availableWorkers = EntityFetcher
          .fetchEntitiesList(Worker.class);
      container = new BeanItemContainer<Worker>(availableWorkers);
      setContainerDataSource(container);
      setItemCaptionPropertyId("fullName");
    }

Thats the proto of the class that I use. Maybe it’ll help.

Hi,

thanks for your answer. But my case is slightly different because I do not set the data container for myself. Instead of that I use a
BeanItem
based form and the related field factory.

There must be (or maybe not?) an automatic matching between bean field and form. But how to build this field?

Regards, Thorsten

Well, probably you misunderstand the way this field works. The contents of your property inside of the bean represent the VALUE of the TwinColSelect. But the value is just the subset of possible variants. In your case the set of possibles is empty. That is why you don’t see anything in your TwinColSelect.

I also use this for the form that represents the project. TwinColSelect is used for the team field. There is a number of employees, but only some of them are taking part in this or that project. But you can always add or remove somebody. Thus, my TwinColSelect is populated with the whole crew and the value of it - is the set of workers from the team-field of the current BeanItem.

You are probably close. Using Set for both the getter/setter in the bean is key to getting and setting the values of the selected items.

When you build the TwinColSelect, you need to specify the possible values and labels that should be shown. It is easier when the display label and value are the same, but I created mine using something like:

    private TwinColSelect createSelectBox(String vaadinMessageCaption) {
    	TwinColSelect selectList = new TwinColSelect(vaadinApp.getMsg(vaadinMessageCaption));
    	selectList.setWidth(100, UNITS_PERCENTAGE);
		
		// Set all the possible values
		for( int i=0; i < allIds.length; ++i ) {
			selectList.addItem(allIds[ i ]
);
		}
		// Associate labels with our ID values
		selectList.setItemCaptionMode(TwinColSelect.ITEM_CAPTION_MODE_EXPLICIT);
		for( int i=0; i < allIds.length; ++i ) {
			selectList.setItemCaption(allIds[ i ]
, allNames[ i ]
.toString());
		}
		
		selectList.setRows( Math.min(allIds.length,5) );
		selectList.setNullSelectionAllowed(true);
		selectList.setMultiSelect(true);
		return selectList;
    }

I think if the values set as items are the same as the labels that should be displayed, you don’t need to do the mapping (associate labels with our ID values) and setItemCaptionMode as it’s the default. But in my case above, the value is a unique ID, and the labels are the “name” of the item.

Well, yeah. That what i was talking about. But in my case i have some other bean objects as a possible values for TwinColSelect. So instead of setting the label for every single value setItemCaptionPropertyId(…) could be used.

OK - I’ve got it now :grin:

Thanks for your input!

Regards,
Thorsten