alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.form.util;

import java.awt.*;

/**
 * Simple layout managers which arrange components in a column. Components can
 * be left-aligned, horizontally center-aligned, right-aligned or stretched
 * horizontally to fill the available space
 * @author Tran Duc Trung
 */

public class ColumnLayout implements LayoutManager
{
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int CENTER = 3;
    public static final int FILL = 4;

    private int _align;
    private int _gap;

    public ColumnLayout() {
        this(0);
    }

    public ColumnLayout(int gap) {
        this(gap, FILL);
    }

    public ColumnLayout(int gap, int align) {
        _gap = gap;
        _align = align;
    }

    public void addLayoutComponent(String name, Component comp) {}

    public void removeLayoutComponent(Component comp) {}

    public Dimension preferredLayoutSize(Container parent) {
        Dimension size = new Dimension();

        synchronized(parent.getTreeLock()) {
            Component[] comps = parent.getComponents();

            for (int i = 0; i < comps.length; i++) {
                Dimension d;
                d = comps[i].getPreferredSize();
                size.height += d.height;
                size.width = Math.max(size.width, d.width);
                if (i < comps.length - 1)
                    size.height += _gap;
            }

            Insets insets = parent.getInsets();
            size.width += insets.left + insets.right;
            size.height += insets.top + insets.bottom;
        }

        return size;
    }

    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }

    public void layoutContainer(Container parent) {
        synchronized(parent.getTreeLock()) {
            Component[] comps = parent.getComponents();
            Insets insets = parent.getInsets();
            Dimension size = parent.getSize();

            int y = insets.top;
            int width = size.width - insets.left - insets.right;

            for (int i = 0; i < comps.length; i++) {
                Component c = comps[i];
                Dimension d = c.getPreferredSize();

                int ofs = 0;
                int w = d.width;

                switch (_align) {
                    case LEFT:
                        ofs = 0;
                        break;
                    case RIGHT:
                        ofs = width - d.width;
                        break;

                    case CENTER:
                        ofs =(width - d.width) / 2;
                        break;

                    case FILL:
                        ofs = 0;
                        break;
                }

                c.setBounds(insets.left + ofs, y, w, d.height);
                y += d.height + _gap;
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.