Object as a property and Form with combobox

Hello everyone,
I have several problems in working with forms linked to items that have objects as properties. My object is a POJO with several fields, and one of these is a foreign key (in a database). The objects look like this:


public class Entity {
   protected int id;
   protected String name;
   // other fields...
   protected ForeignKey relation;

   // Constructor, getters and setters, equal and toString...
}

public class ForeignKey {
   protected int id;
   protected String name;

   // Constructor, getters and setters, equal and toString...
}

I set to the form the Entity object encapsulated into a BeanItem as datasource, and I wrote a fieldfactory like this:


public class EntityFieldFactory extends DefaultFieldFactory {
    public static List<Field> customFields=new ArrayList(); // These are comboboxes filled with BeanItem(ForeignKey)
    public static List<String> customFieldsNames=new ArrayList();
    
    public Field createField(Item item,Object propertyId,Component uiContext) {
        for(int i=0;i<customFields.size();i++) {
            if (customFieldsNames.get(i).equalsIgnoreCase(propertyId.toString())) {
                return customFields.get(i);
            }
        }
        return super.createField(item, propertyId, uiContext);
    }
}

In the customFields there are several Comboboxes (one for each set of selections I will need), which in turn have their datasource set to a container that provides the ForeignKey objects for the related table, encapsulated in BeanItems.

(To better explain the sense of all, I can have a “Resource” object which can be committed to a given “Project”, so I want the user to be able to select from a combobox the project related to the resource that is being edited.)

I have two problems:

  • the first time I set the Entity object as datasource to the form, I got a call to the equal method of ForeignKey with null as argument. I suppose this is called by the setValue method to select into the combobox the item which corresponds to the “relation” field of the Entity object, but there should be nothing null there… the second time the datasource is set, no more nulls…
  • the current “relation” value is not selected into the comboboxes, even if with some system.out.print I figured out that the equal methos is checked several ways, and returning true when it arrives at the correct one

Please tell me if you need more explanation, the code is quite long so I tried to summarize it… thanks!
Matteo

Just to clarify better: my biggest problem is the selection of the right item of the combobox.

I found out that if I directly feed, at startup, the comboboxes with several ForeignKey objects it works:

ComboBox cb=new ComboBox();
List<ForeignKey> fks=this.tableKeys.getForeignKeys();
for(int i=0;i<fks.size();i++) {
     cb.addItem(fks.get(i));
}

On the other hand, if I use the container to feed the comboboxes it does not work:

ComboBox cb=new ComboBox();
cb.setContainerDataSource(this.tableKeys);

The main difference is that in the first case, I put “ForeignKey” objects, while in the second case the container gives “Item” objects (actually ForeignKeys enclosed in BeanItem objects)… indeed, double checking I found out that putting directly the BeanItems is not working for the selection:

ComboBox cb=new ComboBox();
Iterator it=this.tableKeys.getItemIds().iterator();
while(it.hasNext()) {
      cb.addItem(this.tableKeys.getItem(it.next()));
}

I guess that this is somehow due the equal method of ForeignKey, but I found out that every time it is called it behaves correctly… any suggestion?