Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs @ 2916

Last change on this file since 2916 was 2916, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on engines and algorithms
File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [Content(typeof(Algorithm), true)]
35  [Content(typeof(IAlgorithm), false)]
36  public partial class AlgorithmView : NamedItemView {
37    private TypeSelectorDialog problemTypeSelectorDialog;
38    private int executionTimeCounter;
39
40    public new IAlgorithm Content {
41      get { return (IAlgorithm)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="ItemBaseView"/>.
47    /// </summary>
48    public AlgorithmView() {
49      InitializeComponent();
50    }
51    /// <summary>
52    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
53    /// </summary>
54    /// <param name="item">The item that should be displayed.</param>
55    public AlgorithmView(IAlgorithm content)
56      : this() {
57      Content = content;
58    }
59
60    protected override void OnInitialized(EventArgs e) {
61      // Set order of tab pages according to z order.
62      // NOTE: This is required due to a bug in the VS designer.
63      List<Control> tabPages = new List<Control>();
64      for (int i = 0; i < tabControl.Controls.Count; i++) {
65        tabPages.Add(tabControl.Controls[i]);
66      }
67      tabControl.Controls.Clear();
68      foreach (Control control in tabPages)
69        tabControl.Controls.Add(control);
70
71      base.OnInitialized(e);
72    }
73
74    protected override void DeregisterContentEvents() {
75      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
76      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
77      Content.Prepared -= new EventHandler(Content_Prepared);
78      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
79      Content.Started -= new EventHandler(Content_Started);
80      Content.Stopped -= new EventHandler(Content_Stopped);
81      base.DeregisterContentEvents();
82    }
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
86      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
87      Content.Prepared += new EventHandler(Content_Prepared);
88      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
89      Content.Started += new EventHandler(Content_Started);
90      Content.Stopped += new EventHandler(Content_Stopped);
91    }
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95      stopButton.Enabled = false;
96      if (Content == null) {
97        parameterCollectionView.Content = null;
98        problemViewHost.Content = null;
99        resultsView.Content = null;
100        tabControl.Enabled = false;
101        startButton.Enabled = resetButton.Enabled = false;
102        executionTimeTextBox.Text = "-";
103        executionTimeTextBox.Enabled = false;
104      } else {
105        parameterCollectionView.Content = Content.Parameters;
106        saveProblemButton.Enabled = Content.Problem != null;
107        problemViewHost.Content = Content.Problem;
108        resultsView.Content = Content.Results;
109        tabControl.Enabled = true;
110        startButton.Enabled = !Content.Finished;
111        resetButton.Enabled = true;
112        UpdateExecutionTimeTextBox();
113        executionTimeTextBox.Enabled = true;
114      }
115    }
116
117    #region Content Events
118    protected virtual void Content_Prepared(object sender, EventArgs e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler(Content_Prepared), sender, e);
121      else {
122        parameterCollectionView.Enabled = true;
123        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
124        problemViewHost.Enabled = true;
125        resultsView.Content = Content.Results;
126        resultsView.Enabled = true;
127        startButton.Enabled = !Content.Finished;
128        stopButton.Enabled = false;
129        resetButton.Enabled = true;
130        UpdateExecutionTimeTextBox();
131      }
132    }
133    protected void Content_ProblemChanged(object sender, EventArgs e) {
134      if (InvokeRequired)
135        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
136      else {
137        problemViewHost.Content = Content.Problem;
138        saveProblemButton.Enabled = Content.Problem != null;
139      }
140    }
141    protected virtual void Content_Started(object sender, EventArgs e) {
142      executionTimeCounter = 0;
143      if (InvokeRequired)
144        Invoke(new EventHandler(Content_Started), sender, e);
145      else {
146        parameterCollectionView.Enabled = false;
147        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
148        problemViewHost.Enabled = false;
149        resultsView.Enabled = false;
150        startButton.Enabled = false;
151        stopButton.Enabled = true;
152        resetButton.Enabled = false;
153        UpdateExecutionTimeTextBox();
154      }
155    }
156    protected virtual void Content_Stopped(object sender, EventArgs e) {
157      if (InvokeRequired)
158        Invoke(new EventHandler(Content_Stopped), sender, e);
159      else {
160        parameterCollectionView.Enabled = true;
161        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
162        problemViewHost.Enabled = true;
163        resultsView.Enabled = true;
164        startButton.Enabled = !Content.Finished;
165        stopButton.Enabled = false;
166        resetButton.Enabled = true;
167        UpdateExecutionTimeTextBox();
168      }
169    }
170    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
171      executionTimeCounter++;
172      if ((executionTimeCounter == 100) || !Content.Running) {
173        executionTimeCounter = 0;
174        UpdateExecutionTimeTextBox();
175      }
176    }
177    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
178      if (InvokeRequired)
179        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
180      else
181        Auxiliary.ShowErrorMessageBox(e.Value);
182    }
183    #endregion
184
185    #region Button events
186    protected void newProblemButton_Click(object sender, EventArgs e) {
187      if (problemTypeSelectorDialog == null) {
188        problemTypeSelectorDialog = new TypeSelectorDialog();
189        problemTypeSelectorDialog.Caption = "Select Problem";
190        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
191      }
192      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
193        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
194      }
195    }
196    protected void openProblemButton_Click(object sender, EventArgs e) {
197      openFileDialog.Title = "Open Problem";
198      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
199        IProblem problem = null;
200        try {
201          problem = XmlParser.Deserialize(openFileDialog.FileName) as IProblem;
202        }
203        catch (Exception ex) {
204          Auxiliary.ShowErrorMessageBox(ex);
205        }
206        if (problem == null)
207          MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
208        else if (!Content.ProblemType.IsInstanceOfType(problem))
209          MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
210        else
211          Content.Problem = problem;
212      }
213    }
214    protected void saveProblemButton_Click(object sender, EventArgs e) {
215      saveFileDialog.Title = "Save Problem";
216      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
217        try {
218          if (saveFileDialog.FilterIndex == 1)
219            XmlGenerator.Serialize(Content.Problem, saveFileDialog.FileName, 0);
220          else
221            XmlGenerator.Serialize(Content.Problem, saveFileDialog.FileName, 9);
222        }
223        catch (Exception ex) {
224          Auxiliary.ShowErrorMessageBox(ex);
225        }
226      }
227    }
228    protected virtual void startButton_Click(object sender, EventArgs e) {
229      Content.Start();
230    }
231    protected virtual void stopButton_Click(object sender, EventArgs e) {
232      Content.Stop();
233    }
234    protected virtual void resetButton_Click(object sender, EventArgs e) {
235      Content.Prepare();
236    }
237    #endregion
238
239    #region Helpers
240    protected virtual void UpdateExecutionTimeTextBox() {
241      if (InvokeRequired)
242        Invoke(new Action(UpdateExecutionTimeTextBox));
243      else
244        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
245    }
246    #endregion
247  }
248}
Note: See TracBrowser for help on using the repository browser.