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
RevLine 
[3226]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;
[3265]25using HeuristicLab.Core;
[3226]26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Persistence.Default.Xml;
[3758]29using HeuristicLab.PluginInfrastructure;
[3226]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() {
[3265]53      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
[3226]54      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]55      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[3226]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();
[3265]62      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
[3226]63      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]64      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[3226]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;
[3260]74        runsView.Content = null;
[3226]75        executionTimeTextBox.Text = "-";
76      } else {
77        repetitionsNumericUpDown.Value = Content.Repetitions;
78        algorithmViewHost.Content = Content.Algorithm;
[3260]79        runsView.Content = Content.Runs;
[3265]80        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]81      }
82    }
[3904]83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
[3454]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();
[3367]92    }
[3226]93
94    protected override void OnClosed(FormClosedEventArgs e) {
[3265]95      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[3226]96      base.OnClosed(e);
97    }
98
99    #region Content Events
[3265]100    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
[3226]101      if (InvokeRequired)
[3265]102        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
[3226]103      else {
[3367]104        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
[3416]105        Locked = Content.ExecutionState == ExecutionState.Started;
[3454]106        SetEnabledStateOfExecutableButtons();
[3226]107      }
108    }
[3265]109    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3226]110      if (InvokeRequired)
[3265]111        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
112      else
113        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]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
[3758]119        ErrorHandling.ShowErrorDialog(this, e.Value);
[3226]120    }
[3265]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    }
[3226]128    private void Content_RepetitionsChanged(object sender, EventArgs e) {
129      if (InvokeRequired)
130        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
[3265]131      else
[3226]132        repetitionsNumericUpDown.Value = Content.Repetitions;
133    }
134    #endregion
135
136    #region Control events
[3265]137    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
138      if (repetitionsNumericUpDown.Text == string.Empty)
139        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
140    }
[3226]141    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
[4011]142      if (Content != null)
143        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
[3226]144    }
145    private void newAlgorithmButton_Click(object sender, EventArgs e) {
146      if (algorithmTypeSelectorDialog == null) {
147        algorithmTypeSelectorDialog = new TypeSelectorDialog();
148        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
[3407]149        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
[3588]150        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
[3226]151      }
152      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]153        try {
154          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
155        }
156        catch (Exception ex) {
[3758]157          ErrorHandling.ShowErrorDialog(this, ex);
[3407]158        }
[3226]159      }
160    }
161    private void openAlgorithmButton_Click(object sender, EventArgs e) {
162      openFileDialog.Title = "Open Algorithm";
163      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]164        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
[3226]165        algorithmViewHost.Enabled = false;
166
[3500]167        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[3226]168          try {
[3500]169            if (error != null) throw error;
170            IAlgorithm algorithm = content as IAlgorithm;
[3226]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;
[3500]175          }
176          catch (Exception ex) {
[3758]177            ErrorHandling.ShowErrorDialog(this, ex);
[3500]178          }
179          finally {
180            Invoke(new Action(delegate() {
181              algorithmViewHost.Enabled = true;
182              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
183            }));
184          }
185        });
[3226]186      }
187    }
188    private void startButton_Click(object sender, EventArgs e) {
189      Content.Start();
190    }
[3265]191    private void pauseButton_Click(object sender, EventArgs e) {
192      Content.Pause();
193    }
[3226]194    private void stopButton_Click(object sender, EventArgs e) {
195      Content.Stop();
196    }
197    private void resetButton_Click(object sender, EventArgs e) {
[3716]198      Content.Prepare(false);
[3226]199    }
[4011]200    private void algorithmViewHost_DragEnterOver(object sender, DragEventArgs e) {
[3299]201      e.Effect = DragDropEffects.None;
[3367]202      if (ReadOnly)
203        return;
[3299]204      Type type = e.Data.GetData("Type") as Type;
205      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
[3694]206        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]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;
[3526]210        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]211      }
212    }
[4011]213    private void algorithmViewHost_DragDrop(object sender, DragEventArgs e) {
[3299]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    }
[3226]220    #endregion
221
222    #region Helpers
[3454]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      }
[3226]232    }
233    #endregion
234  }
235}
Note: See TracBrowser for help on using the repository browser.