Advertisement
mitrakov

GWT ClickableCheckboxCell

Oct 12th, 2019
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. import com.google.gwt.cell.client.AbstractEditableCell;
  2. import com.google.gwt.cell.client.Cell;
  3. import com.google.gwt.cell.client.ValueUpdater;
  4. import com.google.gwt.dom.client.BrowserEvents;
  5. import com.google.gwt.dom.client.Element;
  6. import com.google.gwt.dom.client.InputElement;
  7. import com.google.gwt.dom.client.NativeEvent;
  8. import com.google.gwt.event.dom.client.KeyCodes;
  9. import com.google.gwt.safehtml.shared.SafeHtml;
  10. import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
  11. import com.google.gwt.safehtml.shared.SafeHtmlUtils;
  12.  
  13. /**
  14.  * Original: {@link com.google.gwt.cell.client.CheckboxCell CheckboxCell}.<br>
  15.  * A {@link Cell} used to render a checkbox. The value of the checkbox may be
  16.  * toggled using the ENTER key as well as via mouse click.
  17.  */
  18. public class ClickableCheckboxCell extends AbstractEditableCell<Boolean, Boolean> {
  19.  
  20.     /**
  21.      * An html string representation of a checked input box.
  22.      */
  23.     private static final SafeHtml INPUT_CHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\" checked/>");
  24.  
  25.     /**
  26.      * An html string representation of an unchecked input box.
  27.      */
  28.     private static final SafeHtml INPUT_UNCHECKED = SafeHtmlUtils.fromSafeConstant("<input type=\"checkbox\" tabindex=\"-1\"/>");
  29.  
  30.     private final boolean dependsOnSelection;
  31.     private final boolean handlesSelection;
  32.  
  33.     /**
  34.      * Construct a new {@link com.google.gwt.cell.client.CheckboxCell} that optionally controls selection.
  35.      *
  36.      * @param dependsOnSelection true if the cell depends on the selection state
  37.      * @param handlesSelection true if the cell modifies the selection state
  38.      */
  39.     public ClickableCheckboxCell(boolean dependsOnSelection, boolean handlesSelection) {
  40.         super(BrowserEvents.CHANGE, BrowserEvents.KEYDOWN, BrowserEvents.CLICK);
  41.         this.dependsOnSelection = dependsOnSelection;
  42.         this.handlesSelection = handlesSelection;
  43.     }
  44.  
  45.     @Override
  46.     public boolean dependsOnSelection() {
  47.         return dependsOnSelection;
  48.     }
  49.  
  50.     @Override
  51.     public boolean handlesSelection() {
  52.         return handlesSelection;
  53.     }
  54.  
  55.     @Override
  56.     public boolean isEditing(Context context, Element parent, Boolean value) {
  57.         // A checkbox is never in "edit mode". There is no intermediate state
  58.         // between checked and unchecked.
  59.         return false;
  60.     }
  61.  
  62.     @Override
  63.     public void onBrowserEvent(Context context, Element parent, Boolean value,
  64.                                NativeEvent event, ValueUpdater<Boolean> valueUpdater) {
  65.         String type = event.getType();
  66.  
  67.         boolean enterPressed = BrowserEvents.KEYDOWN.equals(type)
  68.                 && event.getKeyCode() == KeyCodes.KEY_ENTER;
  69.         if (BrowserEvents.CHANGE.equals(type) || enterPressed) {
  70.             InputElement input = parent.getFirstChild().cast();
  71.             Boolean isChecked = input.isChecked();
  72.  
  73.             /*
  74.              * Toggle the value if the enter key was pressed and the cell handles
  75.              * selection or doesn't depend on selection. If the cell depends on
  76.              * selection but doesn't handle selection, then ignore the enter key and
  77.              * let the SelectionEventManager determine which keys will trigger a
  78.              * change.
  79.              */
  80.             if (enterPressed && (handlesSelection() || !dependsOnSelection())) {
  81.                 isChecked = !isChecked;
  82.                 input.setChecked(isChecked);
  83.             }
  84.  
  85.             /*
  86.              * Save the new value. However, if the cell depends on the selection, then
  87.              * do not save the value because we can get into an inconsistent state.
  88.              */
  89.             if (value != isChecked && !dependsOnSelection()) {
  90.                 setViewData(context.getKey(), isChecked);
  91.             } else {
  92.                 clearViewData(context.getKey());
  93.             }
  94.  
  95.             if (valueUpdater != null) {
  96.                 valueUpdater.update(isChecked);
  97.             }
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
  103.         // Get the view data.
  104.         Object key = context.getKey();
  105.         Boolean viewData = getViewData(key);
  106.         if (viewData != null && viewData.equals(value)) {
  107.             clearViewData(key);
  108.             viewData = null;
  109.         }
  110.  
  111.         if (value != null && ((viewData != null) ? viewData : value)) {
  112.             sb.append(INPUT_CHECKED);
  113.         } else {
  114.             sb.append(INPUT_UNCHECKED);
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement