001: /**
002: * Wizard Framework
003: * Copyright 2004 - 2005 Andrew Pietsch
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * $Id: StaticModel.java,v 1.5 2005/05/22 07:13:31 pietschy Exp $
020: */package org.pietschy.wizard.models;
021:
022: import org.pietschy.wizard.AbstractWizardModel;
023: import org.pietschy.wizard.WizardStep;
024: import org.pietschy.wizard.OverviewProvider;
025: import org.pietschy.wizard.Wizard;
026:
027: import javax.swing.*;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.Collections;
031: import java.beans.PropertyChangeListener;
032: import java.beans.PropertyChangeEvent;
033: import java.awt.*;
034:
035: /**
036: * This class provides the basis for a simple linear wizard model. Steps are added by calling the
037: * {@link #add} method and are traversed in the order of addition.
038: */
039: public class StaticModel extends AbstractWizardModel implements
040: OverviewProvider {
041:
042: private ArrayList steps = new ArrayList();
043:
044: private int currentStep = 0;
045: private StaticModelOverview overviewComponent;
046:
047: public StaticModel() {
048: }
049:
050: /**
051: * Resest this model. This method rewinds to the first step in the wizard.
052: */
053: public void reset() {
054: currentStep = 0;
055: setActiveStep((WizardStep) steps.get(currentStep));
056: }
057:
058: public void nextStep() {
059: if (currentStep >= steps.size() - 1)
060: throw new IllegalStateException("Already on last step");
061:
062: currentStep++;
063: setActiveStep((WizardStep) steps.get(currentStep));
064: }
065:
066: public void previousStep() {
067: if (currentStep == 0)
068: throw new IllegalStateException("Already at first step");
069:
070: currentStep--;
071: setActiveStep((WizardStep) steps.get(currentStep));
072: }
073:
074: public void lastStep() {
075: currentStep = steps.size() - 1;
076: setActiveStep((WizardStep) steps.get(currentStep));
077: }
078:
079: public boolean isLastStep(WizardStep step) {
080: return steps.indexOf(step) == steps.size() - 1;
081: }
082:
083: public Iterator stepIterator() {
084: return Collections.unmodifiableList(steps).iterator();
085: }
086:
087: /**
088: * Adds a step to the end of the wizard.
089: * @param step the {@link WizardStep} to add.
090: */
091: public void add(WizardStep step) {
092: steps.add(step);
093: addCompleteListener(step);
094: }
095:
096: /**
097: * This method is invoked after the current step has been changed to update the state
098: * of the model.
099: */
100: public void refreshModelState() {
101: setNextAvailable(getActiveStep().isComplete()
102: && !isLastStep(getActiveStep()));
103: setPreviousAvailable(currentStep > 0);
104: setLastAvailable(allStepsComplete()
105: && !isLastStep(getActiveStep()));
106: setCancelAvailable(true);
107: }
108:
109: /**
110: * Returns true if all the steps in the wizard return <tt>true</tt> from
111: * {@link WizardStep#isComplete}. This is primarily used to determine if the last button
112: * can be enabled.
113: * @return <tt>true</tt> if all the steps in the wizard are complete, <tt>false</tt> otherwise.
114: */
115: public boolean allStepsComplete() {
116: for (Iterator iterator = steps.iterator(); iterator.hasNext();) {
117: if (!((WizardStep) iterator.next()).isComplete())
118: return false;
119: }
120:
121: return true;
122: }
123:
124: /**
125: * Returns an JComponent that will serve as an overview for this wizard. The overview can be
126: * disabled by calling {@link Wizard#setOverviewVisible} with a value of <tt>false</tt>.
127: * @return a component that provides an overview of this wizard and its current state.
128: */
129: public JComponent getOverviewComponent() {
130: if (overviewComponent == null)
131: overviewComponent = new StaticModelOverview(this);
132:
133: return overviewComponent;
134: }
135: }
|