home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- public class BorderLayout implements LayoutManager {
- int hgap;
- int vgap;
- Component north;
- Component west;
- Component east;
- Component south;
- Component center;
-
- public BorderLayout() {
- }
-
- public BorderLayout(int hgap, int vgap) {
- this.hgap = hgap;
- this.vgap = vgap;
- }
-
- public void addLayoutComponent(String name, Component comp) {
- if ("Center".equals(name)) {
- this.center = comp;
- } else if ("North".equals(name)) {
- this.north = comp;
- } else if ("South".equals(name)) {
- this.south = comp;
- } else if ("East".equals(name)) {
- this.east = comp;
- } else {
- if ("West".equals(name)) {
- this.west = comp;
- }
-
- }
- }
-
- public void removeLayoutComponent(Component comp) {
- if (comp == this.center) {
- this.center = null;
- } else if (comp == this.north) {
- this.north = null;
- } else if (comp == this.south) {
- this.south = null;
- } else if (comp == this.east) {
- this.east = null;
- } else {
- if (comp == this.west) {
- this.west = null;
- }
-
- }
- }
-
- public Dimension minimumLayoutSize(Container target) {
- Dimension dim = new Dimension(0, 0);
- if (this.east != null && this.east.visible) {
- Dimension d = this.east.minimumSize();
- dim.width += d.width + this.hgap;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.west != null && this.west.visible) {
- Dimension d = this.west.minimumSize();
- dim.width += d.width + this.hgap;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.center != null && this.center.visible) {
- Dimension d = this.center.minimumSize();
- dim.width += d.width;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.north != null && this.north.visible) {
- Dimension d = this.north.minimumSize();
- dim.width = Math.max(d.width, dim.width);
- dim.height += d.height + this.vgap;
- }
-
- if (this.south != null && this.south.visible) {
- Dimension d = this.south.minimumSize();
- dim.width = Math.max(d.width, dim.width);
- dim.height += d.height + this.vgap;
- }
-
- Insets insets = target.insets();
- dim.width += insets.left + insets.right;
- dim.height += insets.top + insets.bottom;
- return dim;
- }
-
- public Dimension preferredLayoutSize(Container target) {
- Dimension dim = new Dimension(0, 0);
- if (this.east != null && this.east.visible) {
- Dimension d = this.east.preferredSize();
- dim.width += d.width + this.hgap;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.west != null && this.west.visible) {
- Dimension d = this.west.preferredSize();
- dim.width += d.width + this.hgap;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.center != null && this.center.visible) {
- Dimension d = this.center.preferredSize();
- dim.width += d.width;
- dim.height = Math.max(d.height, dim.height);
- }
-
- if (this.north != null && this.north.visible) {
- Dimension d = this.north.preferredSize();
- dim.width = Math.max(d.width, dim.width);
- dim.height += d.height + this.vgap;
- }
-
- if (this.south != null && this.south.visible) {
- Dimension d = this.south.preferredSize();
- dim.width = Math.max(d.width, dim.width);
- dim.height += d.height + this.vgap;
- }
-
- Insets insets = target.insets();
- dim.width += insets.left + insets.right;
- dim.height += insets.top + insets.bottom;
- return dim;
- }
-
- public void layoutContainer(Container target) {
- Insets insets = target.insets();
- int top = insets.top;
- int bottom = target.height - insets.bottom;
- int left = insets.left;
- int right = target.width - insets.right;
- if (this.north != null && this.north.visible) {
- this.north.resize(right - left, this.north.height);
- Dimension d = this.north.preferredSize();
- this.north.reshape(left, top, right - left, d.height);
- top += d.height + this.vgap;
- }
-
- if (this.south != null && this.south.visible) {
- this.south.resize(right - left, this.south.height);
- Dimension d = this.south.preferredSize();
- this.south.reshape(left, bottom - d.height, right - left, d.height);
- bottom -= d.height + this.vgap;
- }
-
- if (this.east != null && this.east.visible) {
- this.east.resize(this.east.width, bottom - top);
- Dimension d = this.east.preferredSize();
- this.east.reshape(right - d.width, top, d.width, bottom - top);
- right -= d.width + this.hgap;
- }
-
- if (this.west != null && this.west.visible) {
- this.west.resize(this.west.width, bottom - top);
- Dimension d = this.west.preferredSize();
- this.west.reshape(left, top, d.width, bottom - top);
- left += d.width + this.hgap;
- }
-
- if (this.center != null && this.center.visible) {
- this.center.reshape(left, top, right - left, bottom - top);
- }
-
- }
-
- public String toString() {
- return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + "]";
- }
- }
-