import java.awt.*;
import java.util.*;
public class AbsoluteLayout implements LayoutManager2 {
protected Vector components = new Vector();
public void addLayoutComponent(final String name, final Component component) {
addLayoutComponent(component, null);
}
public void addLayoutComponent(final Component component, final Object constraints) {
if (component == null) {
return;
}
components.add(component);
}
public void removeLayoutComponent(final Component component) {
if (component != null) {
components.remove(component);
}
}
public Dimension minimumLayoutSize(final Container parent) {
return preferredLayoutSize(parent);
}
public Dimension preferredLayoutSize(final Container parent) {
Rectangle limits = null;
Rectangle bounds = null;
for (int i = 0; i < components.size(); i++) {
Component c = (Component) components.get(i);
bounds = getPreferredBounds(c);
if (limits == null) {
limits = bounds;
} else {
limits = limits.union(bounds);
}
}
if (limits == null) {
return new Dimension();
}
Insets insets = parent.getInsets();
if (insets == null) {
insets = new Insets(0, 0, 0, 0);
}
int w = limits.width + insets.left + insets.right;
int h = limits.height + insets.top + insets.bottom;
return new Dimension(w, h);
}
public Dimension maximumLayoutSize(final Container parent) {
return preferredLayoutSize(parent);
}
public void layoutContainer(final Container parent) {
Insets insets = parent.getInsets();
if (insets == null) {
insets = new Insets(0, 0, 0, 0);
}
int a = insets.left;
int b = insets.top;
for (int i = 0; i < components.size(); i++) {
Component c = (Component) components.get(i);
if (c == null) {
continue;
}
Rectangle bounds = getPreferredBounds(c);
bounds.x += a;
bounds.y += b;
c.setBounds(bounds);
}
}
public float getLayoutAlignmentY(final Container target) {
return 0.5f;
}
public float getLayoutAlignmentX(final Container target) {
return 0.5f;
}
public void invalidateLayout(final Container parent) {
parent.repaint();
}
public static Rectangle getPreferredBounds(final Component c) {
if (c == null) {
return new Rectangle();
}
Point corner = c.getLocation();
Rectangle bounds = c.getBounds();
bounds.x = corner.x;
bounds.y = corner.y;
Dimension d;
Dimension e;
if (c instanceof Container) {
Container x = (Container) c;
d = x.getPreferredSize();
bounds.width = d.width;
bounds.height = d.height;
} else {
d = c.getMinimumSize();
e = c.getSize();
bounds.width = Math.max(d.width, e.width);
bounds.height = Math.max(d.height, e.height);
}
return bounds;
}
}