1   package com.atlassian.config.wizard;
2   
3   import com.atlassian.config.ConfigurationException;
4   
5   import java.util.ArrayList;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   public class SetupWizard
10  {
11      private List steps = new ArrayList();
12  
13      private SetupStep currentStep = null;
14  
15      private SaveStrategy saveStrategy = null;
16  
17      public void addStep(SetupStep step)
18      {
19          addStep(step, steps.size());
20      }
21  
22      public void addStep(SetupStep step, int index)
23      {
24          step.setIndex(index);
25          steps.add(index, step);
26      }
27  
28      public SetupStep getCurrentStep()
29      {
30          if (currentStep == null && steps.size() > 0)
31          {
32              currentStep = getStep(0);
33          }
34          return currentStep;
35      }
36  
37      protected void setCurrentStep(SetupStep step)
38      {
39          currentStep = step;
40      }
41  
42      public String getStepNameByIndex(int index)
43      {
44          if (index >= steps.size())
45          {
46              return null;
47          }
48          else
49          {
50              return ((SetupStep) steps.get(index)).getName();
51          }
52      }
53  
54      public int getStepIndexByName(String name)
55      {
56          SetupStep setupStep;
57          for (Iterator iterator = steps.iterator(); iterator.hasNext();)
58          {
59              setupStep = (SetupStep) iterator.next();
60              if (setupStep.getName().equals(name))
61              {
62                  return setupStep.getIndex();
63              }
64          }
65          return -1;
66      }
67  
68      /**
69       * @return true if a given step is finished
70       */
71      public boolean isSetupStepFinished(String stepName) throws StepNotFoundException
72      {
73          return isStepBeforeCurrentStep(stepName);
74      }
75  
76  
77      private boolean isStepBeforeCurrentStep(String stepName) throws StepNotFoundException
78      {
79          int pos = getStepIndexByName(stepName);
80          if (pos == -1)
81              throw new StepNotFoundException(stepName);
82          return pos < getCurrentStep().getIndex();
83      }
84  
85  
86      public boolean isSetupComplete()
87      {
88          return (steps.size() - 1) == getCurrentStep().getIndex();
89      }
90  
91      public void previous() throws ConfigurationException
92      {
93          if (getCurrentStep().getIndex() > 0)
94          {
95              int index = getCurrentStep().getIndex() - 1;
96              setCurrentStep((SetupStep) steps.get(index));
97              getCurrentStep().onStart();
98              save();
99          }
100     }
101 
102     public void next() throws ConfigurationException
103     {
104         if (!isSetupComplete())
105         {
106             int nextIndex = getCurrentStep().getIndex() + 1;
107             getCurrentStep().onNext();
108             setCurrentStep((SetupStep) steps.get(nextIndex));
109             getCurrentStep().onStart();
110             save();
111         }
112     }
113 
114     public void next(String stepName) throws StepNotFoundException, ConfigurationException
115     {
116         int step = getStepIndexByName(stepName);
117         if (step == -1)
118         {
119             throw new StepNotFoundException(stepName);
120         }
121         if (getCurrentStep() != null) getCurrentStep().onNext();
122         setCurrentStep((SetupStep) steps.get(step));
123         getCurrentStep().onStart();
124         save();
125     }
126 
127     public void start() throws StepNotFoundException, ConfigurationException
128     {
129         if (steps.size() == 0)
130         {
131             throw new StepNotFoundException("There are no steps associated with this wizard");
132         }
133         else
134         {
135             currentStep = (SetupStep) steps.get(0);
136             currentStep.onStart();
137             save();
138         }
139 
140     }
141 
142     public void finish() throws ConfigurationException
143     {
144         try
145         {
146             String current = getStepNameByIndex(steps.size() - 1);
147             if (!current.equals(getCurrentStep().getName()))
148             {
149                 next(current);
150             }
151             else
152             {
153                 getCurrentStep().onNext();
154                 save();
155             }
156         }
157         catch (StepNotFoundException e)
158         {  /*ignore*/  }
159     }
160 
161     protected synchronized void save() throws ConfigurationException
162     {
163         if (saveStrategy != null)
164         {
165             saveStrategy.save(this);
166         }
167     }
168 
169     public SaveStrategy getSaveStrategy()
170     {
171         return saveStrategy;
172     }
173 
174     public void setSaveStrategy(SaveStrategy saveStrategy)
175     {
176         this.saveStrategy = saveStrategy;
177     }
178 
179     public SetupStep getStep(String name)
180     {
181         int index = getStepIndexByName(name);
182         return getStep(index);
183     }
184 
185     public SetupStep getStep(int index)
186     {
187         if (index == -1)
188         {
189             return null;
190         }
191         return (SetupStep) steps.get(index);
192     }
193 
194 }