Mercurial > hg > jslab
diff src/samer/core_/util/VLayout.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/VLayout.java Tue Jan 17 17:50:20 2012 +0000 @@ -0,0 +1,124 @@ +/* + * VLayout.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.*; + +/** + Things are layed out horizontally. Everyone gets to + be their preferred size, except for the filler, which + is stretched horizontally (but not vertically) to fill + the space available. The whole thing is centered vertically. + */ + +public class VLayout implements LayoutManager, java.io.Serializable +{ + private int gap=4; + private int filler=1; // index of filler component + private int fillerHeight=10; + + public VLayout( int f, int g) { gap=g; filler=f; } + public VLayout( int f) { gap=0; filler=f; } + + public void setGap(int a) { gap=a; } + public int getGap() { return gap; } + public void setFiller(int f) { filler=f; } + public int getFiller() { return filler; } + 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.width += d2.width; + d1.height = Math.max(d1.height,d2.height); + } + + if (n>1) d1.width += (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.width += d2.width; + d1.height = Math.max(d1.height,d2.height); + } + + if (n>1) d1.width += (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 xl, xr, y, havail, h; + Dimension p=parent.getSize(), d; + + havail = p.height - (insets.top+insets.bottom); + + int n=c.length; + + // first layout components left of filler + + xl = insets.left; + for (int i=0; i<filler && i<n; i++) { + d = c[i].getPreferredSize(); + if (d.height>havail) { h=havail; y=insets.bottom; } + else { h=d.height; y=(p.height-h)/2; } + c[i].setBounds(xl,y,d.width,h); + xl += d.width + gap; + } + // xl is now position of left hand edge of filler + + // next, layout components right of filler + // x is now position of RIGHTHAND edge + + xr = p.width-insets.right; + for (int i=n-1; i>filler && i>=0; i--) { + d = c[i].getPreferredSize(); + if (d.height>havail) { h=havail; y=insets.bottom; } + else { h=d.height; y=(p.height-h)/2; } + c[i].setBounds(xr-d.width,y,d.width,h); + xr -= d.width + gap; + } + // xr is now position of right hand edge of filler + + // now we can position the filler component + if (filler>=0 && filler<n) { + d = c[filler].getPreferredSize(); + if (d.height>havail) { h=havail; y=insets.bottom; } + else { h=d.height; y=(p.height-h)/2; } + c[filler].setBounds(xl,y,xr-xl,h); + } + } +} \ No newline at end of file