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