001: package com.nexes.wizard;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.*;
006: import javax.swing.*;
007: import javax.swing.border.*;
008:
009: /**
010: * This class is responsible for reacting to events generated by pushing any of the
011: * three buttons, 'Next', 'Previous', and 'Cancel.' Based on what button is pressed,
012: * the controller will update the model to show a new panel and reset the state of
013: * the buttons as necessary.
014: */
015: public class WizardController implements ActionListener {
016:
017: private Wizard wizard;
018:
019: /**
020: * This constructor accepts a reference to the Wizard component that created it,
021: * which it uses to update the button components and access the WizardModel.
022: * @param w A callback to the Wizard component that created this controller.
023: */
024: public WizardController(Wizard w) {
025: wizard = w;
026: }
027:
028: /**
029: * Calling method for the action listener interface. This class listens for actions
030: * performed by the buttons in the Wizard class, and calls methods below to determine
031: * the correct course of action.
032: * @param evt The ActionEvent that occurred.
033: */
034: public void actionPerformed(java.awt.event.ActionEvent evt) {
035:
036: if (evt.getActionCommand().equals(
037: Wizard.CANCEL_BUTTON_ACTION_COMMAND))
038: cancelButtonPressed();
039: else if (evt.getActionCommand().equals(
040: Wizard.BACK_BUTTON_ACTION_COMMAND))
041: backButtonPressed();
042: else if (evt.getActionCommand().equals(
043: Wizard.NEXT_BUTTON_ACTION_COMMAND))
044: nextButtonPressed();
045:
046: }
047:
048: private void cancelButtonPressed() {
049:
050: wizard.close(Wizard.CANCEL_RETURN_CODE);
051: }
052:
053: private void nextButtonPressed() {
054:
055: WizardModel model = wizard.getModel();
056: WizardPanelDescriptor descriptor = model
057: .getCurrentPanelDescriptor();
058:
059: // If it is a finishable panel, close down the dialog. Otherwise,
060: // get the ID that the current panel identifies as the next panel,
061: // and display it.
062:
063: Object nextPanelDescriptor = descriptor
064: .getNextPanelDescriptor();
065:
066: if (nextPanelDescriptor instanceof WizardPanelDescriptor.FinishIdentifier) {
067: wizard.close(Wizard.FINISH_RETURN_CODE);
068: } else {
069: wizard.setCurrentPanel(nextPanelDescriptor);
070: }
071:
072: }
073:
074: private void backButtonPressed() {
075:
076: WizardModel model = wizard.getModel();
077: WizardPanelDescriptor descriptor = model
078: .getCurrentPanelDescriptor();
079:
080: // Get the descriptor that the current panel identifies as the previous
081: // panel, and display it.
082:
083: Object backPanelDescriptor = descriptor
084: .getBackPanelDescriptor();
085: wizard.setCurrentPanel(backPanelDescriptor);
086:
087: }
088:
089: void resetButtonsToPanelRules() {
090:
091: // Reset the buttons to support the original panel rules,
092: // including whether the next or back buttons are enabled or
093: // disabled, or if the panel is finishable.
094:
095: WizardModel model = wizard.getModel();
096: WizardPanelDescriptor descriptor = model
097: .getCurrentPanelDescriptor();
098:
099: model.setCancelButtonText(Wizard.CANCEL_TEXT);
100: model.setCancelButtonIcon(Wizard.CANCEL_ICON);
101:
102: // If the panel in question has another panel behind it, enable
103: // the back button. Otherwise, disable it.
104:
105: model.setBackButtonText(Wizard.BACK_TEXT);
106: model.setBackButtonIcon(Wizard.BACK_ICON);
107:
108: if (descriptor.getBackPanelDescriptor() != null)
109: model.setBackButtonEnabled(Boolean.TRUE);
110: else
111: model.setBackButtonEnabled(Boolean.FALSE);
112:
113: // If the panel in question has one or more panels in front of it,
114: // enable the next button. Otherwise, disable it.
115:
116: if (descriptor.getNextPanelDescriptor() != null)
117: model.setNextFinishButtonEnabled(Boolean.TRUE);
118: else
119: model.setNextFinishButtonEnabled(Boolean.FALSE);
120:
121: // If the panel in question is the last panel in the series, change
122: // the Next button to Finish. Otherwise, set the text back to Next.
123:
124: if (descriptor.getNextPanelDescriptor() instanceof WizardPanelDescriptor.FinishIdentifier) {
125: model.setNextFinishButtonText(Wizard.FINISH_TEXT);
126: model.setNextFinishButtonIcon(Wizard.FINISH_ICON);
127: } else {
128: model.setNextFinishButtonText(Wizard.NEXT_TEXT);
129: model.setNextFinishButtonIcon(Wizard.NEXT_ICON);
130: }
131:
132: }
133:
134: }
|