Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable CheckboxCell in a CellTable

Tags:

gwt

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");
like image 263
LongFlick Avatar asked May 22 '11 16:05

LongFlick


1 Answers

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.append(INPUT_UNCHECKED_DISABLED);
    } else if (checked && enabled) {
      sb.append(INPUT_CHECKED);
    } else if (!checked && enabled) {
      sb.append(INPUT_UNCHECKED);
  }
}
like image 83
Riley Lark Avatar answered Nov 18 '22 08:11

Riley Lark