Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3714 was 3709, checked in by mkommend, 15 years ago

corrected collection views and ViewHost (ticket #972)

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