001 /* 002 * $Id: VerticalLayout.java,v 1.3 2005/10/10 18:01:47 rbair Exp $ 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 */ 021 package org.jdesktop.swingx; 022 023 import java.awt.Component; 024 import java.awt.Container; 025 import java.awt.Dimension; 026 import java.awt.Insets; 027 import java.awt.LayoutManager; 028 import java.awt.LayoutManager2; 029 import javax.swing.JPanel; 030 031 /** 032 * Organizes components in a vertical layout. 033 * 034 * @author fred 035 */ 036 public class VerticalLayout 037 implements LayoutManager2 038 { 039 private float alignX = Component.LEFT_ALIGNMENT; 040 private float alignY = Component.TOP_ALIGNMENT; 041 042 private int gap = 0; 043 044 public VerticalLayout() { 045 } 046 047 public VerticalLayout(int gap) { 048 this.gap = gap; 049 } 050 051 public int getGap() { 052 return gap; 053 } 054 055 public void setGap(int gap) { 056 this.gap = gap; 057 } 058 059 public void removeLayoutComponent(Component c) { 060 } 061 062 public void addLayoutComponent(Component comp, Object constraints) { 063 } 064 public void addLayoutComponent(String name, Component comp) { 065 } 066 067 public void layoutContainer(Container parent) { 068 Insets insets = parent.getInsets(); 069 Dimension size = parent.getSize(); 070 int width = size.width - insets.left - insets.right; 071 int height = insets.top; 072 for (int i = 0, c = parent.getComponentCount(); i < c; i++ ) { 073 Component m = parent.getComponent(i); 074 if (m.isVisible()) { 075 m.setBounds(insets.left, height, width, 076 m.getPreferredSize().height); 077 height += m.getSize().height + gap; 078 } 079 } 080 } 081 082 public Dimension preferredLayoutSize(Container parent) { 083 Insets insets = parent.getInsets(); 084 Dimension pref = new Dimension(0, 0); 085 for (int i = 0, c = parent.getComponentCount(); i < c; i++ ) { 086 Component m = parent.getComponent(i); 087 if (m.isVisible()) { 088 Dimension componentPreferredSize = parent.getComponent(i) 089 .getPreferredSize(); 090 pref.height += componentPreferredSize.height + gap; 091 pref.width = Math.max(pref.width, componentPreferredSize.width); 092 } 093 } 094 pref.width += insets.left + insets.right; 095 pref.height += insets.top + insets.bottom; 096 return pref; 097 } 098 099 public Dimension minimumLayoutSize(Container parent) { 100 return preferredLayoutSize(parent); 101 } 102 103 public Dimension maximumLayoutSize(Container target) { 104 return preferredLayoutSize(target); 105 } 106 107 public float getLayoutAlignmentX(Container target) { 108 return target.getAlignmentX(); 109 } 110 111 public float getLayoutAlignmentY(Container target) { 112 return target.getAlignmentY(); 113 } 114 115 public void invalidateLayout(Container target) { 116 } 117 118 }