Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.0 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.PluginInfrastructure;
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.Content = Content.Algorithm;
78        runsView.Content = Content.Runs;
79        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
80      }
81    }
82    protected override void SetEnabledStateOfControls() {
83      base.SetEnabledStateOfControls();
84      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
85      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
86      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
87      algorithmViewHost.Enabled = Content != null;
88      runsView.Enabled = Content != null;
89      executionTimeTextBox.Enabled = Content != null;
90      SetEnabledStateOfExecutableButtons();
91    }
92
93    protected override void OnClosed(FormClosedEventArgs e) {
94      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
95      base.OnClosed(e);
96    }
97
98    #region Content Events
99    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
100      if (InvokeRequired)
101        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
102      else {
103        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
104        Locked = Content.ExecutionState == ExecutionState.Started;
105        SetEnabledStateOfExecutableButtons();
106      }
107    }
108    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
109      if (InvokeRequired)
110        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
111      else
112        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
113    }
114    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
117      else
118        ErrorHandling.ShowErrorDialog(this, e.Value);
119    }
120    private void Content_AlgorithmChanged(object sender, EventArgs e) {
121      if (InvokeRequired)
122        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
123      else {
124        algorithmViewHost.Content = Content.Algorithm;
125      }
126    }
127    private void Content_RepetitionsChanged(object sender, EventArgs e) {
128      if (InvokeRequired)
129        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
130      else
131        repetitionsNumericUpDown.Value = Content.Repetitions;
132    }
133    #endregion
134
135    #region Control events
136    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
137      if (repetitionsNumericUpDown.Text == string.Empty)
138        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
139    }
140    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
141      if (Content != null)
142        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
143    }
144    private void newAlgorithmButton_Click(object sender, EventArgs e) {
145      if (algorithmTypeSelectorDialog == null) {
146        algorithmTypeSelectorDialog = new TypeSelectorDialog();
147        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
148        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
149        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
150      }
151      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
152        try {
153          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
154        }
155        catch (Exception ex) {
156          ErrorHandling.ShowErrorDialog(this, ex);
157        }
158      }
159    }
160    private void openAlgorithmButton_Click(object sender, EventArgs e) {
161      openFileDialog.Title = "Open Algorithm";
162      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
163        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
164        algorithmViewHost.Enabled = false;
165
166        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
167          try {
168            if (error != null) throw error;
169            IAlgorithm algorithm = content as IAlgorithm;
170            if (algorithm == null)
171              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
172            else
173              Content.Algorithm = algorithm;
174          }
175          catch (Exception ex) {
176            ErrorHandling.ShowErrorDialog(this, ex);
177          }
178          finally {
179            Invoke(new Action(delegate() {
180              algorithmViewHost.Enabled = true;
181              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
182            }));
183          }
184        });
185      }
186    }
187    private void startButton_Click(object sender, EventArgs e) {
188      Content.Start();
189    }
190    private void pauseButton_Click(object sender, EventArgs e) {
191      Content.Pause();
192    }
193    private void stopButton_Click(object sender, EventArgs e) {
194      Content.Stop();
195    }
196    private void resetButton_Click(object sender, EventArgs e) {
197      Content.Prepare(false);
198    }
199    private void algorithmViewHost_DragEnterOver(object sender, DragEventArgs e) {
200      e.Effect = DragDropEffects.None;
201      if (ReadOnly)
202        return;
203      Type type = e.Data.GetData("Type") as Type;
204      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
205        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
206        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
207        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
208        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
209        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
210      }
211    }
212    private void algorithmViewHost_DragDrop(object sender, DragEventArgs e) {
213      if (e.Effect != DragDropEffects.None) {
214        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
215        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
216        Content.Algorithm = algorithm;
217      }
218    }
219    #endregion
220
221    #region Helpers
222    private void SetEnabledStateOfExecutableButtons() {
223      if (Content == null) {
224        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
225      } else {
226        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
227        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
228        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
229        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
230      }
231    }
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.