Alex Rivera | Logout

Disable CheckboxCell in a CellTable

Asked 2011-05-22T16:40:37.537
16

I have a GWT CellTable<MyType>. This table have rows which should display a CheckBox. This CheckBox should checked or unchecked depending on a getter in MyType, but it should disabled so the user can't click it. Does anybody know how this could be implemented?

Some code snippets:

Column<MyType, Boolean> checkboxColumn = new Column<MyType, Boolean>(new CheckboxCell()) {
    @Override
    public Boolean getValue(MyType object) {
        return object.isItTrue();
    }
};
CellTable<MyType> cellTable = new CellTable<MyType>();
cellTable.addColumn(checkboxColumn, "Header title");
gwt
Edit
Report

1 Answer

14

CheckboxCell does not support this functionality, but you can make your own cell that does. Untested code (copied largely from the CheckboxCell code, Copyright Google (see CheckboxCell source for license)) follows! The important part is in the render code. Your new cell would go in a Column<MyType, MyType>.

public class DisableableCheckboxCell extends AbstractEditableCell<MyType> {

  /**
   * An html string representation of a checked input box.
   */
  private static final SafeHtml INPUT_CHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked/>");

  /**
   * An html string representation of an unchecked input box.
   */
  private static final SafeHtml INPUT_UNCHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\"/>");

  private static final SafeHtml INPUT_CHECKED_DISABLED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked disabled=\"disabled\"/>");

  private static final SafeHtml INPUT_UNCHECKED_DISABLED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" disabled=\"disabled\"/>");

/** You'd copy the rest of the code from CheckboxCell, or implement the other required functions yourself **/

  @Override
  public void render(Context context, MyType value, SafeHtmlBuilder sb) {
    // Get the view data.
    Object key = context.getKey();
    MyType viewData = getViewData(key);
    if (viewData != null && viewData.equals(value)) {
      clearViewData(key);
      viewData = null;
    }

    MyType relevantValue = viewData != null ? viewData : value;
    boolean checked = relevantValue.shouldBeChecked();
    boolean enabled = relevantValue.shouldBeEnabled();

    if (checked && !enabled)) {
      sb.append(INPUT_CHECKED_DISABLED);
    } else if (!checked && !enabled) {
      sb.ap
answered 2011-05-22T18:55:54.803

Your Answer