Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4049 was 4011, checked in by mkommend, 14 years ago
  • refactored ViewHost and various views to use fewer nested controls
  • added UnitTests for ContentViews to ensure proper using of the ContentAttribute
  • fixed some views which could not handle null as Content

(ticket #972)

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