Search in sources :

Example 21 with SpringLayout

use of javax.swing.SpringLayout in project com.revolsys.open by revolsys.

the class SpringLayoutUtil method makeColumns.

public static void makeColumns(final Container container, final int numColumns, final int initialX, final int initialY, final int xPad, final int yPad) {
    final Spring xPadSpring = Spring.constant(xPad);
    final Spring yPadSpring = Spring.constant(yPad);
    LayoutManager layout = container.getLayout();
    if (!(layout instanceof SpringLayout)) {
        layout = new SpringLayout();
        container.setLayout(layout);
    }
    final SpringLayout springLayout = (SpringLayout) layout;
    final Component[] components = container.getComponents();
    if (components.length > 0) {
        final Spring[] columnWidths = new Spring[numColumns];
        for (int i = 0; i < columnWidths.length; i++) {
            columnWidths[i] = Spring.constant(0);
        }
        final int numRows = (int) Math.ceil((double) components.length / numColumns);
        final Spring[] rowHeights = new Spring[numRows];
        for (int i = 0; i < rowHeights.length; i++) {
            rowHeights[i] = Spring.constant(0);
        }
        for (int i = 0; i < components.length; i++) {
            final Component component = components[i];
            final Constraints componentConstraints = springLayout.getConstraints(component);
            final Spring width = componentConstraints.getWidth();
            final Spring height = componentConstraints.getHeight();
            final int column = i % numColumns;
            final int row = (int) Math.floor((double) i / numColumns);
            columnWidths[column] = Spring.max(columnWidths[column], width);
            rowHeights[row] = Spring.max(rowHeights[row], height);
        }
        final Spring[] columnX = new Spring[numColumns];
        final Spring initialXSpring = Spring.constant(initialX);
        columnX[0] = initialXSpring;
        for (int i = 1; i < columnX.length; i++) {
            columnX[i] = Spring.sum(columnX[i - 1], Spring.sum(xPadSpring, columnWidths[i - 1]));
        }
        final Spring[] rowY = new Spring[numRows];
        final Spring initialYSpring = Spring.constant(initialY);
        rowY[0] = initialYSpring;
        for (int i = 1; i < rowY.length; i++) {
            rowY[i] = Spring.sum(rowY[i - 1], Spring.sum(yPadSpring, rowHeights[i - 1]));
        }
        final Constraints containerConstraints = springLayout.getConstraints(container);
        containerConstraints.setWidth(Spring.sum(columnX[numColumns - 1], Spring.sum(columnWidths[numColumns - 1], initialXSpring)));
        containerConstraints.setHeight(Spring.sum(rowY[numRows - 1], Spring.sum(rowHeights[numRows - 1], initialYSpring)));
        for (int i = 0; i < components.length; i++) {
            final Component component = components[i];
            final Constraints componentConstraints = springLayout.getConstraints(component);
            final int column = i % numColumns;
            final int row = (int) Math.floor((double) i / numColumns);
            componentConstraints.setX(columnX[column]);
            componentConstraints.setWidth(Spring.width(component));
            componentConstraints.setY(rowY[row]);
            componentConstraints.setHeight(Spring.height(component));
        }
    }
}
Also used : Constraints(javax.swing.SpringLayout.Constraints) LayoutManager(java.awt.LayoutManager) SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring) Component(java.awt.Component)

Example 22 with SpringLayout

use of javax.swing.SpringLayout in project com.revolsys.open by revolsys.

the class SpringLayoutUtil method makeGrid.

/**
 * Aligns the first <code>rows</code> <code>cols</code> components of
 * <code>parent</code> in a grid. Each component is as big as the maximum
 * preferred width and height of the components. The parent is made just big
 * enough to fit them all.
 *
 * @param rows number of rows
 * @param cols number of columns
 * @param initialX x location to start the grid at
 * @param initialY y location to start the grid at
 * @param xPad x padding between cells
 * @param yPad y padding between cells
 */
public static void makeGrid(final Container parent, final int rows, final int cols, final int initialX, final int initialY, final int xPad, final int yPad) {
    LayoutManager layout = parent.getLayout();
    if (!(layout instanceof SpringLayout)) {
        layout = new SpringLayout();
        parent.setLayout(layout);
    }
    final SpringLayout springLayout = (SpringLayout) parent.getLayout();
    final Spring xPadSpring = Spring.constant(xPad);
    final Spring yPadSpring = Spring.constant(yPad);
    final Spring initialXSpring = Spring.constant(initialX);
    final Spring initialYSpring = Spring.constant(initialY);
    final int max = rows * cols;
    // Calculate Springs that are the max of the width/height so that all
    // cells have the same size.
    Spring maxWidthSpring = springLayout.getConstraints(parent.getComponent(0)).getWidth();
    Spring maxHeightSpring = springLayout.getConstraints(parent.getComponent(0)).getWidth();
    for (int i = 1; i < max; i++) {
        final SpringLayout.Constraints cons = springLayout.getConstraints(parent.getComponent(i));
        maxWidthSpring = Spring.max(maxWidthSpring, cons.getWidth());
        maxHeightSpring = Spring.max(maxHeightSpring, cons.getHeight());
    }
    // components to have the same size.
    for (int i = 0; i < max; i++) {
        final SpringLayout.Constraints cons = springLayout.getConstraints(parent.getComponent(i));
        cons.setWidth(maxWidthSpring);
        cons.setHeight(maxHeightSpring);
    }
    // Then adjust the x/y constraints of all the cells so that they
    // are aligned in a grid.
    SpringLayout.Constraints lastCons = null;
    SpringLayout.Constraints lastRowCons = null;
    for (int i = 0; i < max; i++) {
        final SpringLayout.Constraints cons = springLayout.getConstraints(parent.getComponent(i));
        if (i % cols == 0) {
            // start of new row
            lastRowCons = lastCons;
            cons.setX(initialXSpring);
        } else {
            // x position depends on previous component
            cons.setX(Spring.sum(lastCons.getConstraint(SpringLayout.EAST), xPadSpring));
        }
        if (i / cols == 0) {
            // first row
            cons.setY(initialYSpring);
        } else {
            // y position depends on previous row
            cons.setY(Spring.sum(lastRowCons.getConstraint(SpringLayout.SOUTH), yPadSpring));
        }
        lastCons = cons;
    }
    // Set the parent's size.
    final SpringLayout.Constraints pCons = springLayout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, Spring.sum(Spring.constant(yPad), lastCons.getConstraint(SpringLayout.SOUTH)));
    pCons.setConstraint(SpringLayout.EAST, Spring.sum(Spring.constant(xPad), lastCons.getConstraint(SpringLayout.EAST)));
}
Also used : LayoutManager(java.awt.LayoutManager) SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring) Constraints(javax.swing.SpringLayout.Constraints)

Example 23 with SpringLayout

use of javax.swing.SpringLayout in project com.revolsys.open by revolsys.

the class SpringLayoutUtil method makeRows.

public static void makeRows(final Container container, final int initialX, final int initialY, final int xPad, final int yPad, final int... componentsPerRow) {
    final SpringLayout layout = (SpringLayout) container.getLayout();
    final Spring xPadSpring = Spring.constant(xPad);
    final Spring yPadSpring = Spring.constant(yPad);
    final Spring initialXSpring = Spring.constant(initialX);
    final Spring initialYSpring = Spring.constant(initialY);
    int i = 0;
    Spring width = null;
    final int componentCount = container.getComponentCount();
    Spring y = initialYSpring;
    for (int row = 0; row < componentsPerRow.length && i < componentCount; row++) {
        final int numComponents = componentsPerRow[row];
        Spring rowHeight = Spring.constant(0);
        if (row > 0) {
            y = Spring.sum(y, yPadSpring);
        }
        Spring x = initialXSpring;
        for (int col = 0; col < numComponents && i < componentCount; col++) {
            final Component component = container.getComponent(i);
            if (col > 0) {
                x = Spring.sum(x, xPadSpring);
            }
            final Constraints componentConstraints = layout.getConstraints(component);
            componentConstraints.setX(x);
            componentConstraints.setWidth(Spring.width(component));
            componentConstraints.setY(y);
            componentConstraints.setHeight(Spring.height(component));
            x = Spring.sum(x, Spring.width(component));
            rowHeight = Spring.max(rowHeight, Spring.height(component));
            i++;
        }
        y = Spring.sum(y, rowHeight);
        if (width == null) {
            width = x;
        } else {
            width = Spring.max(width, x);
        }
    }
    final Constraints containerConstraints = layout.getConstraints(container);
    containerConstraints.setWidth(Spring.sum(initialXSpring, width));
    containerConstraints.setHeight(Spring.sum(initialYSpring, y));
}
Also used : Constraints(javax.swing.SpringLayout.Constraints) SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring) Component(java.awt.Component)

Example 24 with SpringLayout

use of javax.swing.SpringLayout in project com.revolsys.open by revolsys.

the class SpringLayoutUtil method singleColumn.

public static void singleColumn(final Container container, final int initialY, final int yPad) {
    Spring height = Spring.constant(initialY + yPad * container.getComponentCount());
    Spring width = Spring.constant(0);
    final SpringLayout layout = (SpringLayout) container.getLayout();
    Component previous = container;
    for (final Component component : container.getComponents()) {
        final Constraints constraints = layout.getConstraints(component);
        width = Spring.max(width, constraints.getWidth());
        height = Spring.sum(height, constraints.getHeight());
        if (previous == container) {
            layout.putConstraint(SpringLayout.NORTH, previous, initialY, SpringLayout.NORTH, component);
        } else {
            layout.putConstraint(SpringLayout.SOUTH, previous, yPad, SpringLayout.NORTH, component);
        }
        previous = component;
    }
    final Constraints containerConstraints = layout.getConstraints(container);
    containerConstraints.setConstraint(SpringLayout.SOUTH, height);
    containerConstraints.setConstraint(SpringLayout.EAST, width);
}
Also used : Constraints(javax.swing.SpringLayout.Constraints) SpringLayout(javax.swing.SpringLayout) Spring(javax.swing.Spring) Component(java.awt.Component)

Example 25 with SpringLayout

use of javax.swing.SpringLayout in project com.revolsys.open by revolsys.

the class SpringLayoutUtil method getConstraintsForCell.

/* Used by makeCompactGrid. */
private static SpringLayout.Constraints getConstraintsForCell(final int row, final int col, final Container parent, final int cols) {
    final SpringLayout layout = (SpringLayout) parent.getLayout();
    final Component c = parent.getComponent(row * cols + col);
    return layout.getConstraints(c);
}
Also used : SpringLayout(javax.swing.SpringLayout) Component(java.awt.Component)

Aggregations

SpringLayout (javax.swing.SpringLayout)57 JLabel (javax.swing.JLabel)21 JPanel (javax.swing.JPanel)21 Spring (javax.swing.Spring)21 BorderLayout (java.awt.BorderLayout)18 JTextField (javax.swing.JTextField)17 ActionEvent (java.awt.event.ActionEvent)15 ActionListener (java.awt.event.ActionListener)15 Component (java.awt.Component)12 JDialog (javax.swing.JDialog)12 Dimension (java.awt.Dimension)11 JButton (javax.swing.JButton)10 JScrollPane (javax.swing.JScrollPane)10 EmptyBorder (javax.swing.border.EmptyBorder)9 FlowLayout (java.awt.FlowLayout)8 JMenuItem (javax.swing.JMenuItem)7 Constraints (javax.swing.SpringLayout.Constraints)6 ItemEvent (java.awt.event.ItemEvent)5 Callable (java.util.concurrent.Callable)5 JMenu (javax.swing.JMenu)5