Example usage for java.awt Container getInsets

List of usage examples for java.awt Container getInsets

Introduction

In this page you can find the example usage for java.awt Container getInsets.

Prototype

public Insets getInsets() 

Source Link

Document

Determines the insets of this container, which indicate the size of the container's border.

Usage

From source file:TableLayout.java

public Dimension preferredLayoutSize(Container parent) {
    Insets insets = parent.getInsets();
    measureComponents(parent);/*  ww w .j a v a2 s.co  m*/
    // System.out.println("Pref Size: "+PrefWidth+","+PrefHeight);
    // System.out.println("+ insets LR "+insets.left+"+"+insets.right+", TB "+insets.top+"+"+insets.bottom);
    return new Dimension(insets.left + insets.right + PrefWidth, insets.top + insets.bottom + PrefHeight);
}

From source file:TableLayout.java

public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    measureComponents(parent);/*from w  w w  .  j a v a2s . c om*/
    int width = parent.getSize().width - (insets.left + insets.right);
    int height = parent.getSize().height - (insets.top + insets.bottom);
    // System.out.println("Resize "+width+","+height);

    // Decide whether to base our scaling on minimum or preferred sizes, or
    // a mixture of both, separately for width and height scaling.
    // This weighting also tells us how much of the hgap/vgap to use.

    double widthWeighting = 0.0;
    if (width >= PrefWidth || PrefWidth == MinWidth)
        widthWeighting = 1.0;
    else if (width <= MinWidth) {
        widthWeighting = 0.0;
        width = MinWidth;
    } else
        widthWeighting = (double) (width - MinWidth) / (double) (PrefWidth - MinWidth);

    double heightWeighting = 0.0;
    if (height >= PrefHeight || PrefHeight == MinHeight)
        heightWeighting = 1.0;
    else if (height <= MinHeight) {
        heightWeighting = 0.0;
        height = MinHeight;
    } else
        heightWeighting = (double) (height - MinHeight) / (double) (PrefHeight - MinHeight);

    // calculate scale factors to scale components to size of container, based
    // on weighted combination of minimum and preferred sizes

    double minWidthScale = (1.0 - widthWeighting) * width / MinWidth;
    //double prefWidthScale = widthWeighting * (width-hgap*(ncols+1))/(PrefWidth-hgap*(ncols+1));
    double minHeightScale = (1.0 - heightWeighting) * height / MinHeight;
    double prefHeightScale = heightWeighting * (height - vgap * (nrows + 1))
            / (PrefHeight - vgap * (nrows + 1));

    // only get the full amount of gap if we're working to preferred size
    int vGap = (int) (vgap * heightWeighting);
    int hGap = (int) (hgap * widthWeighting);

    int y = insets.top + vGap;

    for (int c = 0; c < ncols; ++c)
        weight[c] = prefWidth[c];

    for (int r = 0; r < nrows; ++r) {
        int x = insets.left + hGap;
        int rowHeight = (int) (minHeight[r] * minHeightScale + prefHeight[r] * prefHeightScale);

        // Column padding can vary from row to row, so we need several
        // passes through the columns for each row:

        // First, work out the weighting that deterimines how we distribute column padding
        for (int c = 0; c < ncols; ++c) {
            Component comp = components[c][r];
            if (comp != null) {
                TableOption option = (TableOption) options.get(comp);
                if (option == null)
                    option = defaultOption;
                if (option.weight >= 0)
                    weight[c] = option.weight;
                else if (option.weight == -1)
                    weight[c] = prefWidth[c];
            }
        }
        int totalWeight = 0;
        for (int c = 0; c < ncols; ++c)
            totalWeight += weight[c];
        int horizSurplus = width - hgap * (ncols + 1) - PrefWidth;

        // Then work out column sizes, essentially preferred size + share of padding
        for (int c = 0; c < ncols; ++c) {
            columnWidth[c] = (int) (minWidthScale * minWidth[c] + widthWeighting * prefWidth[c]);
            if (horizSurplus > 0 && totalWeight > 0)
                columnWidth[c] += (int) (widthWeighting * horizSurplus * weight[c] / totalWeight);
        }

        // Only now do we know enough to position all the columns within this row...
        for (int c = 0; c < ncols; ++c) {
            Component comp = components[c][r];
            if (comp != null) {
                TableOption option = (TableOption) options.get(comp);
                if (option == null)
                    option = defaultOption;

                // cell size may be bigger than row/column size due to spanning
                int cellHeight = rowHeight;
                int cellWidth = columnWidth[c];
                for (int i = 1; i < option.colSpan; ++i)
                    cellWidth += columnWidth[c + i];
                for (int i = 1; i < option.rowSpan; ++i)
                    cellHeight += (int) (minHeight[r + i] * minHeightScale + prefHeight[r + i] * prefHeightScale
                            + vGap);

                Dimension d = new Dimension(comp.getPreferredSize());

                if (d.width > cellWidth || option.horizontal == TableOption.FILL)
                    d.width = cellWidth;
                if (d.height > cellHeight || option.vertical == TableOption.FILL)
                    d.height = cellHeight;

                int yoff = 0;
                if (option.vertical == TableOption.BOTTOM)
                    yoff = cellHeight - d.height;
                else if (option.vertical == TableOption.CENTRE)
                    yoff = (cellHeight - d.height) / 2;

                int xoff = 0;
                if (option.horizontal == TableOption.RIGHT)
                    xoff = cellWidth - d.width;
                else if (option.horizontal == TableOption.CENTRE)
                    xoff = (cellWidth - d.width) / 2;

                // System.out.println(" "+comp.getClass().getName()+" at ("+x+"+"+xoff+","+y+"+"+yoff+"), size "+d.width+","+d.height);
                comp.setBounds(x + xoff, y + yoff, d.width, d.height);
            }
            x += columnWidth[c] + hGap;
        }
        y += rowHeight + vGap;
    }
}

From source file:StackLayout.java

private void layoutComponents(Container parent) {
    Component[] components = parent.getComponents();
    Insets in = parent.getInsets();

    int maxHeight = parent.getHeight() - in.top - in.bottom;
    int maxWidth = parent.getWidth() - in.left - in.right;
    boolean horiz = (ourOrientation == HORIZONTAL);

    int totalDepth = calculatePreferredDepth(components, ourOrientation);
    totalDepth = Math.max(totalDepth, (horiz ? maxHeight : maxWidth));

    int prefLength = (ourLengthsMatched ? calculateAdjustedLength(components, ourOrientation, ourSpacing)
            : calculatePreferredLength(components, ourOrientation, ourSpacing));
    int totalLength = Math.min(prefLength, (horiz ? maxWidth : maxHeight));

    int a = (horiz ? in.left : in.top);
    int b = (horiz ? in.top : in.left);
    int l = 0, d = 0, sum = 0;
    int matchedLength = 0;
    Dimension prefsize = null;/*from   ww  w  .j av  a  2 s . c  o  m*/

    if (ourLengthsMatched) {
        matchedLength = (horiz ? getMaxPrefWidth(components) : getMaxPrefHeight(components));

        if (prefLength > totalLength && ourSqueezeFactor < 100) {
            int minLength = calculateMinimumLength(components, ourOrientation, ourSpacing);

            if (minLength >= totalLength) {
                matchedLength = (matchedLength * ourSqueezeFactor) / 100;
            }

            else {
                int numSeparators = countSeparators(components);
                int numComponents = components.length - numSeparators;
                int diff = (prefLength - totalLength) / numComponents;
                if ((prefLength - totalLength) % numComponents > 0)
                    diff++;
                matchedLength -= diff;
            }
        }
    }

    for (int i = 0; i < components.length; i++) {
        prefsize = components[i].getPreferredSize();
        if (!ourLengthsMatched)
            l = (horiz ? prefsize.width : prefsize.height);
        else
            l = matchedLength;

        if (components[i] instanceof JSeparator) {
            // l = Math.min(prefsize.width, prefsize.height);
            l = (horiz ? prefsize.width : prefsize.height);
            d = totalDepth;
            sum += l;
            if (ourDrop && sum > totalLength)
                l = 0;
        }

        else {
            sum += l;
            if (ourDrop && sum > totalLength)
                l = 0;

            else if (ourFill && !ourLengthsMatched && i == components.length - 1) {
                l = Math.max(l, (horiz ? maxWidth : maxHeight));
            }

            if (ourDepthsMatched)
                d = totalDepth;
            else
                d = (horiz ? prefsize.height : prefsize.width);
        }

        if (horiz)
            components[i].setBounds(a, b + (totalDepth - d) / 2, l, d);
        else
            components[i].setBounds(b + (totalDepth - d) / 2, a, d, l);

        a += l + ourSpacing;
        sum += ourSpacing;
    }

}

From source file:BeanContainer.java

public void layoutContainer(Container parent) {
        int divider = getDivider(parent);

        Insets insets = parent.getInsets();
        int w = parent.getWidth() - insets.left - insets.right - divider;
        int x = insets.left;
        int y = insets.top;

        for (int k = 1; k < parent.getComponentCount(); k += 2) {
            Component comp1 = parent.getComponent(k - 1);
            Component comp2 = parent.getComponent(k);
            Dimension d = comp2.getPreferredSize();

            comp1.setBounds(x, y, divider - m_hGap, d.height);
            comp2.setBounds(x + divider, y, w, d.height);
            y += d.height + m_vGap;/*w  w w  .  ja va  2s  .c  o  m*/
        }
    }

From source file:VerticalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    Dimension d;/*from  w  w  w . j  ava 2 s. c  o  m*/
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                d = minimum ? c.getMinimumSize() : c.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                dim.height += d.height;
                if (i > 0)
                    dim.height += vgap;
            }
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom + vgap + vgap;
    return dim;
}

From source file:com.equitysoft.cellspark.ProportionalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            int maxhgt = 0;
            if (c.isVisible()) {
                Dimension d = (minimum) ? c.getMinimumSize() : c.getPreferredSize();
                dim.width += d.width;// www .  j ava2 s  . c  o m
                if (d.height > dim.height)
                    dim.height = d.height;
            }
            cnt++;
            if (cnt == num)
                break;
        }
    }
    Insets insets = parent.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
}

From source file:EdgeLayoutExample.java

public Dimension minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        int width = 0;
        int height = 0;

        //Add the minimum  widths of all EAST/WEST components
        //Add the minimum height of all NORTH/SOUTH components
        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            if (this.constraints.get(c).equals(WEST) || this.constraints.get(c).equals(EAST)) {
                width += c.getMinimumSize().getWidth();
            } else {
                height += c.getMinimumSize().getHeight();
            }/*from  w  ww . ja v a  2  s . c om*/
        }

        width += parent.getInsets().right + parent.getInsets().left;
        height += parent.getInsets().top + parent.getInsets().bottom;

        return new Dimension(width, height);
    }
}

From source file:EdgeLayoutExample.java

public Dimension preferredLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        int width = 0;
        int height = 0;

        //Add the preferred widths of all EAST/WEST components
        //Add the preferred height of all NORTH/SOUTH components
        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            if (this.constraints.get(c).equals(WEST) || this.constraints.get(c).equals(EAST)) {
                width += c.getPreferredSize().getWidth();
            } else {
                height += c.getPreferredSize().getHeight();
            }//from www .  j  a v a 2  s.c  o m
        }

        width += parent.getInsets().right + parent.getInsets().left;
        height += parent.getInsets().top + parent.getInsets().bottom;

        return new Dimension(width, height);
    }
}

From source file:TableLayout.java

/**
 * Lays out the specified container. Throws an
 * <code>IllegalStateException</code> if any of the components added via the
 * <code>addLayoutComponent</code> method have a different parent than the
 * specified Container./* w  ww  .j  av a 2 s. c  o  m*/
 */

public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        int rowCount = rows.size();

        Insets parentInsets = parent.getInsets();

        // Collect the preferred sizes.
        Dimension[][] prefSizes = getPreferredSizes(parent);
        Pair layout = calculateLayout(prefSizes);
        int[] columnWidths = (int[]) layout.getFirst();
        int[] rowHeights = (int[]) layout.getSecond();

        Dimension prefParentSize = calculatePreferredLayoutSize(parent, columnWidths, rowHeights);
        Dimension parentSize = parent.getSize();
        Dimension layoutSize = new Dimension(
                parentSize.width - xGap * (rowCount - 1) - parentInsets.left - parentInsets.right,
                parentSize.height - yGap * (columnCount - 1) - parentInsets.top - parentInsets.bottom);
        Dimension prefLayoutSize = new Dimension(
                prefParentSize.width - xGap * (rowCount - 1) - parentInsets.left - parentInsets.right,
                prefParentSize.height - yGap * (columnCount - 1) - parentInsets.top - parentInsets.bottom);

        // Layout the components.
        int y = parentInsets.top;
        for (int i = 0; i < rowCount; i++) {
            int x = parentInsets.left;
            int cellHeight = (rowHeights[i] * layoutSize.height) / prefLayoutSize.height;
            Component[] row = (Component[]) rows.elementAt(i);
            for (int j = 0; j < row.length; j++) {
                int cellWidth = (columnWidths[j] * layoutSize.width) / prefLayoutSize.width;
                Component component = row[j];

                // Can only happen on the last line when all the remaining components are null as well
                if (component == null)
                    break;

                Dimension maxSize = component.getMaximumSize();

                int compWidth = Math.min(maxSize.width, cellWidth);
                int compHeight = Math.min(maxSize.height, cellHeight);

                int compX = x + (int) ((cellWidth - compWidth) * component.getAlignmentX());
                int compY = y + (int) ((cellHeight - compHeight) * component.getAlignmentY());

                component.setBounds(compX, compY, compWidth, compHeight);

                x += cellWidth + xGap;
            }

            y += cellHeight + yGap;
        }
    }
}

From source file:XYLayout.java

Dimension getLayoutSize(Container target, boolean doPreferred) {
    Dimension dim = new Dimension(0, 0);
    if (width <= 0 || height <= 0) {
        int count = target.getComponentCount();
        for (int i = 0; i < count; i++) {
            Component component = target.getComponent(i);
            if (component.isVisible()) {
                Rectangle r = getComponentBounds(component, doPreferred);
                dim.width = Math.max(dim.width, r.x + r.width);
                dim.height = Math.max(dim.height, r.y + r.height);
            }/* ww w.ja  va2s.c  om*/
        }

    }
    if (width > 0)
        dim.width = width;
    if (height > 0)
        dim.height = height;
    Insets insets = target.getInsets();
    dim.width += insets.left + insets.right;
    dim.height += insets.top + insets.bottom;
    return dim;
}