1 7 8 package java.awt; 9 10 import java.util.Hashtable ; 11 import java.util.Vector ; 12 import java.util.Enumeration ; 13 14 import java.io.Serializable ; 15 import java.io.ObjectInputStream ; 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectStreamField ; 18 import java.io.IOException ; 19 20 41 42 public class CardLayout implements LayoutManager2 , 43 Serializable { 44 45 private static final long serialVersionUID = -4328196481005934313L; 46 47 52 Vector vector = new Vector (); 53 54 57 class Card implements Serializable { 58 static final long serialVersionUID = 6640330810709497518L; 59 public String name; 60 public Component comp; 61 public Card(String cardName, Component cardComponent) { 62 name = cardName; 63 comp = cardComponent; 64 } 65 } 66 67 70 int currentCard = 0; 71 72 73 81 int hgap; 82 83 91 int vgap; 92 93 101 private static final ObjectStreamField [] serialPersistentFields = { 102 new ObjectStreamField ("tab", Hashtable .class), 103 new ObjectStreamField ("hgap", Integer.TYPE), 104 new ObjectStreamField ("vgap", Integer.TYPE), 105 new ObjectStreamField ("vector", Vector .class), 106 new ObjectStreamField ("currentCard", Integer.TYPE) 107 }; 108 109 112 public CardLayout() { 113 this(0, 0); 114 } 115 116 124 public CardLayout(int hgap, int vgap) { 125 this.hgap = hgap; 126 this.vgap = vgap; 127 } 128 129 136 public int getHgap() { 137 return hgap; 138 } 139 140 147 public void setHgap(int hgap) { 148 this.hgap = hgap; 149 } 150 151 157 public int getVgap() { 158 return vgap; 159 } 160 161 168 public void setVgap(int vgap) { 169 this.vgap = vgap; 170 } 171 172 185 public void addLayoutComponent(Component comp, Object constraints) { 186 synchronized (comp.getTreeLock()) { 187 if (constraints instanceof String ) { 188 addLayoutComponent((String )constraints, comp); 189 } else { 190 throw new IllegalArgumentException ("cannot add to layout: constraint must be a string"); 191 } 192 } 193 } 194 195 199 @Deprecated 200 public void addLayoutComponent(String name, Component comp) { 201 synchronized (comp.getTreeLock()) { 202 if (!vector.isEmpty()) { 203 comp.setVisible(false); 204 } 205 for (int i=0; i < vector.size(); i++) { 206 if (((Card)vector.get(i)).name.equals(name)) { 207 ((Card)vector.get(i)).comp = comp; 208 return; 209 } 210 } 211 vector.add(new Card(name, comp)); 212 } 213 } 214 215 221 public void removeLayoutComponent(Component comp) { 222 synchronized (comp.getTreeLock()) { 223 for (int i = 0; i < vector.size(); i++) { 224 if (((Card)vector.get(i)).comp == comp) { 225 if (comp.isVisible() && (comp.getParent() != null)) { 227 next(comp.getParent()); 228 } 229 230 vector.remove(i); 231 232 if (currentCard > i) { 234 currentCard--; 235 } 236 break; 237 } 238 } 239 } 240 } 241 242 251 public Dimension preferredLayoutSize(Container parent) { 252 synchronized (parent.getTreeLock()) { 253 Insets insets = parent.getInsets(); 254 int ncomponents = parent.getComponentCount(); 255 int w = 0; 256 int h = 0; 257 258 for (int i = 0 ; i < ncomponents ; i++) { 259 Component comp = parent.getComponent(i); 260 Dimension d = comp.getPreferredSize(); 261 if (d.width > w) { 262 w = d.width; 263 } 264 if (d.height > h) { 265 h = d.height; 266 } 267 } 268 return new Dimension (insets.left + insets.right + w + hgap*2, 269 insets.top + insets.bottom + h + vgap*2); 270 } 271 } 272 273 281 public Dimension minimumLayoutSize(Container parent) { 282 synchronized (parent.getTreeLock()) { 283 Insets insets = parent.getInsets(); 284 int ncomponents = parent.getComponentCount(); 285 int w = 0; 286 int h = 0; 287 288 for (int i = 0 ; i < ncomponents ; i++) { 289 Component comp = parent.getComponent(i); 290 Dimension d = comp.getMinimumSize(); 291 if (d.width > w) { 292 w = d.width; 293 } 294 if (d.height > h) { 295 h = d.height; 296 } 297 } 298 return new Dimension (insets.left + insets.right + w + hgap*2, 299 insets.top + insets.bottom + h + vgap*2); 300 } 301 } 302 303 311 public Dimension maximumLayoutSize(Container target) { 312 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 313 } 314 315 322 public float getLayoutAlignmentX(Container parent) { 323 return 0.5f; 324 } 325 326 333 public float getLayoutAlignmentY(Container parent) { 334 return 0.5f; 335 } 336 337 341 public void invalidateLayout(Container target) { 342 } 343 344 354 public void layoutContainer(Container parent) { 355 synchronized (parent.getTreeLock()) { 356 Insets insets = parent.getInsets(); 357 int ncomponents = parent.getComponentCount(); 358 Component comp = null; 359 boolean currentFound = false; 360 361 for (int i = 0 ; i < ncomponents ; i++) { 362 comp = parent.getComponent(i); 363 comp.setBounds(hgap + insets.left, vgap + insets.top, 364 parent.width - (hgap*2 + insets.left + insets.right), 365 parent.height - (vgap*2 + insets.top + insets.bottom)); 366 if (comp.isVisible()) { 367 currentFound = true; 368 } 369 } 370 371 if (!currentFound && ncomponents > 0) { 372 parent.getComponent(0).setVisible(true); 373 } 374 } 375 } 376 377 381 void checkLayout(Container parent) { 382 if (parent.getLayout() != this) { 383 throw new IllegalArgumentException ("wrong parent for CardLayout"); 384 } 385 } 386 387 392 public void first(Container parent) { 393 synchronized (parent.getTreeLock()) { 394 checkLayout(parent); 395 int ncomponents = parent.getComponentCount(); 396 for (int i = 0 ; i < ncomponents ; i++) { 397 Component comp = parent.getComponent(i); 398 if (comp.isVisible()) { 399 comp.setVisible(false); 400 break; 401 } 402 } 403 if (ncomponents > 0) { 404 currentCard = 0; 405 parent.getComponent(0).setVisible(true); 406 parent.validate(); 407 } 408 } 409 } 410 411 418 public void next(Container parent) { 419 synchronized (parent.getTreeLock()) { 420 checkLayout(parent); 421 int ncomponents = parent.getComponentCount(); 422 for (int i = 0 ; i < ncomponents ; i++) { 423 Component comp = parent.getComponent(i); 424 if (comp.isVisible()) { 425 comp.setVisible(false); 426 currentCard = (i + 1) % ncomponents; 427 comp = parent.getComponent(currentCard); 428 comp.setVisible(true); 429 parent.validate(); 430 return; 431 } 432 } 433 showDefaultComponent(parent); 434 } 435 } 436 437 444 public void previous(Container parent) { 445 synchronized (parent.getTreeLock()) { 446 checkLayout(parent); 447 int ncomponents = parent.getComponentCount(); 448 for (int i = 0 ; i < ncomponents ; i++) { 449 Component comp = parent.getComponent(i); 450 if (comp.isVisible()) { 451 comp.setVisible(false); 452 currentCard = ((i > 0) ? i-1 : ncomponents-1); 453 comp = parent.getComponent(currentCard); 454 comp.setVisible(true); 455 parent.validate(); 456 return; 457 } 458 } 459 showDefaultComponent(parent); 460 } 461 } 462 463 void showDefaultComponent(Container parent) { 464 if (parent.getComponentCount() > 0) { 465 currentCard = 0; 466 parent.getComponent(0).setVisible(true); 467 parent.validate(); 468 } 469 } 470 471 476 public void last(Container parent) { 477 synchronized (parent.getTreeLock()) { 478 checkLayout(parent); 479 int ncomponents = parent.getComponentCount(); 480 for (int i = 0 ; i < ncomponents ; i++) { 481 Component comp = parent.getComponent(i); 482 if (comp.isVisible()) { 483 comp.setVisible(false); 484 break; 485 } 486 } 487 if (ncomponents > 0) { 488 currentCard = ncomponents - 1; 489 parent.getComponent(currentCard).setVisible(true); 490 parent.validate(); 491 } 492 } 493 } 494 495 503 public void show(Container parent, String name) { 504 synchronized (parent.getTreeLock()) { 505 checkLayout(parent); 506 Component next = null; 507 int ncomponents = vector.size(); 508 for (int i = 0; i < ncomponents; i++) { 509 Card card = (Card)vector.get(i); 510 if (card.name.equals(name)) { 511 next = card.comp; 512 currentCard = i; 513 break; 514 } 515 } 516 if ((next != null) && !next.isVisible()) { 517 ncomponents = parent.getComponentCount(); 518 for (int i = 0; i < ncomponents; i++) { 519 Component comp = parent.getComponent(i); 520 if (comp.isVisible()) { 521 comp.setVisible(false); 522 break; 523 } 524 } 525 next.setVisible(true); 526 parent.validate(); 527 } 528 } 529 } 530 531 535 public String toString() { 536 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]"; 537 } 538 539 542 private void readObject(ObjectInputStream s) 543 throws ClassNotFoundException , IOException 544 { 545 ObjectInputStream.GetField f = s.readFields(); 546 547 hgap = f.get("hgap", 0); 548 vgap = f.get("vgap", 0); 549 550 if (f.defaulted("vector")) { 551 Hashtable tab = (Hashtable )f.get("tab", null); 553 vector = new Vector (); 554 if (tab != null && !tab.isEmpty()) { 555 for (Enumeration e = tab.keys() ; e.hasMoreElements() ; ) { 556 String key = (String )e.nextElement(); 557 Component comp = (Component )tab.get(key); 558 vector.add(new Card(key, comp)); 559 if (comp.isVisible()) { 560 currentCard = vector.size() - 1; 561 } 562 } 563 } 564 } else { 565 vector = (Vector )f.get("vector", null); 566 currentCard = f.get("currentCard", 0); 567 } 568 } 569 570 573 private void writeObject(ObjectOutputStream s) 574 throws IOException 575 { 576 Hashtable tab = new Hashtable (); 577 int ncomponents = vector.size(); 578 for (int i = 0; i < ncomponents; i++) { 579 Card card = (Card)vector.get(i); 580 tab.put(card.name, card.comp); 581 } 582 583 ObjectOutputStream.PutField f = s.putFields(); 584 f.put("hgap", hgap); 585 f.put("vgap", vgap); 586 f.put("vector", vector); 587 f.put("currentCard", currentCard); 588 f.put("tab", tab); 589 s.writeFields(); 590 } 591 } 592 | Popular Tags |