Search in sources :

Example 1 with Modifier

use of org.apache.pivot.wtk.Keyboard.Modifier 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 Modifier

use of org.apache.pivot.wtk.Keyboard.Modifier in project pivot by apache.

the class TerraTextInputSkin method keyPressed.

/**
 * {@link KeyCode#DELETE DELETE}
 * Delete the character after the caret or the entire selection if there is one.<br>
 * {@link KeyCode#BACKSPACE BACKSPACE}
 * Delete the character before the caret or the entire selection if there is one.
 * <p> {@link KeyCode#HOME HOME}
 * Move the caret to the beginning of the text. <br>
 * {@link KeyCode#LEFT LEFT} + {@link Modifier#META META}
 * Move the caret to the beginning of the text.
 * <p> {@link KeyCode#HOME HOME} + {@link Modifier#SHIFT SHIFT}
 * Select from the caret to the beginning of the text.<br>
 * {@link KeyCode#LEFT LEFT} + {@link Modifier#META META} + {@link Modifier#SHIFT SHIFT}
 * Select from the caret to the beginning of the text.
 * <p> {@link KeyCode#END END}
 * Move the caret to the end of the text.<br>
 * {@link KeyCode#RIGHT RIGHT} + {@link Modifier#META META}
 * Move the caret to the end of the text.
 * <p> {@link KeyCode#END END} + {@link Modifier#SHIFT SHIFT}
 * Select from the caret to the end of the text.<br>
 * {@link KeyCode#RIGHT RIGHT} + {@link Modifier#META META} + {@link Modifier#SHIFT SHIFT}
 * Select from the caret to the end of the text.
 * <p> {@link KeyCode#LEFT LEFT}
 * Clear the selection and move the caret back by one character.<br>
 * {@link KeyCode#LEFT LEFT} + {@link Modifier#SHIFT SHIFT}
 * Add the previous character to the selection.<br>
 * {@link KeyCode#LEFT LEFT} + {@link Modifier#CTRL CTRL}
 * Clear the selection and move the caret to the beginning of the text.<br>
 * {@link KeyCode#LEFT LEFT} + {@link Modifier#CTRL CTRL} + {@link Modifier#SHIFT SHIFT}
 * Add all preceding text to the selection.
 * <p> {@link KeyCode#RIGHT RIGHT}
 * Clear the selection and move the caret forward by one character.<br>
 * {@link KeyCode#RIGHT RIGHT} + {@link Modifier#SHIFT SHIFT}
 * Add the next character to the selection.<br>
 * {@link KeyCode#RIGHT RIGHT} + {@link Modifier#CTRL CTRL}
 * Clear the selection and move the caret to the end of the text.<br>
 * {@link KeyCode#RIGHT RIGHT} + {@link Modifier#CTRL CTRL} + {@link Modifier#SHIFT SHIFT}
 * Add all subsequent text to the selection.
 * <p> CommandModifier + {@link KeyCode#A A}
 * Select all.<br>
 * CommandModifier + {@link KeyCode#X X}
 * Cut selection to clipboard (if not a password TextInput).<br>
 * CommandModifier + {@link KeyCode#C C}
 * Copy selection to clipboard (if not a password TextInput).<br>
 * CommandModifier + {@link KeyCode#V V}
 * Paste from clipboard.<br>
 * CommandModifier + {@link KeyCode#Z Z}
 * Undo.
 *
 * @see Platform#getCommandModifier()
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    TextInput textInput = (TextInput) getComponent();
    boolean isEditable = textInput.isEditable();
    int start = textInput.getSelectionStart();
    int length = textInput.getSelectionLength();
    Keyboard.Modifier commandModifier = Platform.getCommandModifier();
    Keyboard.Modifier wordNavigationModifier = Platform.getWordNavigationModifier();
    boolean isMetaPressed = Keyboard.isPressed(Keyboard.Modifier.META);
    boolean isShiftPressed = Keyboard.isPressed(Keyboard.Modifier.SHIFT);
    if (keyCode == Keyboard.KeyCode.DELETE && isEditable) {
        if (start < textInput.getCharacterCount()) {
            int count = Math.max(length, 1);
            textInput.removeText(start, count);
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.BACKSPACE && isEditable) {
        if (length == 0 && start > 0) {
            textInput.removeText(start - 1, 1);
            consumed = true;
        } else {
            textInput.removeText(start, length);
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.HOME || (keyCode == Keyboard.KeyCode.LEFT && isMetaPressed)) {
        if (isShiftPressed) {
            // Select from the beginning of the text to the current pivot position
            if (selectDirection == SelectDirection.LEFT) {
                textInput.setSelection(0, start + length);
            } else {
                textInput.setSelection(0, start);
            }
            selectDirection = SelectDirection.LEFT;
        } else {
            // Move the caret to the beginning of the text
            textInput.setSelection(0, 0);
            selectDirection = null;
        }
        scrollCharacterToVisible(0);
        consumed = true;
    } else if (keyCode == Keyboard.KeyCode.END || (keyCode == Keyboard.KeyCode.RIGHT && isMetaPressed)) {
        int n = textInput.getCharacterCount();
        if (isShiftPressed) {
            // Select from current pivot position to the end of the text
            if (selectDirection == SelectDirection.LEFT) {
                start += length;
            }
            textInput.setSelection(start, n - start);
            selectDirection = SelectDirection.RIGHT;
        } else {
            // Move the caret to the end of the text
            textInput.setSelection(n, 0);
            selectDirection = null;
        }
        scrollCharacterToVisible(n);
        consumed = true;
    } else if (keyCode == Keyboard.KeyCode.LEFT) {
        // Sometimes while selecting we need to make the opposite end visible
        SelectDirection visiblePosition = SelectDirection.LEFT;
        if (Keyboard.isPressed(wordNavigationModifier)) {
            int wordStart = (selectDirection == SelectDirection.RIGHT) ? start + length : start;
            // Find the start of the next word to the left
            if (wordStart > 0) {
                wordStart = CharUtils.findPriorWord(textInput.getCharacters(), wordStart);
                if (isShiftPressed) {
                    if (wordStart >= start) {
                        // We've just reduced the previous right selection, so leave the anchor alone
                        length = wordStart - start;
                        wordStart = start;
                        visiblePosition = selectDirection;
                    } else {
                        if (selectDirection == SelectDirection.RIGHT) {
                            // We've "crossed over" the start, so reverse direction
                            length = start - wordStart;
                        } else {
                            // Just increase the selection in the same direction
                            length += start - wordStart;
                        }
                        selectDirection = SelectDirection.LEFT;
                    }
                } else {
                    length = 0;
                    selectDirection = null;
                }
                start = wordStart;
            }
        } else if (isShiftPressed) {
            // else decrease the selection back to the anchor.
            if (selectDirection != null) {
                switch(selectDirection) {
                    case LEFT:
                        if (start > 0) {
                            start--;
                            length++;
                        }
                        break;
                    case RIGHT:
                        if (length == 0) {
                            if (start > 0) {
                                start--;
                                length++;
                                selectDirection = SelectDirection.LEFT;
                            }
                        } else {
                            if (--length == 0) {
                                if (start > 0) {
                                    start--;
                                    length++;
                                    selectDirection = SelectDirection.LEFT;
                                }
                            } else {
                                visiblePosition = selectDirection;
                            }
                        }
                        break;
                }
            } else {
                // Add one to the selection
                if (start > 0) {
                    start--;
                    length++;
                    selectDirection = SelectDirection.LEFT;
                }
            }
        } else {
            selectDirection = null;
            // Move the caret back by one character
            if (length == 0 && start > 0) {
                start--;
            }
            // Clear the selection
            length = 0;
        }
        if (start >= 0) {
            textInput.setSelection(start, length);
            switch(visiblePosition) {
                case LEFT:
                    scrollCharacterToVisible(start);
                    break;
                case RIGHT:
                    scrollCharacterToVisible(start + length);
                    break;
            }
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.RIGHT) {
        // Sometimes while selecting we need to make the opposite end visible
        SelectDirection visiblePosition = SelectDirection.RIGHT;
        if (Keyboard.isPressed(wordNavigationModifier)) {
            int wordStart = (selectDirection == SelectDirection.LEFT) ? start : start + length;
            // Find the start of the next word to the right
            if (wordStart < textInput.getCharacterCount()) {
                wordStart = CharUtils.findNextWord(textInput.getCharacters(), wordStart);
                if (isShiftPressed) {
                    if (wordStart <= start + length) {
                        // We've just reduced the previous left selection, so leave the anchor alone
                        length -= wordStart - start;
                        start = wordStart;
                        visiblePosition = selectDirection;
                    } else {
                        if (selectDirection == SelectDirection.LEFT) {
                            // We've "crossed over" the start, so reverse direction
                            start += length;
                            length = wordStart - start;
                        } else {
                            // Just increase the selection in the same direction
                            length = wordStart - start;
                        }
                        selectDirection = SelectDirection.RIGHT;
                    }
                } else {
                    start = wordStart;
                    length = 0;
                    selectDirection = null;
                }
            }
        } else if (isShiftPressed) {
            // else decrease the selection back to the anchor.
            if (selectDirection != null) {
                switch(selectDirection) {
                    case RIGHT:
                        length++;
                        break;
                    case LEFT:
                        if (length == 0) {
                            length++;
                            selectDirection = SelectDirection.RIGHT;
                        } else {
                            start++;
                            if (--length == 0) {
                                length++;
                                selectDirection = SelectDirection.RIGHT;
                            } else {
                                visiblePosition = selectDirection;
                            }
                        }
                        break;
                }
            } else {
                // Add the next character to the selection
                length++;
                selectDirection = SelectDirection.RIGHT;
            }
        } else {
            selectDirection = null;
            // Move the caret forward by one character
            if (length == 0) {
                start++;
            } else {
                start += length;
            }
            // Clear the selection
            length = 0;
        }
        if (start + length <= textInput.getCharacterCount()) {
            textInput.setSelection(start, length);
            switch(visiblePosition) {
                case LEFT:
                    scrollCharacterToVisible(start);
                    break;
                case RIGHT:
                    scrollCharacterToVisible(start + length);
                    break;
            }
            consumed = true;
        }
    } else if (Keyboard.isPressed(commandModifier)) {
        if (keyCode == Keyboard.KeyCode.A) {
            textInput.setSelection(0, textInput.getCharacterCount());
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.X && isEditable) {
            if (textInput.isPassword()) {
                Toolkit.getDefaultToolkit().beep();
            } else {
                textInput.cut();
            }
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.C) {
            if (textInput.isPassword()) {
                Toolkit.getDefaultToolkit().beep();
            } else {
                textInput.copy();
            }
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.V && isEditable) {
            textInput.paste();
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.Z && isEditable) {
            if (!isShiftPressed) {
                textInput.undo();
            }
            consumed = true;
        }
    } else if (keyCode == Keyboard.KeyCode.INSERT) {
        if (isShiftPressed && isEditable) {
            textInput.paste();
            consumed = true;
        }
    } else {
        consumed = super.keyPressed(component, keyCode, keyLocation);
    }
    return consumed;
}
Also used : Modifier(org.apache.pivot.wtk.Keyboard.Modifier) Keyboard(org.apache.pivot.wtk.Keyboard) SelectDirection(org.apache.pivot.wtk.SelectDirection) TextInput(org.apache.pivot.wtk.TextInput)

Example 3 with Modifier

use of org.apache.pivot.wtk.Keyboard.Modifier in project pivot by apache.

the class TerraAccordionSkin method keyPressed.

/**
 * Key presses have no effect if the event has already been consumed.<p>
 * CommandModifier + {@link KeyCode#KEYPAD_1 KEYPAD_1} to
 * {@link KeyCode#KEYPAD_9 KEYPAD_9}<br>or CommandModifier +
 * {@link KeyCode#N1 1} to {@link KeyCode#N9 9} Select the (enabled) pane at
 * index 0 to 8 respectively<p> {@link Modifier#ALT ALT} +
 * {@link KeyCode#UP UP} Select the next enabled panel.<br>
 * {@link Modifier#ALT ALT} + {@link KeyCode#DOWN DOWN} Select the previous
 * enabled panel.
 *
 * @see Platform#getCommandModifier()
 */
@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = super.keyPressed(component, keyCode, keyLocation);
    if (!consumed) {
        Accordion accordion = (Accordion) getComponent();
        Accordion.PanelSequence panels = accordion.getPanels();
        Keyboard.Modifier commandModifier = Platform.getCommandModifier();
        if (Keyboard.isPressed(commandModifier)) {
            int selectedIndex = -1;
            switch(keyCode) {
                case Keyboard.KeyCode.KEYPAD_1:
                case Keyboard.KeyCode.N1:
                    {
                        selectedIndex = 0;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_2:
                case Keyboard.KeyCode.N2:
                    {
                        selectedIndex = 1;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_3:
                case Keyboard.KeyCode.N3:
                    {
                        selectedIndex = 2;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_4:
                case Keyboard.KeyCode.N4:
                    {
                        selectedIndex = 3;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_5:
                case Keyboard.KeyCode.N5:
                    {
                        selectedIndex = 4;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_6:
                case Keyboard.KeyCode.N6:
                    {
                        selectedIndex = 5;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_7:
                case Keyboard.KeyCode.N7:
                    {
                        selectedIndex = 6;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_8:
                case Keyboard.KeyCode.N8:
                    {
                        selectedIndex = 7;
                        break;
                    }
                case Keyboard.KeyCode.KEYPAD_9:
                case Keyboard.KeyCode.N9:
                    {
                        selectedIndex = 8;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (selectedIndex >= 0 && selectedIndex < panels.getLength() && panels.get(selectedIndex).isEnabled()) {
                accordion.setSelectedIndex(selectedIndex);
                consumed = true;
            }
        } else if (Keyboard.isPressed(Keyboard.Modifier.ALT)) {
            int n = panels.getLength();
            int selectedIndex = accordion.getSelectedIndex();
            switch(keyCode) {
                case Keyboard.KeyCode.UP:
                    {
                        do {
                            selectedIndex--;
                        } while (selectedIndex >= 0 && !panels.get(selectedIndex).isEnabled());
                        break;
                    }
                case Keyboard.KeyCode.DOWN:
                    {
                        do {
                            selectedIndex++;
                        } while (selectedIndex < n && !panels.get(selectedIndex).isEnabled());
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            if (selectedIndex >= 0 && selectedIndex < n && panels.get(selectedIndex).isEnabled()) {
                accordion.setSelectedIndex(selectedIndex);
                consumed = true;
            }
        }
    }
    return consumed;
}
Also used : Accordion(org.apache.pivot.wtk.Accordion) Modifier(org.apache.pivot.wtk.Keyboard.Modifier) Keyboard(org.apache.pivot.wtk.Keyboard) GradientPaint(java.awt.GradientPaint)

Example 4 with Modifier

use of org.apache.pivot.wtk.Keyboard.Modifier in project pivot by apache.

the class ComponentSkin method keyPressed.

/**
 * {@link KeyCode#TAB TAB} Transfers focus forwards<br> {@link KeyCode#TAB
 * TAB} + {@link Modifier#SHIFT SHIFT} Transfers focus backwards
 */
@Override
public boolean keyPressed(Component componentArgument, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    EnumSet<Keyboard.Modifier> otherModifiers = EnumSet.noneOf(Keyboard.Modifier.class);
    otherModifiers.addAll(Keyboard.Modifier.allModifiers);
    otherModifiers.remove(Keyboard.Modifier.SHIFT);
    if (keyCode == Keyboard.KeyCode.TAB && !Keyboard.areAnyPressed(otherModifiers) && getComponent().isFocused()) {
        FocusTraversalDirection direction = (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) ? FocusTraversalDirection.BACKWARD : FocusTraversalDirection.FORWARD;
        // Transfer focus to the next component
        Component focusedComponent = component.transferFocus(direction);
        // Ensure that the focused component is visible
        if (component != focusedComponent && focusedComponent != null) {
            focusedComponent.scrollAreaToVisible(0, 0, focusedComponent.getWidth(), focusedComponent.getHeight());
        }
        consumed = true;
    }
    return consumed;
}
Also used : FocusTraversalDirection(org.apache.pivot.wtk.FocusTraversalDirection) Keyboard(org.apache.pivot.wtk.Keyboard) Component(org.apache.pivot.wtk.Component) Modifier(org.apache.pivot.wtk.Keyboard.Modifier)

Aggregations

Modifier (org.apache.pivot.wtk.Keyboard.Modifier)4 Keyboard (org.apache.pivot.wtk.Keyboard)3 GradientPaint (java.awt.GradientPaint)1 Accordion (org.apache.pivot.wtk.Accordion)1 Component (org.apache.pivot.wtk.Component)1 FocusTraversalDirection (org.apache.pivot.wtk.FocusTraversalDirection)1 SelectDirection (org.apache.pivot.wtk.SelectDirection)1 TableView (org.apache.pivot.wtk.TableView)1 SelectMode (org.apache.pivot.wtk.TableView.SelectMode)1 TextInput (org.apache.pivot.wtk.TextInput)1