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:VerticalFlowLayout.java

/**
 *  Description of the Method//w w w  . j  a v  a 2  s  .c o m
 *
 *@param  target  Description of Parameter
 *@return         Description of the Returned Value
 */
public Dimension minimumLayoutSize(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;
    }
}

From source file:GraphPaperLayout.java

/**
 * Lays out the container in the specified container.
 * /*www .j a v a2  s  .  co  m*/
 * @param parent
 *          the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:StackLayout.java

/**
 * Set the currently displayed component.  If passed null for the component,
 * all contained components will be made invisible (sliding windows do this)
 * @param c Component to show//from   w  w w. j  av a 2s .c o m
 * @param parent Parent container
 */
public void showComponent(Component c, Container parent) {
    Component comp = getVisibleComponent();
    if (comp != c) {
        if (!parent.isAncestorOf(c) && c != null) {
            parent.add(c);
        }
        synchronized (parent.getTreeLock()) {
            if (comp != null) {
                comp.setVisible(false);
            }
            visibleComp = new WeakReference<Component>(c);
            if (c != null) {
                c.setVisible(true);
            }
            // trigger re-layout
            if (c instanceof JComponent) {
                ((JComponent) c).revalidate();
            } else {
                parent.validate(); //XXX revalidate should work!
            }
        }
    }
}

From source file:VerticalFlowLayout.java

/**
 *  Description of the Method/*  w  w w.j  a v  a  2  s.  c o  m*/
 *
 *@param  target  Description of Parameter
 */
public void layoutContainer(Container target) {
    synchronized (target.getTreeLock()) {
        Insets insets = target.getInsets();
        int maxheight = target.getHeight() - (insets.top + insets.bottom + _vgap * 2);
        int nmembers = target.getComponentCount();
        int y = 0;

        Dimension preferredSize = preferredLayoutSize(target);
        Dimension targetSize = target.getSize();

        switch (_valign) {
        case TOP:
            y = insets.top;
            break;
        case CENTER:
            y = (targetSize.height - preferredSize.height) / 2;
            break;
        case BOTTOM:
            y = targetSize.height - preferredSize.height - insets.bottom;
            break;
        }

        for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
                Dimension d = m.getPreferredSize();
                m.setSize(d.width, d.height);

                if ((y + d.height) <= maxheight) {
                    if (y > 0) {
                        y += _vgap;
                    }

                    int x = 0;
                    switch (_halign) {
                    case LEFT:
                        x = insets.left;
                        break;
                    case CENTER:
                        x = (targetSize.width - d.width) / 2;
                        break;
                    case RIGHT:
                        x = targetSize.width - d.width - insets.right;
                        break;
                    }

                    m.setLocation(x, y);

                    y += d.getHeight();

                } else {
                    break;
                }
            }
        }
    }
}

From source file:GraphPaperTest.java

/**
 * Lays out the container in the specified container.
 * //from w  ww  .  ja  v a2  s  . com
 * @param parent
 *            the component which needs to be laid out
 */
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        Insets insets = parent.getInsets();
        int ncomponents = parent.getComponentCount();

        if (ncomponents == 0) {
            return;
        }

        // Total parent dimensions
        Dimension size = parent.getSize();
        int totalW = size.width - (insets.left + insets.right);
        int totalH = size.height - (insets.top + insets.bottom);

        // Cell dimensions, including padding
        int totalCellW = totalW / gridSize.width;
        int totalCellH = totalH / gridSize.height;

        // Cell dimensions, without padding
        int cellW = (totalW - ((gridSize.width + 1) * hgap)) / gridSize.width;
        int cellH = (totalH - ((gridSize.height + 1) * vgap)) / gridSize.height;

        for (int i = 0; i < ncomponents; i++) {
            Component c = parent.getComponent(i);
            Rectangle rect = (Rectangle) compTable.get(c);
            if (rect != null) {
                int x = insets.left + (totalCellW * rect.x) + hgap;
                int y = insets.top + (totalCellH * rect.y) + vgap;
                int w = (cellW * rect.width) - hgap;
                int h = (cellH * rect.height) - vgap;
                c.setBounds(x, y, w, h);
            }
        }
    }
}

From source file:StackLayout.java

/**
 * Lays out the child components within the indicated parent container.
 *///from w  ww . jav  a  2  s .c  om
public void layoutContainer(Container parent) {
    synchronized (parent.getTreeLock()) {
        if (parent instanceof JToolBar) {
            setOrientation(((JToolBar) parent).getOrientation());
        }

        layoutComponents(parent);

    }
}

From source file:StackLayout.java

/**
 * Returns the minimum size for this layout to arrange the
 * indicated parent's children at the specified orientation.
 */// w w w.java  2 s.  com
public Dimension minimumLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        if (parent instanceof JToolBar) {
            setOrientation(((JToolBar) parent).getOrientation());
        }

        Component[] comps = parent.getComponents();
        Dimension total = new Dimension(0, 0);

        int depth = calculatePreferredDepth(comps, ourOrientation);
        int length = calculateMinimumLength(comps, ourOrientation, ourSpacing);

        total.width = (ourOrientation == HORIZONTAL ? length : depth);
        total.height = (ourOrientation == HORIZONTAL ? depth : length);

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

        return total;
    }
}

From source file:StackLayout.java

/**
 * Returns the preferred size for this layout to arrange the
 * indicated parent's children at the specified orientation.
 *///  ww  w .  j a va 2s .co  m
// public, because it's useful - not one of the LayoutManager methods
public Dimension preferredLayoutSize(Container parent, int orientation) {
    synchronized (parent.getTreeLock()) {
        Component[] comps = parent.getComponents();
        Dimension total = new Dimension(0, 0);

        int depth = calculatePreferredDepth(comps, orientation);

        int length = (ourLengthsMatched ? calculateAdjustedLength(comps, orientation, ourSpacing)
                : calculatePreferredLength(comps, orientation, ourSpacing));

        total.width = (orientation == HORIZONTAL ? length : depth);
        total.height = (orientation == HORIZONTAL ? depth : length);

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

        return total;
    }
}

From source file:TableLayout.java

/**
 * Returns the preferred layout for the specified parent container.
 *//*from www.j  av  a 2 s  .  co  m*/

public Dimension preferredLayoutSize(Container parent) {
    synchronized (parent.getTreeLock()) {
        Dimension[][] prefSizes = getPreferredSizes(parent);
        Pair layout = calculateLayout(prefSizes);
        int[] columnWidths = (int[]) layout.getFirst();
        int[] rowHeights = (int[]) layout.getSecond();

        return calculatePreferredLayoutSize(parent, columnWidths, rowHeights);
    }
}

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./*www  . j a  v  a2 s .co 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;
        }
    }
}