setting all protected methods on WizardStepPage as virtual
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / WizardUtils.cs
blob272db4495d0e2514e2034045674027233ff55f3a
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Internal
17 using System;
18 using System.Collections;
20 /// <summary>
21 /// Utility class for wizard related queries and operations
22 /// </summary>
23 public static class WizardUtils
25 /// <summary>
26 /// Constructs the wizard namespace.
27 /// </summary>
28 /// <param name="controllerContext">The controller context.</param>
29 /// <returns></returns>
30 public static String ConstructWizardNamespace(IControllerContext controllerContext)
32 return String.Format("wizard.{0}", controllerContext.Name);
35 /// <summary>
36 /// Determines whether the current request is within a wizard context.
37 /// </summary>
38 /// <param name="engineContext">The engine context.</param>
39 /// <param name="controllerContext">The controller context.</param>
40 /// <returns>
41 /// <c>true</c> if is on wizard context; otherwise, <c>false</c>.
42 /// </returns>
43 public static bool IsOnWizard(IEngineContext engineContext, IControllerContext controllerContext)
45 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
46 return engineContext.Session.Contains(wizardName + "currentstepindex");
49 /// <summary>
50 /// Determines whether the current wizard has a previous step.
51 /// </summary>
52 /// <param name="engineContext">The engine context.</param>
53 /// <param name="controller">The controller.</param>
54 /// <param name="controllerContext">The controller context.</param>
55 /// <returns>
56 /// <c>true</c> if has previous step; otherwise, <c>false</c>.
57 /// </returns>
58 public static bool HasPreviousStep(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
60 if (!IsOnWizard(engineContext, controllerContext))
62 return false;
65 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
67 int currentIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
69 return currentIndex > 0;
72 /// <summary>
73 /// Determines whether the current wizard has a next step.
74 /// </summary>
75 /// <param name="engineContext">The engine context.</param>
76 /// <param name="controller">The controller.</param>
77 /// <param name="controllerContext">The controller context.</param>
78 /// <returns>
79 /// <c>true</c> if has next step; otherwise, <c>false</c>.
80 /// </returns>
81 public static bool HasNextStep(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
83 if (!IsOnWizard(engineContext, controllerContext))
85 return false;
88 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
90 IList stepList = (IList) engineContext.Items["wizard.step.list"];
92 int currentIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
94 return (currentIndex + 1) < stepList.Count;
97 /// <summary>
98 /// Gets the index of the current step.
99 /// </summary>
100 /// <param name="engineContext">The engine context.</param>
101 /// <param name="controller">The controller.</param>
102 /// <param name="controllerContext">The controller context.</param>
103 /// <returns></returns>
104 public static int GetCurrentStepIndex(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
106 if (!IsOnWizard(engineContext, controllerContext))
108 return -1;
111 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
113 int curIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
115 return curIndex;
118 /// <summary>
119 /// Gets the name of the current step.
120 /// </summary>
121 /// <param name="engineContext">The engine context.</param>
122 /// <param name="controller">The controller.</param>
123 /// <param name="controllerContext">The controller context.</param>
124 /// <returns></returns>
125 public static String GetCurrentStepName(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
127 if (!IsOnWizard(engineContext, controllerContext))
129 return null;
132 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
134 int curIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
136 IList stepList = (IList) engineContext.Items["wizard.step.list"];
138 return (String) stepList[curIndex];
141 /// <summary>
142 /// Gets the name of the previous step.
143 /// </summary>
144 /// <param name="engineContext">The engine context.</param>
145 /// <param name="controller">The controller.</param>
146 /// <param name="controllerContext">The controller context.</param>
147 /// <returns></returns>
148 public static String GetPreviousStepName(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
150 if (!IsOnWizard(engineContext, controllerContext))
152 return null;
155 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
157 int curIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
159 IList stepList = (IList) engineContext.Items["wizard.step.list"];
161 if ((curIndex - 1) >= 0)
163 return (String) stepList[curIndex - 1];
166 return null;
169 /// <summary>
170 /// Gets the name of the next step.
171 /// </summary>
172 /// <param name="index">The step index.</param>
173 /// <param name="engineContext">The engine context.</param>
174 /// <param name="controller">The controller.</param>
175 /// <param name="controllerContext">The controller context.</param>
176 /// <returns></returns>
177 public static string GetStepName(int index, IEngineContext engineContext, IController controller, IControllerContext controllerContext)
179 if (!IsOnWizard(engineContext, controllerContext))
181 return null;
184 IList stepList = (IList) engineContext.Items["wizard.step.list"];
186 if ((index) < stepList.Count)
188 return (String) stepList[index];
191 return null;
194 /// <summary>
195 /// Gets the name of the next step.
196 /// </summary>
197 /// <param name="engineContext">The engine context.</param>
198 /// <param name="controller">The controller.</param>
199 /// <param name="controllerContext">The controller context.</param>
200 /// <returns></returns>
201 public static String GetNextStepName(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
203 if (!IsOnWizard(engineContext, controllerContext))
205 return null;
208 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
210 int curIndex = (int) engineContext.Session[wizardName + "currentstepindex"];
212 IList stepList = (IList) engineContext.Items["wizard.step.list"];
214 if ((curIndex + 1) < stepList.Count)
216 return (String) stepList[curIndex + 1];
219 return null;
222 /// <summary>
223 /// Registers the current step info/state.
224 /// </summary>
225 /// <param name="engineContext">The engine context.</param>
226 /// <param name="controller">The controller.</param>
227 /// <param name="controllerContext">The controller context.</param>
228 /// <param name="actionName">Name of the action.</param>
229 public static void RegisterCurrentStepInfo(IEngineContext engineContext, IController controller,
230 IControllerContext controllerContext, String actionName)
232 IList stepList = (IList) engineContext.Items["wizard.step.list"];
234 for(int i = 0; i < stepList.Count; i++)
236 String stepName = (String) stepList[i];
238 if (actionName == stepName)
240 RegisterCurrentStepInfo(engineContext, controller, controllerContext, i, stepName);
241 break;
246 /// <summary>
247 /// Registers the current step info/state.
248 /// </summary>
249 /// <param name="engineContext">The engine context.</param>
250 /// <param name="controller">The controller.</param>
251 /// <param name="controllerContext">The controller context.</param>
252 /// <param name="stepIndex">Index of the step.</param>
253 /// <param name="stepName">Name of the step.</param>
254 public static void RegisterCurrentStepInfo(IEngineContext engineContext, IController controller,
255 IControllerContext controllerContext, int stepIndex, String stepName)
257 String wizardName = WizardUtils.ConstructWizardNamespace(controllerContext);
259 engineContext.Session[wizardName + "currentstepindex"] = stepIndex;
260 engineContext.Session[wizardName + "currentstep"] = stepName;