001/*
002 * $Id: VerticalLayout.java 4147 2012-02-01 17:13:24Z kschaefe $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx;
022
023import java.awt.Component;
024import java.awt.Container;
025import java.awt.Dimension;
026import java.awt.Insets;
027
028import org.jdesktop.beans.JavaBean;
029import org.jdesktop.swingx.util.Separator;
030
031/**
032 * Organizes components in a vertical layout.
033 * 
034 * @author fred
035 * @author Karl Schaefer
036 */
037@JavaBean
038public class VerticalLayout extends AbstractLayoutManager {
039    private static final long serialVersionUID = 5342270033773736441L;
040    
041    private int gap;
042
043    /**
044     * Creates a layout without a gap between components.
045     */
046    public VerticalLayout() {
047        this(0);
048    }
049
050    /**
051     * Creates a layout with the specified gap between components.
052     * 
053     * @param gap
054     *            the gap between components
055     */
056    //TODO should we allow negative gaps?
057    public VerticalLayout(int gap) {
058        this.gap = gap;
059    }
060
061    /**
062     * The current gap to place between components.
063     * 
064     * @return the current gap
065     */
066    public int getGap() {
067        return gap;
068    }
069
070    /**
071     * The new gap to place between components.
072     * 
073     * @param gap
074     *            the new gap
075     */
076    //TODO should we allow negative gaps?
077    public void setGap(int gap) {
078        this.gap = gap;
079    }
080    
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public Dimension preferredLayoutSize(Container parent) {
086        Dimension pref = new Dimension(0, 0);
087        Separator<Integer> sep = new Separator<Integer>(0, gap);
088        
089        for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
090            Component m = parent.getComponent(i);
091            
092            if (m.isVisible()) {
093                Dimension componentPreferredSize = parent.getComponent(i).getPreferredSize();
094                pref.height += componentPreferredSize.height + sep.get();
095                pref.width = Math.max(pref.width, componentPreferredSize.width);
096            }
097        }
098        
099        Insets insets = parent.getInsets();
100        pref.width += insets.left + insets.right;
101        pref.height += insets.top + insets.bottom;
102        
103        return pref;
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public void layoutContainer(Container parent) {
111        Insets insets = parent.getInsets();
112        Dimension size = parent.getSize();
113        int width = size.width - insets.left - insets.right;
114        int height = insets.top;
115
116        for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
117            Component m = parent.getComponent(i);
118            if (m.isVisible()) {
119                m.setBounds(insets.left, height, width, m.getPreferredSize().height);
120                height += m.getSize().height + gap;
121            }
122        }
123    }
124}