Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 3694

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

Implemented reviewers' comments (#893)

File size: 10.5 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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
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  [View("BatchRun View")]
35  [Content(typeof(BatchRun), true)]
36  public sealed partial class BatchRunView : NamedItemView {
37    private TypeSelectorDialog algorithmTypeSelectorDialog;
38
39    public new BatchRun Content {
40      get { return (BatchRun)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public BatchRunView() {
48      InitializeComponent();
49    }
50
51    protected override void DeregisterContentEvents() {
52      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
53      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
54      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
55      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
56      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
62      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
63      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
64      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
65      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content == null) {
71        repetitionsNumericUpDown.Value = 1;
72        algorithmViewHost.Content = null;
73        runsView.Content = null;
74        executionTimeTextBox.Text = "-";
75      } else {
76        repetitionsNumericUpDown.Value = Content.Repetitions;
77        algorithmViewHost.ViewType = null;
78        algorithmViewHost.Content = Content.Algorithm;
79        runsView.Content = Content.Runs;
80        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
81      }
82      SetEnableStateOfControls();
83    }
84    protected override void OnReadOnlyChanged() {
85      base.OnReadOnlyChanged();
86      SetEnableStateOfControls();
87    }
88    private void SetEnableStateOfControls() {
89      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
90      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
91      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
92      algorithmViewHost.Enabled = Content != null;
93      runsView.Enabled = Content != null;
94      executionTimeTextBox.Enabled = Content != null;
95      SetEnabledStateOfExecutableButtons();
96    }
97
98    protected override void OnClosed(FormClosedEventArgs e) {
99      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
100      base.OnClosed(e);
101    }
102
103    #region Content Events
104    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
105      if (InvokeRequired)
106        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
107      else {
108        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
109        Locked = Content.ExecutionState == ExecutionState.Started;
110        SetEnabledStateOfExecutableButtons();
111      }
112    }
113    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
116      else
117        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
118    }
119    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
120      if (InvokeRequired)
121        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
122      else
123        Auxiliary.ShowErrorMessageBox(e.Value);
124    }
125    private void Content_AlgorithmChanged(object sender, EventArgs e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
128      else {
129        algorithmViewHost.ViewType = null;
130        algorithmViewHost.Content = Content.Algorithm;
131      }
132    }
133    private void Content_RepetitionsChanged(object sender, EventArgs e) {
134      if (InvokeRequired)
135        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
136      else
137        repetitionsNumericUpDown.Value = Content.Repetitions;
138    }
139    #endregion
140
141    #region Control events
142    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
143      if (repetitionsNumericUpDown.Text == string.Empty)
144        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
145    }
146    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
147      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
148    }
149    private void newAlgorithmButton_Click(object sender, EventArgs e) {
150      if (algorithmTypeSelectorDialog == null) {
151        algorithmTypeSelectorDialog = new TypeSelectorDialog();
152        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
153        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
154        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
155      }
156      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
157        try {
158          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
159        }
160        catch (Exception ex) {
161          Auxiliary.ShowErrorMessageBox(ex);
162        }
163      }
164    }
165    private void openAlgorithmButton_Click(object sender, EventArgs e) {
166      openFileDialog.Title = "Open Algorithm";
167      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
168        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
169        algorithmViewHost.Enabled = false;
170
171        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
172          try {
173            if (error != null) throw error;
174            IAlgorithm algorithm = content as IAlgorithm;
175            if (algorithm == null)
176              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
177            else
178              Content.Algorithm = algorithm;
179          }
180          catch (Exception ex) {
181            Auxiliary.ShowErrorMessageBox(ex);
182          }
183          finally {
184            Invoke(new Action(delegate() {
185              algorithmViewHost.Enabled = true;
186              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
187            }));
188          }
189        });
190      }
191    }
192    private void startButton_Click(object sender, EventArgs e) {
193      Content.Start();
194    }
195    private void pauseButton_Click(object sender, EventArgs e) {
196      Content.Pause();
197    }
198    private void stopButton_Click(object sender, EventArgs e) {
199      Content.Stop();
200    }
201    private void resetButton_Click(object sender, EventArgs e) {
202      if (Content.Runs.Count > 0) {
203        if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
204          Content.Prepare(true);
205        else
206          Content.Prepare(false);
207      } else {
208        Content.Prepare();
209      }
210    }
211    private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
212      e.Effect = DragDropEffects.None;
213      if (ReadOnly)
214        return;
215      Type type = e.Data.GetData("Type") as Type;
216      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
217        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
218        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
219        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
220        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
221        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
222      }
223    }
224    private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
225      if (e.Effect != DragDropEffects.None) {
226        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
227        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
228        Content.Algorithm = algorithm;
229      }
230    }
231    #endregion
232
233    #region Helpers
234    private void SetEnabledStateOfExecutableButtons() {
235      if (Content == null) {
236        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
237      } else {
238        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
239        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
240        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
241        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
242      }
243    }
244    #endregion
245  }
246}
Note: See TracBrowser for help on using the repository browser.