/branch/QueueSecurityConfig/CCNetConfig/CCNetConfig.Core/Wizard/WizardForm.cs
# · C# · 339 lines · 206 code · 28 blank · 105 comment · 18 complexity · 94ff5a4fda4bb040254a374225e0ef7b MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
-
- namespace CCNetConfig.Core.Wizard
- {
- /// <summary>
- /// Displays a sequence of pages.
- /// </summary>
- public partial class WizardForm : Form
- {
- #region Private fields
- private WizardPage currentPage;
- private List<IWizardAction> actions = new List<IWizardAction>();
- #endregion
-
- #region Constructors
- /// <summary>
- /// Initialises a new instance of a <see cref="WizardForm"/>.
- /// </summary>
- private WizardForm()
- {
- InitializeComponent();
- }
- #endregion
-
- #region Public properties
- #region Configuration
- /// <summary>
- /// The current configuration.
- /// </summary>
- public CruiseControl Configuration { get; set; }
- #endregion
-
- #region NumberOfActions
- /// <summary>
- /// The number of actions that have been queued.
- /// </summary>
- public int NumberOfActions
- {
- get { return actions.Count; }
- }
- #endregion
-
- #region DefaultImage
- /// <summary>
- /// The default image to show if the page doesn't have an image.
- /// </summary>
- public Image DefaultImage { get; set; }
- #endregion
- #endregion
-
- #region Public methods
- #region Show()
- /// <summary>
- /// Displays the wizard.
- /// </summary>
- /// <param name="startPage">The first page of the wizard.</param>
- /// <param name="title">The title of the wizard.</param>
- /// <param name="configuration">The current configuration.</param>
- /// <param name="owner">The form that owns the wizard.</param>
- public static void Show(WizardPage startPage, string title, CruiseControl configuration, Form owner)
- {
- Show(startPage, title, configuration, owner, null);
- }
-
- /// <summary>
- /// Displays the wizard.
- /// </summary>
- /// <param name="startPage">The first page of the wizard.</param>
- /// <param name="title">The title of the wizard.</param>
- /// <param name="configuration">The current configuration.</param>
- /// <param name="owner">The form that owns the wizard.</param>
- /// <param name="defaultImage">The default image to use.</param>
- public static void Show(WizardPage startPage, string title, CruiseControl configuration, Form owner, Image defaultImage)
- {
- WizardForm form = new WizardForm();
- if (defaultImage != null) form.DefaultImage = defaultImage;
- form.Configuration = configuration;
- form.Text = title;
- form.LoadPage(startPage);
- form.ShowDialog(owner);
- }
- #endregion
-
- #region LoadPage()
- /// <summary>
- /// Loads a new page.
- /// </summary>
- /// <param name="page">The page to load.</param>
- public virtual void LoadPage(WizardPage page)
- {
- // Hook up all the properties and load the control
- currentPage = page;
- page.Wizard = this;
- pagePanel.Controls.Clear();
- pagePanel.Controls.Add(page);
- page.Dock = DockStyle.Fill;
- CheckButtons();
-
- // Check if the image needs to be set
- if ((page.HeaderImage == null) && (DefaultImage != null))
- {
- page.HeaderImage = DefaultImage;
- }
-
- // Tell the page that it is now displayed
- page.Display();
- }
- #endregion
-
- #region CheckButtons()
- /// <summary>
- /// Checks the buttons to see what state they should be in.
- /// </summary>
- public virtual void CheckButtons()
- {
- cancelButton.Enabled = currentPage.IsCancelEnabled;
- previousButton.Enabled = currentPage.IsPreviousEnabled;
- nextButton.Enabled = currentPage.IsNextEnabled;
- finishButton.Enabled = currentPage.IsFinishEnabled;
- }
- #endregion
-
- #region AddAction()
- /// <summary>
- /// Adds an action to the actions to be performed.
- /// </summary>
- /// <param name="action">The action to add.</param>
- public virtual void AddAction(IWizardAction action)
- {
- actions.Add(action);
- }
- #endregion
-
- #region RemoveAction()
- /// <summary>
- /// Removes an action from the actions to be performed.
- /// </summary>
- /// <param name="action">The action to remove.</param>
- public virtual void RemoveAction(IWizardAction action)
- {
- actions.Remove(action);
- }
- #endregion
-
- #region RunActions()
- /// <summary>
- /// Runs all the actions and clears the list.
- /// </summary>
- public virtual void RunActions()
- {
- List<IWizardAction> completedActions = new List<IWizardAction>();
- Exception runError = null;
-
- // Attempt to run each action
- int position = 0;
- foreach (IWizardAction action in actions)
- {
- try
- {
- action.Configuration = Configuration;
- FireActionStarting(action, position);
- action.Run();
- completedActions.Add(action);
- FireActionFinished(action, position++);
- }
- catch (Exception error)
- {
- runError = error;
- break;
- }
- }
-
- // If there is an error then reverse all the completed actions and throw an error
- if (runError != null)
- {
- position = 0;
- foreach (IWizardAction action in completedActions)
- {
- FireActionReversing(action, position);
- action.Reverse();
- FireActionReversed(action, position++);
- }
- throw new ApplicationException("RunActions() failed", runError);
- }
- }
- #endregion
- #endregion
-
- #region Public events
- #region ActionStarting
- /// <summary>
- /// An action is starting to run.
- /// </summary>
- public event EventHandler<WizardActionEventArgs> ActionStarting;
- #endregion
-
- #region ActionFinished
- /// <summary>
- /// An action has finished its run.
- /// </summary>
- public event EventHandler<WizardActionEventArgs> ActionFinished;
- #endregion
-
- #region ActionReversing
- /// <summary>
- /// An action is starting to reverse.
- /// </summary>
- public event EventHandler<WizardActionEventArgs> ActionReversing;
- #endregion
-
- #region ActionReversed
- /// <summary>
- /// An action has been reversed.
- /// </summary>
- public event EventHandler<WizardActionEventArgs> ActionReversed;
- #endregion
- #endregion
-
- #region Private methods
- #region cancelButton_Click()
- /// <summary>
- /// The cancel button has been clicked.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void cancelButton_Click(object sender, EventArgs e)
- {
- if (currentPage.DoCancel()) this.Close();
- }
- #endregion
-
- #region previousButton_Click()
- /// <summary>
- /// The previous button has been clicked
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void previousButton_Click(object sender, EventArgs e)
- {
- currentPage.DoPrevious();
- }
- #endregion
-
- #region nextButton_Click()
- /// <summary>
- /// The next button has been clicked.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void nextButton_Click(object sender, EventArgs e)
- {
- currentPage.DoNext();
- }
- #endregion
-
- #region finishButton_Click()
- /// <summary>
- /// The finish button has been clicked.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void finishButton_Click(object sender, EventArgs e)
- {
- if (currentPage.DoFinish()) this.Close();
- }
- #endregion
-
- #region FireActionStarting
- /// <summary>
- /// Fires the <see cref="ActionStarting"/> event.
- /// </summary>
- /// <param name="action">The action that is starting.</param>
- /// <param name="position">The position of the action within the queue.</param>
- public void FireActionStarting(IWizardAction action, int position)
- {
- if (ActionStarting != null)
- {
- WizardActionEventArgs args = new WizardActionEventArgs(action, position);
- ActionStarting(this, args);
- }
- }
- #endregion
-
- #region FireActionFinished
- /// <summary>
- /// Fires the <see cref="ActionFinished"/> event.
- /// </summary>
- /// <param name="action">The action that has finished.</param>
- /// <param name="position">The position of the action within the queue.</param>
- public void FireActionFinished(IWizardAction action, int position)
- {
- if (ActionFinished != null)
- {
- WizardActionEventArgs args = new WizardActionEventArgs(action, position);
- ActionFinished(this, args);
- }
- }
- #endregion
-
- #region FireActionReversing
- /// <summary>
- /// Fires the <see cref="ActionReversing"/> event.
- /// </summary>
- /// <param name="action">The action that is reversing.</param>
- /// <param name="position">The position of the action within the queue.</param>
- public void FireActionReversing(IWizardAction action, int position)
- {
- if (ActionReversing != null)
- {
- WizardActionEventArgs args = new WizardActionEventArgs(action, position);
- ActionReversing(this, args);
- }
- }
- #endregion
-
- #region FireActionReversed
- /// <summary>
- /// Fires the <see cref="ActionReversed"/> event.
- /// </summary>
- /// <param name="action">The action that has reversed.</param>
- /// <param name="position">The position of the action within the queue.</param>
- public void FireActionReversed(IWizardAction action, int position)
- {
- if (ActionReversed != null)
- {
- WizardActionEventArgs args = new WizardActionEventArgs(action, position);
- ActionReversed(this, args);
- }
- }
- #endregion
- #endregion
- }
- }