001    // Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002    // The Molly framework is freely distributable under the terms of an
003    // MIT-style license. For details, see the molly pages web site at:
004    // http://www.mollypages.org/. Use, modify, have fun !
005    
006    package fc.web.forms;
007    
008    import javax.servlet.*;
009    import javax.servlet.http.*;
010    import java.io.*;
011    import java.util.*;
012    
013    import fc.io.*;
014    import fc.util.*;
015    
016    /** 
017    A container for a sequence of forms. 
018    <p>
019    
020    @author hursh jain
021    **/
022    public class FormSequence 
023    {
024    Map sequence;
025    Map doneMap;
026    
027    public FormSequence()
028      {
029      sequence = new LinkedHashMap(); 
030      doneMap = new HashMap();
031      }
032      
033    /** 
034    Adds a form to the sequence 
035    
036    @throws IllegalStateException   if a form with the same name already
037                    exists in this sequence.            
038    **/
039    public void add(Form f) {
040      Argcheck.notnull(f, "param f was null");
041      sequence.put(f.getName(), f);
042      doneMap.put(f.getName(), Boolean.valueOf(false)); 
043      }
044    
045    /** specify <tt>true</tt> to mark the form as done**/
046    public void setDone(Form form, boolean done) 
047      {
048      Form f = (Form) sequence.get(form.getName());
049      if (f == null) {
050        return;
051        }
052      doneMap.put(form.getName(), Boolean.valueOf(done)); 
053      }
054    
055    /** 
056    @return <tt>true</tt> is the specified form is marked
057        as done.
058    **/
059    public boolean isDone(Form f) 
060      {
061      if (! sequence.containsKey(f.getName()) )
062        return false;
063      
064      Boolean b = (Boolean) doneMap.get(f.getName()); 
065      return b.booleanValue();
066      }
067    
068    /** 
069    Returns the form with the specified name or <tt>null</tt> 
070    if a form with that name does not exist
071    **/
072    public Form get(String name) {
073      return (Form) sequence.get(name);
074      }
075    
076    /** 
077    @return the first form which is still not done or <tt>null</tt>
078        if none are remaining to be done.
079    **/
080    public Form getFirstRemaining() 
081      {
082      Form f = null;
083      Iterator it = sequence.values().iterator();
084      while (it.hasNext()) {
085        f = (Form) it.next();
086        if (! isDone(f) ) {
087          break;
088          }
089        } 
090      return f;
091      }
092    
093    /**
094    @return <tt>true</tt> is all forms in the sequence are
095        marked as done <tt>false</tt> otherwise. Also 
096        returns <tt>false</tt> if this sequence contains
097        no forms at all.
098    **/
099    public boolean isFinished() {
100      return getFirstRemaining() == null; 
101      }
102      
103    }