001/*
002 * $Id: HorizontalLayout.java 4147 2012-02-01 17:13:24Z kschaefe $
003 *
004 * Copyright 2006 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 */
021
022package org.jdesktop.swingx;
023
024import java.awt.Component;
025import java.awt.Container;
026import java.awt.Dimension;
027import java.awt.Insets;
028
029import org.jdesktop.beans.JavaBean;
030import org.jdesktop.swingx.util.Separator;
031
032/**
033 * Organizes components in a horizontal layout.
034 *
035 * @author Romain Guy <romain.guy@mac.com>
036 * @author Karl Schaefer
037 */
038@JavaBean
039public class HorizontalLayout extends AbstractLayoutManager {
040    private static final long serialVersionUID = 8640046926840737487L;
041    
042    private int gap;
043
044    public HorizontalLayout() {
045        this(0);
046    }
047
048    //TODO should we allow negative gaps?
049    public HorizontalLayout(int gap) {
050        this.gap = gap;
051    }
052
053    public int getGap() {
054        return gap;
055    }
056
057    //TODO should we allow negative gaps?
058    public void setGap(int gap) {
059        this.gap = gap;
060    }
061
062    @Override
063    public void layoutContainer(Container parent) {
064        Insets insets = parent.getInsets();
065        Dimension size = parent.getSize();
066        
067        int height = size.height - insets.top - insets.bottom;
068        int width = insets.left;
069        
070        for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
071            Component m = parent.getComponent(i);
072            
073            if (m.isVisible()) {
074                m.setBounds(width, insets.top, m.getPreferredSize().width, height);
075                width += m.getSize().width + gap;
076            }
077        }
078    }
079
080    @Override
081    public Dimension preferredLayoutSize(Container parent) {
082        Dimension pref = new Dimension(0, 0);
083        Separator<Integer> sep = new Separator<Integer>(0, gap);
084
085        for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
086            Component m = parent.getComponent(i);
087            if (m.isVisible()) {
088                Dimension componentPreferredSize =
089                        parent.getComponent(i).getPreferredSize();
090                pref.height = Math.max(pref.height, componentPreferredSize.height);
091                pref.width += componentPreferredSize.width + sep.get();
092            }
093        }
094        
095        Insets insets = parent.getInsets();
096        pref.width += insets.left + insets.right;
097        pref.height += insets.top + insets.bottom;
098        
099        return pref;
100    }
101}