package jas.util.layout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.LayoutManager2; import java.util.Enumeration; import java.util.Hashtable; /** * Lays out components within a Container such that each component takes a fixed percentage of the size. * * Each Component added to the Container must have a Constraint object that specifies what proportion * of the container it will fill. The Component will be stretched to fill exactly that percentage. * * @see Constraint */ public class PercentLayout implements LayoutManager2 { public void addLayoutComponent(Component component, Object constraint) { if (constraint instanceof Constraint) { hash.put(component,constraint); } else throw new IllegalArgumentException("Invalid constraint"); } public void addLayoutComponent(String constraint, Component comp) { throw new IllegalArgumentException("Invalid constraint"); } public void removeLayoutComponent(Component component) { hash.remove(component); } public Dimension preferredLayoutSize(Container p1) { int prefx=0; int prefy=0; Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Component comp = (Component) keys.nextElement(); Constraint constraint = (Constraint) hash.get(comp); Dimension pref = comp.getPreferredSize(); prefx += pref.width * 100/constraint.width; prefy += pref.height * 100/constraint.height; } int n = hash.size(); return new Dimension(prefx/n,prefy/n); } public Dimension minimumLayoutSize(Container p1) { int minx=0; int miny=0; Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Component comp = (Component) keys.nextElement(); Constraint constraint = (Constraint) hash.get(comp); Dimension min = comp.getMinimumSize(); int mx = (int) (min.width * 100/constraint.width); int my = (int) (min.height * 100/constraint.height); if (mx>minx) minx=mx; if (my>miny) miny=my; } return new Dimension(minx,miny); } public Dimension maximumLayoutSize(Container p1) { int maxx=Integer.MAX_VALUE; int maxy=Integer.MAX_VALUE; Enumeration keys = hash.keys(); while (keys.hasMoreElements()) { Component comp = (Component) keys.nextElement(); Constraint constraint = (Constraint) hash.get(comp); Dimension max = comp.getMaximumSize(); int mx = max.width==Integer.MAX_VALUE ? max.width : (int) (max.width * 100/constraint.width); int my = max.height==Integer.MAX_VALUE ? max.height : (int) (max.height * 100/constraint.height); if (mx