Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.2/HeuristicLab.Optimization.Views/3.3/BatchRunView.cs @ 16381

Last change on this file since 16381 was 4540, checked in by mkommend, 14 years ago

Corrected the status of executable views in the OnContentChanged method (ticket #1225).

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