1 7 package javax.swing; 8 9 10 import java.awt.*; 11 import java.io.Serializable ; 12 13 36 public class OverlayLayout implements LayoutManager2,Serializable { 37 38 45 public OverlayLayout(Container target) { 46 this.target = target; 47 } 48 49 55 public void invalidateLayout(Container target) { 56 checkContainer(target); 57 xChildren = null; 58 yChildren = null; 59 xTotal = null; 60 yTotal = null; 61 } 62 63 70 public void addLayoutComponent(String name, Component comp) { 71 invalidateLayout(comp.getParent()); 72 } 73 74 80 public void removeLayoutComponent(Component comp) { 81 invalidateLayout(comp.getParent()); 82 } 83 84 92 public void addLayoutComponent(Component comp, Object constraints) { 93 invalidateLayout(comp.getParent()); 94 } 95 96 106 public Dimension preferredLayoutSize(Container target) { 107 checkContainer(target); 108 checkRequests(); 109 110 Dimension size = new Dimension(xTotal.preferred, yTotal.preferred); 111 Insets insets = target.getInsets(); 112 size.width += insets.left + insets.right; 113 size.height += insets.top + insets.bottom; 114 return size; 115 } 116 117 126 public Dimension minimumLayoutSize(Container target) { 127 checkContainer(target); 128 checkRequests(); 129 130 Dimension size = new Dimension(xTotal.minimum, yTotal.minimum); 131 Insets insets = target.getInsets(); 132 size.width += insets.left + insets.right; 133 size.height += insets.top + insets.bottom; 134 return size; 135 } 136 137 148 public Dimension maximumLayoutSize(Container target) { 149 checkContainer(target); 150 checkRequests(); 151 152 Dimension size = new Dimension(xTotal.maximum, yTotal.maximum); 153 Insets insets = target.getInsets(); 154 size.width += insets.left + insets.right; 155 size.height += insets.top + insets.bottom; 156 return size; 157 } 158 159 165 public float getLayoutAlignmentX(Container target) { 166 checkContainer(target); 167 checkRequests(); 168 return xTotal.alignment; 169 } 170 171 177 public float getLayoutAlignmentY(Container target) { 178 checkContainer(target); 179 checkRequests(); 180 return yTotal.alignment; 181 } 182 183 191 public void layoutContainer(Container target) { 192 checkContainer(target); 193 checkRequests(); 194 195 int nChildren = target.getComponentCount(); 196 int[] xOffsets = new int[nChildren]; 197 int[] xSpans = new int[nChildren]; 198 int[] yOffsets = new int[nChildren]; 199 int[] ySpans = new int[nChildren]; 200 201 Dimension alloc = target.getSize(); 203 Insets in = target.getInsets(); 204 alloc.width -= in.left + in.right; 205 alloc.height -= in.top + in.bottom; 206 SizeRequirements.calculateAlignedPositions(alloc.width, xTotal, 207 xChildren, xOffsets, 208 xSpans); 209 SizeRequirements.calculateAlignedPositions(alloc.height, yTotal, 210 yChildren, yOffsets, 211 ySpans); 212 213 for (int i = 0; i < nChildren; i++) { 215 Component c = target.getComponent(i); 216 c.setBounds(in.left + xOffsets[i], in.top + yOffsets[i], 217 xSpans[i], ySpans[i]); 218 } 219 } 220 221 void checkContainer(Container target) { 222 if (this.target != target) { 223 throw new AWTError("OverlayLayout can't be shared"); 224 } 225 } 226 227 void checkRequests() { 228 if (xChildren == null || yChildren == null) { 229 int n = target.getComponentCount(); 232 xChildren = new SizeRequirements [n]; 233 yChildren = new SizeRequirements [n]; 234 for (int i = 0; i < n; i++) { 235 Component c = target.getComponent(i); 236 Dimension min = c.getMinimumSize(); 237 Dimension typ = c.getPreferredSize(); 238 Dimension max = c.getMaximumSize(); 239 xChildren[i] = new SizeRequirements (min.width, typ.width, 240 max.width, 241 c.getAlignmentX()); 242 yChildren[i] = new SizeRequirements (min.height, typ.height, 243 max.height, 244 c.getAlignmentY()); 245 } 246 247 xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren); 248 yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren); 249 } 250 } 251 252 private Container target; 253 private SizeRequirements [] xChildren; 254 private SizeRequirements [] yChildren; 255 private SizeRequirements xTotal; 256 private SizeRequirements yTotal; 257 258 } 259 260 | Popular Tags |