Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4447 was 4216, checked in by swagner, 14 years ago

Fixed exception which was thrown when hiding views while the execution time is updated (#1148)

File size: 12.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;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
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
53    protected override void OnInitialized(EventArgs e) {
54      // Set order of tab pages according to z order.
55      // NOTE: This is required due to a bug in the VS designer.
56      List<Control> tabPages = new List<Control>();
57      for (int i = 0; i < tabControl.Controls.Count; i++) {
58        tabPages.Add(tabControl.Controls[i]);
59      }
60      tabControl.Controls.Clear();
61      foreach (Control control in tabPages)
62        tabControl.Controls.Add(control);
63
64      base.OnInitialized(e);
65    }
66
67    protected override void DeregisterContentEvents() {
68      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
69      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
70      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
71      Content.Prepared -= new EventHandler(Content_Prepared);
72      Content.Started -= new EventHandler(Content_Started);
73      Content.Paused -= new EventHandler(Content_Paused);
74      Content.Stopped -= new EventHandler(Content_Stopped);
75      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
76      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
77      base.DeregisterContentEvents();
78    }
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
82      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
83      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
84      Content.Prepared += new EventHandler(Content_Prepared);
85      Content.Started += new EventHandler(Content_Started);
86      Content.Paused += new EventHandler(Content_Paused);
87      Content.Stopped += new EventHandler(Content_Stopped);
88      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
89      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
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        storeAlgorithmInEachRunCheckBox.Checked = true;
100        executionTimeTextBox.Text = "-";
101      } else {
102        parameterCollectionView.Content = Content.Parameters;
103        problemViewHost.ViewType = null;
104        problemViewHost.Content = Content.Problem;
105        resultsView.Content = Content.Results.AsReadOnly();
106        runsView.Content = Content.Runs;
107        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
108        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
109      }
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      parameterCollectionView.Enabled = Content != null;
115      newProblemButton.Enabled = Content != null && !ReadOnly;
116      openProblemButton.Enabled = Content != null && !ReadOnly;
117      problemViewHost.Enabled = Content != null;
118      resultsView.Enabled = Content != null;
119      runsView.Enabled = Content != null;
120      storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
121      executionTimeTextBox.Enabled = Content != null;
122      SetEnabledStateOfExecutableButtons();
123    }
124
125    protected override void OnClosed(FormClosedEventArgs e) {
126      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
127      base.OnClosed(e);
128    }
129
130    #region Content Events
131    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
134      else {
135        problemViewHost.ViewType = null;
136        problemViewHost.Content = Content.Problem;
137      }
138    }
139    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
140      if (InvokeRequired)
141        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
142      else
143        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
144    }
145    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
148      else
149        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
150    }
151    protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
152      if (InvokeRequired)
153        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
154      else
155        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
156    }
157    protected virtual void Content_Prepared(object sender, EventArgs e) {
158      if (InvokeRequired)
159        Invoke(new EventHandler(Content_Prepared), sender, e);
160      else {
161        resultsView.Content = Content.Results.AsReadOnly();
162        ReadOnly = Locked = false;
163        SetEnabledStateOfExecutableButtons();
164      }
165    }
166    protected virtual void Content_Started(object sender, EventArgs e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler(Content_Started), sender, e);
169      else {
170        ReadOnly = Locked = true;
171        SetEnabledStateOfExecutableButtons();
172      }
173    }
174    protected virtual void Content_Paused(object sender, EventArgs e) {
175      if (InvokeRequired)
176        Invoke(new EventHandler(Content_Paused), sender, e);
177      else {
178        ReadOnly = Locked = false;
179        SetEnabledStateOfExecutableButtons();
180      }
181    }
182    protected virtual void Content_Stopped(object sender, EventArgs e) {
183      if (InvokeRequired)
184        Invoke(new EventHandler(Content_Stopped), sender, e);
185      else {
186        ReadOnly = Locked = false;
187        SetEnabledStateOfExecutableButtons();
188      }
189    }
190    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
191      if (InvokeRequired)
192        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
193      else
194        ErrorHandling.ShowErrorDialog(this, e.Value);
195    }
196    #endregion
197
198    #region Control Events
199    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
200      if (problemTypeSelectorDialog == null) {
201        problemTypeSelectorDialog = new TypeSelectorDialog();
202        problemTypeSelectorDialog.Caption = "Select Problem";
203        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
204        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
205      }
206      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
207        try {
208          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
209        }
210        catch (Exception ex) {
211          ErrorHandling.ShowErrorDialog(this, ex);
212        }
213      }
214    }
215    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
216      openFileDialog.Title = "Open Problem";
217      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
218        newProblemButton.Enabled = openProblemButton.Enabled = false;
219        problemViewHost.Enabled = false;
220
221        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
222          try {
223            if (error != null) throw error;
224            IProblem problem = content as IProblem;
225            if (problem == null)
226              Invoke(new Action(() =>
227                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
228            else if (!Content.ProblemType.IsInstanceOfType(problem))
229              Invoke(new Action(() =>
230                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
231            else
232              Content.Problem = problem;
233          }
234          catch (Exception ex) {
235            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
236          }
237          finally {
238            Invoke(new Action(delegate() {
239              problemViewHost.Enabled = true;
240              newProblemButton.Enabled = openProblemButton.Enabled = true;
241            }));
242          }
243        });
244      }
245    }
246    protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
247      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
248    }
249    protected virtual void startButton_Click(object sender, EventArgs e) {
250      Content.Start();
251    }
252    protected virtual void pauseButton_Click(object sender, EventArgs e) {
253      Content.Pause();
254    }
255    protected virtual void stopButton_Click(object sender, EventArgs e) {
256      Content.Stop();
257    }
258    protected virtual void resetButton_Click(object sender, EventArgs e) {
259      Content.Prepare(false);
260    }
261    protected virtual void problemViewHost_DragEnterOver(object sender, DragEventArgs e) {
262      e.Effect = DragDropEffects.None;
263      Type type = e.Data.GetData("Type") as Type;
264      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
265        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
266        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
267        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
268        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
269        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
270      }
271    }
272    protected virtual void problemViewHost_DragDrop(object sender, DragEventArgs e) {
273      if (e.Effect != DragDropEffects.None) {
274        IProblem problem = e.Data.GetData("Value") as IProblem;
275        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
276        Content.Problem = problem;
277      }
278    }
279    #endregion
280
281    #region Helpers
282    private void SetEnabledStateOfExecutableButtons() {
283      if (Content == null) {
284        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
285      } else {
286        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
287        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
288        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
289        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
290      }
291    }
292    #endregion
293  }
294}
Note: See TracBrowser for help on using the repository browser.