Search in sources :

Example 16 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class SuggestionDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    textInput = (TextInput) namespace.get("textInput");
    activityIndicator = (ActivityIndicator) namespace.get("activityIndicator");
    textInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textInserted(TextInput textInputArgument, int index, int count) {
            getSuggestions();
        }

        @Override
        public void textRemoved(TextInput textInputArgument, int index, int count) {
            if (suggestionQuery != null) {
                suggestionQuery.abort();
            }
            suggestionPopup.close();
        }
    });
}
Also used : TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) TextInput(org.apache.pivot.wtk.TextInput)

Example 17 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class ComponentKeyListenerExample method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    TextInput textInput = (TextInput) namespace.get("textInput");
    textInput.getComponentKeyListeners().add(new ComponentKeyListener() {

        @Override
        public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
            if (keyCode == Keyboard.KeyCode.S && Keyboard.isPressed(Keyboard.Modifier.CTRL)) {
                Alert.alert("You pressed Control-S!", component.getWindow());
            }
            return false;
        }
    });
}
Also used : ComponentKeyListener(org.apache.pivot.wtk.ComponentKeyListener) Keyboard(org.apache.pivot.wtk.Keyboard) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component)

Example 18 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TerraTextInputSkin method paint.

@Override
public void paint(Graphics2D graphics) {
    TextInput textInput = (TextInput) getComponent();
    int width = getWidth();
    int height = getHeight();
    Color backgroundColorLocal;
    Color borderColorLocal;
    Color bevelColorLocal;
    if (textInput.isEnabled()) {
        if (textInput.isTextValid()) {
            backgroundColorLocal = this.backgroundColor;
            bevelColorLocal = this.bevelColor;
        } else {
            backgroundColorLocal = invalidBackgroundColor;
            bevelColorLocal = invalidBevelColor;
        }
        borderColorLocal = this.borderColor;
    } else {
        backgroundColorLocal = disabledBackgroundColor;
        borderColorLocal = disabledBorderColor;
        bevelColorLocal = disabledBevelColor;
    }
    graphics.setStroke(new BasicStroke());
    // Paint the background
    graphics.setColor(backgroundColorLocal);
    graphics.fillRect(0, 0, width, height);
    if (!themeIsFlat()) {
        // Paint the bevel
        graphics.setColor(bevelColorLocal);
        GraphicsUtilities.drawLine(graphics, 0, 0, width, Orientation.HORIZONTAL);
    }
    // Paint the content
    FontRenderContext fontRenderContext = Platform.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("", fontRenderContext);
    float ascent = lm.getAscent();
    float textHeight = lm.getHeight();
    String prompt = textInput.getPrompt();
    Color caretColor;
    TextLayout drawingTextLayout = textLayout;
    if (textLayout == null && prompt != null && !prompt.isEmpty()) {
        AttributedStringCharacterIterator promptText = new AttributedStringCharacterIterator(prompt);
        drawingTextLayout = new TextLayout(promptText, fontRenderContext);
    }
    int alignmentDeltaX = getAlignmentDeltaX(drawingTextLayout);
    int xpos = padding.left - scrollLeft + 1 + alignmentDeltaX;
    if (textLayout == null && prompt != null && !prompt.isEmpty()) {
        GraphicsUtilities.prepareForText(graphics, fontRenderContext, font, promptColor);
        graphics.drawString(prompt, xpos, (height - textHeight) / 2 + ascent);
        caretColor = color;
    } else {
        boolean textValid = textInput.isTextValid();
        Color colorLocal;
        if (textInput.isEnabled()) {
            if (!textValid) {
                colorLocal = invalidColor;
            } else {
                colorLocal = this.color;
            }
        } else {
            colorLocal = disabledColor;
        }
        caretColor = colorLocal;
        if (textLayout != null) {
            graphics.setFont(font);
            float ypos = (height - textHeight) / 2 + ascent;
            if (selection == null) {
                // Paint the text
                graphics.setColor(colorLocal);
                textLayout.draw(graphics, xpos, ypos);
            } 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(colorLocal);
                textGraphics.clip(unselectedArea);
                textLayout.draw(textGraphics, xpos, ypos);
                textGraphics.dispose();
                // Paint the selection
                Color selectionColorLocal;
                Color selectionBackgroundColorLocal;
                if (textInput.isFocused() && textInput.isEditable()) {
                    selectionColorLocal = this.selectionColor;
                    selectionBackgroundColorLocal = this.selectionBackgroundColor;
                } else {
                    selectionColorLocal = inactiveSelectionColor;
                    selectionBackgroundColorLocal = inactiveSelectionBackgroundColor;
                }
                graphics.setColor(selectionBackgroundColorLocal);
                graphics.fill(selection);
                Graphics2D selectedTextGraphics = (Graphics2D) graphics.create();
                selectedTextGraphics.setColor(selectionColorLocal);
                selectedTextGraphics.clip(selection.getBounds());
                textLayout.draw(selectedTextGraphics, xpos, ypos);
                selectedTextGraphics.dispose();
            }
        }
    }
    // Paint the caret
    if (selection == null && caretOn && textInput.isFocused()) {
        graphics.setColor(caretColor);
        graphics.fill(caret);
    }
    if (!themeIsFlat()) {
        // Paint the border
        graphics.setColor(borderColorLocal);
        GraphicsUtilities.drawRect(graphics, 0, 0, width, height);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) Rectangle(java.awt.Rectangle) TextLayout(java.awt.font.TextLayout) AttributedStringCharacterIterator(org.apache.pivot.text.AttributedStringCharacterIterator) Graphics2D(java.awt.Graphics2D) Area(java.awt.geom.Area) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) TextInput(org.apache.pivot.wtk.TextInput)

Example 19 with TextInput

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

Example 20 with TextInput

use of org.apache.pivot.wtk.TextInput in project pivot by apache.

the class TerraTextInputSkin method focusedChanged.

@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 {
        // clear the selection
        if (obverseComponent == null || obverseComponent.getWindow() == window) {
            textInput.clearSelection();
        }
        showCaret(false);
    }
    repaintComponent();
}
Also used : Window(org.apache.pivot.wtk.Window) TextInput(org.apache.pivot.wtk.TextInput)

Aggregations

TextInput (org.apache.pivot.wtk.TextInput)51 Component (org.apache.pivot.wtk.Component)21 BoxPane (org.apache.pivot.wtk.BoxPane)15 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)11 TextInputContentListener (org.apache.pivot.wtk.TextInputContentListener)9 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)8 Label (org.apache.pivot.wtk.Label)8 Point (org.apache.pivot.wtk.Point)8 IntValidator (org.apache.pivot.wtk.validation.IntValidator)8 FlowPane (org.apache.pivot.wtk.FlowPane)7 ArrayList (org.apache.pivot.collections.ArrayList)6 IOException (java.io.IOException)5 Button (org.apache.pivot.wtk.Button)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)5 Keyboard (org.apache.pivot.wtk.Keyboard)5 Mouse (org.apache.pivot.wtk.Mouse)5 Sequence (org.apache.pivot.collections.Sequence)4 SerializationException (org.apache.pivot.serialization.SerializationException)4 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)4