/branch/QueueSecurityConfig/CCNetConfig/CCNetConfig.Core/Wizard/WizardForm.cs

# · C# · 339 lines · 206 code · 28 blank · 105 comment · 18 complexity · 94ff5a4fda4bb040254a374225e0ef7b MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace CCNetConfig.Core.Wizard
  9. {
  10. /// <summary>
  11. /// Displays a sequence of pages.
  12. /// </summary>
  13. public partial class WizardForm : Form
  14. {
  15. #region Private fields
  16. private WizardPage currentPage;
  17. private List<IWizardAction> actions = new List<IWizardAction>();
  18. #endregion
  19. #region Constructors
  20. /// <summary>
  21. /// Initialises a new instance of a <see cref="WizardForm"/>.
  22. /// </summary>
  23. private WizardForm()
  24. {
  25. InitializeComponent();
  26. }
  27. #endregion
  28. #region Public properties
  29. #region Configuration
  30. /// <summary>
  31. /// The current configuration.
  32. /// </summary>
  33. public CruiseControl Configuration { get; set; }
  34. #endregion
  35. #region NumberOfActions
  36. /// <summary>
  37. /// The number of actions that have been queued.
  38. /// </summary>
  39. public int NumberOfActions
  40. {
  41. get { return actions.Count; }
  42. }
  43. #endregion
  44. #region DefaultImage
  45. /// <summary>
  46. /// The default image to show if the page doesn't have an image.
  47. /// </summary>
  48. public Image DefaultImage { get; set; }
  49. #endregion
  50. #endregion
  51. #region Public methods
  52. #region Show()
  53. /// <summary>
  54. /// Displays the wizard.
  55. /// </summary>
  56. /// <param name="startPage">The first page of the wizard.</param>
  57. /// <param name="title">The title of the wizard.</param>
  58. /// <param name="configuration">The current configuration.</param>
  59. /// <param name="owner">The form that owns the wizard.</param>
  60. public static void Show(WizardPage startPage, string title, CruiseControl configuration, Form owner)
  61. {
  62. Show(startPage, title, configuration, owner, null);
  63. }
  64. /// <summary>
  65. /// Displays the wizard.
  66. /// </summary>
  67. /// <param name="startPage">The first page of the wizard.</param>
  68. /// <param name="title">The title of the wizard.</param>
  69. /// <param name="configuration">The current configuration.</param>
  70. /// <param name="owner">The form that owns the wizard.</param>
  71. /// <param name="defaultImage">The default image to use.</param>
  72. public static void Show(WizardPage startPage, string title, CruiseControl configuration, Form owner, Image defaultImage)
  73. {
  74. WizardForm form = new WizardForm();
  75. if (defaultImage != null) form.DefaultImage = defaultImage;
  76. form.Configuration = configuration;
  77. form.Text = title;
  78. form.LoadPage(startPage);
  79. form.ShowDialog(owner);
  80. }
  81. #endregion
  82. #region LoadPage()
  83. /// <summary>
  84. /// Loads a new page.
  85. /// </summary>
  86. /// <param name="page">The page to load.</param>
  87. public virtual void LoadPage(WizardPage page)
  88. {
  89. // Hook up all the properties and load the control
  90. currentPage = page;
  91. page.Wizard = this;
  92. pagePanel.Controls.Clear();
  93. pagePanel.Controls.Add(page);
  94. page.Dock = DockStyle.Fill;
  95. CheckButtons();
  96. // Check if the image needs to be set
  97. if ((page.HeaderImage == null) && (DefaultImage != null))
  98. {
  99. page.HeaderImage = DefaultImage;
  100. }
  101. // Tell the page that it is now displayed
  102. page.Display();
  103. }
  104. #endregion
  105. #region CheckButtons()
  106. /// <summary>
  107. /// Checks the buttons to see what state they should be in.
  108. /// </summary>
  109. public virtual void CheckButtons()
  110. {
  111. cancelButton.Enabled = currentPage.IsCancelEnabled;
  112. previousButton.Enabled = currentPage.IsPreviousEnabled;
  113. nextButton.Enabled = currentPage.IsNextEnabled;
  114. finishButton.Enabled = currentPage.IsFinishEnabled;
  115. }
  116. #endregion
  117. #region AddAction()
  118. /// <summary>
  119. /// Adds an action to the actions to be performed.
  120. /// </summary>
  121. /// <param name="action">The action to add.</param>
  122. public virtual void AddAction(IWizardAction action)
  123. {
  124. actions.Add(action);
  125. }
  126. #endregion
  127. #region RemoveAction()
  128. /// <summary>
  129. /// Removes an action from the actions to be performed.
  130. /// </summary>
  131. /// <param name="action">The action to remove.</param>
  132. public virtual void RemoveAction(IWizardAction action)
  133. {
  134. actions.Remove(action);
  135. }
  136. #endregion
  137. #region RunActions()
  138. /// <summary>
  139. /// Runs all the actions and clears the list.
  140. /// </summary>
  141. public virtual void RunActions()
  142. {
  143. List<IWizardAction> completedActions = new List<IWizardAction>();
  144. Exception runError = null;
  145. // Attempt to run each action
  146. int position = 0;
  147. foreach (IWizardAction action in actions)
  148. {
  149. try
  150. {
  151. action.Configuration = Configuration;
  152. FireActionStarting(action, position);
  153. action.Run();
  154. completedActions.Add(action);
  155. FireActionFinished(action, position++);
  156. }
  157. catch (Exception error)
  158. {
  159. runError = error;
  160. break;
  161. }
  162. }
  163. // If there is an error then reverse all the completed actions and throw an error
  164. if (runError != null)
  165. {
  166. position = 0;
  167. foreach (IWizardAction action in completedActions)
  168. {
  169. FireActionReversing(action, position);
  170. action.Reverse();
  171. FireActionReversed(action, position++);
  172. }
  173. throw new ApplicationException("RunActions() failed", runError);
  174. }
  175. }
  176. #endregion
  177. #endregion
  178. #region Public events
  179. #region ActionStarting
  180. /// <summary>
  181. /// An action is starting to run.
  182. /// </summary>
  183. public event EventHandler<WizardActionEventArgs> ActionStarting;
  184. #endregion
  185. #region ActionFinished
  186. /// <summary>
  187. /// An action has finished its run.
  188. /// </summary>
  189. public event EventHandler<WizardActionEventArgs> ActionFinished;
  190. #endregion
  191. #region ActionReversing
  192. /// <summary>
  193. /// An action is starting to reverse.
  194. /// </summary>
  195. public event EventHandler<WizardActionEventArgs> ActionReversing;
  196. #endregion
  197. #region ActionReversed
  198. /// <summary>
  199. /// An action has been reversed.
  200. /// </summary>
  201. public event EventHandler<WizardActionEventArgs> ActionReversed;
  202. #endregion
  203. #endregion
  204. #region Private methods
  205. #region cancelButton_Click()
  206. /// <summary>
  207. /// The cancel button has been clicked.
  208. /// </summary>
  209. /// <param name="sender"></param>
  210. /// <param name="e"></param>
  211. private void cancelButton_Click(object sender, EventArgs e)
  212. {
  213. if (currentPage.DoCancel()) this.Close();
  214. }
  215. #endregion
  216. #region previousButton_Click()
  217. /// <summary>
  218. /// The previous button has been clicked
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. private void previousButton_Click(object sender, EventArgs e)
  223. {
  224. currentPage.DoPrevious();
  225. }
  226. #endregion
  227. #region nextButton_Click()
  228. /// <summary>
  229. /// The next button has been clicked.
  230. /// </summary>
  231. /// <param name="sender"></param>
  232. /// <param name="e"></param>
  233. private void nextButton_Click(object sender, EventArgs e)
  234. {
  235. currentPage.DoNext();
  236. }
  237. #endregion
  238. #region finishButton_Click()
  239. /// <summary>
  240. /// The finish button has been clicked.
  241. /// </summary>
  242. /// <param name="sender"></param>
  243. /// <param name="e"></param>
  244. private void finishButton_Click(object sender, EventArgs e)
  245. {
  246. if (currentPage.DoFinish()) this.Close();
  247. }
  248. #endregion
  249. #region FireActionStarting
  250. /// <summary>
  251. /// Fires the <see cref="ActionStarting"/> event.
  252. /// </summary>
  253. /// <param name="action">The action that is starting.</param>
  254. /// <param name="position">The position of the action within the queue.</param>
  255. public void FireActionStarting(IWizardAction action, int position)
  256. {
  257. if (ActionStarting != null)
  258. {
  259. WizardActionEventArgs args = new WizardActionEventArgs(action, position);
  260. ActionStarting(this, args);
  261. }
  262. }
  263. #endregion
  264. #region FireActionFinished
  265. /// <summary>
  266. /// Fires the <see cref="ActionFinished"/> event.
  267. /// </summary>
  268. /// <param name="action">The action that has finished.</param>
  269. /// <param name="position">The position of the action within the queue.</param>
  270. public void FireActionFinished(IWizardAction action, int position)
  271. {
  272. if (ActionFinished != null)
  273. {
  274. WizardActionEventArgs args = new WizardActionEventArgs(action, position);
  275. ActionFinished(this, args);
  276. }
  277. }
  278. #endregion
  279. #region FireActionReversing
  280. /// <summary>
  281. /// Fires the <see cref="ActionReversing"/> event.
  282. /// </summary>
  283. /// <param name="action">The action that is reversing.</param>
  284. /// <param name="position">The position of the action within the queue.</param>
  285. public void FireActionReversing(IWizardAction action, int position)
  286. {
  287. if (ActionReversing != null)
  288. {
  289. WizardActionEventArgs args = new WizardActionEventArgs(action, position);
  290. ActionReversing(this, args);
  291. }
  292. }
  293. #endregion
  294. #region FireActionReversed
  295. /// <summary>
  296. /// Fires the <see cref="ActionReversed"/> event.
  297. /// </summary>
  298. /// <param name="action">The action that has reversed.</param>
  299. /// <param name="position">The position of the action within the queue.</param>
  300. public void FireActionReversed(IWizardAction action, int position)
  301. {
  302. if (ActionReversed != null)
  303. {
  304. WizardActionEventArgs args = new WizardActionEventArgs(action, position);
  305. ActionReversed(this, args);
  306. }
  307. }
  308. #endregion
  309. #endregion
  310. }
  311. }