Search in sources :

Example 11 with Dimensions

use of org.apache.pivot.wtk.Dimensions 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 12 with Dimensions

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

the class TextPaneSkinVerticalElementView method getPreferredSize.

@Override
public Dimensions getPreferredSize(int breakWidth) {
    int width = 0;
    int height = 0;
    for (TextPaneSkinNodeView nodeView : this) {
        Dimensions childDimensions = nodeView.getPreferredSize(breakWidth);
        width = Math.max(width, childDimensions.width);
        height += childDimensions.height;
    }
    return new Dimensions(width, height);
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions)

Example 13 with Dimensions

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

the class BoxPaneSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    BoxPane boxPane = (BoxPane) getComponent();
    int preferredWidth = 0;
    int preferredHeight = 0;
    switch(boxPane.getOrientation()) {
        case HORIZONTAL:
            {
                // Preferred width is the sum of the preferred widths of all
                // components
                int j = 0;
                for (int i = 0, n = boxPane.getLength(); i < n; i++) {
                    Component component = boxPane.get(i);
                    if (component.isVisible()) {
                        Dimensions preferredSize = component.getPreferredSize();
                        preferredWidth += preferredSize.width;
                        preferredHeight = Math.max(preferredSize.height, preferredHeight);
                        j++;
                    }
                }
                // Include spacing
                if (j > 1) {
                    preferredWidth += spacing * (j - 1);
                }
                break;
            }
        case VERTICAL:
            {
                // Preferred height is the sum of the preferred heights of all components
                int j = 0;
                for (int i = 0, n = boxPane.getLength(); i < n; i++) {
                    Component component = boxPane.get(i);
                    if (component.isVisible()) {
                        Dimensions preferredSize = component.getPreferredSize();
                        preferredWidth = Math.max(preferredSize.width, preferredWidth);
                        preferredHeight += preferredSize.height;
                        j++;
                    }
                }
                // Include spacing
                if (j > 1) {
                    preferredHeight += spacing * (j - 1);
                }
                break;
            }
        default:
            {
                break;
            }
    }
    // Include padding
    preferredWidth += padding.getWidth();
    preferredHeight += padding.getHeight();
    return new Dimensions(preferredWidth, preferredHeight);
}
Also used : BoxPane(org.apache.pivot.wtk.BoxPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 14 with Dimensions

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

the class BoxPaneSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    BoxPane boxPane = (BoxPane) getComponent();
    int baseline = -1;
    int contentHeight = 0;
    switch(boxPane.getOrientation()) {
        case HORIZONTAL:
            {
                if (fill) {
                    int clientHeight = Math.max(height - padding.getHeight(), 0);
                    for (Component component : boxPane) {
                        if (component.isVisible()) {
                            int componentWidth = component.getPreferredWidth(clientHeight);
                            baseline = Math.max(baseline, component.getBaseline(componentWidth, clientHeight));
                        }
                    }
                } else {
                    contentHeight = 0;
                    for (Component component : boxPane) {
                        if (component.isVisible()) {
                            contentHeight = Math.max(contentHeight, component.getPreferredHeight());
                        }
                    }
                    for (Component component : boxPane) {
                        if (component.isVisible()) {
                            Dimensions size = component.getPreferredSize();
                            int componentBaseline = component.getBaseline(size.width, size.height);
                            if (componentBaseline != -1) {
                                switch(verticalAlignment) {
                                    case CENTER:
                                        {
                                            componentBaseline += (contentHeight - size.height) / 2;
                                            break;
                                        }
                                    case BOTTOM:
                                        {
                                            componentBaseline += contentHeight - size.height;
                                            break;
                                        }
                                    case TOP:
                                        {
                                            break;
                                        }
                                    default:
                                        {
                                            break;
                                        }
                                }
                            }
                            baseline = Math.max(baseline, componentBaseline);
                        }
                    }
                }
                break;
            }
        case VERTICAL:
            {
                int clientWidth = Math.max(width - padding.getWidth(), 0);
                for (Component component : boxPane) {
                    if (component.isVisible()) {
                        Dimensions size;
                        if (fill) {
                            size = new Dimensions(clientWidth, component.getPreferredHeight(clientWidth));
                        } else {
                            size = component.getPreferredSize();
                        }
                        if (baseline == -1) {
                            baseline = component.getBaseline(size.width, size.height);
                            if (baseline != -1) {
                                baseline += contentHeight;
                            }
                        }
                        contentHeight += size.height + spacing;
                    }
                }
                contentHeight -= spacing;
                break;
            }
        default:
            {
                break;
            }
    }
    if (baseline != -1) {
        if (fill) {
            baseline += padding.top;
        } else {
            switch(verticalAlignment) {
                case TOP:
                    {
                        baseline += padding.top;
                        break;
                    }
                case CENTER:
                    {
                        baseline += (height - contentHeight) / 2;
                        break;
                    }
                case BOTTOM:
                    {
                        baseline += height - (contentHeight + padding.bottom);
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
    return baseline;
}
Also used : BoxPane(org.apache.pivot.wtk.BoxPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 15 with Dimensions

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

the class CardPaneSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    int preferredWidth = 0;
    int preferredHeight = 0;
    CardPane cardPane = (CardPane) getComponent();
    if (sizeToSelection) {
        if (selectionChangeTransition == null) {
            Component selectedCard = cardPane.getSelectedCard();
            if (selectedCard != null) {
                Dimensions cardSize = selectedCard.getPreferredSize();
                preferredWidth = cardSize.width;
                preferredHeight = cardSize.height;
            }
        } else {
            float percentComplete = selectionChangeTransition.getPercentComplete();
            int previousWidth;
            int previousHeight;
            if (selectionChangeTransition.fromCard == null) {
                previousWidth = 0;
                previousHeight = 0;
            } else {
                Dimensions fromSize = selectionChangeTransition.fromCard.getPreferredSize();
                previousWidth = fromSize.width;
                previousHeight = fromSize.height;
            }
            int width;
            int height;
            if (selectionChangeTransition.toCard == null) {
                width = 0;
                height = 0;
            } else {
                Dimensions toSize = selectionChangeTransition.toCard.getPreferredSize();
                width = toSize.width;
                height = toSize.height;
            }
            preferredWidth = previousWidth + (int) ((width - previousWidth) * percentComplete);
            preferredHeight = previousHeight + (int) ((height - previousHeight) * percentComplete);
        }
    } else {
        for (Component card : cardPane) {
            Dimensions cardSize = card.getPreferredSize();
            preferredWidth = Math.max(cardSize.width, preferredWidth);
            preferredHeight = Math.max(cardSize.height, preferredHeight);
        }
    }
    preferredWidth += padding.getWidth();
    preferredHeight += padding.getHeight();
    return new Dimensions(preferredWidth, preferredHeight);
}
Also used : CardPane(org.apache.pivot.wtk.CardPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Aggregations

Dimensions (org.apache.pivot.wtk.Dimensions)76 Component (org.apache.pivot.wtk.Component)40 GradientPaint (java.awt.GradientPaint)21 Point (org.apache.pivot.wtk.Point)16 FontRenderContext (java.awt.font.FontRenderContext)9 Button (org.apache.pivot.wtk.Button)9 Paint (java.awt.Paint)7 Rectangle2D (java.awt.geom.Rectangle2D)6 LineMetrics (java.awt.font.LineMetrics)5 BoxPane (org.apache.pivot.wtk.BoxPane)5 FlowPane (org.apache.pivot.wtk.FlowPane)5 Label (org.apache.pivot.wtk.Label)5 ScrollPane (org.apache.pivot.wtk.ScrollPane)4 Separator (org.apache.pivot.wtk.Separator)4 Form (org.apache.pivot.wtk.Form)3 ImageView (org.apache.pivot.wtk.ImageView)3 Image (org.apache.pivot.wtk.media.Image)3 Color (java.awt.Color)2 Font (java.awt.Font)2 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)2