Example #1
0
  private void handleRowClick(MouseDownEvent event, TableRowElement row) {
    int modifiers = KeyboardShortcut.getModifierValue(event.getNativeEvent());
    modifiers &= ~KeyboardShortcut.ALT; // ALT has no effect

    if (!allowMultiSelect_) modifiers = KeyboardShortcut.NONE;

    // We'll treat Ctrl and Meta as equivalent--and normalize to Ctrl.
    if (KeyboardShortcut.META == (modifiers & KeyboardShortcut.META))
      modifiers |= KeyboardShortcut.CTRL;
    modifiers &= ~KeyboardShortcut.META;

    if (modifiers == KeyboardShortcut.NONE) {
      // Select only the target row
      clearSelection();
      setSelected(row, true);
    } else if (modifiers == KeyboardShortcut.CTRL) {
      // Toggle the target row
      setSelected(row, !isSelected(row));
    } else {
      // SHIFT or CTRL+SHIFT

      int target = row.getRowIndex();
      Integer min = null;
      Integer max = null;
      for (TableRowElement selectedRow : selectedRows_) {
        if (min == null) min = selectedRow.getRowIndex();
        max = selectedRow.getRowIndex();
      }

      int offset; // selection offset
      int length; // selection length

      if (min == null) {
        // Nothing is selected
        offset = target;
        length = 1;
      } else if (target < min) {
        // Select target..max
        offset = target;
        length = max - target + 1;
      } else if (target > max) {
        offset = min;
        length = target - min + 1;
      } else {
        // target is in between min and max
        if (modifiers == (KeyboardShortcut.CTRL | KeyboardShortcut.SHIFT)) {
          offset = min;
          length = target - min + 1;
        } else {
          offset = target;
          length = 1;
        }
      }

      clearSelection();
      if (length > 0) {
        setSelectedPhysical(offset, length, true);
      }
    }
  }