0

Is there a way to implent TableFactory interface with specific fields related to propertyId ? I only get one type of field since i'm using a generic class for all my tables, and the i'm missing CheckBox boolean value (groovy code):

class DefaultTableFieldFactory implements TableFieldFactory {
    @Override
    public Field<?> createField(Container container, Object itemId, Object propertyId, Component component) {
        TextField t = new TextField()

        switch(propertyId) {
            case "firstname": t.setNullRepresentation("");
            case "lastname": t.setNullRepresentation("");
            case "mobile": t.setNullRepresentation("");
            case "tel": t.setNullRepresentation("");
            case "email": t.setNullRepresentation("");
            default: break;
        }
        t.setWidth("95px")

        return t
    }
}

So i need to use this class above which implments DefaultTableFieldfactory in order to have the null representation as "" (instead of "null" ) in my whole application.

The goal is to provide for my custom components (more than 30) this null representation in a single place, I want to use this class as my default factory for every table, and connect it like i've done before:

def contacts = (Grails.get(FundService)).getAllContacts(fundId)
        def cContainer = new BeanItemContainer<Contact>(Contact.class,contacts)


        def t = new Table()
        t.containerDataSource = cContainer
        t.setTableFieldFactory(new DefaultTableFieldFactory())
ludo_rj
  • 3,877
  • 1
  • 18
  • 37

1 Answers1

1

Vaadin provides a DefaultTableFieldFactory which does map

  • Date to a DateField
  • Boolean to a CheckBox
  • other to TextField

The DefaultTableFieldFactory is already set on the table. So in your case, if you just want to have CheckBoxes for your boolean fields, I wouldn't implement an own TableFieldFactory. Here's an example:

Table table = new Table();

table.addContainerProperty("text", String.class, "");
table.addContainerProperty("boolean", Boolean.class, false);
table.setEditable(true);

Object itemId = table.addItem();
table.getItem(itemId).getItemProperty("text").setValue("has accepted");
table.getItem(itemId).getItemProperty("boolean").setValue(true);

If you really need to have your own TableFieldFactory then Vaadin recommends:

You could just implement the TableFieldFactory interface, but we recommend that you extend the DefaultFieldFactory according to your needs. In the default implementation, the mappings are defined in the createFieldByPropertyType() method (you might want to look at the source code) both for tables and forms.

In your code given in the question you always return a TextField. For your missing CheckBoxes you need to return in the specific case a CheckBox.

Don't forget to setEditable(true) when using FieldFactories.

More information here under 5.16.3. Editing the Values in a Table.

nexus
  • 2,937
  • 3
  • 17
  • 22
  • Thanks, but i already know what is DefaultTableFactory since i'm implementing it in my code. The goal here is to provide this factory to handle different types of Fields in a single factory, an to apply this pattern to the table's container datasource (see code modified above) – ludo_rj Dec 21 '13 at 11:23
  • 1
    @ludo_rj Then have a look at the createFieldByType() method in the DefaultFieldFactory class provided by Vaadin which returns a CheckBox when appropriate. – nexus Dec 21 '13 at 12:19
  • yep that's the one i was looking for :) Because DefaultFieldFactory.createFieldByPropertyType is static it cannot be overridden so it requires extracode to be implemented correctly in TableFieldFactory.. – ludo_rj Dec 21 '13 at 12:37
  • @ludo_rj well I already mentioned that in my answer, if you have a close look at it. – nexus Dec 21 '13 at 13:45