samer@0: /* samer@0: * VLayout.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util; samer@0: import java.awt.*; samer@0: samer@0: /** samer@0: Things are layed out horizontally. Everyone gets to samer@0: be their preferred size, except for the filler, which samer@0: is stretched horizontally (but not vertically) to fill samer@0: the space available. The whole thing is centered vertically. samer@0: */ samer@0: samer@0: public class VLayout implements LayoutManager, java.io.Serializable samer@0: { samer@0: private int gap=4; samer@0: private int filler=1; // index of filler component samer@0: private int fillerHeight=10; samer@0: samer@0: public VLayout( int f, int g) { gap=g; filler=f; } samer@0: public VLayout( int f) { gap=0; filler=f; } samer@0: samer@0: public void setGap(int a) { gap=a; } samer@0: public int getGap() { return gap; } samer@0: public void setFiller(int f) { filler=f; } samer@0: public int getFiller() { return filler; } samer@0: public void addLayoutComponent(String name, Component comp) {} samer@0: public void removeLayoutComponent(Component comp) {} samer@0: samer@0: public Dimension preferredLayoutSize(Container parent) samer@0: { samer@0: Component[] c=parent.getComponents(); samer@0: Dimension d1=new Dimension(0,0), d2; samer@0: samer@0: int n=c.length; samer@0: for (int i=0; i1) d1.width += (n-1)*gap; samer@0: samer@0: Insets insets=parent.getInsets(); samer@0: d1.width += insets.left + insets.right; samer@0: d1.height += insets.top + insets.bottom; samer@0: samer@0: return d1; samer@0: } samer@0: samer@0: public Dimension minimumLayoutSize(Container parent) samer@0: { samer@0: Component[] c=parent.getComponents(); samer@0: Dimension d1=new Dimension(0,0), d2; samer@0: samer@0: int n=c.length; samer@0: for (int i=0; i1) d1.width += (n-1)*gap; samer@0: samer@0: Insets insets=parent.getInsets(); samer@0: d1.width += insets.left + insets.right; samer@0: d1.height += insets.top + insets.bottom; samer@0: samer@0: return d1; samer@0: } samer@0: samer@0: public void layoutContainer(Container parent) samer@0: { samer@0: Component[] c=parent.getComponents(); samer@0: Insets insets=parent.getInsets(); samer@0: int xl, xr, y, havail, h; samer@0: Dimension p=parent.getSize(), d; samer@0: samer@0: havail = p.height - (insets.top+insets.bottom); samer@0: samer@0: int n=c.length; samer@0: samer@0: // first layout components left of filler samer@0: samer@0: xl = insets.left; samer@0: for (int i=0; ihavail) { h=havail; y=insets.bottom; } samer@0: else { h=d.height; y=(p.height-h)/2; } samer@0: c[i].setBounds(xl,y,d.width,h); samer@0: xl += d.width + gap; samer@0: } samer@0: // xl is now position of left hand edge of filler samer@0: samer@0: // next, layout components right of filler samer@0: // x is now position of RIGHTHAND edge samer@0: samer@0: xr = p.width-insets.right; samer@0: for (int i=n-1; i>filler && i>=0; i--) { samer@0: d = c[i].getPreferredSize(); samer@0: if (d.height>havail) { h=havail; y=insets.bottom; } samer@0: else { h=d.height; y=(p.height-h)/2; } samer@0: c[i].setBounds(xr-d.width,y,d.width,h); samer@0: xr -= d.width + gap; samer@0: } samer@0: // xr is now position of right hand edge of filler samer@0: samer@0: // now we can position the filler component samer@0: if (filler>=0 && fillerhavail) { h=havail; y=insets.bottom; } samer@0: else { h=d.height; y=(p.height-h)/2; } samer@0: c[filler].setBounds(xl,y,xr-xl,h); samer@0: } samer@0: } samer@0: }