1 34 35 package swingwt.awt; 36 37 38 39 40 public class BorderLayout implements LayoutManager2 { 41 42 private int hgap; 43 private int vgap; 44 private Component north; 45 private Component west; 46 private Component east; 47 private Component south; 48 private Component center; 49 private Component firstLine; 50 private Component lastLine; 51 private Component firstItem; 52 private Component lastItem; 53 54 public static final String NORTH = "North"; 55 public static final String SOUTH = "South"; 56 public static final String EAST = "East"; 57 public static final String WEST = "West"; 58 public static final String CENTER = "Center"; 59 public static final String BEFORE_FIRST_LINE = "First"; 60 public static final String AFTER_LAST_LINE = "Last"; 61 public static final String BEFORE_LINE_BEGINS = "Before"; 62 public static final String AFTER_LINE_ENDS = "After"; 63 public static final String PAGE_START = BEFORE_FIRST_LINE; 64 public static final String PAGE_END = AFTER_LAST_LINE; 65 public static final String LINE_START = BEFORE_LINE_BEGINS; 66 public static final String LINE_END = AFTER_LINE_ENDS; 67 68 public BorderLayout() { 69 this(0, 0); 70 } 71 72 public BorderLayout(int hgap, int vgap) { 73 this.hgap = hgap; 74 this.vgap = vgap; 75 } 76 77 public int getHgap() { 78 return hgap; 79 } 80 81 public void setHgap(int hgap) { 82 this.hgap = hgap; 83 } 84 85 public int getVgap() { 86 return vgap; 87 } 88 89 public void setVgap(int vgap) { 90 this.vgap = vgap; 91 } 92 93 public void addLayoutComponent(Component comp, Object constraints) { 94 if ((constraints == null) || (constraints instanceof String )) { 95 addLayoutComponent((String ) constraints, comp); 96 } else { 97 throw new IllegalArgumentException ("constraint must be a string"); 98 } 99 } 100 101 public void addLayoutComponent(String name, Component comp) { 102 103 if (name == null) { 104 name = "Center"; 105 } 106 if ("Center".equals(name)) { 107 center = comp; 108 } 109 else if ("North".equals(name)) { 110 north = comp; 111 } 112 else if ("South".equals(name)) { 113 south = comp; 114 } 115 else if ("East".equals(name)) { 116 east = comp; 117 } 118 else if ("West".equals(name)) { 119 west = comp; 120 } 121 else if (BEFORE_FIRST_LINE.equals(name)) { 122 firstLine = comp; 123 } 124 else if (AFTER_LAST_LINE.equals(name)) { 125 lastLine = comp; 126 } 127 else if (BEFORE_LINE_BEGINS.equals(name)) { 128 firstItem = comp; 129 } 130 else if (AFTER_LINE_ENDS.equals(name)) { 131 lastItem = comp; 132 } 133 else { 134 throw new IllegalArgumentException ( 135 "unknown constraint: " + name); 136 } 137 138 } 139 140 public void removeLayoutComponent(Component comp) { 141 142 if (comp == center) { 143 center = null; 144 } 145 else if (comp == north) { 146 north = null; 147 } 148 else if (comp == south) { 149 south = null; 150 } 151 else if (comp == east) { 152 east = null; 153 } 154 else if (comp == west) { 155 west = null; 156 } 157 if (comp == firstLine) { 158 firstLine = null; 159 } 160 else if (comp == lastLine) { 161 lastLine = null; 162 } 163 else if (comp == firstItem) { 164 firstItem = null; 165 } 166 else if (comp == lastItem) { 167 lastItem = null; 168 } 169 170 } 171 172 public Dimension minimumLayoutSize(Container target) { 173 174 Dimension dim = new Dimension(0, 0); 175 176 boolean ltr = target.getComponentOrientation().isLeftToRight(); 177 Component c = null; 178 179 if ((c = getChild(EAST, ltr)) != null) { 180 Dimension d = c.getMinimumSize(); 181 dim.width += d.width + hgap; 182 dim.height = Math.max(d.height, dim.height); 183 } 184 if ((c = getChild(WEST, ltr)) != null) { 185 Dimension d = c.getMinimumSize(); 186 dim.width += d.width + hgap; 187 dim.height = Math.max(d.height, dim.height); 188 } 189 if ((c = getChild(CENTER, ltr)) != null) { 190 Dimension d = c.getMinimumSize(); 191 dim.width += d.width; 192 dim.height = Math.max(d.height, dim.height); 193 } 194 if ((c = getChild(NORTH, ltr)) != null) { 195 Dimension d = c.getMinimumSize(); 196 dim.width = Math.max(d.width, dim.width); 197 dim.height += d.height + vgap; 198 } 199 if ((c = getChild(SOUTH, ltr)) != null) { 200 Dimension d = c.getMinimumSize(); 201 dim.width = Math.max(d.width, dim.width); 202 dim.height += d.height + vgap; 203 } 204 205 Insets insets = target.getInsets(); 206 dim.width += insets.left + insets.right; 207 dim.height += insets.top + insets.bottom; 208 209 return dim; 210 211 } 212 213 public Dimension preferredLayoutSize(Container target) { 214 215 Dimension dim = new Dimension(0, 0); 216 217 boolean ltr = target.getComponentOrientation().isLeftToRight(); 218 Component c = null; 219 220 if ((c = getChild(EAST, ltr)) != null) { 221 Dimension d = c.getPreferredSize(); 222 dim.width += d.width + hgap; 223 dim.height = Math.max(d.height, dim.height); 224 } 225 if ((c = getChild(WEST, ltr)) != null) { 226 Dimension d = c.getPreferredSize(); 227 dim.width += d.width + hgap; 228 dim.height = Math.max(d.height, dim.height); 229 } 230 if ((c = getChild(CENTER, ltr)) != null) { 231 Dimension d = c.getPreferredSize(); 232 dim.width += d.width; 233 dim.height = Math.max(d.height, dim.height); 234 } 235 if ((c = getChild(NORTH, ltr)) != null) { 236 Dimension d = c.getPreferredSize(); 237 dim.width = Math.max(d.width, dim.width); 238 dim.height += d.height + vgap; 239 } 240 if ((c = getChild(SOUTH, ltr)) != null) { 241 Dimension d = c.getPreferredSize(); 242 dim.width = Math.max(d.width, dim.width); 243 dim.height += d.height + vgap; 244 } 245 246 Insets insets = target.getInsets(); 247 dim.width += insets.left + insets.right; 248 dim.height += insets.top + insets.bottom; 249 250 return dim; 251 252 } 253 254 public Dimension maximumLayoutSize(Container target) { 255 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); 256 } 257 258 public float getLayoutAlignmentX(Container parent) { 259 return (float) 0.5; 260 } 261 262 public float getLayoutAlignmentY(Container parent) { 263 return (float) 0.5; 264 } 265 266 public void invalidateLayout(Container target) { 267 } 268 269 public void layoutContainer(Container target) { 270 Insets insets = target.getInsets(); 271 int top = insets.top; 272 int bottom = target.getHeight() - insets.bottom; 273 int left = insets.left; 274 int right = target.getWidth() - insets.right; 275 276 boolean ltr = target.getComponentOrientation().isLeftToRight(); 277 Component c = null; 278 279 if ((c = getChild(NORTH, ltr)) != null) { 280 c.setSize(right - left, c.getHeight()); 281 Dimension d = c.getPreferredSize(); 282 c.setBounds(left, top, right - left, d.height); 283 top += d.height + vgap; 284 } 285 if ((c = getChild(SOUTH, ltr)) != null) { 286 c.setSize(right - left, c.getHeight()); 287 Dimension d = c.getPreferredSize(); 288 c.setBounds(left, bottom - d.height, right - left, d.height); 289 bottom -= d.height + vgap; 290 } 291 if ((c = getChild(EAST, ltr)) != null) { 292 c.setSize(c.getWidth(), bottom - top); 293 Dimension d = c.getPreferredSize(); 294 c.setBounds(right - d.width, top, d.width, bottom - top); 295 right -= d.width + hgap; 296 } 297 if ((c = getChild(WEST, ltr)) != null) { 298 c.setSize(c.getWidth(), bottom - top); 299 Dimension d = c.getPreferredSize(); 300 c.setBounds(left, top, d.width, bottom - top); 301 left += d.width + hgap; 302 } 303 if ((c = getChild(CENTER, ltr)) != null) { 304 c.setBounds(left, top, right - left, bottom - top); 305 } 306 } 307 308 private Component getChild(String key, boolean ltr) { 309 Component result = null; 310 311 if (key == NORTH) { 312 result = (firstLine != null) ? firstLine : north; 313 } else if (key == SOUTH) { 314 result = (lastLine != null) ? lastLine : south; 315 } else if (key == WEST) { 316 result = ltr ? firstItem : lastItem; 317 if (result == null) { 318 result = west; 319 } 320 } else if (key == EAST) { 321 result = ltr ? lastItem : firstItem; 322 if (result == null) { 323 result = east; 324 } 325 } else if (key == CENTER) { 326 result = center; 327 } 328 if (result != null && !result.isVisible()) { 329 result = null; 330 } 331 return result; 332 } 333 334 } 335 | Popular Tags |