Package org.apache.pivot.wtk

Examples of org.apache.pivot.wtk.TextInput


    }

    @Override
    public boolean keyTyped(Component component, char character) {
        boolean consumed = super.keyTyped(component, character);
        TextInput textInput = (TextInput)getComponent();

        if (textInput.isEditable()) {
            // Ignore characters in the control range and the ASCII delete
            // character as well as meta key presses
            if (character > 0x1F
                && character != 0x7F
                && !Keyboard.isPressed(Keyboard.Modifier.META)) {
                int selectionLength = textInput.getSelectionLength();

                if (textInput.getCharacterCount() - selectionLength + 1 > textInput.getMaximumLength()) {
                    Toolkit.getDefaultToolkit().beep();
                } else {
                    // NOTE We explicitly call getSelectionStart() twice here in case the remove
                    // event is vetoed
                    textInput.removeText(textInput.getSelectionStart(), selectionLength);
                    textInput.insertText(Character.toString(character), textInput.getSelectionStart());
                }
            }
        }

        return consumed;
View Full Code Here


     */
    @Override
    public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
        boolean consumed = super.keyPressed(component, keyCode, keyLocation);

        TextInput textInput = (TextInput)getComponent();
        Keyboard.Modifier commandModifier = Platform.getCommandModifier();
        Keyboard.Modifier wordNavigationModifier = Platform.getWordNavigationModifier();

        if (keyCode == Keyboard.KeyCode.DELETE && textInput.isEditable()) {
            int index = textInput.getSelectionStart();

            if (index < textInput.getCharacterCount()) {
                int count = Math.max(textInput.getSelectionLength(), 1);
                textInput.removeText(index, count);

                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.BACKSPACE && textInput.isEditable()) {
            int index = textInput.getSelectionStart();
            int count = textInput.getSelectionLength();

            if (count == 0
                && index > 0) {
                textInput.removeText(index - 1, 1);
                consumed = true;
            } else {
                textInput.removeText(index, count);
                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.HOME
            || (keyCode == Keyboard.KeyCode.LEFT
                && Keyboard.isPressed(Keyboard.Modifier.META))) {
            // Move the caret to the beginning of the text
            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                textInput.setSelection(0, textInput.getSelectionStart());
            } else {
                textInput.setSelection(0, 0);
            }

            scrollCharacterToVisible(0);

            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.END
            || (keyCode == Keyboard.KeyCode.RIGHT
                && Keyboard.isPressed(Keyboard.Modifier.META))) {
            // Move the caret to the end of the text
            int n = textInput.getCharacterCount();

            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                int selectionStart = textInput.getSelectionStart();
                textInput.setSelection(selectionStart, n - selectionStart);
            } else {
                textInput.setSelection(n, 0);
            }

            scrollCharacterToVisible(n);

            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.LEFT) {
            int selectionStart = textInput.getSelectionStart();
            int selectionLength = textInput.getSelectionLength();

            if (Keyboard.isPressed(wordNavigationModifier)) {
                // Move the caret to the start of the next word to the left
                if (selectionStart > 0) {
                    // Skip over any space immediately to the left
                    int index = selectionStart;
                    while (index > 0
                        && Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
                        index--;
                    }

                    // Skip over any word-letters to the left
                    while (index > 0
                        && !Character.isWhitespace(textInput.getCharacterAt(index - 1))) {
                        index--;
                    }

                    if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                        selectionLength += selectionStart - index;
                    } else {
                        selectionLength = 0;
                    }

                    selectionStart = index;
                }
            } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                // Add the previous character to the selection
                if (selectionStart > 0) {
                    selectionStart--;
                    selectionLength++;
                }
            } else {
                // Move the caret back by one character
                if (selectionLength == 0
                    && selectionStart > 0) {
                    selectionStart--;
                }

                // Clear the selection
                selectionLength = 0;
            }

            if (selectionStart >= 0) {
                textInput.setSelection(selectionStart, selectionLength);
                scrollCharacterToVisible(selectionStart);

                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.RIGHT) {
            int selectionStart = textInput.getSelectionStart();
            int selectionLength = textInput.getSelectionLength();

            if (Keyboard.isPressed(wordNavigationModifier)) {
                // Move the caret to the start of the next word to the right
                if (selectionStart < textInput.getCharacterCount()) {
                    int index = selectionStart + selectionLength;

                    // Skip over any space immediately to the right
                    while (index < textInput.getCharacterCount()
                        && Character.isWhitespace(textInput.getCharacterAt(index))) {
                        index++;
                    }

                    // Skip over any word-letters to the right
                    while (index < textInput.getCharacterCount()
                        && !Character.isWhitespace(textInput.getCharacterAt(index))) {
                        index++;
                    }

                    if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                        selectionLength = index - selectionStart;
                    } else {
                        selectionStart = index;
                        selectionLength = 0;
                    }
                }
            } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                // Add the next character to the selection
                selectionLength++;
            } else {
                // Move the caret forward by one character
                if (selectionLength == 0) {
                    selectionStart++;
                } else {
                    selectionStart += selectionLength;
                }

                // Clear the selection
                selectionLength = 0;
            }

            if (selectionStart + selectionLength <= textInput.getCharacterCount()) {
                textInput.setSelection(selectionStart, selectionLength);
                scrollCharacterToVisible(selectionStart + selectionLength);

                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 && textInput.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 && textInput.isEditable()) {
                textInput.paste();
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.Z && textInput.isEditable()) {
                if (!Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    textInput.undo();
                }

                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.INSERT) {
            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT) && textInput.isEditable()) {
                textInput.paste();
                consumed = true;
            }
        } else {
            consumed = super.keyPressed(component, keyCode, keyLocation);
        }
View Full Code Here

    @Override
    public void focusedChanged(Component component, Component obverseComponent) {
        super.focusedChanged(component, obverseComponent);

        TextInput textInput = (TextInput)component;
        Window window = textInput.getWindow();

        if (component.isFocused()) {
            // If focus was permanently transferred within this window,
            // select all
            if (obverseComponent == null
                || obverseComponent.getWindow() == window) {
                if (Mouse.getCapturer() != component) {
                    textInput.selectAll();
                }
            }

            if (textInput.getSelectionLength() == 0) {
                int selectionStart = textInput.getSelectionStart();
                if (selectionStart < textInput.getCharacterCount()) {
                    scrollCharacterToVisible(selectionStart);
                }

                showCaret(true);
            } else {
                showCaret(false);
            }
        } else {
            // If focus was permanently transferred within this window,
            // clear the selection
            if (obverseComponent == null
                || obverseComponent.getWindow() == window) {
                textInput.clearSelection();
            }

            showCaret(false);
        }
View Full Code Here

            }
        }
    }

    private void updateSelection() {
        TextInput textInput = (TextInput)getComponent();

        int height = getHeight();

        int selectionStart = textInput.getSelectionStart();
        int selectionLength = textInput.getSelectionLength();

        int n = textInput.getCharacterCount();

        Bounds leadingSelectionBounds;
        if (selectionStart < n) {
            leadingSelectionBounds = getCharacterBounds(selectionStart);
        } else {
View Full Code Here

        @Override
        public void run() {
            caretOn = !caretOn;

            if (caret != null) {
                TextInput textInput = (TextInput)getComponent();
                textInput.repaint(caret.x, caret.y, caret.width, caret.height);
            }
        }
View Full Code Here

    }

    private class ScrollSelectionCallback implements Runnable {
        @Override
        public void run() {
            TextInput textInput = (TextInput)getComponent();
            int selectionStart = textInput.getSelectionStart();
            int selectionLength = textInput.getSelectionLength();
            int selectionEnd = selectionStart + selectionLength - 1;

            switch (scrollDirection) {
                case FORWARD: {
                    if (selectionEnd < textInput.getCharacterCount() - 1) {
                        selectionEnd++;
                        textInput.setSelection(selectionStart, selectionEnd - selectionStart + 1);
                        scrollCharacterToVisible(selectionEnd);
                    }

                    break;
                }

                case BACKWARD: {
                    if (selectionStart > 0) {
                        selectionStart--;
                        textInput.setSelection(selectionStart, selectionEnd - selectionStart + 1);
                        scrollCharacterToVisible(selectionStart);
                    }

                    break;
                }
View Full Code Here

        BoxPane boxPane = (BoxPane)controls.get(key);

        if (boxPane != null) {
            Span span = (Span)dictionary.get(key);

            TextInput startTextInput = (TextInput)((FlowPane)boxPane.get(0)).get(0);
            TextInput endTextInput = (TextInput)((FlowPane)boxPane.get(1)).get(0);

            startTextInput.setText(span == null ? "" : String.valueOf(span.start));
            endTextInput.setText(span == null ? "" : String.valueOf(span.end));
        }
    }
View Full Code Here

        FlowPane flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        TextInput textInput = new TextInput();
        textInput.setTextSize(4);
        textInput.setMaximumLength(4);
        textInput.setValidator(new IntValidator());
        textInput.setText(String.valueOf(cornerRadii.topLeft));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    CornerRadii cornerRadiiLocal = (CornerRadii)dictionary.get(key);

                    try {
                        int topLeft = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new CornerRadii(topLeft, cornerRadiiLocal.topRight,
                            cornerRadiiLocal.bottomLeft, cornerRadiiLocal.bottomRight));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(String.valueOf(cornerRadiiLocal.topLeft));
                    }
                }
            }
        });

        Label label = new Label("topLeft");
        label.getStyles().put("font", "{italic:true}");
        flowPane.add(label);

        flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        textInput = new TextInput();
        textInput.setTextSize(4);
        textInput.setMaximumLength(4);
        textInput.setValidator(new IntValidator());
        textInput.setText(String.valueOf(cornerRadii.topRight));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    CornerRadii cornerRadiiLocal = (CornerRadii)dictionary.get(key);

                    try {
                        int topRight = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new CornerRadii(cornerRadiiLocal.topLeft, topRight,
                            cornerRadiiLocal.bottomLeft, cornerRadiiLocal.bottomRight));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(String.valueOf(cornerRadiiLocal.topRight));
                    }
                }
            }
        });

        label = new Label("topRight");
        label.getStyles().put("font", "{italic:true}");
        flowPane.add(label);

        flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        textInput = new TextInput();
        textInput.setTextSize(4);
        textInput.setMaximumLength(4);
        textInput.setValidator(new IntValidator());
        textInput.setText(String.valueOf(cornerRadii.bottomLeft));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    CornerRadii cornerRadiiLocal = (CornerRadii)dictionary.get(key);

                    try {
                        int bottomLeft = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new CornerRadii(cornerRadiiLocal.topLeft,
                            cornerRadiiLocal.topRight, bottomLeft, cornerRadiiLocal.bottomRight));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(String.valueOf(cornerRadiiLocal.bottomLeft));
                    }
                }
            }
        });

        label = new Label("bottomLeft");
        label.getStyles().put("font", "{italic:true}");
        flowPane.add(label);

        flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        textInput = new TextInput();
        textInput.setTextSize(4);
        textInput.setMaximumLength(4);
        textInput.setValidator(new IntValidator());
        textInput.setText(String.valueOf(cornerRadii.bottomRight));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    CornerRadii cornerRadiiLocal = (CornerRadii)dictionary.get(key);

                    try {
                        int bottomRight = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new CornerRadii(cornerRadiiLocal.topLeft,
                            cornerRadiiLocal.topRight, cornerRadiiLocal.bottomLeft, bottomRight));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(String.valueOf(cornerRadiiLocal.bottomRight));
                    }
                }
            }
        });
View Full Code Here

        FlowPane flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        TextInput textInput = new TextInput();
        textInput.setTextSize(10);
        textInput.setMaximumLength(10);
        textInput.setValidator(new IntValidator());
        textInput.setText(scope == null ? "" : String.valueOf(scope.start));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    Scope scopeLocal = (Scope)dictionary.get(key);

                    try {
                        int start = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new Scope(start, scopeLocal == null ? start : scopeLocal.end,
                            scopeLocal == null ? start : scopeLocal.extent));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(scopeLocal == null ? "" : String.valueOf(scopeLocal.start));
                    }
                }
            }
        });

        Label label = new Label("start");
        label.getStyles().put("font", "{italic:true}");
        flowPane.add(label);

        flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        textInput = new TextInput();
        textInput.setTextSize(10);
        textInput.setMaximumLength(10);
        textInput.setValidator(new IntValidator());
        textInput.setText(scope == null ? "" : String.valueOf(scope.end));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    Scope scopeLocal = (Scope)dictionary.get(key);

                    try {
                        int end = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new Scope(scopeLocal == null ? end : scopeLocal.start, end,
                            scopeLocal == null ? end : scopeLocal.extent));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(scopeLocal == null ? "" : String.valueOf(scopeLocal.end));
                    }
                }
            }
        });

        label = new Label("end");
        label.getStyles().put("font", "{italic:true}");
        flowPane.add(label);

        flowPane = new FlowPane();
        flowPane.getStyles().put("alignToBaseline", true);
        flowPane.getStyles().put("horizontalSpacing", 5);
        boxPane.add(flowPane);

        textInput = new TextInput();
        textInput.setTextSize(10);
        textInput.setMaximumLength(10);
        textInput.setValidator(new IntValidator());
        textInput.setText(scope == null ? "" : String.valueOf(scope.extent));
        flowPane.add(textInput);

        textInput.getComponentStateListeners().add(new ComponentStateListener.Adapter() {
            @Override
            public void focusedChanged(Component component, Component obverseComponent) {
                if (!component.isFocused()) {
                    TextInput textInputLocal = (TextInput)component;
                    Scope scopeLocal = (Scope)dictionary.get(key);

                    try {
                        int extent = Integer.parseInt(textInputLocal.getText());
                        dictionary.put(key, new Scope(scopeLocal == null ? extent : scopeLocal.start,
                            scopeLocal == null ? extent : scopeLocal.end, extent));
                    } catch (Exception exception) {
                        displayErrorMessage(exception, component.getWindow());
                        textInputLocal.setText(scopeLocal == null ? "" : String.valueOf(scopeLocal.extent));
                    }
                }
            }
        });
View Full Code Here

        BoxPane boxPane = (BoxPane)controls.get(key);

        if (boxPane != null) {
            Scope scope = (Scope)dictionary.get(key);

            TextInput startTextInput = (TextInput)((FlowPane)boxPane.get(0)).get(0);
            TextInput endTextInput = (TextInput)((FlowPane)boxPane.get(1)).get(0);
            TextInput extentTextInput = (TextInput)((FlowPane)boxPane.get(2)).get(0);

            startTextInput.setText(scope == null ? "" : String.valueOf(scope.start));
            endTextInput.setText(scope == null ? "" : String.valueOf(scope.end));
            extentTextInput.setText(scope == null ? "" : String.valueOf(scope.extent));
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.pivot.wtk.TextInput

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.