49 * through these cards sequentially, or to show a specified card.
50 * The {@link CardLayout#addLayoutComponent}
51 * method can be used to associate a string identifier with a given card
52 * for fast random access.
53 *
54 * @author Arthur van Hoff
55 * @see java.awt.Container
56 * @since JDK1.0
57 */
58
59 public class CardLayout implements LayoutManager2,
60 Serializable {
61
62 private static final long serialVersionUID = -4328196481005934313L;
63
64 /*
65 * This creates a Vector to store associated
66 * pairs of components and their names.
67 * @see java.util.Vector
68 */
69 Vector vector = new Vector();
70
71 /*
72 * A pair of Component and String that represents its name.
73 */
74 class Card implements Serializable {
75 static final long serialVersionUID = 6640330810709497518L;
76 public String name;
77 public Component comp;
78 public Card(String cardName, Component cardComponent) {
79 name = cardName;
80 comp = cardComponent;
81 }
82 }
83
84 /*
85 * Index of Component currently displayed by CardLayout.
86 */
87 int currentCard = 0;
88
89
553 * Returns a string representation of the state of this card layout.
554 * @return a string representation of this card layout.
555 */
556 public String toString() {
557 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
558 }
559
560 /**
561 * Reads serializable fields from stream.
562 */
563 private void readObject(ObjectInputStream s)
564 throws ClassNotFoundException, IOException
565 {
566 ObjectInputStream.GetField f = s.readFields();
567
568 hgap = f.get("hgap", 0);
569 vgap = f.get("vgap", 0);
570
571 if (f.defaulted("vector")) {
572 // pre-1.4 stream
573 Hashtable tab = (Hashtable)f.get("tab", null);
574 vector = new Vector();
575 if (tab != null && !tab.isEmpty()) {
576 for (Enumeration e = tab.keys() ; e.hasMoreElements() ; ) {
577 String key = (String)e.nextElement();
578 Component comp = (Component)tab.get(key);
579 vector.add(new Card(key, comp));
580 if (comp.isVisible()) {
581 currentCard = vector.size() - 1;
582 }
583 }
584 }
585 } else {
586 vector = (Vector)f.get("vector", null);
587 currentCard = f.get("currentCard", 0);
588 }
589 }
590
591 /**
592 * Writes serializable fields to stream.
593 */
594 private void writeObject(ObjectOutputStream s)
595 throws IOException
596 {
597 Hashtable tab = new Hashtable();
598 int ncomponents = vector.size();
599 for (int i = 0; i < ncomponents; i++) {
600 Card card = (Card)vector.get(i);
601 tab.put(card.name, card.comp);
602 }
603
604 ObjectOutputStream.PutField f = s.putFields();
605 f.put("hgap", hgap);
606 f.put("vgap", vgap);
607 f.put("vector", vector);
608 f.put("currentCard", currentCard);
609 f.put("tab", tab);
610 s.writeFields();
611 }
612 }
|
49 * through these cards sequentially, or to show a specified card.
50 * The {@link CardLayout#addLayoutComponent}
51 * method can be used to associate a string identifier with a given card
52 * for fast random access.
53 *
54 * @author Arthur van Hoff
55 * @see java.awt.Container
56 * @since JDK1.0
57 */
58
59 public class CardLayout implements LayoutManager2,
60 Serializable {
61
62 private static final long serialVersionUID = -4328196481005934313L;
63
64 /*
65 * This creates a Vector to store associated
66 * pairs of components and their names.
67 * @see java.util.Vector
68 */
69 Vector<Card> vector = new Vector<>();
70
71 /*
72 * A pair of Component and String that represents its name.
73 */
74 class Card implements Serializable {
75 static final long serialVersionUID = 6640330810709497518L;
76 public String name;
77 public Component comp;
78 public Card(String cardName, Component cardComponent) {
79 name = cardName;
80 comp = cardComponent;
81 }
82 }
83
84 /*
85 * Index of Component currently displayed by CardLayout.
86 */
87 int currentCard = 0;
88
89
553 * Returns a string representation of the state of this card layout.
554 * @return a string representation of this card layout.
555 */
556 public String toString() {
557 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
558 }
559
560 /**
561 * Reads serializable fields from stream.
562 */
563 private void readObject(ObjectInputStream s)
564 throws ClassNotFoundException, IOException
565 {
566 ObjectInputStream.GetField f = s.readFields();
567
568 hgap = f.get("hgap", 0);
569 vgap = f.get("vgap", 0);
570
571 if (f.defaulted("vector")) {
572 // pre-1.4 stream
573 Hashtable<String, Component> tab = (Hashtable)f.get("tab", null);
574 vector = new Vector<>();
575 if (tab != null && !tab.isEmpty()) {
576 for (Enumeration<String> e = tab.keys() ; e.hasMoreElements() ; ) {
577 String key = (String)e.nextElement();
578 Component comp = (Component)tab.get(key);
579 vector.add(new Card(key, comp));
580 if (comp.isVisible()) {
581 currentCard = vector.size() - 1;
582 }
583 }
584 }
585 } else {
586 vector = (Vector)f.get("vector", null);
587 currentCard = f.get("currentCard", 0);
588 }
589 }
590
591 /**
592 * Writes serializable fields to stream.
593 */
594 private void writeObject(ObjectOutputStream s)
595 throws IOException
596 {
597 Hashtable<String, Component> tab = new Hashtable<>();
598 int ncomponents = vector.size();
599 for (int i = 0; i < ncomponents; i++) {
600 Card card = (Card)vector.get(i);
601 tab.put(card.name, card.comp);
602 }
603
604 ObjectOutputStream.PutField f = s.putFields();
605 f.put("hgap", hgap);
606 f.put("vgap", vgap);
607 f.put("vector", vector);
608 f.put("currentCard", currentCard);
609 f.put("tab", tab);
610 s.writeFields();
611 }
612 }
|