1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
21 /// Utility class for wizard related queries and operations
23 public static class WizardUtils
26 /// Constructs the wizard namespace.
28 /// <param name="controller">The controller.</param>
29 /// <returns></returns>
30 public static String
ConstructWizardNamespace(Controller controller
)
32 if (controller
is WizardStepPage
)
34 return ConstructWizardNamespace( (controller
as WizardStepPage
).WizardController
);
37 return String
.Format("wizard.{0}", controller
.Name
);
41 /// Determines whether the current wizard has a previous step.
43 /// <param name="controller">The controller.</param>
45 /// <c>true</c> if has previous step; otherwise, <c>false</c>.
47 public static bool HasPreviousStep(Controller controller
)
49 IRailsEngineContext context
= controller
.Context
;
51 String wizardName
= WizardUtils
.ConstructWizardNamespace(controller
);
53 int currentIndex
= (int) context
.Session
[wizardName
+ "currentstepindex"];
55 return currentIndex
> 0;
59 /// Determines whether the current wizard has a next step.
61 /// <param name="controller">The controller.</param>
63 /// <c>true</c> if has next step; otherwise, <c>false</c>.
65 public static bool HasNextStep(Controller controller
)
67 IRailsEngineContext context
= controller
.Context
;
69 String wizardName
= WizardUtils
.ConstructWizardNamespace(controller
);
71 IList stepList
= (IList
) context
.Items
["wizard.step.list"];
73 int currentIndex
= (int) context
.Session
[wizardName
+ "currentstepindex"];
75 return (currentIndex
+ 1) < stepList
.Count
;
79 /// Gets the name of the previous step.
81 /// <param name="controller">The controller.</param>
82 /// <returns></returns>
83 public static String
GetPreviousStepName(Controller controller
)
85 IRailsEngineContext context
= controller
.Context
;
87 String wizardName
= WizardUtils
.ConstructWizardNamespace(controller
);
89 int curIndex
= (int) context
.Session
[wizardName
+ "currentstepindex"];
91 IList stepList
= (IList
) context
.Items
["wizard.step.list"];
93 if ((curIndex
- 1) >= 0)
95 return (String
) stepList
[curIndex
- 1];
102 /// Gets the name of the next step.
104 /// <param name="controller">The controller.</param>
105 /// <returns></returns>
106 public static String
GetNextStepName(Controller controller
)
108 IRailsEngineContext context
= controller
.Context
;
110 String wizardName
= WizardUtils
.ConstructWizardNamespace(controller
);
112 int curIndex
= (int) context
.Session
[wizardName
+ "currentstepindex"];
114 IList stepList
= (IList
) context
.Items
["wizard.step.list"];
116 if ((curIndex
+ 1) < stepList
.Count
)
118 return (String
) stepList
[curIndex
+ 1];
125 /// Registers the current step info/state.
127 /// <param name="controller">The controller.</param>
128 /// <param name="actionName">Name of the action.</param>
129 public static void RegisterCurrentStepInfo(Controller controller
, String actionName
)
131 IRailsEngineContext context
= controller
.Context
;
133 IList stepList
= (IList
) context
.Items
["wizard.step.list"];
135 for(int i
=0; i
< stepList
.Count
; i
++)
137 String stepName
= (String
) stepList
[i
];
139 if (actionName
== stepName
)
141 RegisterCurrentStepInfo(controller
, i
, stepName
);
149 /// Registers the current step info/state.
151 /// <param name="controller">The controller.</param>
152 /// <param name="stepIndex">Index of the step.</param>
153 /// <param name="stepName">Name of the step.</param>
154 public static void RegisterCurrentStepInfo(Controller controller
, int stepIndex
, String stepName
)
156 IRailsEngineContext context
= controller
.Context
;
157 String wizardName
= WizardUtils
.ConstructWizardNamespace(controller
);
159 context
.Session
[wizardName
+ "currentstepindex"] = stepIndex
;
160 context
.Session
[wizardName
+ "currentstep"] = stepName
;