Search in sources :

Example 6 with FlowPane

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

the class FlowPaneSkin method getPreferredWidth.

@Override
public int getPreferredWidth(int height) {
    FlowPane flowPane = (FlowPane) getComponent();
    int preferredWidth = 0;
    // Preferred width is the sum of the preferred widths of all components
    // (height constraint is ignored)
    int j = 0;
    for (int i = 0, n = flowPane.getLength(); i < n; i++) {
        Component component = flowPane.get(i);
        if (component.isVisible()) {
            preferredWidth += component.getPreferredWidth();
            j++;
        }
    }
    // Include spacing
    if (j > 1) {
        preferredWidth += horizontalSpacing * (j - 1);
    }
    // Include left and right padding values
    preferredWidth += padding.getWidth();
    return preferredWidth;
}
Also used : FlowPane(org.apache.pivot.wtk.FlowPane) Component(org.apache.pivot.wtk.Component)

Example 7 with FlowPane

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

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

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

the class ComponentInspectorSkin method addInsetsControl.

private static Component addInsetsControl(final Dictionary<String, Object> dictionary, final String key, Form.Section section) {
    Insets insets = (Insets) dictionary.get(key);
    BoxPane boxPane = new BoxPane(Orientation.VERTICAL);
    section.add(boxPane);
    Form.setLabel(boxPane, key);
    FlowPane flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    TextInput textInput = new TextInput();
    textInput.setTextSize(4);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(insets.top));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Insets insetsLocal = (Insets) dictionary.get(key);
                try {
                    int top = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Insets(top, insetsLocal.left, insetsLocal.bottom, insetsLocal.right));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(insetsLocal.top));
                }
            }
        }
    });
    Label label = new Label("top");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(4);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(insets.left));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Insets insetsLocal = (Insets) dictionary.get(key);
                try {
                    int left = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Insets(insetsLocal.top, left, insetsLocal.bottom, insetsLocal.right));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(insetsLocal.left));
                }
            }
        }
    });
    label = new Label("left");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(4);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(insets.bottom));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Insets insetsLocal = (Insets) dictionary.get(key);
                try {
                    int bottom = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Insets(insetsLocal.top, insetsLocal.left, bottom, insetsLocal.right));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(insetsLocal.bottom));
                }
            }
        }
    });
    label = new Label("bottom");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(4);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(insets.right));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Insets insetsLocal = (Insets) dictionary.get(key);
                try {
                    int right = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Insets(insetsLocal.top, insetsLocal.left, insetsLocal.bottom, right));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(insetsLocal.right));
                }
            }
        }
    });
    label = new Label("right");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    return boxPane;
}
Also used : Insets(org.apache.pivot.wtk.Insets) IntValidator(org.apache.pivot.wtk.validation.IntValidator) BoxPane(org.apache.pivot.wtk.BoxPane) Label(org.apache.pivot.wtk.Label) FlowPane(org.apache.pivot.wtk.FlowPane) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component) ComponentStateListener(org.apache.pivot.wtk.ComponentStateListener)

Example 10 with FlowPane

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

the class ComponentInspectorSkin method addPointControl.

private static Component addPointControl(final Dictionary<String, Object> dictionary, final String key, Form.Section section) {
    Point point = (Point) dictionary.get(key);
    BoxPane boxPane = new BoxPane(Orientation.VERTICAL);
    section.add(boxPane);
    Form.setLabel(boxPane, key);
    FlowPane flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    TextInput textInput = new TextInput();
    textInput.setTextSize(3);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(point.x));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Point pointLocal = (Point) dictionary.get(key);
                try {
                    int x = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Point(x, pointLocal.y));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(pointLocal.x));
                }
            }
        }
    });
    Label label = new Label("x");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    flowPane = new FlowPane();
    flowPane.getStyles().put(Style.alignToBaseline, true);
    flowPane.getStyles().put(Style.horizontalSpacing, 5);
    boxPane.add(flowPane);
    textInput = new TextInput();
    textInput.setTextSize(3);
    textInput.setMaximumLength(4);
    textInput.setValidator(new IntValidator());
    textInput.setText(String.valueOf(point.y));
    flowPane.add(textInput);
    textInput.getComponentStateListeners().add(new ComponentStateListener() {

        @Override
        public void focusedChanged(Component component, Component obverseComponent) {
            if (!component.isFocused()) {
                TextInput textInputLocal = (TextInput) component;
                Point pointLocal = (Point) dictionary.get(key);
                try {
                    int y = Integer.parseInt(textInputLocal.getText());
                    dictionary.put(key, new Point(pointLocal.x, y));
                } catch (Exception exception) {
                    displayErrorMessage(exception, component.getWindow());
                    textInputLocal.setText(String.valueOf(pointLocal.y));
                }
            }
        }
    });
    label = new Label("y");
    label.getStyles().put(Style.font, "{italic:true}");
    flowPane.add(label);
    return boxPane;
}
Also used : IntValidator(org.apache.pivot.wtk.validation.IntValidator) BoxPane(org.apache.pivot.wtk.BoxPane) Label(org.apache.pivot.wtk.Label) FlowPane(org.apache.pivot.wtk.FlowPane) Point(org.apache.pivot.wtk.Point) TextInput(org.apache.pivot.wtk.TextInput) Component(org.apache.pivot.wtk.Component) ComponentStateListener(org.apache.pivot.wtk.ComponentStateListener)

Aggregations

Component (org.apache.pivot.wtk.Component)12 FlowPane (org.apache.pivot.wtk.FlowPane)12 BoxPane (org.apache.pivot.wtk.BoxPane)7 ComponentStateListener (org.apache.pivot.wtk.ComponentStateListener)7 Label (org.apache.pivot.wtk.Label)7 TextInput (org.apache.pivot.wtk.TextInput)7 IntValidator (org.apache.pivot.wtk.validation.IntValidator)7 Dimensions (org.apache.pivot.wtk.Dimensions)5 ArrayList (org.apache.pivot.collections.ArrayList)1 CornerRadii (org.apache.pivot.wtk.CornerRadii)1 Insets (org.apache.pivot.wtk.Insets)1 Limits (org.apache.pivot.wtk.Limits)1 Point (org.apache.pivot.wtk.Point)1 Scope (org.apache.pivot.wtk.ScrollBar.Scope)1 Span (org.apache.pivot.wtk.Span)1