Search in sources :

Example 1 with SelectDirection

use of org.apache.pivot.wtk.SelectDirection 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)

Aggregations

Keyboard (org.apache.pivot.wtk.Keyboard)1 Modifier (org.apache.pivot.wtk.Keyboard.Modifier)1 SelectDirection (org.apache.pivot.wtk.SelectDirection)1 TextInput (org.apache.pivot.wtk.TextInput)1