001: package org.enhydra.tool.swing.layout;
002:
003: import java.awt.Component;
004: import java.awt.Container;
005: import java.awt.Dimension;
006: import java.awt.Insets;
007: import java.awt.LayoutManager2;
008: import java.awt.Rectangle;
009: import java.io.Serializable;
010: import java.util.Hashtable;
011:
012: public class XYLayout implements LayoutManager2, Serializable {
013:
014: private static final long serialVersionUID = 200L;
015: int width;
016: int height;
017: Hashtable info;
018: static final XYConstraints defaultConstraints = new XYConstraints();
019:
020: public XYLayout() {
021: info = new Hashtable();
022: }
023:
024: public XYLayout(int width, int height) {
025: info = new Hashtable();
026: this .width = width;
027: this .height = height;
028: }
029:
030: public int getWidth() {
031: return width;
032: }
033:
034: public void setWidth(int width) {
035: this .width = width;
036: }
037:
038: public int getHeight() {
039: return height;
040: }
041:
042: public void setHeight(int height) {
043: this .height = height;
044: }
045:
046: public String toString() {
047: return String.valueOf(String.valueOf((new StringBuffer(
048: "XYLayout[width=")).append(width).append(",height=")
049: .append(height).append("]")));
050: }
051:
052: public void addLayoutComponent(String s, Component component1) {
053: }
054:
055: public void removeLayoutComponent(Component component) {
056: info.remove(component);
057: }
058:
059: public Dimension preferredLayoutSize(Container target) {
060: return getLayoutSize(target, true);
061: }
062:
063: public Dimension minimumLayoutSize(Container target) {
064: return getLayoutSize(target, false);
065: }
066:
067: public void layoutContainer(Container target) {
068: Insets insets = target.getInsets();
069: int count = target.getComponentCount();
070: for (int i = 0; i < count; i++) {
071: Component component = target.getComponent(i);
072: if (component.isVisible()) {
073: Rectangle r = getComponentBounds(component, true);
074: component.setBounds(insets.left + r.x,
075: insets.top + r.y, r.width, r.height);
076: }
077: }
078:
079: }
080:
081: public void addLayoutComponent(Component component,
082: Object constraints) {
083: if (constraints instanceof XYConstraints)
084: info.put(component, constraints);
085: }
086:
087: public Dimension maximumLayoutSize(Container target) {
088: return new Dimension(0x7fffffff, 0x7fffffff);
089: }
090:
091: public float getLayoutAlignmentX(Container target) {
092: return 0.5F;
093: }
094:
095: public float getLayoutAlignmentY(Container target) {
096: return 0.5F;
097: }
098:
099: public void invalidateLayout(Container container) {
100: }
101:
102: Rectangle getComponentBounds(Component component,
103: boolean doPreferred) {
104: XYConstraints constraints = (XYConstraints) info.get(component);
105: if (constraints == null)
106: constraints = defaultConstraints;
107: Rectangle r = new Rectangle(constraints.x, constraints.y,
108: constraints.width, constraints.height);
109: if (r.width <= 0 || r.height <= 0) {
110: Dimension d = doPreferred ? component.getPreferredSize()
111: : component.getMinimumSize();
112: if (r.width <= 0)
113: r.width = d.width;
114: if (r.height <= 0)
115: r.height = d.height;
116: }
117: return r;
118: }
119:
120: Dimension getLayoutSize(Container target, boolean doPreferred) {
121: Dimension dim = new Dimension(0, 0);
122: if (width <= 0 || height <= 0) {
123: int count = target.getComponentCount();
124: for (int i = 0; i < count; i++) {
125: Component component = target.getComponent(i);
126: if (component.isVisible()) {
127: Rectangle r = getComponentBounds(component,
128: doPreferred);
129: dim.width = Math.max(dim.width, r.x + r.width);
130: dim.height = Math.max(dim.height, r.y + r.height);
131: }
132: }
133:
134: }
135: if (width > 0)
136: dim.width = width;
137: if (height > 0)
138: dim.height = height;
139: Insets insets = target.getInsets();
140: dim.width += insets.left + insets.right;
141: dim.height += insets.top + insets.bottom;
142: return dim;
143: }
144:
145: }
|