Package org.apache.pivot.wtk.text

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


            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
                // so get to the bottom rightmost descendant and add there
                Paragraph paragraph = (Paragraph)descendant;

                Node node = getRightmostDescendant(paragraph);
                if (node instanceof TextNode) {
                    // Insert the text into the existing node
                    TextNode textNode = (TextNode)node;
                    textNode.insertText(text, selectionStart - textNode.getDocumentOffset());
                } else if (node instanceof Element) {
                    // Append a new text node
                    Element element = (Element)node;
                    element.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(), 0);
View Full Code Here


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

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

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

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

        }

        @Override
        public void validate() {
            if (!isValid()) {
                TextNode textNode = (TextNode)getNode();

                int breakWidth = getBreakWidth();
                CharacterIterator ci = textNode.getCharacterIterator(start);

                float lineWidth = 0;
                int lastWhitespaceIndex = -1;

                char c = ci.first();
                while (c != CharacterIterator.DONE
                    && lineWidth < breakWidth) {
                    if (Character.isWhitespace(c)) {
                        lastWhitespaceIndex = ci.getIndex();
                    }

                    int i = ci.getIndex();
                    Rectangle2D characterBounds = font.getStringBounds(ci, i, i + 1,
                        FONT_RENDER_CONTEXT);
                    lineWidth += characterBounds.getWidth();

                    c = ci.current();
                }

                int end;
                if (lineWidth <= breakWidth) {
                    end = ci.getEndIndex();
                } else {
                    if (lastWhitespaceIndex == -1) {
                        end = ci.getIndex() - 1;
                        if (end <= start) {
                            end = start + 1;
                        }
                    } else {
                        end = lastWhitespaceIndex + 1;
                    }
                }

                glyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT,
                    textNode.getCharacterIterator(start, end));

                if (end < ci.getEndIndex()) {
                    length = end - start;
                    next = new TextNodeView(textNode, end);
                } else {
View Full Code Here

            invalidate();
        }

        @Override
        public String toString() {
            TextNode textNode = (TextNode)getNode();
            String text = textNode.getText();
            return "[" + text.substring(start, start + length) + "]";
        }
View Full Code Here

        textInput.getTextInputCharacterListeners().add(this);
        textInput.getTextInputSelectionListeners().add(this);

        textInput.setCursor(Cursor.TEXT);

        TextNode textNode = textInput.getTextNode();
        if (textNode != null) {
            updateSelection();
        }
    }
View Full Code Here

    }

    @Override
    public void layout() {
        TextInput textInput = (TextInput)getComponent();
        TextNode textNode = textInput.getTextNode();

        glyphVector = null;

        if (textNode != null) {
            int n = textNode.getCharacterCount();

            if (n > 0) {
                CharacterIterator ci = null;
                if (textInput.isPassword()) {
                    StringBuilder buf = new StringBuilder(n);
                    for (int i = 0; i < n; i++) {
                        buf.append(BULLET);
                    }

                    ci = new StringCharacterIterator(buf.toString());
                } else {
                    ci= textNode.getCharacterIterator();
                }

                glyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, ci);

                Rectangle2D textBounds = glyphVector.getLogicalBounds();
View Full Code Here

    @Override
    public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
        if (button == Mouse.Button.LEFT
            && count > 1) {
            TextInput textInput = (TextInput)getComponent();
            TextNode textNode = textInput.getTextNode();

            if (textNode != null) {
                textInput.setSelection(0, textNode.getCharacterCount());
            }
        }

        return super.mouseClick(component, button, x, y, count);
    }
View Full Code Here

        // character as well as meta key presses
        if (character > 0x1F
            && character != 0x7F
            && !Keyboard.isPressed(Keyboard.Modifier.META)) {
            TextInput textInput = (TextInput)getComponent();
            TextNode textNode = textInput.getTextNode();

            if (textNode != null) {
                if (textInput.getSelectionLength() == 0
                    && textNode.getCharacterCount() == textInput.getMaximumLength()) {
                    Toolkit.getDefaultToolkit().beep();
                } else {
                    int index = textInput.getSelectionStart();
                    Validator validator = textInput.getValidator();

                    if (validator != null
                        && strictValidation) {
                        StringBuilder buf = new StringBuilder(textNode.getText());
                        buf.insert(index, character);

                        if (validator.isValid(buf.toString())) {
                            textInput.insertText(character, index);
                        } else {
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();
        TextNode textNode = textInput.getTextNode();

        if (textNode != null) {
            Keyboard.Modifier commandModifier = Platform.getCommandModifier();
            if (keyCode == Keyboard.KeyCode.DELETE
                || keyCode == Keyboard.KeyCode.BACKSPACE) {
                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 {
                        Toolkit.getDefaultToolkit().beep();
                    }
                } else {
                    textInput.delete(direction);
                }

                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.LEFT) {
                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;
                    consumed = true;
                } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    // Add the previous character to the selection
                    if (selectionStart > 0) {
                        selectionStart--;
                        selectionLength++;
                    }

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

                    selectionLength = 0;
                }

                textInput.setSelection(selectionStart, selectionLength);

                if (textNode.getCharacterCount() > 0) {
                    scrollCharacterToVisible(selectionStart);
                } else {
                    setScrollLeft(0);
                }
            } else if (keyCode == Keyboard.KeyCode.RIGHT) {
                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;
                    consumed = true;
                } else if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    // Add the next character to the selection
                    if (selectionStart + selectionLength < textNode.getCharacterCount()) {
                        selectionLength++;
                    }

                    consumed = true;
                } 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;
                    consumed = true;
                } else {
                    // Clear the selection and move the caret forward by one
                    // character
                    selectionStart += selectionLength;

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

                    selectionLength = 0;
                }

                textInput.setSelection(selectionStart, selectionLength);

                if (textNode.getCharacterCount() > 0) {
                    scrollCharacterToVisible(selectionStart + selectionLength - 1);
                } else {
                    scrollLeft = 0;
                    updateSelection();
                }
            } else if (keyCode == Keyboard.KeyCode.HOME) {
                // 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);
                }

                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.END) {
                // Move the caret to the end of the text
                if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    int selectionStart = textInput.getSelectionStart();
                    textInput.setSelection(selectionStart, textNode.getCharacterCount()
                        - selectionStart);
                } else {
                    textInput.setSelection(textNode.getCharacterCount(), 0);
                }

                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.A
                && Keyboard.isPressed(commandModifier)) {
                // Select all
                textInput.setSelection(0, textNode.getCharacterCount());
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.X
                && Keyboard.isPressed(commandModifier)) {
                if (textInput.isPassword()) {
                    Toolkit.getDefaultToolkit().beep();
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.