Search in sources :

Example 41 with Dimensions

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

the class FlowPaneSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    FlowPane flowPane = (FlowPane) getComponent();
    int preferredWidth = 0;
    int ascent = 0;
    int descent = 0;
    int j = 0;
    for (int i = 0, n = flowPane.getLength(); i < n; i++) {
        Component component = flowPane.get(i);
        if (component.isVisible()) {
            Dimensions size = component.getPreferredSize();
            preferredWidth += size.width;
            if (alignToBaseline) {
                int baseline = component.getBaseline(size.width, size.height);
                ascent = Math.max(ascent, baseline);
                descent = Math.max(descent, size.height - baseline);
            } else {
                ascent = Math.max(ascent, size.height);
            }
            j++;
        }
    }
    // Include horizontal spacing
    if (j > 1) {
        preferredWidth += horizontalSpacing * (j - 1);
    }
    // Include padding
    preferredWidth += padding.getWidth();
    return new Dimensions(preferredWidth, ascent + descent + padding.getHeight());
}
Also used : FlowPane(org.apache.pivot.wtk.FlowPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 42 with Dimensions

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

the class FlowPaneSkin method layout.

@Override
public void layout() {
    FlowPane flowPane = (FlowPane) getComponent();
    int width = getWidth();
    int contentWidth = Math.max(width - padding.getWidth(), 0);
    // Break the components into multiple rows
    ArrayList<ArrayList<Component>> rows = new ArrayList<>();
    ArrayList<Component> row = new ArrayList<>();
    int rowWidth = 0;
    for (int i = 0, n = flowPane.getLength(); i < n; i++) {
        Component component = flowPane.get(i);
        if (component.isVisible()) {
            Dimensions componentSize = component.getPreferredSize();
            component.setSize(componentSize);
            if (rowWidth + componentSize.width > contentWidth && rowWidth > 0) {
                // The component is too big to fit in the remaining space,
                // and it is not the only component in this row
                rows.add(row);
                row = new ArrayList<>();
                rowWidth = 0;
            }
            // Add the component to the row
            row.add(component);
            rowWidth += componentSize.width + horizontalSpacing;
        }
    }
    // Add the last row
    if (row.getLength() > 0) {
        rows.add(row);
    }
    // Lay out the rows
    int rowY = padding.top;
    for (int i = 0, n = rows.getLength(); i < n; i++) {
        row = rows.get(i);
        // Determine the row dimensions
        rowWidth = 0;
        int rowHeight = 0;
        int baseline = -1;
        for (Component component : row) {
            rowWidth += component.getWidth();
            rowHeight = Math.max(rowHeight, component.getHeight());
            baseline = Math.max(baseline, component.getBaseline(component.getWidth(), component.getHeight()));
        }
        rowWidth += horizontalSpacing * (row.getLength() - 1);
        int x = 0;
        switch(alignment) {
            case LEFT:
                {
                    x = padding.left;
                    break;
                }
            case CENTER:
                {
                    x = (width - rowWidth) / 2;
                    break;
                }
            case RIGHT:
                {
                    x = width - rowWidth - padding.right;
                    break;
                }
            default:
                {
                    break;
                }
        }
        for (Component component : row) {
            int y;
            int componentBaseline = component.getBaseline(component.getWidth(), component.getHeight());
            if (alignToBaseline && baseline != -1 && componentBaseline != -1) {
                // Align to baseline
                y = baseline - componentBaseline;
            } else {
                // Align to bottom
                y = rowHeight - component.getHeight();
            }
            component.setLocation(x, y + rowY);
            x += (component.getWidth() + horizontalSpacing);
        }
        rowY += (rowHeight + verticalSpacing);
    }
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) FlowPane(org.apache.pivot.wtk.FlowPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 43 with Dimensions

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

the class FlowPaneSkin method getPreferredHeight.

@Override
public int getPreferredHeight(int width) {
    FlowPane flowPane = (FlowPane) getComponent();
    int preferredHeight;
    if (width == -1) {
        if (alignToBaseline) {
            // Delegate to preferred size calculations
            Dimensions preferredSize = getPreferredSize();
            preferredHeight = preferredSize.height;
        } else {
            // Preferred height is the maximum preferred height of all
            // components
            preferredHeight = 0;
            for (int i = 0, n = flowPane.getLength(); i < n; i++) {
                Component component = flowPane.get(i);
                if (component.isVisible()) {
                    preferredHeight = Math.max(preferredHeight, component.getPreferredHeight());
                }
            }
        }
    } else {
        // Break the components into multiple rows
        preferredHeight = 0;
        int contentWidth = Math.max(width - padding.getWidth(), 0);
        int rowCount = 0;
        int rowWidth = 0;
        int rowAscent = 0;
        int rowDescent = 0;
        for (int i = 0, n = flowPane.getLength(); i < n; i++) {
            Component component = flowPane.get(i);
            if (component.isVisible()) {
                Dimensions size = component.getPreferredSize();
                if (rowWidth + size.width > contentWidth && rowWidth > 0) {
                    // The component is too big to fit in the remaining
                    // space,
                    // and it is not the only component in this row; wrap
                    preferredHeight += rowAscent + rowDescent;
                    rowCount++;
                    rowWidth = 0;
                    rowAscent = 0;
                    rowDescent = 0;
                }
                rowWidth += size.width + horizontalSpacing;
                if (alignToBaseline) {
                    int baseline = component.getBaseline(size.width, size.height);
                    rowAscent = Math.max(rowAscent, baseline);
                    rowDescent = Math.max(rowDescent, size.height - baseline);
                } else {
                    rowAscent = Math.max(rowAscent, size.height);
                }
            }
        }
        // Add the last row
        int lastRowHeight = rowAscent + rowDescent;
        if (lastRowHeight > 0) {
            preferredHeight += lastRowHeight;
            rowCount++;
        }
        // Include spacing
        if (rowCount > 0) {
            preferredHeight += verticalSpacing * (rowCount - 1);
        }
    }
    // Include top and bottom padding values
    preferredHeight += padding.getHeight();
    return preferredHeight;
}
Also used : FlowPane(org.apache.pivot.wtk.FlowPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 44 with Dimensions

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

the class BoxPaneSkin method layout.

@Override
public void layout() {
    BoxPane boxPane = (BoxPane) getComponent();
    int n = boxPane.getLength();
    int width = getWidth();
    int height = getHeight();
    Orientation orientation = boxPane.getOrientation();
    if (orientation == Orientation.HORIZONTAL) {
        int preferredWidth = getPreferredWidth(fill ? height : -1);
        // Determine the starting x-coordinate
        int x = 0;
        switch(horizontalAlignment) {
            case CENTER:
                {
                    x = (width - preferredWidth) / 2;
                    break;
                }
            case RIGHT:
                {
                    x = width - preferredWidth;
                    break;
                }
            case LEFT:
                {
                    break;
                }
            default:
                {
                    break;
                }
        }
        x += padding.left;
        // Lay out the components
        for (int i = 0; i < n; i++) {
            Component component = boxPane.get(i);
            if (component.isVisible()) {
                int componentWidth = 0;
                int componentHeight = 0;
                int y = 0;
                if (fill) {
                    componentHeight = Math.max(height - padding.getHeight(), 0);
                    componentWidth = component.getPreferredWidth(componentHeight);
                } else {
                    Dimensions preferredComponentSize = component.getPreferredSize();
                    componentWidth = preferredComponentSize.width;
                    componentHeight = preferredComponentSize.height;
                }
                switch(verticalAlignment) {
                    case TOP:
                        {
                            y = padding.top;
                            break;
                        }
                    case CENTER:
                        {
                            y = (height - componentHeight) / 2;
                            break;
                        }
                    case BOTTOM:
                        {
                            y = height - padding.bottom - componentHeight;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                // Set the component's size and position
                component.setSize(componentWidth, componentHeight);
                component.setLocation(x, y);
                // Increment the x-coordinate
                x += componentWidth + spacing;
            }
        }
    } else {
        int preferredHeight = getPreferredHeight(fill ? width : -1);
        // Determine the starting y-coordinate
        int y = 0;
        switch(verticalAlignment) {
            case CENTER:
                {
                    y = (height - preferredHeight) / 2;
                    break;
                }
            case BOTTOM:
                {
                    y = height - preferredHeight;
                    break;
                }
            case TOP:
                break;
            default:
                {
                    break;
                }
        }
        y += padding.top;
        // Lay out the components
        for (int i = 0; i < n; i++) {
            Component component = boxPane.get(i);
            if (component.isVisible()) {
                int componentWidth = 0;
                int componentHeight = 0;
                int x = 0;
                if (fill) {
                    componentWidth = Math.max(width - padding.getWidth(), 0);
                    componentHeight = component.getPreferredHeight(componentWidth);
                } else {
                    Dimensions preferredComponentSize = component.getPreferredSize();
                    componentWidth = preferredComponentSize.width;
                    componentHeight = preferredComponentSize.height;
                }
                switch(horizontalAlignment) {
                    case LEFT:
                        {
                            x = padding.left;
                            break;
                        }
                    case CENTER:
                        {
                            x = (width - componentWidth) / 2;
                            break;
                        }
                    case RIGHT:
                        {
                            x = width - padding.right - componentWidth;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                // Set the component's size and position
                component.setSize(componentWidth, componentHeight);
                component.setLocation(x, y);
                // Increment the y-coordinate
                y += componentHeight + spacing;
            }
        }
    }
}
Also used : BoxPane(org.apache.pivot.wtk.BoxPane) Dimensions(org.apache.pivot.wtk.Dimensions) Orientation(org.apache.pivot.wtk.Orientation) Component(org.apache.pivot.wtk.Component)

Example 45 with Dimensions

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

the class ScrollPaneSkin method layoutHelper.

/**
 * Layout helper method that assumes that the <tt>FILL_TO_CAPACITY</tt>
 * scroll policy doesn't exist.
 *
 * @param horizontalPolicy The assumed horizontal scroll policy; musn't be
 * <tt>FILL_TO_CAPACITY</tt>
 * @param verticalPolicy The assumed vertical scroll policy; musn't be
 * <tt>FILL_TO_CAPACITY</tt>
 */
private void layoutHelper(ScrollBarPolicy horizontalPolicy, ScrollBarPolicy verticalPolicy) {
    ScrollPane scrollPane = (ScrollPane) getComponent();
    int width = getWidth();
    int height = getHeight();
    boolean constrainWidth = (horizontalPolicy == ScrollBarPolicy.FILL);
    boolean constrainHeight = (verticalPolicy == ScrollBarPolicy.FILL);
    Component view = scrollPane.getView();
    Component columnHeader = scrollPane.getColumnHeader();
    Component rowHeader = scrollPane.getRowHeader();
    Component corner = scrollPane.getCorner();
    int rowHeaderWidth = 0;
    if (rowHeader != null) {
        rowHeaderWidth = rowHeader.getPreferredWidth(-1);
    }
    int columnHeaderHeight = 0;
    if (columnHeader != null) {
        columnHeaderHeight = columnHeader.getPreferredHeight(-1);
    }
    int previousViewWidth, viewWidth = 0;
    int previousViewHeight, viewHeight = 0;
    int previousHorizontalScrollBarHeight, horizontalScrollBarHeight = cachedHorizontalScrollBarHeight;
    int previousVerticalScrollBarWidth, verticalScrollBarWidth = cachedVerticalScrollBarWidth;
    int i = 0;
    do {
        previousViewWidth = viewWidth;
        previousViewHeight = viewHeight;
        previousHorizontalScrollBarHeight = horizontalScrollBarHeight;
        previousVerticalScrollBarWidth = verticalScrollBarWidth;
        if (view != null) {
            if (constrainWidth && constrainHeight) {
                viewWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
                viewHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
            } else if (constrainWidth) {
                viewWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
                viewHeight = view.getPreferredHeight(viewWidth);
            } else if (constrainHeight) {
                viewHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
                viewWidth = view.getPreferredWidth(viewHeight);
            } else {
                Dimensions viewPreferredSize = view.getPreferredSize();
                viewWidth = viewPreferredSize.width;
                viewHeight = viewPreferredSize.height;
            }
        }
        if (horizontalPolicy == ScrollBarPolicy.ALWAYS || (horizontalPolicy == ScrollBarPolicy.AUTO && viewWidth > width - rowHeaderWidth - verticalScrollBarWidth)) {
            horizontalScrollBarHeight = horizontalScrollBar.getPreferredHeight(-1);
        } else {
            horizontalScrollBarHeight = 0;
        }
        if (verticalPolicy == ScrollBarPolicy.ALWAYS || (verticalPolicy == ScrollBarPolicy.AUTO && viewHeight > height - columnHeaderHeight - horizontalScrollBarHeight)) {
            verticalScrollBarWidth = verticalScrollBar.getPreferredWidth(-1);
        } else {
            verticalScrollBarWidth = 0;
        }
        if (++i > 4) {
            // Infinite loop protection
            System.err.println("Breaking out of potential infinite loop");
            break;
        }
    } while (viewWidth != previousViewWidth || viewHeight != previousViewHeight || horizontalScrollBarHeight != previousHorizontalScrollBarHeight || verticalScrollBarWidth != previousVerticalScrollBarWidth);
    int scrollTop = scrollPane.getScrollTop();
    int scrollLeft = scrollPane.getScrollLeft();
    if (view != null) {
        view.setSize(viewWidth, viewHeight);
        view.setLocation(rowHeaderWidth - scrollLeft, columnHeaderHeight - scrollTop);
    }
    if (columnHeader != null) {
        columnHeader.setSize(viewWidth, columnHeaderHeight);
        columnHeader.setLocation(rowHeaderWidth - scrollLeft, 0);
    }
    if (rowHeader != null) {
        rowHeader.setSize(rowHeaderWidth, viewHeight);
        rowHeader.setLocation(0, columnHeaderHeight - scrollTop);
    }
    if (horizontalScrollBarHeight > 0) {
        horizontalScrollBar.setVisible(true);
        int horizontalScrollBarWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
        horizontalScrollBar.setSize(horizontalScrollBarWidth, horizontalScrollBarHeight);
        horizontalScrollBar.setLocation(rowHeaderWidth, height - horizontalScrollBarHeight);
    } else {
        horizontalScrollBar.setVisible(false);
    }
    if (verticalScrollBarWidth > 0) {
        verticalScrollBar.setVisible(true);
        int verticalScrollBarHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
        verticalScrollBar.setSize(verticalScrollBarWidth, verticalScrollBarHeight);
        verticalScrollBar.setLocation(width - verticalScrollBarWidth, columnHeaderHeight);
    } else {
        verticalScrollBar.setVisible(false);
    }
    // Handle corner components
    Color backColor = getBackgroundColor();
    if (columnHeaderHeight > 0 && rowHeaderWidth > 0) {
        if (corner != null) {
            corner.setVisible(true);
            corner.setSize(rowHeaderWidth, columnHeaderHeight);
            corner.setLocation(0, 0);
            topLeftCorner.setVisible(false);
        } else {
            topLeftCorner.setVisible(true);
            topLeftCorner.setSize(rowHeaderWidth, columnHeaderHeight);
            topLeftCorner.setLocation(0, 0);
            topLeftCorner.getStyles().put(Style.backgroundColor, backColor);
        }
    } else {
        if (corner != null) {
            corner.setVisible(false);
        }
        topLeftCorner.setVisible(false);
    }
    if (rowHeaderWidth > 0 && horizontalScrollBarHeight > 0) {
        bottomLeftCorner.setVisible(true);
        bottomLeftCorner.setSize(rowHeaderWidth, horizontalScrollBarHeight);
        bottomLeftCorner.setLocation(0, height - horizontalScrollBarHeight);
        bottomLeftCorner.getStyles().put(Style.backgroundColor, backColor);
    } else {
        bottomLeftCorner.setVisible(false);
    }
    if (verticalScrollBarWidth > 0 && horizontalScrollBarHeight > 0) {
        bottomRightCorner.setVisible(true);
        bottomRightCorner.setSize(verticalScrollBarWidth, horizontalScrollBarHeight);
        bottomRightCorner.setLocation(width - verticalScrollBarWidth, height - horizontalScrollBarHeight);
        bottomRightCorner.getStyles().put(Style.backgroundColor, backColor);
    } else {
        bottomRightCorner.setVisible(false);
    }
    if (columnHeaderHeight > 0 && verticalScrollBarWidth > 0) {
        topRightCorner.setVisible(true);
        topRightCorner.setSize(verticalScrollBarWidth, columnHeaderHeight);
        topRightCorner.setLocation(width - verticalScrollBarWidth, 0);
        topRightCorner.getStyles().put(Style.backgroundColor, backColor);
    } else {
        topRightCorner.setVisible(false);
    }
    // Perform bounds checking on the scrollTop and scrollLeft values,
    // and adjust them as necessary. Make sure to do this after we've laid
    // everything out, since our ViewPortListener methods rely on valid
    // sizes from our components.
    int maxScrollTop = getMaxScrollTop();
    if (scrollTop > maxScrollTop) {
        scrollPane.setScrollTop(maxScrollTop);
    }
    int maxScrollLeft = getMaxScrollLeft();
    if (scrollLeft > maxScrollLeft) {
        scrollPane.setScrollLeft(maxScrollLeft);
    }
    // Adjust the structure of our scroll bars. Make sure to do this after
    // we adjust the scrollTop and scrollLeft values; otherwise we might
    // try to set structure values that are out of bounds.
    int viewportWidth = Math.max(width - rowHeaderWidth - verticalScrollBarWidth, 0);
    horizontalScrollBar.setScope(0, viewWidth, Math.min(viewWidth, viewportWidth));
    horizontalScrollBar.setBlockIncrement(Math.max(1, viewportWidth - horizontalReveal));
    int viewportHeight = Math.max(height - columnHeaderHeight - horizontalScrollBarHeight, 0);
    verticalScrollBar.setScope(0, viewHeight, Math.min(viewHeight, viewportHeight));
    verticalScrollBar.setBlockIncrement(Math.max(1, viewportHeight - verticalReveal));
}
Also used : ScrollPane(org.apache.pivot.wtk.ScrollPane) Color(java.awt.Color) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component) Paint(java.awt.Paint)

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