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: BranchingPath.java,v 1.9 2006/05/13 19:54:32 pietschy Exp $
020: */package org.pietschy.wizard.models;
021:
022: import org.pietschy.wizard.WizardStep;
023:
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: /**
028: * BranchingPaths represent a sequence of {@link WizardStep}s that has multiple choices for
029: * the next path to traverse.
030: *
031: * @see #addBranch
032: * @see #addStep
033: */
034: public class BranchingPath extends Path {
035:
036: private ArrayList paths = new ArrayList();
037:
038: /**
039: * Creates a new empth BranchingPath.
040: */
041: public BranchingPath() {
042: }
043:
044: /**
045: * Creates a new BranchingPath that is initialized with the specified step.
046: *
047: * @param step the first step of the path.
048: */
049: public BranchingPath(WizardStep step) {
050: addStep(step);
051: }
052:
053: /**
054: * Gets the path to traverse after this path has exhausted all its steps.
055: * This method will call iterate over each path selector to determine the
056: * path to return.
057: *
058: * @return the next path in the sequence.
059: * @throws IllegalStateException if no matching path is found.
060: */
061: protected Path getNextPath(MultiPathModel model) {
062: for (Iterator iter = paths.iterator(); iter.hasNext();) {
063: BPath entry = (BPath) iter.next();
064: Condition condition = (Condition) entry.getCondition();
065: if (condition.evaluate(model))
066: return (Path) entry.getPath();
067: }
068:
069: throw new IllegalStateException("No next path selected");
070: }
071:
072: /**
073: * Adds a possible branch from this path.
074: *
075: * @param path the {@link Path} to traverse based when the condition returns
076: * <tt>true</tt>.
077: * @param condition a {@link Condition} that activates this path.
078: */
079: public void addBranch(Path path, Condition condition) {
080: paths.add(new BPath(path, condition));
081: }
082:
083: public void acceptVisitor(PathVisitor visitor) {
084: visitor.visitPath(this );
085: }
086:
087: public void visitBranches(PathVisitor visitor) {
088: for (Iterator iter = paths.iterator(); iter.hasNext();) {
089: BPath path = (BPath) iter.next();
090: path.getPath().acceptVisitor(visitor);
091: }
092: }
093:
094: private static class BPath {
095: private Path path;
096: private Condition condition;
097:
098: public BPath(Path path, Condition condition) {
099: this .path = path;
100: this .condition = condition;
101: }
102:
103: public Condition getCondition() {
104: return condition;
105: }
106:
107: public Path getPath() {
108: return path;
109: }
110: }
111: }
|