001: /**
002: * Created on Dec 2, 2004
003: *
004: * @author karthikeyanr
005: *
006: */package wizard.ui;
007:
008: import java.awt.*;
009: import java.awt.event.*;
010: import java.util.List;
011:
012: import javax.swing.*;
013: import javax.swing.tree.DefaultMutableTreeNode;
014:
015: import wizard.action.*;
016: import wizard.helper.*;
017: import wizard.interfaces.Pollable;
018: import wizard.layout.*;
019: import wizard.model.MagicCardHelper;
020:
021: public abstract class AbstractWizard extends JDialog implements
022: CardFlipListenerIf, Pollable {
023: MagicPanelReference currentPanelReference;
024:
025: ResourceBundleManager instance = ResourceBundleManager
026: .getInstance();
027:
028: NextNavigationAction nextAction;
029:
030: private BackNavigationAction backAction;
031:
032: private int cardCount;
033:
034: private CardManagerLayout layout;
035:
036: private JLabel lblTitle;
037:
038: private JPanel magicPanel;
039:
040: private JTree navigationTree;
041:
042: public abstract boolean wizardComplete();
043:
044: public AbstractWizard(Frame owner, String title)
045: throws HeadlessException {
046: super (owner, title, true);
047: setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
048: addWindowListener(new DilaogWindowListner());
049: initComponents();
050: }
051:
052: public void addCards(List cardList) {
053: DefaultMutableTreeNode overviewNode = (DefaultMutableTreeNode) navigationTree
054: .getModel().getRoot();
055: overviewNode.removeAllChildren();
056: magicPanel.removeAll();
057: cardCount = cardList.size();
058: for (int i = 0; i < cardCount; i++) {
059: AbstractMagicPanel panel = (AbstractMagicPanel) cardList
060: .get(i);
061: decoratePanel(panel);
062: MagicCardHelper.getInstance().addCard(panel);
063: magicPanel.add(panel, panel.getID());
064: overviewNode.add(new DefaultMutableTreeNode((i + 1) + ". "
065: + panel.getTitle()));
066: }
067: navigationTree.expandRow(0);
068: gotoCard(((AbstractMagicPanel) cardList.get(0)).getID());
069: pack();
070: MagicPanelListener.install(this );
071: determineNextActionState();
072: }
073:
074: public void cardHasFlipped(int index) {
075: backAction.setEnabled(index != 0);
076: nextAction.becomeFinish(index == (cardCount - 1));
077: navigationTree.scrollRowToVisible(index + 1);
078: navigationTree.setSelectionRow(index + 1);
079: }
080:
081: public void dispose() {
082: MagicCardHelper.getInstance().reset();
083: MagicPanelListener.uninstall();
084: super .dispose();
085: }
086:
087: public void poll() {
088: determineNextActionState();
089: }
090:
091: void determineNextActionState() {
092: nextAction.setEnabled(currentPanelReference.panel()
093: .validateEarly());
094: }
095:
096: void gotoCard(String nextCardID) {
097: layout.gotoCard(nextCardID);
098: currentPanelReference = layout.getCurrentCardReference();
099: lblTitle
100: .setText(currentPanelReference.panel().getDescription());
101: }
102:
103: private JPanel createButtonPanel() {
104: JPanel buttonPanel = new JPanel(
105: new FlowLayout(FlowLayout.RIGHT));
106: backAction = new BackNavigationAction();
107: buttonPanel.add(new JButton(backAction));
108:
109: nextAction = new NextNavigationAction();
110: buttonPanel.add(new JButton(nextAction));
111:
112: CancelWizardAction cancelWizardAction = new CancelWizardAction(
113: this );
114: buttonPanel.add(new JButton(cancelWizardAction));
115: setDisposer(cancelWizardAction);
116: return buttonPanel;
117: }
118:
119: private void decoratePanel(AbstractMagicPanel panel) {
120: panel.setBorder(BorderFactory.createCompoundBorder(
121: BorderFactory.createCompoundBorder(BorderFactory
122: .createEmptyBorder(0, 0, 0, 5), BorderFactory
123: .createLineBorder(Color.LIGHT_GRAY)),
124: BorderFactory.createEmptyBorder(5, 5, 5, 5)));
125: }
126:
127: private void initComponents() {
128: magicPanel = new JPanel();
129: layout = new CardManagerLayout(magicPanel);
130: layout.addCardFlipListener(this );
131: magicPanel.setLayout(layout);
132:
133: JPanel descriptionPanel = new JPanel(new FlowLayout(
134: FlowLayout.LEFT));
135: lblTitle = new JLabel();
136: descriptionPanel.add(lblTitle);
137:
138: JPanel buttonPanel = createButtonPanel();
139:
140: JPanel contentPane = new JPanel(new BorderLayout(5, 5));
141: contentPane.add(createNavigationPanel(), BorderLayout.WEST);
142:
143: JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
144: centerPanel.add(descriptionPanel, BorderLayout.NORTH);
145: centerPanel.add(magicPanel, BorderLayout.CENTER);
146:
147: contentPane.add(centerPanel, BorderLayout.CENTER);
148: contentPane.add(buttonPanel, BorderLayout.SOUTH);
149: setContentPane(contentPane);
150: }
151:
152: private JComponent createNavigationPanel() {
153: DefaultMutableTreeNode overviewNode = new DefaultMutableTreeNode(
154: ResourceBundleManager.getInstance().getString(
155: "overview.label"));
156: navigationTree = new NavigationTree(overviewNode);
157: navigationTree.setOpaque(false);
158: navigationTree.setEnabled(false);
159: navigationTree.putClientProperty("JTree.lineStyle", "None");
160: navigationTree.setCellRenderer(new NavigationRenderer());
161: return navigationTree;
162: }
163:
164: private void setDisposer(Action disposer) {
165: JRootPane rootPane = SwingUtilities.getRootPane(this );
166: KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
167:
168: InputMap inputMap = rootPane
169: .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
170: inputMap.put(stroke, "EscapeAction");
171: rootPane.getActionMap().put("EscapeAction", disposer);
172: }
173:
174: public class BackNavigationAction extends AbstractAction {
175: public BackNavigationAction() {
176: super (instance.getString("back.label"));
177: }
178:
179: public void actionPerformed(ActionEvent e) {
180: gotoCard(currentPanelReference.panel().getPreviousCardID());
181: nextAction.setEnabled(true);
182: }
183: }
184:
185: public class DilaogWindowListner extends WindowAdapter {
186: public void windowClosing(WindowEvent windowEvent) {
187: dispose();
188: }
189: }
190:
191: public class NextNavigationAction extends AbstractAction {
192: private boolean isFinish;
193:
194: public NextNavigationAction() {
195: super (instance.getString("next.label"));
196: }
197:
198: public void actionPerformed(ActionEvent e) {
199: if (currentPanelReference.panel().validatePanel()) {
200: if (isFinish) {
201: if (wizardComplete())
202: AbstractWizard.this .dispose();
203: else
204: gotoCard(MagicCardHelper.getInstance()
205: .getCardIDByIndex(0));
206: } else {
207: gotoCard(currentPanelReference.panel().getNextID());
208: determineNextActionState();
209: }
210: }
211: }
212:
213: public void becomeFinish(boolean isFinish) {
214: this .isFinish = isFinish;
215:
216: if (isFinish)
217: putValue(Action.NAME, instance
218: .getString("finish.label"));
219: else
220: putValue(Action.NAME, instance.getString("next.label"));
221: }
222: }
223: }
|