Search in sources :

Example 1 with Span

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

the class ListViews method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    selectionLabel = (Label) namespace.get("selectionLabel");
    listView = (ListView) namespace.get("listView");
    listView.getListViewSelectionListeners().add(new ListViewSelectionListener() {

        @Override
        public void selectedRangeAdded(ListView listViewArgument, int rangeStart, int rangeEnd) {
            updateSelection(listViewArgument);
        }

        @Override
        public void selectedRangeRemoved(ListView listViewArgument, int rangeStart, int rangeEnd) {
            updateSelection(listViewArgument);
        }

        @Override
        public void selectedRangesChanged(ListView listViewArgument, Sequence<Span> previousSelectedRanges) {
            if (previousSelectedRanges != null && previousSelectedRanges != listViewArgument.getSelectedRanges()) {
                updateSelection(listViewArgument);
            }
        }

        @Override
        public void selectedItemChanged(ListView listViewArgument, Object previousSelectedItem) {
        // No-op
        }

        private void updateSelection(ListView listViewArgument) {
            // TODO: in future use StringBuffer instead ...
            String selectionText = "";
            Sequence<Span> selectedRanges = listViewArgument.getSelectedRanges();
            for (int i = 0, n = selectedRanges.getLength(); i < n; i++) {
                Span selectedRange = selectedRanges.get(i);
                for (int j = selectedRange.start; j <= selectedRange.end; j++) {
                    if (selectionText.length() > 0) {
                        selectionText += ", ";
                    }
                    Object item = listViewArgument.getListData().get(j);
                    String text;
                    if (item instanceof ListItem) {
                        // item is a listItem
                        // (for example because
                        // it has an image)
                        text = ((ListItem) item).getText();
                    } else {
                        // item is a standard item for listData
                        text = item.toString();
                    }
                    selectionText += text;
                }
            }
            selectionLabel.setText(selectionText);
        }
    });
}
Also used : ListView(org.apache.pivot.wtk.ListView) ListViewSelectionListener(org.apache.pivot.wtk.ListViewSelectionListener) Sequence(org.apache.pivot.collections.Sequence) ListItem(org.apache.pivot.wtk.content.ListItem) Span(org.apache.pivot.wtk.Span)

Example 2 with Span

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

the class TextAreaSkinParagraphView method paint.

public void paint(Graphics2D graphics) {
    TextArea textArea = (TextArea) textAreaSkin.getComponent();
    int selectionStart = textArea.getSelectionStart();
    int selectionLength = textArea.getSelectionLength();
    Span selectionRange = new Span(selectionStart, selectionStart + selectionLength - 1);
    int paragraphOffset = paragraph.getOffset();
    Span characterRange = new Span(paragraphOffset, paragraphOffset + paragraph.getCharacters().length() - 1);
    if (selectionLength > 0 && characterRange.intersects(selectionRange)) {
        boolean focused = textArea.isFocused();
        boolean editable = textArea.isEditable();
        // Determine the selected and unselected areas
        Area selection = textAreaSkin.getSelection();
        Area selectedArea = selection.createTransformedArea(AffineTransform.getTranslateInstance(-x, -y));
        Area unselectedArea = new Area();
        unselectedArea.add(new Area(new Rectangle2D.Float(0, 0, width, height)));
        unselectedArea.subtract(new Area(selectedArea));
        // Paint the unselected text
        Graphics2D unselectedGraphics = (Graphics2D) graphics.create();
        unselectedGraphics.clip(unselectedArea);
        paint(unselectedGraphics, focused, editable, false);
        unselectedGraphics.dispose();
        // Paint the selected text
        Graphics2D selectedGraphics = (Graphics2D) graphics.create();
        selectedGraphics.clip(selectedArea);
        paint(selectedGraphics, focused, editable, true);
        selectedGraphics.dispose();
    } else {
        paint(graphics, textArea.isFocused(), textArea.isEditable(), false);
    }
}
Also used : TextArea(org.apache.pivot.wtk.TextArea) Area(java.awt.geom.Area) TextArea(org.apache.pivot.wtk.TextArea) Span(org.apache.pivot.wtk.Span) Graphics2D(java.awt.Graphics2D)

Example 3 with Span

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

Example 4 with Span

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

the class TextPaneSkinTextNodeView method childLayout.

@Override
protected void childLayout(int breakWidth) {
    TextNode textNode = (TextNode) getNode();
    textLayout = null;
    Font effectiveFont = getEffectiveFont();
    FontRenderContext fontRenderContext = Platform.getFontRenderContext();
    TextPane textPane = (TextPane) getTextPaneSkin().getComponent();
    AttributedStringCharacterIterator composedText = textPane.getComposedText();
    Element parent = textNode.getParent();
    // The calculations below really need to know if we're at the end of the paragraph
    // when composing text, so make sure we ignore intervening span or other nodes, but
    // also update the offset relative to the paragraph in the process.
    int relStart = start;
    if (parent != null && !(parent instanceof Paragraph)) {
        relStart += parent.getOffset();
        parent = parent.getParent();
    }
    int parentCount = parent == null ? 0 : parent.getCharacterCount();
    int selectionStart = textPane.getSelectionStart();
    int selectionLength = textPane.getSelectionLength();
    int documentOffset = textNode.getDocumentOffset();
    int charCount = textNode.getCharacterCount();
    boolean composedIntersects = false;
    if (composedText != null) {
        int composedTextBegin = composedText.getBeginIndex();
        int composedTextEnd = composedText.getEndIndex();
        int composedTextLength = composedTextEnd - composedTextBegin;
        /* exclusive - inclusive, so no +1 needed */
        // If this text node is at the end of a paragraph, increase the span by 1 for the newline
        Span ourSpan;
        if (parent instanceof Paragraph && charCount + relStart == parentCount - 1) {
            ourSpan = new Span(documentOffset + start, documentOffset + charCount);
        } else {
            ourSpan = new Span(documentOffset + start, documentOffset + charCount - 1);
        }
        // The "composed span" just encompasses the start position, because this is "phantom" text, so it exists between any two other "real" text positions.
        Span composedSpan = new Span(selectionStart + composedTextBegin);
        composedIntersects = composedSpan.intersects(ourSpan);
    }
    if (charCount == 0 && !composedIntersects) {
        Dimensions charSize = GraphicsUtilities.getAverageCharacterSize(effectiveFont);
        setSize(0, charSize.height);
        length = 0;
        next = null;
    } else {
        AttributedCharacterIterator text = null;
        boolean underlined = getEffectiveUnderline();
        boolean struckthrough = getEffectiveStrikethrough();
        if (composedText != null && composedIntersects) {
            int composedPos = selectionStart - documentOffset;
            if (composedPos == 0) {
                if (charCount - start == 0) {
                    text = composedText;
                } else {
                    AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
                    // Note: only apply the underline and strikethrough to our text, not the composed text
                    fullText.addUnderlineAttribute(underlined);
                    fullText.addStrikethroughAttribute(struckthrough);
                    text = new CompositeIterator(composedText, fullText);
                }
            } else if (composedPos == charCount) {
                // Composed text is at the end
                AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
                // Note: only apply the underline and strikethrough to our text, not the composed text
                fullText.addUnderlineAttribute(underlined);
                fullText.addStrikethroughAttribute(struckthrough);
                text = new CompositeIterator(fullText, composedText);
            } else {
                // Composed text is somewhere in the middle
                AttributedStringCharacterIterator leadingText = getCharIterator(textNode, start, composedPos, effectiveFont);
                leadingText.addUnderlineAttribute(underlined);
                leadingText.addStrikethroughAttribute(struckthrough);
                AttributedStringCharacterIterator trailingText = getCharIterator(textNode, composedPos, charCount, effectiveFont);
                trailingText.addUnderlineAttribute(underlined);
                trailingText.addStrikethroughAttribute(struckthrough);
                text = new CompositeIterator(leadingText, composedText, trailingText);
            }
        } else {
            AttributedStringCharacterIterator fullText = getCharIterator(textNode, start, charCount, effectiveFont);
            fullText.addUnderlineAttribute(underlined);
            fullText.addStrikethroughAttribute(struckthrough);
            text = fullText;
        }
        if (getTextPaneSkin().getWrapText()) {
            LineBreakMeasurer measurer = new LineBreakMeasurer(text, fontRenderContext);
            float wrappingWidth = (float) breakWidth;
            textLayout = measurer.nextLayout(wrappingWidth);
            length = textLayout.getCharacterCount();
            Dimensions size = getTextSize(textLayout);
            float advance = textLayout.getAdvance();
            setSize(size);
            if (start + measurer.getPosition() < textNode.getCharacterCount()) {
                next = new TextPaneSkinTextNodeView(getTextPaneSkin(), textNode, start + measurer.getPosition());
                next.setParent(getParent());
            } else {
                next = null;
            }
        } else {
            // Not wrapping the text, then the width is of the whole thing
            textLayout = new TextLayout(text, fontRenderContext);
            length = textLayout.getCharacterCount();
            Dimensions size = getTextSize(textLayout);
            float advance = textLayout.getAdvance();
            setSize(size);
            // set to null in case this node used to be broken across multiple,
            // but is no longer
            next = null;
        }
    }
}
Also used : CompositeIterator(org.apache.pivot.text.CompositeIterator) Element(org.apache.pivot.wtk.text.Element) LineBreakMeasurer(java.awt.font.LineBreakMeasurer) Dimensions(org.apache.pivot.wtk.Dimensions) TextNode(org.apache.pivot.wtk.text.TextNode) Span(org.apache.pivot.wtk.Span) TextSpan(org.apache.pivot.wtk.text.TextSpan) Font(java.awt.Font) AttributedStringCharacterIterator(org.apache.pivot.text.AttributedStringCharacterIterator) Paragraph(org.apache.pivot.wtk.text.Paragraph) AttributedCharacterIterator(java.text.AttributedCharacterIterator) TextLayout(java.awt.font.TextLayout) TextPane(org.apache.pivot.wtk.TextPane) FontRenderContext(java.awt.font.FontRenderContext)

Example 5 with Span

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

the class FixedColumnTableDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    primaryTableView = (TableView) namespace.get("primaryTableView");
    fixedTableView = (TableView) namespace.get("fixedTableView");
    // Keep selection state in sync
    primaryTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {

        @Override
        public void selectedRangeAdded(TableView tableView, int rangeStart, int rangeEnd) {
            if (!synchronizingSelection) {
                synchronizingSelection = true;
                fixedTableView.addSelectedRange(rangeStart, rangeEnd);
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRangeRemoved(TableView tableView, int rangeStart, int rangeEnd) {
            if (!synchronizingSelection) {
                synchronizingSelection = true;
                fixedTableView.removeSelectedRange(rangeStart, rangeEnd);
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
            if (previousSelectedRanges != null && !synchronizingSelection) {
                synchronizingSelection = true;
                fixedTableView.setSelectedRanges(tableView.getSelectedRanges());
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRowChanged(TableView tableView, Object previousSelectedRow) {
        // No-op
        }
    });
    fixedTableView.getTableViewSelectionListeners().add(new TableViewSelectionListener() {

        @Override
        public void selectedRangeAdded(TableView tableView, int rangeStart, int rangeEnd) {
            if (!synchronizingSelection) {
                synchronizingSelection = true;
                primaryTableView.addSelectedRange(rangeStart, rangeEnd);
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRangeRemoved(TableView tableView, int rangeStart, int rangeEnd) {
            if (!synchronizingSelection) {
                synchronizingSelection = true;
                primaryTableView.removeSelectedRange(rangeStart, rangeEnd);
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRangesChanged(TableView tableView, Sequence<Span> previousSelectedRanges) {
            if (previousSelectedRanges != null && !synchronizingSelection) {
                synchronizingSelection = true;
                primaryTableView.setSelectedRanges(tableView.getSelectedRanges());
                synchronizingSelection = false;
            }
        }

        @Override
        public void selectedRowChanged(TableView tableView, Object previousSelectedRow) {
        // No-op
        }
    });
    // Keep header state in sync
    primaryTableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableView) {
            if (!tableView.getSort().isEmpty()) {
                fixedTableView.clearSort();
            }
            @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
            tableData.setComparator(new TableViewRowComparator(tableView));
        }
    });
    fixedTableView.getTableViewSortListeners().add(new TableViewSortListener() {

        @Override
        public void sortChanged(TableView tableView) {
            if (!tableView.getSort().isEmpty()) {
                primaryTableView.clearSort();
            }
            @SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
            tableData.setComparator(new TableViewRowComparator(tableView));
        }
    });
}
Also used : TableViewRowComparator(org.apache.pivot.wtk.content.TableViewRowComparator) TableViewSortListener(org.apache.pivot.wtk.TableViewSortListener) List(org.apache.pivot.collections.List) TableViewSelectionListener(org.apache.pivot.wtk.TableViewSelectionListener) Span(org.apache.pivot.wtk.Span) TableView(org.apache.pivot.wtk.TableView)

Aggregations

Span (org.apache.pivot.wtk.Span)25 ArrayList (org.apache.pivot.collections.ArrayList)11 List (org.apache.pivot.collections.List)10 Button (org.apache.pivot.wtk.Button)7 Keyboard (org.apache.pivot.wtk.Keyboard)7 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)6 Component (org.apache.pivot.wtk.Component)6 ListView (org.apache.pivot.wtk.ListView)6 TableView (org.apache.pivot.wtk.TableView)6 Sequence (org.apache.pivot.collections.Sequence)5 Bounds (org.apache.pivot.wtk.Bounds)5 ComponentKeyListener (org.apache.pivot.wtk.ComponentKeyListener)5 IOException (java.io.IOException)4 PushButton (org.apache.pivot.wtk.PushButton)4 TableViewSelectionListener (org.apache.pivot.wtk.TableViewSelectionListener)4 TableViewSortListener (org.apache.pivot.wtk.TableViewSortListener)4 TextInput (org.apache.pivot.wtk.TextInput)4 TextSpan (org.apache.pivot.wtk.text.TextSpan)4 File (java.io.File)3 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3