1 15 16 17 package swingwt.awt.swtcustomlayout; 18 19 20 21 import org.eclipse.swt.widgets.*; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.graphics.Rectangle; 24 25 public class SWTBorderLayout extends Layout 26 { 27 private boolean _cached; 28 private int _hgap; 29 private int _vgap; 30 private int _north; 31 private int _west; 32 private int _east; 33 private int _south; 34 private int _center; 35 private int _firstLine; 36 private int _lastLine; 37 private int _firstItem; 38 private int _lastItem; 39 protected static final int INT_NONE = -1; 40 protected static final int INT_NORTH = 0; 41 public static final Integer NORTH = new Integer (0); 42 protected static final int INT_SOUTH = 1; 43 public static final Integer SOUTH = new Integer (1); 44 protected static final int INT_EAST = 2; 45 public static final Integer EAST = new Integer (2); 46 protected static final int INT_WEST = 3; 47 public static final Integer WEST = new Integer (3); 48 protected static final int INT_CENTER = 4; 49 public static final Integer CENTER = new Integer (4); 50 protected static final int INT_BEFORE_FIRST_LINE = 5; 51 public static final Integer BEFORE_FIRST_LINE = new Integer (5); 52 protected static final int INT_AFTER_LAST_LINE = 6; 53 public static final Integer AFTER_LAST_LINE = new Integer (6); 54 protected static final int INT_BEFORE_LINE_BEGINS = 7; 55 public static final Integer BEFORE_LINE_BEGINS = new Integer (7); 56 protected static final int INT_AFTER_LINE_ENDS = 8; 57 public static final Integer AFTER_LINE_ENDS = new Integer (8); 58 59 static 60 { 61 } 62 63 public SWTBorderLayout() 64 { 65 this(0, 0); 66 } 67 68 public SWTBorderLayout(int hgap, int vgap) 69 { 70 setHgap(hgap); 71 setVgap(vgap); 72 } 73 74 public int getHgap() 75 { 76 return _hgap; 77 } 78 79 public void setHgap(int hgap) 80 { 81 _hgap = hgap; 82 } 83 84 public int getVgap() 85 { 86 return _vgap; 87 } 88 89 public void setVgap(int vgap) 90 { 91 _vgap = vgap; 92 } 93 94 protected void determineControlPositions(Control controls[]) 95 { 96 setCenter(-1); 97 setNorth(-1); 98 setSouth(-1); 99 setEast(-1); 100 setWest(-1); 101 setFirstLine(-1); 102 setLastLine(-1); 103 setFirstItem(-1); 104 setLastItem(-1); 105 for (int i = 0; i < controls.length; i++) 106 { 107 int position; 108 Control control = controls[i]; 109 Object layoutData = control.getLayoutData(); 110 if (layoutData != null && !(layoutData instanceof Integer )) 111 throw new IllegalArgumentException ("Bad control layout data"); 112 Integer positionAsInteger = (Integer )layoutData; 113 if (positionAsInteger == null) 114 position = 4; 115 else 116 position = positionAsInteger.intValue(); 117 switch (position) 118 { 119 case 4: 120 setCenter(i); 121 break; 122 123 case 0: 124 setNorth(i); 125 break; 126 127 case 1: 128 setSouth(i); 129 break; 130 131 case 2: 132 setEast(i); 133 break; 134 135 case 3: 136 setWest(i); 137 break; 138 139 case 5: 140 setFirstLine(i); 141 break; 142 143 case 6: 144 setLastLine(i); 145 break; 146 147 case 7: 148 setFirstItem(i); 149 break; 150 151 case 8: 152 setLastItem(i); 153 break; 154 155 default: 156 throw new IllegalArgumentException (new StringBuffer ("cannot add to layout: unknown constraint: ").append(position).toString()); 157 } 158 setCached(true); 159 } 160 } 161 162 protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) 163 { 164 Control children[] = composite.getChildren(); 165 if (!isCached() || flushCache) 166 determineControlPositions(children); 167 Point size = new Point(0, 0); 168 boolean ltr = true; 169 Control c = null; 170 if ((c = getChild(children, 2, ltr)) != null) 171 { 172 Point d = c.computeSize(-1, -1); 173 size.x += d.x + getHgap(); 174 size.y = Math.max(d.y, size.y); 175 } 176 if ((c = getChild(children, 3, ltr)) != null) 177 { 178 Point d = c.computeSize(-1, -1); 179 size.x += d.x + getHgap(); 180 size.y = Math.max(d.y, size.y); 181 } 182 if ((c = getChild(children, 4, ltr)) != null) 183 { 184 Point d = c.computeSize(-1, -1); 185 size.x += d.x; 186 size.y = Math.max(d.y, size.y); 187 } 188 if ((c = getChild(children, 0, ltr)) != null) 189 { 190 Point d = c.computeSize(-1, -1); 191 size.x = Math.max(d.x, size.x); 192 size.y += d.y + getVgap(); 193 } 194 if ((c = getChild(children, 1, ltr)) != null) 195 { 196 Point d = c.computeSize(-1, -1); 197 size.x = Math.max(d.x, size.x); 198 size.y += d.y + getVgap(); 199 } 200 return size; 201 } 202 203 protected void layout(Composite composite, boolean flushCache) 204 { 205 Control children[] = composite.getChildren(); 206 if (!isCached() || flushCache) 207 determineControlPositions(children); 208 Rectangle r = composite.getClientArea(); 209 int count = children.length; 210 if (count == 0) 211 return; 212 boolean ltr = true; 213 Control c = null; 214 if ((c = getChild(children, 0, ltr)) != null) 215 { 216 Point d = c.computeSize(-1, -1); 217 c.setBounds(r.x, r.y, r.width, d.y); 218 int delta = d.y + getVgap(); 219 r.y += delta; 220 } 221 if ((c = getChild(children, 1, ltr)) != null) 222 { 223 Point d = c.computeSize(-1, -1); 224 c.setBounds(r.x, r.height - d.y, r.width, d.y); 225 r.height -= d.y + getVgap(); 226 } 227 if ((c = getChild(children, 2, ltr)) != null) 228 { 229 Point d = c.computeSize(-1, -1); 230 c.setBounds(r.width - d.x, r.y, d.x, r.height - r.y); 231 r.width -= d.x + getHgap(); 232 } 233 if ((c = getChild(children, 3, ltr)) != null) 234 { 235 Point d = c.computeSize(-1, -1); 236 c.setBounds(r.x, r.y, d.x, r.height - r.y); 237 int delta = d.x + getHgap(); 238 r.x += delta; 239 } 240 if ((c = getChild(children, 4, ltr)) != null) 241 c.setBounds(r.x, r.y, r.width - r.x, r.height - r.y); 242 } 243 244 protected Control getChild(Control children[], int key, boolean ltr) 245 { 246 int result = -1; 247 switch (key) 248 { 249 case 0: 250 result = (getFirstLine() != -1) ? getFirstLine() : getNorth(); 251 break; 252 253 case 1: 254 result = (getLastLine() != -1) ? getLastLine() : getSouth(); 255 break; 256 257 case 3: 258 result = ltr ? getFirstItem() : getLastItem(); 259 if (result == -1) 260 result = getWest(); 261 break; 262 263 case 2: 264 result = ltr ? getLastItem() : getFirstItem(); 265 if (result == -1) 266 result = getEast(); 267 break; 268 269 case 4: 270 result = getCenter(); 271 break; 272 } 273 if (result != -1 && !children[result].isVisible()) 274 result = -1; 275 if (result == -1) 276 return null; 277 else 278 return children[result]; 279 } 280 281 public String toString() 282 { 283 return "[" + paramString() + "]"; 284 } 285 286 protected String paramString() 287 { 288 return getHgap() + ",vgap=" + getVgap(); 289 } 290 291 protected int getWest() 292 { 293 return _west; 294 } 295 296 protected void setWest(int west) 297 { 298 _west = west; 299 } 300 301 protected int getSouth() 302 { 303 return _south; 304 } 305 306 protected void setSouth(int south) 307 { 308 _south = south; 309 } 310 311 protected int getNorth() 312 { 313 return _north; 314 } 315 316 protected void setNorth(int north) 317 { 318 _north = north; 319 } 320 321 protected int getLastLine() 322 { 323 return _lastLine; 324 } 325 326 protected void setLastLine(int lastLine) 327 { 328 _lastLine = lastLine; 329 } 330 331 protected int getLastItem() 332 { 333 return _lastItem; 334 } 335 336 protected void setLastItem(int lastItem) 337 { 338 _lastItem = lastItem; 339 } 340 341 protected int getFirstLine() 342 { 343 return _firstLine; 344 } 345 346 protected void setFirstLine(int firstLine) 347 { 348 _firstLine = firstLine; 349 } 350 351 protected int getFirstItem() 352 { 353 return _firstItem; 354 } 355 356 protected void setFirstItem(int firstItem) 357 { 358 _firstItem = firstItem; 359 } 360 361 protected int getEast() 362 { 363 return _east; 364 } 365 366 protected void setEast(int east) 367 { 368 _east = east; 369 } 370 371 protected int getCenter() 372 { 373 return _center; 374 } 375 376 protected void setCenter(int center) 377 { 378 _center = center; 379 } 380 381 protected boolean isCached() 382 { 383 return _cached; 384 } 385 386 protected void setCached(boolean cached) 387 { 388 _cached = cached; 389 } 390 } 391 | Popular Tags |