Package org.apache.pivot.wtk.text

Examples of org.apache.pivot.wtk.text.TextNode


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

        TextInput textInput = (TextInput)getComponent();
        TextNode textNode = textInput.getTextNode();

        if (keyCode == Keyboard.KeyCode.DELETE
            || keyCode == Keyboard.KeyCode.BACKSPACE) {
            consumed = true;

            Direction direction = (keyCode == Keyboard.KeyCode.DELETE ?
                Direction.FORWARD : Direction.BACKWARD);

            Validator validator = textInput.getValidator();

            if (validator != null
                && strictValidation) {
                StringBuilder buf = new StringBuilder(textNode.getText());
                int index = textInput.getSelectionStart();
                int count = textInput.getSelectionLength();

                if (count > 0) {
                    buf.delete(index, index + count);
                } else {
                    if (direction == Direction.BACKWARD) {
                        index--;
                    }

                    if (index >= 0
                        && index < textNode.getCharacterCount()) {
                        buf.deleteCharAt(index);
                    }
                }

                if (validator.isValid(buf.toString())) {
                    textInput.delete(direction);
                } else {
                    ApplicationContext.beep();
                }
            } else {
                textInput.delete(direction);
            }
        } else if (keyCode == Keyboard.KeyCode.LEFT) {
            consumed = true;

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

            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)
                && Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
                // Add all preceding text to the selection
                selectionLength = selectionStart + selectionLength;
                selectionStart = 0;
            } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                // Add the previous character to the selection
                if (selectionStart > 0) {
                    selectionStart--;
                    selectionLength++;
                }
            } else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
                // Clear the selection and move the caret to the beginning of
                // the text
                selectionStart = 0;
                selectionLength = 0;
            } else {
                // Clear the selection and move the caret back by one
                // character
                if (selectionLength == 0) {
                    if (selectionStart > 0) {
                        selectionStart--;
                    } else {
                        consumed = false;
                    }
                }

                selectionLength = 0;
            }

            textInput.setSelection(selectionStart, selectionLength);
        } else if (keyCode == Keyboard.KeyCode.RIGHT) {
            consumed = true;

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

            if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)
                && Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
                // Add all subsequent text to the selection
                selectionLength = textNode.getCharacterCount() - selectionStart;
            } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                // Add the next character to the selection
                if (selectionStart + selectionLength < textNode.getCharacterCount()) {
                    selectionLength++;
                }
            } else if (Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
                // Clear the selection and move the caret to the end of
                // the text
                selectionStart = textNode.getCharacterCount();
                selectionLength = 0;
            } else {
                // Clear the selection and move the caret forward by one
                // character
                selectionStart += selectionLength;

                if (selectionLength == 0) {
                    if (selectionStart < textNode.getCharacterCount()) {
                        selectionStart++;
                    } else {
                        consumed = false;
                    }
                }

                selectionLength = 0;
            }

            textInput.setSelection(selectionStart, selectionLength);
        } else if (keyCode == Keyboard.KeyCode.HOME) {
            consumed = true;

            // Move the caret to the beginning of the text
            textInput.setSelection(0, 0);
        } else if (keyCode == Keyboard.KeyCode.END) {
            consumed = true;

            // Move the caret to the end of the text
            textInput.setSelection(textNode.getCharacterCount(), 0);
        } else if (keyCode == Keyboard.KeyCode.A
            && Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
            consumed = true;

            // Select all
            textInput.setSelection(0, textNode.getCharacterCount());
        } else if (keyCode == Keyboard.KeyCode.X
            && Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
            consumed = true;

            if (textInput.isPassword()) {
View Full Code Here


        });

        symbolTextInput.getTextInputTextListeners().add(new TextInputTextListener() {
            @Override
            public void textChanged(TextInput textInput) {
                TextNode textNode = textInput.getTextNode();
                addSymbolButton.setEnabled(textNode.getCharacterCount() > 0);
            }
        });

        symbolTextInput.getComponentKeyListeners().add(new ComponentKeyListener.Adapter() {
            @Override
View Full Code Here

    @Override
    protected void setParent(ElementAdapter parent) {
        super.setParent(parent);

        TextNode textNode = (TextNode)getNode();

        if (parent == null) {
            textNode.getTextNodeListeners().remove(textNodeListener);
        } else {
            textNode.getTextNodeListeners().add(textNodeListener);
        }
    }
View Full Code Here

    private TextInputSelectionListenerList textInputSelectionListeners = new TextInputSelectionListenerList();

    private static final int DEFAULT_TEXT_SIZE = 20;

    public TextInput() {
        setTextNode(new TextNode());
        installSkin(TextInput.class);
    }
View Full Code Here

        if (textNode.getCharacterCount() > maximumLength) {
            throw new IllegalArgumentException("Text length is greater than maximum length.");
        }

        TextNode previousTextNode = this.textNode;

        if (previousTextNode != textNode) {
            if (previousTextNode != null) {
                previousTextNode.getNodeListeners().remove(textNodeListener);
            }

            if (textNode != null) {
                textNode.getNodeListeners().add(textNodeListener);
            }
View Full Code Here

    public void setText(String text) {
        if (text == null) {
            throw new IllegalArgumentException("text is null.");
        }

        setTextNode(new TextNode(text));
    }
View Full Code Here

    }

    public void cut() {
        // Delete any selected text and put it on the clipboard
        if (selectionLength > 0) {
            TextNode removedRange =
                (TextNode)textNode.removeRange(selectionStart, selectionLength);

            LocalManifest clipboardContent = new LocalManifest();
            clipboardContent.putText(removedRange.getText());
            Clipboard.setContent(clipboardContent);
        }
    }
View Full Code Here

     */
    public String getSelectedText() {
        String selectedText = null;

        if (selectionLength > 0) {
            TextNode selectedRange = (TextNode)textNode.getRange(selectionStart,
                selectionStart + selectionLength);
            selectedText = selectedRange.getText();
        }

        return selectedText;
    }
View Full Code Here

        Node descendant = document.getDescendantAt(selectionStart);
        int offset = selectionStart - descendant.getDocumentOffset();

        if (descendant instanceof TextNode) {
            // The caret is positioned within an existing text node
            TextNode textNode = (TextNode)descendant;
            textNode.insertText(text, offset);
        } else if (descendant instanceof Paragraph) {
            // The caret is positioned on the paragraph terminator
            Paragraph paragraph = (Paragraph)descendant;

            int n = paragraph.getLength();
            if (n > 0) {
                Node node = paragraph.get(n - 1);
                if (node instanceof TextNode) {
                    // Insert the text into the existing node
                    TextNode textNode = (TextNode)node;
                    textNode.insertText(text, offset);
                } else {
                    // Append a new text node
                    paragraph.add(new TextNode(text));
                }
            } else {
                // The paragraph is currently empty
                paragraph.add(new TextNode(text));
            }
        } else {
            // The caret is positioned on a non-text character node; insert
            // the text into the descendant's parent
            Element parent = descendant.getParent();
            int index = parent.indexOf(descendant);
            parent.insert(new TextNode(text), index);
        }

        // Set the selection start to the character following the insertion
        setSelection(selectionStart + text.length(), selectionLength);
    }
View Full Code Here

    @Override
    protected void attach() {
        super.attach();

        TextNode textNode = (TextNode)getNode();
        textNode.getTextNodeListeners().add(this);
    }
View Full Code Here

TOP

Related Classes of org.apache.pivot.wtk.text.TextNode

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.