1 7 8 9 package javax.swing; 10 11 import java.awt.*; 12 import java.io.Serializable ; 13 import java.io.PrintStream ; 14 15 120 public class BoxLayout implements LayoutManager2, Serializable { 121 122 125 public static final int X_AXIS = 0; 126 127 130 public static final int Y_AXIS = 1; 131 132 137 public static final int LINE_AXIS = 2; 138 139 144 public static final int PAGE_AXIS = 3; 145 146 159 public BoxLayout(Container target, int axis) { 160 if (axis != X_AXIS && axis != Y_AXIS && 161 axis != LINE_AXIS && axis != PAGE_AXIS) { 162 throw new AWTError("Invalid axis"); 163 } 164 this.axis = axis; 165 this.target = target; 166 } 167 168 182 BoxLayout(Container target, int axis, PrintStream dbg) { 183 this(target, axis); 184 this.dbg = dbg; 185 } 186 187 201 public synchronized void invalidateLayout(Container target) { 202 checkContainer(target); 203 xChildren = null; 204 yChildren = null; 205 xTotal = null; 206 yTotal = null; 207 } 208 209 215 public void addLayoutComponent(String name, Component comp) { 216 } 217 218 223 public void removeLayoutComponent(Component comp) { 224 } 225 226 232 public void addLayoutComponent(Component comp, Object constraints) { 233 } 234 235 247 public Dimension preferredLayoutSize(Container target) { 248 Dimension size; 249 synchronized(this) { 250 checkContainer(target); 251 checkRequests(); 252 size = new Dimension(xTotal.preferred, yTotal.preferred); 253 } 254 255 Insets insets = target.getInsets(); 256 size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE); 257 size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE); 258 return size; 259 } 260 261 272 public Dimension minimumLayoutSize(Container target) { 273 Dimension size; 274 synchronized(this) { 275 checkContainer(target); 276 checkRequests(); 277 size = new Dimension(xTotal.minimum, yTotal.minimum); 278 } 279 280 Insets insets = target.getInsets(); 281 size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE); 282 size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE); 283 return size; 284 } 285 286 297 public Dimension maximumLayoutSize(Container target) { 298 Dimension size; 299 synchronized(this) { 300 checkContainer(target); 301 checkRequests(); 302 size = new Dimension(xTotal.maximum, yTotal.maximum); 303 } 304 305 Insets insets = target.getInsets(); 306 size.width = (int) Math.min((long) size.width + (long) insets.left + (long) insets.right, Integer.MAX_VALUE); 307 size.height = (int) Math.min((long) size.height + (long) insets.top + (long) insets.bottom, Integer.MAX_VALUE); 308 return size; 309 } 310 311 322 public synchronized float getLayoutAlignmentX(Container target) { 323 checkContainer(target); 324 checkRequests(); 325 return xTotal.alignment; 326 } 327 328 339 public synchronized float getLayoutAlignmentY(Container target) { 340 checkContainer(target); 341 checkRequests(); 342 return yTotal.alignment; 343 } 344 345 354 public void layoutContainer(Container target) { 355 checkContainer(target); 356 int nChildren = target.getComponentCount(); 357 int[] xOffsets = new int[nChildren]; 358 int[] xSpans = new int[nChildren]; 359 int[] yOffsets = new int[nChildren]; 360 int[] ySpans = new int[nChildren]; 361 362 Dimension alloc = target.getSize(); 363 Insets in = target.getInsets(); 364 alloc.width -= in.left + in.right; 365 alloc.height -= in.top + in.bottom; 366 367 ComponentOrientation o = target.getComponentOrientation(); 369 int absoluteAxis = resolveAxis( axis, o ); 370 boolean ltr = (absoluteAxis != axis) ? o.isLeftToRight() : true; 371 372 373 synchronized(this) { 375 checkRequests(); 376 377 if (absoluteAxis == X_AXIS) { 378 SizeRequirements.calculateTiledPositions(alloc.width, xTotal, 379 xChildren, xOffsets, 380 xSpans, ltr); 381 SizeRequirements.calculateAlignedPositions(alloc.height, yTotal, 382 yChildren, yOffsets, 383 ySpans); 384 } else { 385 SizeRequirements.calculateAlignedPositions(alloc.width, xTotal, 386 xChildren, xOffsets, 387 xSpans, ltr); 388 SizeRequirements.calculateTiledPositions(alloc.height, yTotal, 389 yChildren, yOffsets, 390 ySpans); 391 } 392 } 393 394 for (int i = 0; i < nChildren; i++) { 396 Component c = target.getComponent(i); 397 c.setBounds((int) Math.min((long) in.left + (long) xOffsets[i], Integer.MAX_VALUE), 398 (int) Math.min((long) in.top + (long) yOffsets[i], Integer.MAX_VALUE), 399 xSpans[i], ySpans[i]); 400 401 } 402 if (dbg != null) { 403 for (int i = 0; i < nChildren; i++) { 404 Component c = target.getComponent(i); 405 dbg.println(c.toString()); 406 dbg.println("X: " + xChildren[i]); 407 dbg.println("Y: " + yChildren[i]); 408 } 409 } 410 411 } 412 413 void checkContainer(Container target) { 414 if (this.target != target) { 415 throw new AWTError("BoxLayout can't be shared"); 416 } 417 } 418 419 void checkRequests() { 420 if (xChildren == null || yChildren == null) { 421 int n = target.getComponentCount(); 424 xChildren = new SizeRequirements [n]; 425 yChildren = new SizeRequirements [n]; 426 for (int i = 0; i < n; i++) { 427 Component c = target.getComponent(i); 428 if (!c.isVisible()) { 429 xChildren[i] = new SizeRequirements (0,0,0, c.getAlignmentX()); 430 yChildren[i] = new SizeRequirements (0,0,0, c.getAlignmentY()); 431 continue; 432 } 433 Dimension min = c.getMinimumSize(); 434 Dimension typ = c.getPreferredSize(); 435 Dimension max = c.getMaximumSize(); 436 xChildren[i] = new SizeRequirements (min.width, typ.width, 437 max.width, 438 c.getAlignmentX()); 439 yChildren[i] = new SizeRequirements (min.height, typ.height, 440 max.height, 441 c.getAlignmentY()); 442 } 443 444 int absoluteAxis = resolveAxis(axis,target.getComponentOrientation()); 446 447 if (absoluteAxis == X_AXIS) { 448 xTotal = SizeRequirements.getTiledSizeRequirements(xChildren); 449 yTotal = SizeRequirements.getAlignedSizeRequirements(yChildren); 450 } else { 451 xTotal = SizeRequirements.getAlignedSizeRequirements(xChildren); 452 yTotal = SizeRequirements.getTiledSizeRequirements(yChildren); 453 } 454 } 455 } 456 457 467 private int resolveAxis( int axis, ComponentOrientation o ) { 468 int absoluteAxis; 469 if( axis == LINE_AXIS ) { 470 absoluteAxis = o.isHorizontal() ? X_AXIS : Y_AXIS; 471 } else if( axis == PAGE_AXIS ) { 472 absoluteAxis = o.isHorizontal() ? Y_AXIS : X_AXIS; 473 } else { 474 absoluteAxis = axis; 475 } 476 return absoluteAxis; 477 } 478 479 480 private int axis; 481 private Container target; 482 483 private transient SizeRequirements [] xChildren; 484 private transient SizeRequirements [] yChildren; 485 private transient SizeRequirements xTotal; 486 private transient SizeRequirements yTotal; 487 488 private transient PrintStream dbg; 489 } 490 491 | Popular Tags |