Search code examples
javagwtinputgwt-celltable

Generic InputCell for gwt Column (Change Cell-type or know Property's Cell-type before creating Column-constructor)


In our existing GWT project we have a table with multiple cells. We use com.google.gwt.user.cellview.client.Column.Column for the columns of this table. Here is our current code:

private void addColumns() {
    // label column
    final Column<Property, String> labelColumn = new Column<Property, String>(new TextCell()) {
        @Override
        public String getValue(final Property object) {
            return object.getPdf().getName();
        }
    };

    this.datagrid.addColumn(labelColumn, "Label");
    this.datagrid.setColumnWidth(labelColumn, 40, Unit.PX);

    // value column
    final Column<Property, String> valueColumn = new Column<Property, String>(new TextInputCell()) {
        @Override
        public String getValue(final Property object) {
            return object.getValue();
        }
    };
    valueColumn.setFieldUpdater(new FieldUpdater<Property, String>() {
        @Override
        public void update(final int index, final Property object, final String value) {
            final Property existingObject = containsObject(object);
            if (existingObject != null) {
                existingObject.setValue(value);
            } else {
                // else add the object to the "editedFields" list
                object.setValue(value);
                UserDefinedFieldsValuesDiv.this.editedProperties.add(object);
            }
        }
    });
    this.datagrid.addColumn(valueColumn, "Waarde");
    this.datagrid.setColumnWidth(valueColumn, 40, Unit.PX);
}

This will generate a table like this: enter image description here

Currently everything is a TextInputCell, but I want to change this based on the type of the column. So if it's a date, I want a DatePickerCell, etc.

In the following segment of the code:

// value column
final Column<Property, String> valueColumn = new Column<Property, String>(new TextInputCell()) {
    @Override
    public String getValue(final Property object) {
        return object.getValue();
    }
};

It gives a new TextInputCell() as parameter, but only inside this column I can retrieve the type by using object.getType().name().equals(Type.DATE.name()). I've looked in the source-code of the Column, but there is only a getCell method, no setCell. Is there a way to change the cell-type of a Column, OR a way to know beforehand what type the cell is (which I can retrieve from the Property)?

PS: This is my first day using GWT, so I might not be very familiar with some things yet.


EDIT:

So what I'm trying to achieve is something like this:

// value column
AbstractCell cell = new TextInputCell();
Property propertyISomehowRetrieved = ...;
if (propertyISomehowRetrieved.getType().name().equals(Type.DATE.name())){
    cell = new DatePickerCell();
} else if(propertyISomehowRetrieved.getType().name().equals(Type.NUMERIC.name())){
    cell = new NumberCell();
} else if(propertyISomehowRetrieved.getType().name().equals(Type.BOOLEAN.name())){
    cell = new CheckboxCell();
}
final Column<Property, String> valueColumn = new Column<Property, String>(cell) {
    @Override
    public String getValue(final Property object) {
        return object.getValue();
    }
};

But how do I retrieve this property?


Solution

  • Each column may use only one cell. This is why the cell type is passed into the constructor and cannot be changed. You cannot use different cells for different rows, as you show in your example.

    When you build a DataGrid with multiple columns, you can add different columns based on the properties of an object, which this DataGrid is going to display. For example, if your object has two properties "Date" and "Title", you can add two columns with DatePickerCell and TextInputCell. Since this is your object, you know the types of properties before you build the DataGrid, so you can add the correct columns and choose the desired cell type when you build it.