1 package org.enhydra.dods.wizard; 2 3 import java.awt.Component ; 4 import java.awt.Container ; 5 import java.awt.Dimension ; 6 import java.awt.Insets ; 7 import java.awt.LayoutManager2 ; 8 import java.awt.Rectangle ; 9 import java.io.Serializable ; 10 import java.util.Hashtable ; 11 12 public class XYLayout 13 implements LayoutManager2 , Serializable { 14 private static final long serialVersionUID = 200L; 15 int width; 16 int height; 17 Hashtable info; 18 static final XYConstraints defaultConstraints = new XYConstraints(); 19 public XYLayout() { 20 info = new Hashtable (); 21 } 22 23 public XYLayout(int width, int height) { 24 info = new Hashtable (); 25 this.width = width; 26 this.height = height; 27 } 28 29 public int getWidth() { 30 return width; 31 } 32 33 public void setWidth(int width) { 34 this.width = width; 35 } 36 37 public int getHeight() { 38 return height; 39 } 40 41 public void setHeight(int height) { 42 this.height = height; 43 } 44 45 public String toString() { 46 return String.valueOf(String.valueOf((new StringBuffer ("XYLayout[width=")).append(width).append(",height=").append(height).append("]"))); 47 } 48 49 public void addLayoutComponent(String s, Component component1) {} 50 51 public void removeLayoutComponent(Component component) { 52 info.remove(component); 53 } 54 55 public Dimension preferredLayoutSize(Container target) { 56 return getLayoutSize(target, true); 57 } 58 59 public Dimension minimumLayoutSize(Container target) { 60 return getLayoutSize(target, false); 61 } 62 63 public void layoutContainer(Container target) { 64 Insets insets = target.getInsets(); 65 int count = target.getComponentCount(); 66 67 for (int i = 0; i < count; i++) { 68 Component component = target.getComponent(i); 69 70 if (component.isVisible()) { 71 Rectangle r = getComponentBounds(component, true); 72 73 component.setBounds(insets.left + r.x, insets.top + r.y, r.width, 74 r.height); 75 } 76 } 77 } 78 79 public void addLayoutComponent(Component component, Object constraints) { 80 if (constraints instanceof XYConstraints) { 81 info.put(component, constraints); 82 } 83 } 84 85 public Dimension maximumLayoutSize(Container target) { 86 return new Dimension (0x7fffffff, 0x7fffffff); 87 } 88 89 public float getLayoutAlignmentX(Container target) { 90 return 0.5F; 91 } 92 93 public float getLayoutAlignmentY(Container target) { 94 return 0.5F; 95 } 96 97 public void invalidateLayout(Container container) {} 98 99 Rectangle getComponentBounds(Component component, boolean doPreferred) { 100 XYConstraints constraints = (XYConstraints) info.get(component); 101 102 if (constraints == null) { 103 constraints = defaultConstraints; 104 } 105 Rectangle r = new Rectangle (constraints.x, constraints.y, 106 constraints.width, constraints.height); 107 108 if (r.width <= 0 || r.height <= 0) { 109 Dimension d = doPreferred 110 ? component.getPreferredSize() 111 : component.getMinimumSize(); 112 113 if (r.width <= 0) { 114 r.width = d.width; 115 } 116 if (r.height <= 0) { 117 r.height = d.height; 118 } 119 } 120 return r; 121 } 122 123 Dimension getLayoutSize(Container target, boolean doPreferred) { 124 Dimension dim = new Dimension (0, 0); 125 126 if (width <= 0 || height <= 0) { 127 int count = target.getComponentCount(); 128 129 for (int i = 0; i < count; i++) { 130 Component component = target.getComponent(i); 131 132 if (component.isVisible()) { 133 Rectangle r = getComponentBounds(component, doPreferred); 134 135 dim.width = Math.max(dim.width, r.x + r.width); 136 dim.height = Math.max(dim.height, r.y + r.height); 137 } 138 } 139 } 140 if (width > 0) { 141 dim.width = width; 142 } 143 if (height > 0) { 144 dim.height = height; 145 } 146 Insets insets = target.getInsets(); 147 148 dim.width += insets.left + insets.right; 149 dim.height += insets.top + insets.bottom; 150 return dim; 151 } 152 } 153 | Popular Tags |