Search in sources :

Example 1 with SelectMode

use of org.apache.pivot.wtk.TableView.SelectMode in project pivot by apache.

the class TerraTableViewSkin method keyPressed.

/**
 * {@link KeyCode#UP UP} Selects the previous enabled row when select mode
 * is not {@link SelectMode#NONE}<br> {@link KeyCode#DOWN DOWN} Selects the
 * next enabled row when select mode is not {@link SelectMode#NONE}<p>
 * {@link Modifier#SHIFT SHIFT} + {@link KeyCode#UP UP} Increases the
 * selection size by including the previous enabled row when select mode is
 * {@link SelectMode#MULTI}<br> {@link Modifier#SHIFT SHIFT} +
 * {@link KeyCode#DOWN DOWN} Increases the selection size by including the
 * next enabled row when select mode is {@link SelectMode#MULTI}<br>
 * {@code Cmd/Ctrl-A} in {@link SelectMode#MULTI} select mode to select everything<br>
 * {@code Cmd/Ctrl-U} will unselect whatever is selected<br>
 * {@link KeyCode#SPACE SPACE} wil select/unselect the "current" location
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    TableView tableView = (TableView) getComponent();
    TableView.SelectMode selectMode = tableView.getSelectMode();
    switch(keyCode) {
        case Keyboard.KeyCode.UP:
            {
                if (selectMode != TableView.SelectMode.NONE) {
                    int index = tableView.getFirstSelectedIndex();
                    do {
                        index--;
                    } while (index >= 0 && tableView.isRowDisabled(index));
                    if (index >= 0) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && tableView.getSelectMode() == TableView.SelectMode.MULTI) {
                            tableView.addSelectedIndex(index);
                        } else {
                            tableView.setSelectedIndex(index);
                        }
                        lastKeyboardSelectIndex = index;
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.DOWN:
            {
                if (selectMode != TableView.SelectMode.NONE) {
                    int index = tableView.getLastSelectedIndex();
                    int count = tableView.getTableData().getLength();
                    do {
                        index++;
                    } while (index < count && tableView.isRowDisabled(index));
                    if (index < count) {
                        if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && tableView.getSelectMode() == TableView.SelectMode.MULTI) {
                            tableView.addSelectedIndex(index);
                        } else {
                            tableView.setSelectedIndex(index);
                        }
                        lastKeyboardSelectIndex = index;
                    }
                    consumed = true;
                }
                break;
            }
        case Keyboard.KeyCode.SPACE:
            {
                if (lastKeyboardSelectIndex != -1 && selectMode != TableView.SelectMode.NONE) {
                    if (!tableView.isRowDisabled(lastKeyboardSelectIndex)) {
                        switch(selectMode) {
                            case SINGLE:
                                if (tableView.isRowSelected(lastKeyboardSelectIndex)) {
                                    tableView.setSelectedIndex(-1);
                                } else {
                                    tableView.setSelectedIndex(lastKeyboardSelectIndex);
                                }
                                break;
                            case MULTI:
                                if (tableView.isRowSelected(lastKeyboardSelectIndex)) {
                                    tableView.removeSelectedIndex(lastKeyboardSelectIndex);
                                } else {
                                    tableView.addSelectedIndex(lastKeyboardSelectIndex);
                                }
                                break;
                        }
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.A:
            {
                Modifier cmdModifier = Platform.getCommandModifier();
                if (Keyboard.isPressed(cmdModifier)) {
                    if (selectMode == TableView.SelectMode.MULTI) {
                        tableView.selectAll();
                        // TODO: what should it be?
                        lastKeyboardSelectIndex = tableView.getTableData().getLength() - 1;
                        consumed = true;
                    }
                }
                break;
            }
        case Keyboard.KeyCode.U:
            {
                Modifier cmdModifier = Platform.getCommandModifier();
                if (Keyboard.isPressed(cmdModifier)) {
                    switch(selectMode) {
                        case NONE:
                            break;
                        case SINGLE:
                        case MULTI:
                            tableView.clearSelection();
                            // TODO: what should it be?
                            lastKeyboardSelectIndex = 0;
                            consumed = true;
                            break;
                    }
                }
                break;
            }
        default:
            {
                break;
            }
    }
    // Clear the highlight
    if (highlightIndex != -1 && tableView.getSelectMode() != TableView.SelectMode.NONE && showHighlight && consumed) {
        repaintComponent(getRowBounds(highlightIndex));
    }
    highlightIndex = -1;
    return consumed;
}
Also used : SelectMode(org.apache.pivot.wtk.TableView.SelectMode) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) TableView(org.apache.pivot.wtk.TableView)

Example 2 with SelectMode

use of org.apache.pivot.wtk.TableView.SelectMode in project pivot by apache.

the class TerraTableViewSkin method mouseDown.

@Override
public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
    boolean consumed = super.mouseDown(component, button, x, y);
    TableView tableView = (TableView) getComponent();
    int rowIndex = getRowAt(y);
    if (rowIndex >= 0 && !tableView.isRowDisabled(rowIndex)) {
        TableView.SelectMode selectMode = tableView.getSelectMode();
        if (button == Mouse.Button.LEFT) {
            Keyboard.Modifier commandModifier = Platform.getCommandModifier();
            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && selectMode == TableView.SelectMode.MULTI) {
                Filter<?> disabledRowFilter = tableView.getDisabledRowFilter();
                if (disabledRowFilter == null) {
                    // Select the range
                    int startIndex = tableView.getFirstSelectedIndex();
                    int endIndex = tableView.getLastSelectedIndex();
                    // indicated row
                    if (startIndex == -1) {
                        tableView.addSelectedIndex(rowIndex);
                    } else {
                        // otherwise select the range of rows
                        Span selectedRange = (rowIndex > startIndex) ? new Span(startIndex, rowIndex) : new Span(rowIndex, endIndex);
                        ArrayList<Span> selectedRanges = new ArrayList<>();
                        selectedRanges.add(selectedRange);
                        tableView.setSelectedRanges(selectedRanges);
                    }
                }
            } else if (Keyboard.isPressed(commandModifier) && selectMode == TableView.SelectMode.MULTI) {
                // Toggle the item's selection state
                if (tableView.isRowSelected(rowIndex)) {
                    tableView.removeSelectedIndex(rowIndex);
                } else {
                    tableView.addSelectedIndex(rowIndex);
                }
            } else if (Keyboard.isPressed(commandModifier) && selectMode == TableView.SelectMode.SINGLE) {
                // Toggle the item's selection state
                if (tableView.isRowSelected(rowIndex)) {
                    tableView.setSelectedIndex(-1);
                } else {
                    tableView.setSelectedIndex(rowIndex);
                }
            } else {
                if (selectMode != TableView.SelectMode.NONE) {
                    if (!tableView.isRowSelected(rowIndex)) {
                        tableView.setSelectedIndex(rowIndex);
                    }
                    selectIndex = rowIndex;
                }
            }
        }
    }
    tableView.requestFocus();
    if (editOnMouseDown) {
        if (selectIndex != -1 && button == Mouse.Button.LEFT) {
            TableView.RowEditor rowEditor = tableView.getRowEditor();
            if (rowEditor != null) {
                if (rowEditor.isEditing()) {
                    rowEditor.endEdit(true);
                }
                rowEditor.beginEdit(tableView, selectIndex, getColumnAt(x));
            }
        }
    }
    return consumed;
}
Also used : Modifier(org.apache.pivot.wtk.Keyboard.Modifier) SelectMode(org.apache.pivot.wtk.TableView.SelectMode) Keyboard(org.apache.pivot.wtk.Keyboard) ArrayList(org.apache.pivot.collections.ArrayList) Span(org.apache.pivot.wtk.Span) TableView(org.apache.pivot.wtk.TableView)

Aggregations

Modifier (org.apache.pivot.wtk.Keyboard.Modifier)2 TableView (org.apache.pivot.wtk.TableView)2 SelectMode (org.apache.pivot.wtk.TableView.SelectMode)2 ArrayList (org.apache.pivot.collections.ArrayList)1 Keyboard (org.apache.pivot.wtk.Keyboard)1 Span (org.apache.pivot.wtk.Span)1