Search in sources :

Example 51 with Bounds

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

the class TextAreaSkin method updateSelection.

private void updateSelection() {
    TextArea textArea = (TextArea) getComponent();
    if (paragraphViews.getLength() > 0) {
        // Update the caret
        int selectionStart = textArea.getSelectionStart();
        Bounds leadingSelectionBounds = getCharacterBounds(selectionStart);
        caret = leadingSelectionBounds.toRectangle();
        caret.width = 1;
        // Update the selection
        int selectionLength = textArea.getSelectionLength();
        if (selectionLength > 0) {
            int selectionEnd = selectionStart + selectionLength - 1;
            Bounds trailingSelectionBounds = getCharacterBounds(selectionEnd);
            selection = new Area();
            int firstRowIndex = getRowAt(selectionStart);
            int lastRowIndex = getRowAt(selectionEnd);
            if (firstRowIndex == lastRowIndex) {
                selection.add(new Area(new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - leadingSelectionBounds.x, trailingSelectionBounds.y + trailingSelectionBounds.height - leadingSelectionBounds.y)));
            } else {
                int width = getWidth();
                selection.add(new Area(new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, width - margin.right - leadingSelectionBounds.x, leadingSelectionBounds.height)));
                if (lastRowIndex - firstRowIndex > 0) {
                    selection.add(new Area(new Rectangle(margin.left, leadingSelectionBounds.y + leadingSelectionBounds.height, width - margin.getWidth(), trailingSelectionBounds.y - (leadingSelectionBounds.y + leadingSelectionBounds.height))));
                }
                selection.add(new Area(new Rectangle(margin.left, trailingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - margin.left, trailingSelectionBounds.height)));
            }
        } else {
            selection = null;
        }
    } else {
        // Clear the caret and the selection
        caret = new Rectangle();
        selection = null;
    }
}
Also used : Area(java.awt.geom.Area) TextArea(org.apache.pivot.wtk.TextArea) TextArea(org.apache.pivot.wtk.TextArea) Bounds(org.apache.pivot.wtk.Bounds) Rectangle(java.awt.Rectangle)

Example 52 with Bounds

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

the class TextPaneSkin method updateSelection.

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);
        }
        if (composedTextCaret != null) {
            caret = getCaretRectangle(composedTextCaret);
        } else {
            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();
            int firstRowIndex = getRowAt(selectionStart);
            int lastRowIndex = getRowAt(selectionEnd);
            if (firstRowIndex == lastRowIndex) {
                selection.add(new Area(new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - leadingSelectionBounds.x, trailingSelectionBounds.y + trailingSelectionBounds.height - leadingSelectionBounds.y)));
            } else {
                int width = getWidth();
                selection.add(new Area(new Rectangle(leadingSelectionBounds.x, leadingSelectionBounds.y, width - margin.right - leadingSelectionBounds.x, leadingSelectionBounds.height)));
                if (lastRowIndex - firstRowIndex > 0) {
                    selection.add(new Area(new Rectangle(margin.left, leadingSelectionBounds.y + leadingSelectionBounds.height, width - (margin.left + margin.right), trailingSelectionBounds.y - (leadingSelectionBounds.y + leadingSelectionBounds.height))));
                }
                selection.add(new Area(new Rectangle(margin.left, trailingSelectionBounds.y, trailingSelectionBounds.x + trailingSelectionBounds.width - margin.left, trailingSelectionBounds.height)));
            }
        } else {
            selection = null;
        }
    } else {
        // Clear the caret and the selection
        caret = new Rectangle();
        selection = null;
    }
}
Also used : Area(java.awt.geom.Area) TextPane(org.apache.pivot.wtk.TextPane) Bounds(org.apache.pivot.wtk.Bounds) Rectangle(java.awt.Rectangle)

Example 53 with Bounds

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

the class TextPaneSkin method keyPressed.

@Override
public boolean keyPressed(final Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
    boolean consumed = false;
    final TextPane textPane = getTextPane();
    Document document = textPane.getDocument();
    int selectionStart = textPane.getSelectionStart();
    int selectionLength = textPane.getSelectionLength();
    boolean commandPressed = Keyboard.isPressed(Platform.getCommandModifier());
    boolean wordNavPressed = Keyboard.isPressed(Platform.getWordNavigationModifier());
    boolean shiftPressed = Keyboard.isPressed(Keyboard.Modifier.SHIFT);
    boolean metaPressed = Keyboard.isPressed(Keyboard.Modifier.META);
    boolean isEditable = textPane.isEditable();
    if (document != null) {
        if (keyCode == Keyboard.KeyCode.ENTER && isEditable) {
            textPane.insertParagraph();
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.DELETE && isEditable) {
            textPane.delete(false);
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.BACKSPACE && isEditable) {
            textPane.delete(true);
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.HOME || (keyCode == Keyboard.KeyCode.LEFT && metaPressed)) {
            int start;
            if (commandPressed) {
                // Move the caret to the beginning of the text
                start = 0;
            } else {
                // Move the caret to the beginning of the line
                start = getRowOffset(document, selectionStart);
            }
            if (shiftPressed) {
                // TODO: if last direction was left, then extend further left
                // but if right, then reverse selection from the pivot point
                selectionLength += selectionStart - start;
            } else {
                selectionLength = 0;
            }
            if (start >= 0) {
                textPane.setSelection(start, selectionLength);
                scrollCharacterToVisible(start);
                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.END || (keyCode == Keyboard.KeyCode.RIGHT && metaPressed)) {
            int end;
            int index = selectionStart + selectionLength;
            if (commandPressed) {
                // Move the caret to end of the text
                end = textPane.getCharacterCount() - 1;
            } else {
                // Move the caret to the end of the line
                int rowOffset = getRowOffset(document, index);
                int rowLength = getRowLength(document, index);
                end = rowOffset + rowLength;
            }
            if (shiftPressed) {
                // TODO: if last direction was right, then extend further right
                // but if left, then reverse selection from the pivot point
                selectionLength += end - index;
            } else {
                selectionStart = end;
                selectionLength = 0;
            }
            if (selectionStart + selectionLength <= textPane.getCharacterCount()) {
                textPane.setSelection(selectionStart, selectionLength);
                scrollCharacterToVisible(selectionStart + selectionLength);
                consumed = true;
            }
        } else if (keyCode == Keyboard.KeyCode.LEFT) {
            if (wordNavPressed) {
                // Move the caret to the start of the next word to our left
                if (selectionStart > 0) {
                    int originalStart = selectionStart;
                    // first, skip over any space immediately to our left
                    while (selectionStart > 0 && Character.isWhitespace(document.getCharacterAt(selectionStart - 1))) {
                        selectionStart--;
                    }
                    // then, skip over any word-letters to our left
                    while (selectionStart > 0 && !Character.isWhitespace(document.getCharacterAt(selectionStart - 1))) {
                        selectionStart--;
                    }
                    if (shiftPressed) {
                        selectionLength += (originalStart - selectionStart);
                    } else {
                        selectionLength = 0;
                    }
                }
            } else if (shiftPressed) {
                // Add the previous character to the selection
                if (selectionStart > 0) {
                    selectionStart--;
                    selectionLength++;
                }
            } else {
                // Clear the selection and move the caret back by one character
                if (selectionLength == 0 && selectionStart > 0) {
                    selectionStart--;
                }
                selectionLength = 0;
            }
            textPane.setSelection(selectionStart, selectionLength);
            scrollCharacterToVisible(selectionStart);
            caretX = caret.x;
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.RIGHT) {
            if (shiftPressed) {
                // Add the next character to the selection
                if (selectionStart + selectionLength < document.getCharacterCount()) {
                    selectionLength++;
                }
                textPane.setSelection(selectionStart, selectionLength);
                scrollCharacterToVisible(selectionStart + selectionLength);
            } else if (wordNavPressed) {
                // Move the caret to the start of the next word to our right
                if (selectionStart < document.getCharacterCount()) {
                    // first, skip over any word-letters to our right
                    while (selectionStart < document.getCharacterCount() - 1 && !Character.isWhitespace(document.getCharacterAt(selectionStart))) {
                        selectionStart++;
                    }
                    // then, skip over any space immediately to our right
                    while (selectionStart < document.getCharacterCount() - 1 && Character.isWhitespace(document.getCharacterAt(selectionStart))) {
                        selectionStart++;
                    }
                    textPane.setSelection(selectionStart, 0);
                    scrollCharacterToVisible(selectionStart);
                    caretX = caret.x;
                }
            } else {
                // Clear the selection and move the caret forward by one character
                if (selectionLength > 0) {
                    selectionStart += selectionLength - 1;
                }
                if (selectionStart < document.getCharacterCount() - 1) {
                    selectionStart++;
                }
                textPane.setSelection(selectionStart, 0);
                scrollCharacterToVisible(selectionStart);
                caretX = caret.x;
            }
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.UP) {
            int offset = getNextInsertionPoint(caretX, selectionStart, TextPane.ScrollDirection.UP);
            if (offset == -1) {
                offset = 0;
            }
            if (shiftPressed) {
                selectionLength = selectionStart + selectionLength - offset;
            } else {
                selectionLength = 0;
            }
            textPane.setSelection(offset, selectionLength);
            scrollCharacterToVisible(offset);
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.DOWN) {
            if (shiftPressed) {
                int from;
                int x;
                if (selectionLength == 0) {
                    // Get next insertion point from leading selection character
                    from = selectionStart;
                    x = caretX;
                } else {
                    // Get next insertion point from right edge of trailing
                    // selection character
                    from = selectionStart + selectionLength - 1;
                    Bounds trailingSelectionBounds = getCharacterBounds(from);
                    x = trailingSelectionBounds.x + trailingSelectionBounds.width;
                }
                int offset = getNextInsertionPoint(x, from, TextPane.ScrollDirection.DOWN);
                if (offset == -1) {
                    offset = documentView.getCharacterCount() - 1;
                } else {
                    // final terminator character, increment the selection
                    if (document.getCharacterAt(offset) == '\n' && offset < documentView.getCharacterCount() - 1) {
                        offset++;
                    }
                }
                textPane.setSelection(selectionStart, offset - selectionStart);
                scrollCharacterToVisible(offset);
            } else {
                int from;
                if (selectionLength == 0) {
                    // Get next insertion point from leading selection character
                    from = selectionStart;
                } else {
                    // Get next insertion point from trailing selection character
                    from = selectionStart + selectionLength - 1;
                }
                int offset = getNextInsertionPoint(caretX, from, TextPane.ScrollDirection.DOWN);
                if (offset == -1) {
                    offset = documentView.getCharacterCount() - 1;
                }
                textPane.setSelection(offset, 0);
                scrollCharacterToVisible(offset);
            }
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.TAB && (acceptsTab != Keyboard.isPressed(Keyboard.Modifier.CTRL)) && isEditable) {
            if (textPane.getExpandTabs()) {
                int linePos = selectionStart - getRowOffset(document, selectionStart);
                StringBuilder tabBuilder = new StringBuilder(tabWidth);
                for (int i = 0; i < tabWidth - (linePos % tabWidth); i++) {
                    tabBuilder.append(" ");
                }
                textPane.insert(tabBuilder.toString());
            } else {
                textPane.insert("\t");
            }
            showCaret(true);
            consumed = true;
        } else if (keyCode == Keyboard.KeyCode.INSERT) {
            if (shiftPressed && isEditable) {
                textPane.paste();
                consumed = true;
            }
        } else if (commandPressed) {
            if (keyCode == Keyboard.KeyCode.A) {
                textPane.setSelection(0, document.getCharacterCount());
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.X && isEditable) {
                textPane.cut();
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.C) {
                textPane.copy();
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.V && isEditable) {
                textPane.paste();
                consumed = true;
            } else if (keyCode == Keyboard.KeyCode.Z && isEditable) {
                if (shiftPressed) {
                    textPane.redo();
                } else {
                    textPane.undo();
                }
                consumed = true;
            }
        } else {
            consumed = super.keyPressed(component, keyCode, keyLocation);
        }
    }
    return consumed;
}
Also used : TextPane(org.apache.pivot.wtk.TextPane) Bounds(org.apache.pivot.wtk.Bounds) Document(org.apache.pivot.wtk.text.Document)

Example 54 with Bounds

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

the class TextPaneSkinElementView method getCharacterBounds.

@Override
public Bounds getCharacterBounds(int offset) {
    Bounds characterBounds = null;
    for (int i = 0, n = nodeViews.getLength(); i < n; i++) {
        TextPaneSkinNodeView nodeView = nodeViews.get(i);
        int nodeViewOffset = nodeView.getOffset();
        int characterCount = nodeView.getCharacterCount();
        if (offset >= nodeViewOffset && offset < nodeViewOffset + characterCount) {
            characterBounds = nodeView.getCharacterBounds(offset - nodeViewOffset);
            if (characterBounds != null) {
                characterBounds = characterBounds.translate(nodeView.getX(), nodeView.getY());
            }
            break;
        }
    }
    if (characterBounds != null) {
        characterBounds = characterBounds.intersect(0, 0, getWidth(), getHeight());
    }
    return characterBounds;
}
Also used : Bounds(org.apache.pivot.wtk.Bounds)

Example 55 with Bounds

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

the class TextPaneSkinElementView method paintChild.

protected final void paintChild(Graphics2D graphics, Bounds paintBounds, TextPaneSkinNodeView nodeView) {
    Bounds nodeViewBounds = nodeView.getBounds();
    // Only paint node views that intersect the current clip rectangle
    if (nodeViewBounds.intersects(paintBounds)) {
        // Create a copy of the current graphics context and
        // translate to the node view's coordinate system
        Graphics2D nodeViewGraphics = (Graphics2D) graphics.create();
        Color styledBackgroundColor = getStyledBackgroundColor();
        if (styledBackgroundColor != null) {
            // don't paint over the selection background
            Area selection = getTextPaneSkin().getSelection();
            if (selection != null) {
                Area fillArea = new Area(new Rectangle(nodeViewBounds.x, nodeViewBounds.y, nodeViewBounds.width, nodeViewBounds.height));
                selection = selection.createTransformedArea(AffineTransform.getTranslateInstance(-skinX, -skinY));
                fillArea.subtract(selection);
                nodeViewGraphics.setColor(styledBackgroundColor);
                nodeViewGraphics.fill(fillArea);
            } else {
                nodeViewGraphics.setColor(styledBackgroundColor);
                nodeViewGraphics.fillRect(nodeViewBounds.x, nodeViewBounds.y, nodeViewBounds.width, nodeViewBounds.height);
            }
        }
        nodeViewGraphics.translate(nodeViewBounds.x, nodeViewBounds.y);
        // NOTE We don't clip here because views should generally
        // not overlap and clipping would impose an unnecessary
        // performance penalty
        // Paint the node view
        nodeView.paint(nodeViewGraphics);
        // Dispose of the node views's graphics
        nodeViewGraphics.dispose();
    }
}
Also used : Area(java.awt.geom.Area) Bounds(org.apache.pivot.wtk.Bounds) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Graphics2D(java.awt.Graphics2D)

Aggregations

Bounds (org.apache.pivot.wtk.Bounds)77 Point (org.apache.pivot.wtk.Point)21 GradientPaint (java.awt.GradientPaint)15 Color (java.awt.Color)10 Graphics2D (java.awt.Graphics2D)9 Rectangle (java.awt.Rectangle)7 TableView (org.apache.pivot.wtk.TableView)7 BasicStroke (java.awt.BasicStroke)6 GeneralPath (java.awt.geom.GeneralPath)5 Button (org.apache.pivot.wtk.Button)5 Component (org.apache.pivot.wtk.Component)5 Span (org.apache.pivot.wtk.Span)5 TextArea (org.apache.pivot.wtk.TextArea)5 TextPane (org.apache.pivot.wtk.TextPane)5 FontRenderContext (java.awt.font.FontRenderContext)4 LineMetrics (java.awt.font.LineMetrics)4 Area (java.awt.geom.Area)4 RoundRectangle2D (java.awt.geom.RoundRectangle2D)4 ArrayList (org.apache.pivot.collections.ArrayList)4 ScrollPane (org.apache.pivot.wtk.ScrollPane)4