Search in sources :

Example 21 with TextInput

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

the class TerraTextInputSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    TextInput textInput = (TextInput) getComponent();
    int textSize = textInput.getTextSize();
    return averageCharacterSize.width * textSize + padding.getWidth() + 2;
}
Also used : TextInput(org.apache.pivot.wtk.TextInput)

Example 22 with TextInput

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

the class TerraTextInputSkin method layout.

@Override
public void layout() {
    TextInput textInput = (TextInput) getComponent();
    textLayout = null;
    int n = textInput.getCharacterCount();
    AttributedStringCharacterIterator composedText = textInput.getComposedText();
    if (n > 0 || composedText != null) {
        AttributedCharacterIterator text = null;
        if (n > 0) {
            int insertPos = textInput.getSelectionStart();
            if (composedText == null) {
                text = getCharIterator(textInput, 0, n);
            } else if (insertPos == n) {
                // The composed text position is the end of the committed text
                text = new CompositeIterator(getCharIterator(textInput, 0, n), composedText);
            } else if (insertPos == 0) {
                text = new CompositeIterator(composedText, getCharIterator(textInput, 0, n));
            } else {
                // The composed text is somewhere in the middle of the text
                text = new CompositeIterator(getCharIterator(textInput, 0, insertPos), composedText, getCharIterator(textInput, insertPos, n));
            }
        } else {
            text = composedText;
        }
        FontRenderContext fontRenderContext = Platform.getFontRenderContext();
        textLayout = new TextLayout(text, fontRenderContext);
        int textWidth = getTextWidth(textLayout);
        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.getWidth() + 2 - width, 0);
        } else {
            // Scroll selection start to visible
            int selectionStart = textInput.getSelectionStart();
            if (textInput.isFocused()) {
                if (composedVisiblePosition != null) {
                    scrollCharacterToVisible(selectionStart + composedVisiblePosition.getInsertionIndex());
                } else {
                    if (selectionStart <= n) {
                        scrollCharacterToVisible(selectionStart);
                    }
                }
            }
        }
    } else {
        scrollLeft = 0;
    }
    updateSelection();
    showCaret(textInput.isFocused() && textInput.getSelectionLength() == 0);
}
Also used : CompositeIterator(org.apache.pivot.text.CompositeIterator) FontRenderContext(java.awt.font.FontRenderContext) TextInput(org.apache.pivot.wtk.TextInput) AttributedStringCharacterIterator(org.apache.pivot.text.AttributedStringCharacterIterator) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout)

Example 23 with TextInput

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

the class TerraTextInputSkin method mouseClick.

@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();
        if (count == 2) {
            CharSpan charSpan = CharUtils.selectWord(textInput.getCharacters(), getInsertionPoint(x));
            if (charSpan != null) {
                textInput.setSelection(charSpan);
            }
        } else if (count == 3) {
            textInput.selectAll();
        }
        selectDirection = null;
    }
    return super.mouseClick(component, button, x, y, count);
}
Also used : CharSpan(org.apache.pivot.text.CharSpan) TextInput(org.apache.pivot.wtk.TextInput)

Example 24 with TextInput

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

the class TerraTextInputSkin method updateSelection.

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 {
        // The insertion point is after the last character
        int x = padding.left + 1 - scrollLeft + getAlignmentDeltaX(textLayout);
        if (n > 0) {
            x += getTextWidth(textLayout);
        }
        int y = padding.top + 1;
        leadingSelectionBounds = new Bounds(x, y, 0, height - (padding.top + padding.bottom + 2));
    }
    if (composedTextCaret != null) {
        caret = getCaretRectangle(composedTextCaret);
    } else {
        caret = leadingSelectionBounds.toRectangle();
    }
    caret.width = 1;
    if (selectionLength > 0) {
        Bounds trailingSelectionBounds = getCharacterBounds(selectionStart + selectionLength - 1);
        selection = new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - leadingSelectionBounds.x, trailingSelectionBounds.y + trailingSelectionBounds.height - leadingSelectionBounds.y);
    } else {
        selection = null;
    }
}
Also used : Bounds(org.apache.pivot.wtk.Bounds) Rectangle(java.awt.Rectangle) TextInput(org.apache.pivot.wtk.TextInput)

Example 25 with TextInput

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

the class TerraFileBrowserSheetSkin method install.

@Override
public void install(Component component) {
    super.install(component);
    final FileBrowserSheet fileBrowserSheet = (FileBrowserSheet) component;
    fileBrowserSheet.setMinimumWidth(360);
    fileBrowserSheet.setMinimumHeight(180);
    // Load the sheet content
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    Component content;
    try {
        content = (Component) bxmlSerializer.readObject(TerraFileBrowserSheetSkin.class, "terra_file_browser_sheet_skin.bxml", true);
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    fileBrowserSheet.setContent(content);
    bxmlSerializer.bind(this, TerraFileBrowserSheetSkin.class);
    // set the same rootDirectory to fileBrowser
    fileBrowser.setRootDirectory(fileBrowserSheet.getRootDirectory());
    saveAsTextInput.getTextInputContentListeners().add(new TextInputContentListener() {

        @Override
        public void textChanged(TextInput textInput) {
            Form.clearFlag(saveAsBoxPane);
            updateOKButtonState();
        }
    });
    fileBrowser.getFileBrowserListeners().add(new FileBrowserListener() {

        @Override
        public void rootDirectoryChanged(FileBrowser fileBrowserArgument, File previousRootDirectory) {
            updatingSelection = true;
            fileBrowserSheet.setRootDirectory(fileBrowserArgument.getRootDirectory());
            updatingSelection = false;
            selectedDirectoryCount = 0;
            updateOKButtonState();
        }

        @Override
        public void selectedFileAdded(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount++;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFileRemoved(FileBrowser fileBrowserArgument, File file) {
            if (file.isDirectory()) {
                selectedDirectoryCount--;
            }
            updateOKButtonState();
        }

        @Override
        public void selectedFilesChanged(FileBrowser fileBrowserArgument, Sequence<File> previousSelectedFiles) {
            selectedDirectoryCount = 0;
            Sequence<File> selectedFiles = fileBrowserArgument.getSelectedFiles();
            for (int i = 0, n = selectedFiles.getLength(); i < n; i++) {
                File selectedFile = selectedFiles.get(i);
                if (selectedFile.isDirectory()) {
                    selectedDirectoryCount++;
                }
            }
            if (!fileBrowserArgument.isMultiSelect()) {
                File selectedFile = fileBrowserArgument.getSelectedFile();
                if (selectedFile != null && !selectedFile.isDirectory()) {
                    saveAsTextInput.setText(selectedFile.getName());
                }
            }
            updateOKButtonState();
        }
    });
    fileBrowser.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private File file = null;

        @Override
        public boolean mouseClick(Component componentArgument, Mouse.Button button, int x, int y, int count) {
            boolean consumed = false;
            FileBrowserSheet.Mode mode = fileBrowserSheet.getMode();
            if (count == 1) {
                file = fileBrowser.getFileAt(x, y);
            } else if (count == 2) {
                File fileLocal = fileBrowser.getFileAt(x, y);
                if (fileLocal != null && this.file != null && fileLocal.equals(this.file) && fileBrowser.isFileSelected(fileLocal)) {
                    if (mode == FileBrowserSheet.Mode.OPEN || mode == FileBrowserSheet.Mode.OPEN_MULTIPLE) {
                        if (!fileLocal.isDirectory()) {
                            fileBrowserSheet.close(true);
                            consumed = true;
                        }
                    }
                }
            }
            return consumed;
        }
    });
    okButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(true);
        }
    });
    cancelButton.getButtonPressListeners().add(new ButtonPressListener() {

        @Override
        public void buttonPressed(Button button) {
            fileBrowserSheet.close(false);
        }
    });
    // Add this as a file browser sheet listener
    fileBrowserSheet.getFileBrowserSheetListeners().add(this);
    modeChanged(fileBrowserSheet, null);
    rootDirectoryChanged(fileBrowserSheet, null);
    selectedFilesChanged(fileBrowserSheet, null);
}
Also used : FileBrowserListener(org.apache.pivot.wtk.FileBrowserListener) SerializationException(org.apache.pivot.serialization.SerializationException) FileBrowserSheet(org.apache.pivot.wtk.FileBrowserSheet) IOException(java.io.IOException) Sequence(org.apache.pivot.collections.Sequence) ButtonPressListener(org.apache.pivot.wtk.ButtonPressListener) TextInputContentListener(org.apache.pivot.wtk.TextInputContentListener) Mouse(org.apache.pivot.wtk.Mouse) PushButton(org.apache.pivot.wtk.PushButton) Button(org.apache.pivot.wtk.Button) FileBrowser(org.apache.pivot.wtk.FileBrowser) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component) TextInput(org.apache.pivot.wtk.TextInput) File(java.io.File) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

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