Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893)

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