01: package wizard.layout;
02:
03: import java.awt.CardLayout;
04: import java.util.*;
05:
06: import javax.swing.JPanel;
07:
08: import wizard.helper.MagicPanelReference;
09: import wizard.model.MagicCardHelper;
10: import wizard.ui.AbstractMagicPanel;
11:
12: public class CardManagerLayout extends CardLayout {
13: private int currentCardIndex = -1;
14:
15: private Vector cardFlippedListenerList;
16:
17: private final JPanel parent;
18:
19: public CardManagerLayout(JPanel containerPanel) {
20: this .parent = containerPanel;
21: cardFlippedListenerList = new Vector();
22: }
23:
24: public void gotoCard(String nextCardID) {
25: int nextCardInSequence = 0;
26:
27: if (nextCardID == AbstractMagicPanel.SUCCESSOR) {
28: nextCardInSequence = currentCardIndex + 1;
29: nextCardID = MagicCardHelper.getInstance()
30: .getCardIDByIndex(nextCardInSequence);
31: } else
32: nextCardInSequence = wizard.model.MagicCardHelper
33: .getInstance().getIndexOf(nextCardID);
34: flipCard(nextCardInSequence, nextCardID);
35: }
36:
37: private void flipCard(int destCardIndex, String destCardID) {
38: if (destCardIndex < 0
39: || destCardIndex >= MagicCardHelper.getInstance()
40: .getCardCount())
41: return;
42:
43: if (destCardIndex > currentCardIndex) {
44: if (currentCardIndex >= 0) {
45: MagicPanelReference destCardReference = MagicCardHelper
46: .getInstance()
47: .getCardReferenceAt(destCardIndex);
48:
49: String currentCardID = getCurrentCardReference()
50: .panel().getID();
51: destCardReference.panel().setPreviousCardID(
52: currentCardID);
53: }
54: fireCardWillShow(destCardIndex);
55: }
56:
57: super .show(parent, destCardID);
58:
59: currentCardIndex = destCardIndex;
60: fireCardFlipped();
61: }
62:
63: public void addCardFlipListener(CardFlipListenerIf listener) {
64: cardFlippedListenerList.add(listener);
65: }
66:
67: public MagicPanelReference getCurrentCardReference() {
68: return MagicCardHelper.getInstance().getCardReferenceAt(
69: currentCardIndex);
70: }
71:
72: private void fireCardFlipped() {
73: for (Iterator iter = cardFlippedListenerList.iterator(); iter
74: .hasNext();) {
75: CardFlipListenerIf listener = (CardFlipListenerIf) iter
76: .next();
77: listener.cardHasFlipped(currentCardIndex);
78: }
79: }
80:
81: private void fireCardWillShow(int cardIndex) {
82: AbstractMagicPanel magicPanel = MagicCardHelper.getInstance()
83: .getCardReferenceAt(cardIndex).panel();
84: magicPanel.initialise();
85: }
86:
87: public static interface CardWillShowIf {
88: public void initialise();
89: }
90: }
|