Mercurial > hg > jslab
diff src/samer/core_/util/StackLayout.java @ 0:bf79fb79ee13
Initial Mercurial check in.
author | samer |
---|---|
date | Tue, 17 Jan 2012 17:50:20 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/samer/core_/util/StackLayout.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,95 @@ +/* + * StackLayout.java + * + * Copyright (c) 2000, Samer Abdallah, King's College London. + * All rights reserved. + * + * This software is provided AS iS and WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + */ + +package samer.core.util; +import java.awt.*; + +/** + This layout manager stacks everything up vertically + letting each component assume it's preferred height, + but stretching them all out to fill the width available. + The preferred width is the height of the widest + component + */ + +public class StackLayout implements LayoutManager, java.io.Serializable +{ + private int gap; + + public StackLayout() { gap=0; } + public StackLayout(int a) { gap=a; } + public void setGap(int a) { gap=a; } + public int getGap(int a) { return gap; } + + public void addLayoutComponent(String name, Component comp) {} + public void removeLayoutComponent(Component comp) {} + + public Dimension preferredLayoutSize(Container parent) + { + Component[] c=parent.getComponents(); + Dimension d1=new Dimension(0,0), d2; + + int n=c.length; + for (int i=0; i<n; i++) { + d2 = c[i].getPreferredSize(); + d1.height += d2.height; + d1.width = Math.max(d1.width,d2.width); + } + + if (n>1) d1.height += (n-1)*gap; + + Insets insets=parent.getInsets(); + d1.width += insets.left + insets.right; + d1.height += insets.top + insets.bottom; + + return d1; + } + + public Dimension minimumLayoutSize(Container parent) + { + Component[] c=parent.getComponents(); + Dimension d1=new Dimension(0,0), d2; + + int n=c.length; + for (int i=0; i<n; i++) { + d2 = c[i].getMinimumSize(); + d1.height += d2.height; + d1.width = Math.max(d1.width,d2.width); + } + + if (n>1) d1.height += (n-1)*gap; + + Insets insets=parent.getInsets(); + d1.width += insets.left + insets.right; + d1.height += insets.top + insets.bottom; + + return d1; + } + + public void layoutContainer(Container parent) + { + Component[] c=parent.getComponents(); + Insets insets=parent.getInsets(); + int x, y, w; + Dimension p=parent.getSize(), d; + + w = p.width - (insets.left+insets.right); + x = insets.left; + y = insets.top; + + int n=c.length; + for (int i=0; i<n; i++) { + d = c[i].getPreferredSize(); + c[i].setBounds(x,y,w,d.height); + y += d.height + gap; + } + } +} \ No newline at end of file