001    /*
002     * $Id: HorizontalLayout.java,v 1.1 2006/04/21 05:38:51 gfx Exp $
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    
022    package org.jdesktop.swingx;
023    
024    import java.awt.Component;
025    import java.awt.Container;
026    import java.awt.Dimension;
027    import java.awt.Insets;
028    import java.awt.LayoutManager;
029    
030    /**
031     * Organizes components in a horizontal layout.
032     *
033     * @author Romain Guy <romain.guy@mac.com>
034     */
035    public class HorizontalLayout implements LayoutManager {
036        private int gap = 0;
037    
038        public HorizontalLayout() {
039        }
040    
041        public HorizontalLayout(int gap) {
042            this.gap = gap;
043        }
044    
045        public int getGap() {
046            return gap;
047        }
048    
049        public void setGap(int gap) {
050            this.gap = gap;
051        }
052    
053        public void addLayoutComponent(String name, Component c) {
054        }
055    
056        public void layoutContainer(Container parent) {
057            Insets insets = parent.getInsets();
058            Dimension size = parent.getSize();
059            int height = size.height - insets.top - insets.bottom;
060            int width = insets.left;
061            for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
062                Component m = parent.getComponent(i);
063                if (m.isVisible()) {
064                    m.setBounds(width, insets.top,
065                                m.getPreferredSize().width, height);
066                    width += m.getSize().width + gap;
067                }
068            }
069        }
070    
071        public Dimension minimumLayoutSize(Container parent) {
072            return preferredLayoutSize(parent);
073        }
074    
075        public Dimension preferredLayoutSize(Container parent) {
076            Insets insets = parent.getInsets();
077            Dimension pref = new Dimension(0, 0);
078            for (int i = 0, c = parent.getComponentCount(); i < c; i++) {
079                Component m = parent.getComponent(i);
080                if (m.isVisible()) {
081                    Dimension componentPreferredSize =
082                            parent.getComponent(i).getPreferredSize();
083                    pref.height += Math.max(pref.height, componentPreferredSize.height);
084                    pref.width = componentPreferredSize.width + gap;
085                }
086            }
087            pref.width += insets.left + insets.right;
088            pref.height += insets.top + insets.bottom;
089            return pref;
090        }
091    
092        public void removeLayoutComponent(Component c) {
093        }
094    }