jeudi 18 février 2016

Convert CheckboxCell with SelectionChangeHandler and createCheckboxManager() to ToggleButtonCell with same functionality

I'm currently refactoring / changing an existing GWT/GXT project. In one of our tables we currently have Checkboxes, and if one of them is selected, it opens a view below the table to edit the selected PropertyDefinition-object. Since me and my co-worker both think a checkbox for this purpose is a bit ugly, I'm trying to change it to a ToggleButton.

Here is the working code with the CheckboxCell:

final Column<PropertyDefinition, Boolean> toggleButtonColumn = new Column<PropertyDefinition, Boolean>(
    new CheckboxCell(true, false)) {

  @Override
  public Boolean getValue(final PropertyDefinition object) {
    // Get the selected PropertyDefinition from the selection model
    return ManagementDiv.this.selectionModel.isSelected(object);
  }
};
this.propertyList.addColumn(toggleButtonColumn);

And we have the selectionModel initialized in the Constructor like this:

public managementDiv(){
    ...

    this.propertyList = new DataGrid<PropertyDefinition>(KEY_PROVIDER);

    this.selectionModel = new SingleSelectionModel<PropertyDefinition>(KEY_PROVIDER);
    this.selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

      @Override
      public void onSelectionChange(final SelectionChangeEvent event) {
        addSelectedProperty();
      }
    });
    this.propertyList.setSelectionModel(this.selectionModel,
    DefaultSelectionEventManager.<PropertyDefinition> createCheckboxManager());

    ...
}

I'm pretty inexperience with GWT/GXT so far, but I see it uses a createCheckboxManager(). Can I somehow modify the code to use a ToggleButtonCell, and still have the same functionality?

I did try replacing the CheckboxCell column with this:

final ToggleButtonCell toggleButtoNCell = new ToggleButtonCell() {

    @Override
    protected void onClick(final com.google.gwt.cell.client.Cell.Context context, final XElement p,
      final Boolean value, final NativeEvent event, final ValueUpdater<Boolean> valueUpdater) {
        super.onClick(context, p, value, event, valueUpdater);

        setText(value ? "ON" : "OFF");
    }
};
toggleButtonCell.setText("OFF");
final Column<PropertyDefinition, Boolean> toggleButtonColumn = new Column<PropertyDefinition, Boolean>(
    toggleButtonCell) {

But I don't really know how to continue, and if my approach is the correct one to begin with..

(We use GXT Sencha btw, so excluding the GXT EXT.)




Aucun commentaire:

Enregistrer un commentaire