Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.ExperimentGeneration.DataAnalysis.Wizard/3.3/WizardBase.cs @ 14604

Last change on this file since 14604 was 8024, checked in by spimming, 13 years ago

#1807:

  • fire event when Cancel button clicked
  • support cancellation when generating experiments
  • fixed page sizes
  • plugin dependencies updated
File size: 5.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Linq;
5using System.Windows.Forms;
6
7namespace HeuristicLab.ExperimentGeneration.DataAnalysis.Wizard {
8  public enum WizardButtons {
9    None,
10    Back,
11    Next,
12    Commit
13  }
14  public partial class WizardBase : Form {
15    private IList<WizardPage> wizardPages;
16    public IList<WizardPage> WizardPages {
17      get { return wizardPages; }
18    }
19
20    private WizardPage activePage;
21    public WizardPage ActivePage {
22      get { return activePage; }
23    }
24
25    public WizardBase() {
26      InitializeComponent();
27      wizardPages = new List<WizardPage>();
28    }
29
30    public void SetActivePage(int pageIndex) {
31      if (pageIndex < 0 || pageIndex >= wizardPages.Count) {
32        throw new ArgumentOutOfRangeException("pageIndex");
33      }
34      SetActivePage(wizardPages[pageIndex]);
35    }
36
37    public void SetActivePage(string pageName) {
38      if (string.IsNullOrEmpty(pageName)) {
39        throw new ArgumentException("Parameter must not be null or empty", "pageName");
40      }
41      WizardPage newPage = WizardPages.Single(p => p.Name == pageName);
42      if (newPage == null) {
43        throw new ArgumentNullException(string.Format("Can't find page with name {0}", pageName));
44      } else {
45        SetActivePage(newPage);
46      }
47
48    }
49
50    public void SetActivePage(WizardPageEventArgs wpea) {
51      if (!wpea.Cancel) {
52        SetActivePage(wpea.NewPage);
53      }
54    }
55
56    public void SetActivePage(WizardPage page) {
57      WizardPage oldActivePage = activePage;
58      if (!pagePanel.Controls.Contains(page)) {
59        pagePanel.Controls.Add(page);
60      }
61      page.Visible = true;
62      page.Dock = DockStyle.Fill;
63      activePage = page;
64
65      CancelEventArgs e = new CancelEventArgs();
66      activePage.OnSetActive(e);
67      if (e.Cancel) {
68        activePage.Visible = false;
69        activePage = oldActivePage;
70      }
71
72      foreach (WizardPage wp in WizardPages) {
73        if (wp != activePage) {
74          wp.Visible = false;
75        }
76      }
77    }
78
79    public void SetWizardButton(WizardButtons button) {
80      if (button == WizardButtons.Back) {
81        btnBack.Enabled = true;
82      } else if (button == WizardButtons.Next) {
83        btnCommit.Visible = false;
84        btnCommit.Enabled = false;
85        btnNext.Visible = true;
86        btnNext.Enabled = true;
87        AcceptButton = btnNext;
88      } else if (button == WizardButtons.Commit) {
89        btnCommit.Visible = true;
90        btnCommit.Enabled = true;
91        btnNext.Visible = false;
92        btnNext.Enabled = false;
93        AcceptButton = btnCommit;
94      } else if (button == WizardButtons.None) {
95        btnCommit.Visible = false;
96        btnCommit.Enabled = false;
97        btnNext.Visible = true;
98        btnNext.Enabled = false;
99        btnBack.Enabled = false;
100        AcceptButton = btnNext;
101      }
102    }
103
104    public void SetButtonText(WizardButtons button, string buttonText) {
105      if (button == WizardButtons.Commit) {
106        btnCommit.Text = buttonText;
107      }
108    }
109
110    public void AddPage(WizardPage page) {
111      WizardPages.Add(page);
112    }
113
114    protected virtual void OnQueryCancel(CancelEventArgs e) {
115      activePage.OnQueryCancel(e);
116    }
117
118    private void SetWizardSizeToFitAllPages() {
119      System.Drawing.Size maxPageSize = new System.Drawing.Size(controlPanel.Width, 0);
120      foreach (WizardPage page in WizardPages) {
121        if (page.Width > maxPageSize.Width) {
122          maxPageSize.Width = page.Width;
123        }
124        if (page.Height > maxPageSize.Height) {
125          maxPageSize.Height = page.Height;
126        }
127      }
128      foreach (WizardPage page in WizardPages) {
129        page.Size = maxPageSize;
130      }
131      System.Drawing.Size newSize = maxPageSize + (Size - pagePanel.Size);
132      Size = newSize;
133    }
134
135    private WizardPageEventArgs BuildChangePageEventArgs(int delta) {
136      int activePageIndex = WizardPages.IndexOf(ActivePage);
137      int nextPageIndex = activePageIndex + delta;
138
139      if (nextPageIndex < 0 || nextPageIndex >= WizardPages.Count)
140        nextPageIndex = activePageIndex;
141
142      WizardPage nextPage = WizardPages[nextPageIndex];
143      WizardPageEventArgs wpea = new WizardPageEventArgs();
144      wpea.NewPage = nextPage.Name;
145      wpea.Cancel = false;
146
147      return wpea;
148    }
149
150    private void btnNext_Click(object sender, EventArgs e) {
151      WizardPageEventArgs wpea = BuildChangePageEventArgs(1);
152      ActivePage.OnWizardNext(wpea);
153      SetActivePage(wpea);
154    }
155
156    private void btnBack_Click(object sender, EventArgs e) {
157      WizardPageEventArgs wpea = BuildChangePageEventArgs(-1);
158      ActivePage.OnWizardBack(wpea);
159      SetActivePage(wpea);
160    }
161
162    private void btnCommit_Click(object sender, EventArgs e) {
163      CancelEventArgs cea = new CancelEventArgs();
164      ActivePage.OnWizardCommit(cea);
165      if (!cea.Cancel) {
166        DialogResult = DialogResult.OK;
167        Close();
168      }
169    }
170
171    private void btnCancel_Click(object sender, EventArgs e) {
172      CancelEventArgs cea = new CancelEventArgs();
173      cea.Cancel = true;
174      ActivePage.OnWizardCancel(cea);
175      if (cea.Cancel) {
176        DialogResult = DialogResult.Cancel;
177        Close();
178      }
179    }
180
181    private void Wizard_Load(object sender, EventArgs e) {
182      if (WizardPages.Count > 0) {
183        SetWizardSizeToFitAllPages();
184        SetActivePage(0);
185      } else {
186        SetWizardButton(WizardButtons.None);
187      }
188    }
189
190    private void Wizard_Closing(object sender, CancelEventArgs e) {
191      if (!btnCancel.Enabled) {
192        e.Cancel = true;
193      } else if (btnCommit.Enabled) {
194        OnQueryCancel(e);
195      }
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.