Package org.apache.pivot.wtk

Examples of org.apache.pivot.wtk.TextInput$InsertTextEdit


                // Get the data being edited
                Object cellData = rowData.get(columnName);

                // Create the text input
                textInput = new TextInput();
                textInput.setText(cellData == null ? "" : cellData.toString());
                textInput.getComponentKeyListeners().add(textInputKeyHandler);

                // Create and open the popup
                popup = new Window(textInput);
View Full Code Here


    @Override
    public void install(Component component) {
        super.install(component);

        TextInput textInput = (TextInput)component;
        textInput.getTextInputListeners().add(this);
        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 int getPreferredWidth(int height) {
        TextInput textInput = (TextInput)getComponent();
        int textSize = textInput.getTextSize();

        return averageCharacterSize.width * textSize + (padding.left + padding.right) + 2;
    }
View Full Code Here

        return baseline;
    }

    @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();
                int textWidth = (int)textBounds.getWidth();
                int width = getWidth();

                if (textWidth - scrollLeft + padding.left + 1 < width - padding.right - 1) {
                    // The right edge of the text is less than the right inset; align
                    // the text's right edge with the inset
                    scrollLeft = Math.max(textWidth + (padding.left + padding.right + 2) - width, 0);
                } else {
                    // Scroll lead selection to visible
                    int selectionStart = textInput.getSelectionStart();
                    if (selectionStart < n
                        && textInput.isFocused()) {
                        scrollCharacterToVisible(selectionStart);
                    }
                }
            }
        } else {
            // Set scrollLeft to 0
            scrollLeft = 0;
            updateSelection();
        }

        updateSelection();
        showCaret(textInput.isFocused()
            && textInput.getSelectionLength() == 0);
    }
View Full Code Here

            && textInput.getSelectionLength() == 0);
    }

    @Override
    public void paint(Graphics2D graphics) {
        TextInput textInput = (TextInput)getComponent();

        int width = getWidth();
        int height = getHeight();

        Color backgroundColor;
        Color borderColor;
        Color bevelColor;

        if (textInput.isEnabled()) {
            if (textInput.isTextValid()) {
                backgroundColor = this.backgroundColor;
                bevelColor = this.bevelColor;
            } else {
                backgroundColor = invalidBackgroundColor;
                bevelColor = invalidBevelColor;
            }

            borderColor = this.borderColor;
        } else {
            backgroundColor = disabledBackgroundColor;
            borderColor = disabledBorderColor;
            bevelColor = disabledBevelColor;
        }

        graphics.setStroke(new BasicStroke());

        // Paint the background
        graphics.setColor(backgroundColor);
        graphics.fillRect(0, 0, width, height);

        // Paint the bevel
        graphics.setColor(bevelColor);
        GraphicsUtilities.drawLine(graphics, 0, 0, width, Orientation.HORIZONTAL);

        // Paint the content
        if (FONT_RENDER_CONTEXT.isAntiAliased()) {
            graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                Platform.getTextAntialiasingHint());
        }

        if (FONT_RENDER_CONTEXT.usesFractionalMetrics()) {
            graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        }

        LineMetrics lm = font.getLineMetrics("", FONT_RENDER_CONTEXT);
        float ascent = lm.getAscent();
        float textHeight = lm.getHeight();

        String prompt = textInput.getPrompt();

        if (glyphVector == null
            && prompt != null
            && !textInput.isFocused()) {
            graphics.setFont(font);
            graphics.setColor(promptColor);
            graphics.drawString(prompt, padding.left - scrollLeft + 1,
                (height - textHeight) / 2 + ascent);
        } else {
            boolean textValid = textInput.isTextValid();

            Color color;
            if (textInput.isEnabled()) {
                if (!textValid) {
                    color = invalidColor;
                } else {
                    color = this.color;
                }
            } else {
               color = disabledColor;
            }

            if (glyphVector != null) {
                graphics.setFont(font);

                if (selection == null) {
                    // Paint the text
                    graphics.setColor(color);
                    graphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1,
                        (height - textHeight) / 2 + ascent);
                } else {
                    // Paint the unselected text
                    Area unselectedArea = new Area();
                    unselectedArea.add(new Area(new Rectangle(0, 0, width, height)));
                    unselectedArea.subtract(new Area(selection));

                    Graphics2D textGraphics = (Graphics2D)graphics.create();
                    textGraphics.setColor(color);
                    textGraphics.clip(unselectedArea);
                    textGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1,
                        (height - textHeight) / 2 + ascent);
                    textGraphics.dispose();

                    // Paint the selection
                    Color selectionColor;
                    Color selectionBackgroundColor;

                    if (textInput.isFocused()) {
                        selectionColor = this.selectionColor;
                        selectionBackgroundColor = this.selectionBackgroundColor;
                    } else {
                        selectionColor = inactiveSelectionColor;
                        selectionBackgroundColor = inactiveSelectionBackgroundColor;
                    }

                    graphics.setColor(selectionBackgroundColor);
                    graphics.fill(selection);

                    Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
                    selectedTextGraphics.setColor(selectionColor);
                    selectedTextGraphics.clip(selection.getBounds());
                    selectedTextGraphics.drawGlyphVector(glyphVector, padding.left - scrollLeft + 1,
                        (height - textHeight) / 2 + ascent);
                    selectedTextGraphics.dispose();
                }
            }

            if (selection == null
                && caretOn
                && textInput.isFocused()) {
                graphics.setColor(color);
                graphics.fill(caret);
            }
        }
View Full Code Here

    @Override
    public boolean mouseMove(Component component, int x, int y) {
        boolean consumed = super.mouseMove(component, x, y);

        if (Mouse.getCapturer() == component) {
            TextInput textInput = (TextInput)getComponent();
            int width = getWidth();

            if (x >= 0
                && x < width) {
                // Stop the scroll selection timer
                if (scheduledScrollSelectionCallback != null) {
                    scheduledScrollSelectionCallback.cancel();
                    scheduledScrollSelectionCallback = null;
                }

                scrollDirection = null;

                int offset = getInsertionPoint(x);

                if (offset != -1) {
                    // Select the range
                    if (offset > anchor) {
                        textInput.setSelection(anchor, offset - anchor);
                    } else {
                        textInput.setSelection(offset, anchor - offset);
                    }
                }
            } else {
                if (scheduledScrollSelectionCallback == null) {
                    scrollDirection = (x < 0) ? Direction.BACKWARD : Direction.FORWARD;
View Full Code Here

    @Override
    public boolean mouseDown(Component component, Mouse.Button button, int x, int y) {
        boolean consumed = super.mouseDown(component, button, x, y);

        if (button == Mouse.Button.LEFT) {
            TextInput textInput = (TextInput)getComponent();

            anchor = getInsertionPoint(x);

            if (anchor != -1) {
                if (Keyboard.isPressed(Keyboard.Modifier.SHIFT)) {
                    // Select the range
                    int selectionStart = textInput.getSelectionStart();

                    if (anchor > selectionStart) {
                        textInput.setSelection(selectionStart, anchor - selectionStart);
                    } else {
                        textInput.setSelection(anchor, selectionStart - anchor);
                    }
                } else {
                    // Move the caret to the insertion point
                    textInput.setSelection(anchor, 0);
                    consumed = true;
                }
            }


            // Set focus to the text input
            textInput.requestFocus();
        }

        return consumed;
    }
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

        // 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)) {
            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 {
                            Toolkit.getDefaultToolkit().beep();
                        }
                    } else {
                        textInput.insertText(character, index);
                    }
                }
            }
        }
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();
                } else {
                    textInput.cut();
                }

                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.C
                && Keyboard.isPressed(commandModifier)) {
                if (textInput.isPassword()) {
                    Toolkit.getDefaultToolkit().beep();
                } else {
                    textInput.copy();
                }

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

TOP

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

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.