Package org.apache.pivot.wtk

Examples of org.apache.pivot.wtk.TextPane


        return nodeView;
    }

    private void updateSelection() {
        if (documentView.getCharacterCount() > 0) {
            TextPane textPane = (TextPane)getComponent();

            // Update the caret
            int selectionStart = textPane.getSelectionStart();

            Bounds leadingSelectionBounds = getCharacterBounds(selectionStart);
            // sanity check - this is where a lot of bugs show up
            if (leadingSelectionBounds == null) {
                throw new IllegalStateException("no bounds for selection " + selectionStart);
            }
            caret = leadingSelectionBounds.toRectangle();
            caret.width = 1;

            // Update the selection
            int selectionLength = textPane.getSelectionLength();

            if (selectionLength > 0) {
                int selectionEnd = selectionStart + selectionLength - 1;
                Bounds trailingSelectionBounds = getCharacterBounds(selectionEnd);
                selection = new Area();
View Full Code Here


        @Override
        public void run() {
            caretOn = !caretOn;

            if (selection == null) {
                TextPane textPane = (TextPane)getComponent();
                textPane.repaint(caret.x, caret.y, caret.width, caret.height);
            }
        }
View Full Code Here

    }

    private class ScrollSelectionCallback implements Runnable {
        @Override
        public void run() {
            TextPane textPane = (TextPane)getComponent();
            int selectionStart = textPane.getSelectionStart();
            int selectionLength = textPane.getSelectionLength();
            int selectionEnd = selectionStart + selectionLength - 1;

            switch (scrollDirection) {
                case UP: {
                    // Get previous offset
                    int offset = getNextInsertionPoint(mouseX, selectionStart, scrollDirection);

                    if (offset != -1) {
                        textPane.setSelection(offset, selectionEnd - offset + 1);
                        scrollCharacterToVisible(offset + 1);
                    }

                    break;
                }

                case DOWN: {
                    // Get next offset
                    int offset = getNextInsertionPoint(mouseX, selectionEnd, scrollDirection);

                    if (offset != -1) {
                        // If the next character is a paragraph terminator and is not the
                        // final terminator character, increment the selection
                        Document document = textPane.getDocument();
                        if (document.getCharacterAt(offset) == '\n'
                            && offset < documentView.getCharacterCount() - 1) {
                            offset++;
                        }

                        textPane.setSelection(selectionStart, offset - selectionStart);
                        scrollCharacterToVisible(offset - 1);
                    }

                    break;
                }
View Full Code Here

    }

    @Override
    public void paint(Graphics2D graphics) {
        if (glyphVector != null) {
            TextPane textPane = (TextPane)getTextPaneSkin().getComponent();

            FontRenderContext fontRenderContext = Platform.getFontRenderContext();
            LineMetrics lm = getEffectiveFont().getLineMetrics("", fontRenderContext);
            float ascent = lm.getAscent();
            int strikethroughX = Math.round(lm.getAscent() + lm.getStrikethroughOffset());
            int underlineX = Math.round(lm.getAscent() + lm.getUnderlineOffset());
            boolean underline = getEffectiveUnderline();
            boolean strikethrough = getEffectiveStrikethrough();

            graphics.setFont(getEffectiveFont());

            int selectionStart = textPane.getSelectionStart();
            int selectionLength = textPane.getSelectionLength();
            Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);

            int documentOffset = getDocumentOffset();
            Span characterRange = new Span(documentOffset, documentOffset + getCharacterCount() - 1);

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

            if (selectionLength > 0
                && characterRange.intersects(selectionRange)) {
                // Determine the selection bounds
                int x0;
                if (selectionRange.start > characterRange.start) {
                    Bounds leadingSelectionBounds = getCharacterBounds(selectionRange.start - documentOffset);
                    x0 = leadingSelectionBounds.x;
                } else {
                    x0 = 0;
                }

                int x1;
                if (selectionRange.end < characterRange.end) {
                    Bounds trailingSelectionBounds = getCharacterBounds(selectionRange.end - documentOffset);
                    x1 = trailingSelectionBounds.x + trailingSelectionBounds.width;
                } else {
                    x1 = width;
                }

                Rectangle selection = new Rectangle(x0, 0, x1 - x0, height);

                // 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(getEffectiveForegroundColor());
                textGraphics.clip(unselectedArea);
                textGraphics.drawGlyphVector(glyphVector, 0, ascent);
                if (underline) {
                    textGraphics.drawLine(x0, underlineX, x1 - x0, underlineX);
                }
                if (strikethrough) {
                    textGraphics.drawLine(x0, strikethroughX, x1 - x0, strikethroughX);
                }
                textGraphics.dispose();

                // Paint the selection
                Color selectionColor;
                if (textPane.isFocused()) {
                    selectionColor = getTextPaneSkin().getSelectionColor();
                } else {
                    selectionColor = getTextPaneSkin().getInactiveSelectionColor();
                }

                Graphics2D selectedTextGraphics = (Graphics2D)graphics.create();
                selectedTextGraphics.setColor(textPane.isFocused() &&
                    textPane.isEditable() ? selectionColor : getTextPaneSkin().getInactiveSelectionColor());
                selectedTextGraphics.clip(selection.getBounds());
                selectedTextGraphics.drawGlyphVector(glyphVector, 0, ascent);
                if (underline) {
                    selectedTextGraphics.drawLine(0, underlineX, width, underlineX);
                }
View Full Code Here

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

        TextPane textPane = (TextPane)component;
        textPane.getTextPaneListeners().add(this);
        textPane.getTextPaneSelectionListeners().add(this);

        textPane.setCursor(Cursor.TEXT);

        Document document = textPane.getDocument();
        if (document != null) {
            documentView = (TextPaneSkinDocumentView)createNodeView(document);
            documentView.attach();
            updateSelection();
        }
View Full Code Here

    }

    @Override
    public void layout() {
        if (documentView != null) {
            TextPane textPane = (TextPane)getComponent();
            int width = getWidth();

            int breakWidth;
            if (wrapText) {
                breakWidth = Math.max(width - (margin.left + margin.right), 0);
            } else {
                breakWidth = Integer.MAX_VALUE;
            }
            documentView.layout(breakWidth);
            documentView.setSkinLocation(margin.left, margin.top);

            updateSelection();
            caretX = caret.x;

            if (textPane.isFocused()) {
                scrollCharacterToVisible(textPane.getSelectionStart());
            }

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

    @Override
    public void paint(Graphics2D graphics) {
        super.paint(graphics);

        TextPane textPane = (TextPane)getComponent();

        if (documentView != null) {
            // Draw the selection highlight
            if (selection != null) {
                graphics.setColor(textPane.isFocused()
                    && textPane.isEditable() ?
                    selectionBackgroundColor : inactiveSelectionBackgroundColor);
                graphics.fill(selection);
            }

            int width = getWidth();
            int breakWidth;
            if (wrapText) {
                breakWidth = Math.max(width - (margin.left + margin.right), 0);
            } else {
                breakWidth = Integer.MAX_VALUE;
            }
            documentView.layout(breakWidth);

            // Draw the document content
            graphics.translate(margin.left, margin.top);
            documentView.paint(graphics);
            graphics.translate(-margin.left, -margin.top);

            // Draw the caret
            if (selection == null
                && caretOn
                && textPane.isFocused()) {
                graphics.setColor(textPane.isEditable() ? color : inactiveColor);
                graphics.fill(caret);
            }
        }
    }
View Full Code Here

        return characterBounds;
    }

    private void scrollCharacterToVisible(int offset) {
        TextPane textPane = (TextPane)getComponent();
        Bounds characterBounds = getCharacterBounds(offset);

        if (characterBounds != null) {
            textPane.scrollAreaToVisible(characterBounds.x, characterBounds.y,
                characterBounds.width, characterBounds.height);
        }
    }
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) {
            TextPane textPane = (TextPane)getComponent();

            Bounds visibleArea = textPane.getVisibleArea();
            visibleArea = new Bounds(visibleArea.x, visibleArea.y,
                visibleArea.width, visibleArea.height);

            if (y >= visibleArea.y
                && y < visibleArea.y + visibleArea.height) {
                // Stop the scroll selection timer
                if (scheduledScrollSelectionCallback != null) {
                    scheduledScrollSelectionCallback.cancel();
                    scheduledScrollSelectionCallback = null;
                }

                scrollDirection = null;
                int offset = getInsertionPoint(x, y);

                if (offset != -1) {
                    // Select the range
                    if (offset > anchor) {
                        textPane.setSelection(anchor, offset - anchor);
                    } else {
                        textPane.setSelection(offset, anchor - offset);
                    }
                }
            } else {
                if (scheduledScrollSelectionCallback == null) {
                    scrollDirection = (y < visibleArea.y) ? TextPane.ScrollDirection.UP : TextPane.ScrollDirection.DOWN;
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) {
            TextPane textPane = (TextPane)component;

            anchor = getInsertionPoint(x, y);

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

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

            caretX = caret.x;

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

        return consumed;
    }
View Full Code Here

TOP

Related Classes of org.apache.pivot.wtk.TextPane

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.