FileDocCategorySizeDatePackage
DiagonalLayout.javaAPI DocExample5494Tue Dec 12 18:59:00 GMT 2000None

DiagonalLayout

public class DiagonalLayout extends Object implements LayoutManager

Fields Summary
private int
vgap
private int
minWidth
private int
minHeight
private int
preferredWidth
private int
preferredHeight
private boolean
sizeUnknown
Constructors Summary
public DiagonalLayout()


      
        this(5);
    
public DiagonalLayout(int v)

        vgap = v;
    
Methods Summary
public voidaddLayoutComponent(java.lang.String name, java.awt.Component comp)

    
public voidlayoutContainer(java.awt.Container parent)

        Insets insets = parent.getInsets();
        int maxWidth = parent.getSize().width
                       - (insets.left + insets.right);
        int maxHeight = parent.getSize().height
                        - (insets.top + insets.bottom);
        int nComps = parent.getComponentCount();
        int previousWidth = 0, previousHeight = 0;
        int x = 0, y = insets.top;
        int rowh = 0, start = 0;
        int xFudge = 0, yFudge = 0;
        boolean oneColumn = false;

        // Go through the components' sizes, if neither 
        // preferredLayoutSize nor minimumLayoutSize has 
        // been called.
        if (sizeUnknown) {
            setSizes(parent);
        }
            
        if (maxWidth <= minWidth) {
            oneColumn = true;
        }

        if (maxWidth != preferredWidth) {
            xFudge = (maxWidth - preferredWidth)/(nComps - 1);
        }

        if (maxHeight > preferredHeight) {
            yFudge = (maxHeight - preferredHeight)/(nComps - 1);
        }

        for (int i = 0 ; i < nComps ; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                Dimension d = c.getPreferredSize();
                
                 // increase x and y, if appropriate
                if (i > 0) { 
                    if (!oneColumn) {
                        x += previousWidth/2 + xFudge;
                    }
                    y += previousHeight + vgap + yFudge;
                }
                
                // If x is too large, 
                if ((!oneColumn) &&
                    (x + d.width) > 
                    (parent.getSize().width - insets.right)) {
                    // reduce x to a reasonable number.
                    x = parent.getSize().width 
                        - insets.bottom - d.width;
                }

                // If y is too large, 
                if ((y + d.height) 
                    > (parent.getSize().height - insets.bottom)) {
                    // do nothing.
                    // Another choice would be to do what we do to x.
                }

                // Set the component's size and position.
                c.setBounds(x, y, d.width, d.height);

                previousWidth = d.width;
                previousHeight = d.height;
            }
        }
    
public java.awt.DimensionminimumLayoutSize(java.awt.Container parent)

        Dimension dim = new Dimension(0, 0);
        int nComps = parent.getComponentCount();

        //Always add the container's insets!
        Insets insets = parent.getInsets();
        dim.width = minWidth 
                    + insets.left + insets.right;
        dim.height = minHeight 
                     + insets.top + insets.bottom;

        sizeUnknown = false;

        return dim;
    
public java.awt.DimensionpreferredLayoutSize(java.awt.Container parent)

        Dimension dim = new Dimension(0, 0);
        int nComps = parent.getComponentCount();

        setSizes(parent);

        //Always add the container's insets!
        Insets insets = parent.getInsets();
        dim.width = preferredWidth 
                    + insets.left + insets.right;
        dim.height = preferredHeight 
                     + insets.top + insets.bottom;

        sizeUnknown = false;

        return dim;
    
public voidremoveLayoutComponent(java.awt.Component comp)

    
private voidsetSizes(java.awt.Container parent)

        int nComps = parent.getComponentCount();
        Dimension d = null;

        //Reset preferred/minimum width and height.
        preferredWidth = 0;
        preferredHeight = 0;
        minWidth = 0;
        minHeight = 0;

        for (int i = 0; i < nComps; i++) {
            Component c = parent.getComponent(i);
            if (c.isVisible()) {
                d = c.getPreferredSize();

                if (i > 0) {
                    preferredWidth += d.width/2; 
                    preferredHeight += vgap;
                } else {
                    preferredWidth = d.width;
                }
                preferredHeight += d.height;

                minWidth = Math.max(c.getMinimumSize().width, 
                                    minWidth);
                minHeight = preferredHeight;
            }
        }
    
public java.lang.StringtoString()

        String str = "";
        return getClass().getName() + "[vgap=" + vgap + str + "]";