001: package tools.tracesviewer;
002:
003: import java.awt.*;
004: import java.util.*;
005:
006: /**
007: * Lays out components within a Container such that each component takes a fixed percentage of the size.
008: *
009: * Each Component added to the Container must have a Constraint object that specifies what proportion
010: * of the container it will fill. The Component will be stretched to fill exactly that percentage.
011: *
012: * @see Constraint
013: */
014: public class PercentLayout implements LayoutManager2 {
015: public void addLayoutComponent(Component component,
016: Object constraint) {
017: if (constraint instanceof PercentLayoutConstraint) {
018: hash.put(component, constraint);
019: } else {
020: throw new IllegalArgumentException("Invalid constraint");
021:
022: }
023: }
024:
025: public void addLayoutComponent(String constraint, Component comp) {
026: throw new IllegalArgumentException("Invalid constraint");
027: }
028:
029: public void removeLayoutComponent(Component component) {
030: hash.remove(component);
031: }
032:
033: public Dimension preferredLayoutSize(Container p1) {
034: int prefx = 0;
035: int prefy = 0;
036:
037: Enumeration keys = hash.keys();
038: while (keys.hasMoreElements()) {
039: Component comp = (Component) keys.nextElement();
040: PercentLayoutConstraint constraint = (PercentLayoutConstraint) hash
041: .get(comp);
042: Dimension pref = comp.getPreferredSize();
043: prefx += pref.width * 100 / constraint.width;
044: prefy += pref.height * 100 / constraint.height;
045: }
046: int n = hash.size();
047: return new Dimension(prefx / n, prefy / n);
048: }
049:
050: public Dimension minimumLayoutSize(Container p1) {
051: int minx = 0;
052: int miny = 0;
053:
054: Enumeration keys = hash.keys();
055: while (keys.hasMoreElements()) {
056: Component comp = (Component) keys.nextElement();
057: PercentLayoutConstraint constraint = (PercentLayoutConstraint) hash
058: .get(comp);
059: Dimension min = comp.getMinimumSize();
060: int mx = (int) (min.width * 100 / constraint.width);
061: int my = (int) (min.height * 100 / constraint.height);
062: if (mx > minx)
063: minx = mx;
064: if (my > miny)
065: miny = my;
066: }
067: return new Dimension(minx, miny);
068: }
069:
070: public Dimension maximumLayoutSize(Container p1) {
071: int maxx = Integer.MAX_VALUE;
072: int maxy = Integer.MAX_VALUE;
073:
074: Enumeration keys = hash.keys();
075: while (keys.hasMoreElements()) {
076: Component comp = (Component) keys.nextElement();
077: PercentLayoutConstraint constraint = (PercentLayoutConstraint) hash
078: .get(comp);
079: Dimension max = comp.getMaximumSize();
080: int mx = max.width == Integer.MAX_VALUE ? max.width
081: : (int) (max.width * 100 / constraint.width);
082: int my = max.height == Integer.MAX_VALUE ? max.height
083: : (int) (max.height * 100 / constraint.height);
084: if (mx < maxx)
085: maxx = mx;
086: if (my < maxy)
087: maxy = my;
088: }
089: return new Dimension(maxx, maxy);
090: }
091:
092: public void layoutContainer(Container p1) {
093: Dimension size = p1.getSize();
094: Enumeration keys = hash.keys();
095: while (keys.hasMoreElements()) {
096: Component comp = (Component) keys.nextElement();
097: PercentLayoutConstraint constraint = (PercentLayoutConstraint) hash
098: .get(comp);
099: int x = (int) (size.width * constraint.x / 100);
100: int y = (int) (size.height * constraint.y / 100);
101: int width = (int) (size.width * constraint.width / 100);
102: int height = (int) (size.height * constraint.height / 100);
103: comp.setBounds(x, y, width, height);
104: }
105: }
106:
107: public void invalidateLayout(Container p1) {
108: }
109:
110: public float getLayoutAlignmentY(Container p1) {
111: return 0.5f;
112: }
113:
114: public float getLayoutAlignmentX(Container p1) {
115: return 0.5f;
116: }
117:
118: private Hashtable hash = new Hashtable();
119:
120: }
|