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