Example usage for java.awt Container getTreeLock

List of usage examples for java.awt Container getTreeLock

Introduction

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

Prototype

public final Object getTreeLock() 

Source Link

Document

Gets this component's locking object (the object that owns the thread synchronization monitor) for AWT component-tree and layout operations.

Usage

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();
            }//  ww  w.ja  v  a  2s  .c om
        }

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

        return new Dimension(width, height);
    }
}

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;//from  w  w  w.ja  v  a2 s.co  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:VerticalLayout.java

/**
 * Lays out the container.//ww w .j a  va  2 s .  c o  m
 */
public void layoutContainer(Container parent) {
    Insets insets = parent.getInsets();
    synchronized (parent.getTreeLock()) {
        int n = parent.getComponentCount();
        Dimension pd = parent.getSize();
        int y = 0;
        //work out the total size
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            y += d.height + vgap;
        }
        y -= vgap; //otherwise there's a vgap too many
        //Work out the anchor paint
        if (anchor == TOP)
            y = insets.top;
        else if (anchor == CENTER)
            y = (pd.height - y) / 2;
        else
            y = pd.height - y - insets.bottom;
        //do layout
        for (int i = 0; i < n; i++) {
            Component c = parent.getComponent(i);
            Dimension d = c.getPreferredSize();
            int x = insets.left;
            int wid = d.width;
            if (alignment == CENTER)
                x = (pd.width - d.width) / 2;
            else if (alignment == RIGHT)
                x = pd.width - d.width - insets.right;
            else if (alignment == BOTH)
                wid = pd.width - insets.left - insets.right;
            c.setBounds(x, y, wid, d.height);
            y += d.height + vgap;
        }
    }
}

From source file:StackLayout.java

public void layoutContainer(Container parent) {
    Component visibleComp = getVisibleComponent();
    if (visibleComp != null) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            visibleComp.setBounds(insets.left, insets.top, parent.getWidth() - (insets.left + insets.right),
                    parent.getHeight() - (insets.top + insets.bottom));
        }//from w  ww. j a  va2 s  .c o  m
    }
}

From source file:VerticalLayout.java

private Dimension layoutSize(Container parent, boolean minimum) {
    Dimension dim = new Dimension(0, 0);
    Dimension d;//  w  w w .j  a v  a 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:LCBLayout.java

/**
 * Returns the preferred size using this layout manager.
 *
 * @param parent  the parent./*from   ww  w  . j av  a2  s. co m*/
 *
 * @return the preferred size using this layout manager.
*/
public Dimension preferredLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.labelGap + this.colWidth[1] + this.buttonGap
                + this.colWidth[2];
        return new Dimension(insets.left + insets.right + totalWidth + this.labelGap + this.buttonGap,
                insets.top + insets.bottom + totalHeight + this.vGap);
    }

}

From source file:LCBLayout.java

/**
 * Returns the minimum size using this layout manager.
 *
 * @param parent  the parent.//  ww w  .j  a  v a  2  s.  co m
 *
 * @return the minimum size using this layout manager.
 */
public Dimension minimumLayoutSize(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getMinimumSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.labelGap + this.colWidth[1] + this.buttonGap
                + this.colWidth[2];
        return new Dimension(insets.left + insets.right + totalWidth + this.labelGap + this.buttonGap,
                insets.top + insets.bottom + totalHeight + this.vGap);
    }

}

From source file:EdgeLayoutExample.java

public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int top = insets.top;
        int left = insets.left;

        Dimension minimumSize = minimumLayoutSize(parent);

        int height = minimumSize.height;
        int width = minimumSize.width;

        int availableHeight = parent.getHeight() - insets.bottom - insets.top;
        int availableWidth = parent.getWidth() - insets.left - insets.right;
        if (height < availableHeight) {
            height = availableHeight;//from   w  w  w. java2 s  . c o  m
        }
        if (width < availableWidth) {
            width = availableWidth;
        }

        int bottom = availableHeight;
        int right = availableWidth;

        Dimension preferredSize = preferredLayoutSize(parent);

        int preferredWidthAvailable = width - preferredSize.width;
        int preferredHeightAvailable = height - preferredSize.height;

        Component centerComp = null;

        for (int i = 0; i < this.components.size(); i++) {
            Component c = (Component) this.components.get(i);
            String constraint = (String) this.constraints.get(c);

            if (constraint.equals(CENTER)) {
                centerComp = c;
            } else {
                int compHeight;
                int compWidth;
                int xOrigin;
                int yOrigin;

                if (constraint.equals(NORTH) || constraint.equals(SOUTH)) {
                    compWidth = width;

                    if (preferredHeightAvailable > 0) {
                        int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height;
                        if (preferredHeightAvailable > preferredHeightNeeded) {
                            compHeight = c.getPreferredSize().height;
                            preferredHeightAvailable -= preferredHeightNeeded;
                        } else {
                            compHeight = c.getMinimumSize().height + preferredHeightAvailable;
                            preferredHeightAvailable = 0;
                        }
                    } else {
                        compHeight = c.getMinimumSize().height;
                    }
                    height = height - compHeight;

                    xOrigin = left;

                    if (constraint.equals(NORTH)) {
                        yOrigin = top;
                        top += compHeight;
                    } else {
                        yOrigin = bottom - compHeight;
                        bottom = yOrigin;
                    }
                } else {
                    compHeight = height;
                    if (preferredWidthAvailable > 0) {
                        int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width;
                        if (preferredWidthAvailable > preferredWidthNeeded) {
                            compWidth = c.getPreferredSize().width;
                            preferredWidthAvailable -= preferredWidthNeeded;
                        } else {
                            compWidth = c.getMinimumSize().width + preferredWidthAvailable;
                            preferredWidthAvailable = 0;
                        }
                    } else {
                        compWidth = c.getMinimumSize().width;
                    }
                    width = width - compWidth;

                    yOrigin = top;

                    if (constraint.equals(WEST)) {
                        xOrigin = left;
                        left += compWidth;
                    } else {
                        xOrigin = right - compWidth;
                        right = xOrigin;
                    }
                }
                c.setSize(compWidth, compHeight);
                c.setBounds(xOrigin, yOrigin, compWidth, compHeight);
            }
            if (centerComp != null) {
                c.setSize(width, height);
                c.setBounds(left, top, width, height);
            }
        }
    }
}

From source file:LCBLayout.java

/**
 * Lays out the components./* w w w  .j  ava 2s .  c  o m*/
 *
 * @param parent  the parent.
 */
public void layoutContainer(final Container parent) {

    synchronized (parent.getTreeLock()) {
        final Insets insets = parent.getInsets();
        final int ncomponents = parent.getComponentCount();
        final int nrows = ncomponents / COLUMNS;
        for (int c = 0; c < COLUMNS; c++) {
            for (int r = 0; r < nrows; r++) {
                final Component component = parent.getComponent(r * COLUMNS + c);
                final Dimension d = component.getPreferredSize();
                if (this.colWidth[c] < d.width) {
                    this.colWidth[c] = d.width;
                }
                if (this.rowHeight[r] < d.height) {
                    this.rowHeight[r] = d.height;
                }
            }
        }
        int totalHeight = this.vGap * (nrows - 1);
        for (int r = 0; r < nrows; r++) {
            totalHeight = totalHeight + this.rowHeight[r];
        }
        final int totalWidth = this.colWidth[0] + this.colWidth[1] + this.colWidth[2];

        // adjust the width of the second column to use up all of parent
        final int available = parent.getWidth() - insets.left - insets.right - this.labelGap - this.buttonGap;
        this.colWidth[1] = this.colWidth[1] + (available - totalWidth);

        // *** DO THE LAYOUT ***
        int x = insets.left;
        for (int c = 0; c < COLUMNS; c++) {
            int y = insets.top;
            for (int r = 0; r < nrows; r++) {
                final int i = r * COLUMNS + c;
                if (i < ncomponents) {
                    final Component component = parent.getComponent(i);
                    final Dimension d = component.getPreferredSize();
                    final int h = d.height;
                    final int adjust = (this.rowHeight[r] - h) / 2;
                    parent.getComponent(i).setBounds(x, y + adjust, this.colWidth[c], h);
                }
                y = y + this.rowHeight[r] + this.vGap;
            }
            x = x + this.colWidth[c];
            if (c == 0) {
                x = x + this.labelGap;
            }
            if (c == 1) {
                x = x + this.buttonGap;
            }
        }

    }

}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method/*  ww  w.  j  av  a2 s .  co m*/
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension preferredLayoutSize(Container target) {
    synchronized (target.getTreeLock()) {
        Dimension dim = new Dimension(0, 0);
        int nmembers = target.getComponentCount();
        boolean firstVisibleComponent = true;

        for (int ii = 0; ii < nmembers; ii++) {
            Component m = target.getComponent(ii);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                dim.width = Math.max(dim.width, d.width);
                if (firstVisibleComponent) {
                    firstVisibleComponent = false;
                } else {
                    dim.height += _vgap;
                }
                dim.height += d.height;
            }
        }
        Insets insets = target.getInsets();
        dim.width += insets.left + insets.right + _hgap * 2;
        dim.height += insets.top + insets.bottom + _vgap * 2;
        return dim;
    }
}