Search in sources :

Example 1 with TextPane

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

the class TextPaneSkin method getCaretRectangle.

private Rectangle getCaretRectangle(TextHitInfo textCaret) {
    TextPane textPane = getTextPane();
    AttributedStringCharacterIterator composedText = textPane.getComposedText();
    // Special case that tweaks the bounds at the end of line so that the entire
    // composed text width isn't added in here.... (yeah, I know it's ugly...)
    int selectionStart = textPane.getSelectionStart();
    this.doingCaretCalculations = true;
    Bounds selectionStartBounds = getCharacterBounds(selectionStart);
    this.doingCaretCalculations = false;
    // without the "doingCaretCalculations" flag to get back something non-null
    if (selectionStartBounds == null) {
        selectionStartBounds = getCharacterBounds(selectionStart);
        org.apache.pivot.util.Console.logMethod("****", "null selection bounds: selectionStart=%1$d, updated bounds=%2$s", selectionStart, selectionStartBounds);
    }
    return GraphicsUtilities.getCaretRectangle(textCaret, composedText, selectionStartBounds.x, selectionStartBounds.y);
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) Bounds(org.apache.pivot.wtk.Bounds) AttributedStringCharacterIterator(org.apache.pivot.text.AttributedStringCharacterIterator)

Example 2 with TextPane

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

the class TextPaneSkin method install.

@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) TextPaneSkinNodeView.createNodeView(this, document);
        documentView.attach();
        updateSelection();
    }
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) Document(org.apache.pivot.wtk.text.Document)

Example 3 with TextPane

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

the class TextPaneSkin method mouseMove.

@Override
public boolean mouseMove(Component component, int x, int y) {
    boolean consumed = super.mouseMove(component, x, y);
    if (Mouse.getCapturer() == component) {
        TextPane textPane = getTextPane();
        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;
                scheduledScrollSelectionCallback = ApplicationContext.runAndScheduleRecurringCallback(scrollSelectionCallback, SCROLL_RATE);
            }
        }
        mouseX = x;
    } else {
        if (Mouse.isPressed(Mouse.Button.LEFT) && Mouse.getCapturer() == null && anchor != -1) {
            // Capture the mouse so we can select text
            Mouse.capture(component);
        }
    }
    return consumed;
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) Bounds(org.apache.pivot.wtk.Bounds)

Example 4 with TextPane

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

the class TextPaneSkin method mouseClick.

@Override
public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
    boolean consumed = super.mouseClick(component, button, x, y, count);
    TextPane textPane = (TextPane) component;
    Document document = textPane.getDocument();
    if (button == Mouse.Button.LEFT) {
        int index = getInsertionPoint(x, y);
        if (index != -1) {
            if (count == 2) {
                CharSpan charSpan = CharUtils.selectWord(getRowCharacters(document, index), index);
                if (charSpan != null) {
                    textPane.setSelection(charSpan);
                }
            } else if (count == 3) {
                textPane.setSelection(getRowOffset(document, index), getRowLength(document, index));
            }
        }
    }
    return consumed;
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) CharSpan(org.apache.pivot.text.CharSpan) Document(org.apache.pivot.wtk.text.Document)

Example 5 with TextPane

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

the class TextPaneSkinTextNodeView method paint.

@Override
public void paint(Graphics2D graphics) {
    if (textLayout != null) {
        TextPane textPane = (TextPane) getTextPaneSkin().getComponent();
        FontRenderContext fontRenderContext = Platform.getFontRenderContext();
        Font effectiveFont = getEffectiveFont();
        LineMetrics lm = effectiveFont.getLineMetrics("", fontRenderContext);
        float ascent = lm.getAscent() + lm.getLeading();
        graphics.setFont(effectiveFont);
        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;
            }
            int selectionWidth = x1 - x0;
            Rectangle selection = new Rectangle(x0, 0, selectionWidth, 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);
            textLayout.draw(textGraphics, 0, ascent);
            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());
            textLayout.draw(selectedTextGraphics, 0, ascent);
            selectedTextGraphics.dispose();
        } else {
            // Draw the text
            graphics.setColor(getEffectiveForegroundColor());
            textLayout.draw(graphics, 0, ascent);
        }
    }
}
Also used : Area(java.awt.geom.Area) TextPane(org.apache.pivot.wtk.TextPane) Bounds(org.apache.pivot.wtk.Bounds) Color(java.awt.Color) Rectangle(java.awt.Rectangle) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics) Span(org.apache.pivot.wtk.Span) TextSpan(org.apache.pivot.wtk.text.TextSpan) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D)

Aggregations

TextPane (org.apache.pivot.wtk.TextPane)14 Bounds (org.apache.pivot.wtk.Bounds)5 Document (org.apache.pivot.wtk.text.Document)5 Font (java.awt.Font)2 Rectangle (java.awt.Rectangle)2 FontRenderContext (java.awt.font.FontRenderContext)2 Area (java.awt.geom.Area)2 AttributedStringCharacterIterator (org.apache.pivot.text.AttributedStringCharacterIterator)2 Span (org.apache.pivot.wtk.Span)2 Paragraph (org.apache.pivot.wtk.text.Paragraph)2 TextNode (org.apache.pivot.wtk.text.TextNode)2 TextSpan (org.apache.pivot.wtk.text.TextSpan)2 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)1 LineMetrics (java.awt.font.LineMetrics)1 TextLayout (java.awt.font.TextLayout)1 AttributedCharacterIterator (java.text.AttributedCharacterIterator)1 CharSpan (org.apache.pivot.text.CharSpan)1 CompositeIterator (org.apache.pivot.text.CompositeIterator)1