Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on algorithm batch processing (#947).

File size: 11.2 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;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Persistence.Default.Xml;
30
31namespace HeuristicLab.Optimization.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("Algorithm View")]
36  [Content(typeof(Algorithm), true)]
37  [Content(typeof(IAlgorithm), false)]
38  public partial class AlgorithmView : NamedItemView {
39    private TypeSelectorDialog problemTypeSelectorDialog;
40
41    public new IAlgorithm Content {
42      get { return (IAlgorithm)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="ItemBaseView"/>.
48    /// </summary>
49    public AlgorithmView() {
50      InitializeComponent();
51    }
52    /// <summary>
53    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
54    /// </summary>
55    /// <param name="item">The item that should be displayed.</param>
56    public AlgorithmView(IAlgorithm content)
57      : this() {
58      Content = content;
59    }
60
61    protected override void OnInitialized(EventArgs e) {
62      // Set order of tab pages according to z order.
63      // NOTE: This is required due to a bug in the VS designer.
64      List<Control> tabPages = new List<Control>();
65      for (int i = 0; i < tabControl.Controls.Count; i++) {
66        tabPages.Add(tabControl.Controls[i]);
67      }
68      tabControl.Controls.Clear();
69      foreach (Control control in tabPages)
70        tabControl.Controls.Add(control);
71
72      base.OnInitialized(e);
73    }
74
75    protected override void DeregisterContentEvents() {
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
77      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
78      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
79      Content.Prepared -= new EventHandler(Content_Prepared);
80      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
81      base.DeregisterContentEvents();
82    }
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
86      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
87      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
88      Content.Prepared += new EventHandler(Content_Prepared);
89      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
90    }
91
92    protected override void OnContentChanged() {
93      base.OnContentChanged();
94      if (Content == null) {
95        parameterCollectionView.Content = null;
96        problemViewHost.Content = null;
97        resultsView.Content = null;
98        runsView.Content = null;
99        tabControl.Enabled = false;
100        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
101        executionTimeTextBox.Text = "-";
102        executionTimeTextBox.Enabled = false;
103      } else {
104        parameterCollectionView.Content = Content.Parameters;
105        saveProblemButton.Enabled = Content.Problem != null;
106        problemViewHost.ViewType = null;
107        problemViewHost.Content = Content.Problem;
108        resultsView.Content = Content.Results.AsReadOnly();
109        runsView.Content = Content.Runs;
110        tabControl.Enabled = true;
111        EnableDisableButtons();
112        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
113        executionTimeTextBox.Enabled = true;
114      }
115    }
116
117    protected override void OnClosed(FormClosedEventArgs e) {
118      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
119      base.OnClosed(e);
120    }
121
122    #region Content Events
123    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
126      else {
127        problemViewHost.ViewType = null;
128        problemViewHost.Content = Content.Problem;
129        saveProblemButton.Enabled = Content.Problem != null;
130      }
131    }
132    protected virtual void Content_Prepared(object sender, EventArgs e) {
133      if (InvokeRequired)
134        Invoke(new EventHandler(Content_Prepared), sender, e);
135      else
136        resultsView.Content = Content.Results.AsReadOnly();
137    }
138    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
141      else {
142        nameTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
143        descriptionTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
144        SaveEnabled = Content.ExecutionState != ExecutionState.Started;
145        parameterCollectionView.Enabled = Content.ExecutionState != ExecutionState.Started;
146        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = Content.ExecutionState != ExecutionState.Started;
147        problemViewHost.Enabled = Content.ExecutionState != ExecutionState.Started;
148        EnableDisableButtons();
149      }
150    }
151    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
152      if (InvokeRequired)
153        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
154      else
155        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
156    }
157    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
158      if (InvokeRequired)
159        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
160      else
161        Auxiliary.ShowErrorMessageBox(e.Value);
162    }
163    #endregion
164
165    #region Button events
166    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
167      if (problemTypeSelectorDialog == null) {
168        problemTypeSelectorDialog = new TypeSelectorDialog();
169        problemTypeSelectorDialog.Caption = "Select Problem";
170        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
171      }
172      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
173        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
174      }
175    }
176    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
177      openFileDialog.Title = "Open Problem";
178      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
179        this.Cursor = Cursors.AppStarting;
180        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
181        problemViewHost.Enabled = false;
182
183        var call = new Func<string, object>(XmlParser.Deserialize);
184        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
185          IProblem problem = null;
186          try {
187            problem = call.EndInvoke(a) as IProblem;
188          } catch (Exception ex) {
189            Auxiliary.ShowErrorMessageBox(ex);
190          }
191          Invoke(new Action(delegate() {
192            if (problem == null)
193              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
194            else if (!Content.ProblemType.IsInstanceOfType(problem))
195              MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
196            else
197              Content.Problem = problem;
198            problemViewHost.Enabled = true;
199            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
200            this.Cursor = Cursors.Default;
201          }));
202        }, null);
203      }
204    }
205    protected virtual void saveProblemButton_Click(object sender, EventArgs e) {
206      saveFileDialog.Title = "Save Problem";
207      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
208        this.Cursor = Cursors.AppStarting;
209        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
210        problemViewHost.Enabled = false;
211
212        var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
213        int compression = 9;
214        if (saveFileDialog.FilterIndex == 1) compression = 0;
215        call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
216          try {
217            call.EndInvoke(a);
218          }
219          catch (Exception ex) {
220            Auxiliary.ShowErrorMessageBox(ex);
221          }
222          Invoke(new Action(delegate() {
223            problemViewHost.Enabled = true;
224            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
225            this.Cursor = Cursors.Default;
226          }));
227        }, null);
228      }
229    }
230    protected virtual void startButton_Click(object sender, EventArgs e) {
231      Content.Start();
232    }
233    protected virtual void pauseButton_Click(object sender, EventArgs e) {
234      Content.Pause();
235    }
236    protected virtual void stopButton_Click(object sender, EventArgs e) {
237      Content.Stop();
238    }
239    protected virtual void resetButton_Click(object sender, EventArgs e) {
240      if (Content.Runs.Count > 0) {
241        if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
242          Content.Prepare(true);
243        else
244          Content.Prepare(false);
245      } else {
246        Content.Prepare();
247      }
248    }
249    #endregion
250
251    #region Helpers
252    private void EnableDisableButtons() {
253      startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
254      pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
255      stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
256      resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
257    }
258    #endregion
259  }
260}
Note: See TracBrowser for help on using the repository browser.