Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5182 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
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;
[3758]28using HeuristicLab.PluginInfrastructure;
[3226]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() {
[3265]52      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
[3226]53      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]54      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[3226]55      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
[4070]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);
[3226]60      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
[3265]65      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
[3226]66      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3265]67      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[3226]68      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
[4070]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);
[3226]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;
[3260]81        runsView.Content = null;
[3226]82        executionTimeTextBox.Text = "-";
83      } else {
[4540]84        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
[3226]85        repetitionsNumericUpDown.Value = Content.Repetitions;
86        algorithmViewHost.Content = Content.Algorithm;
[3260]87        runsView.Content = Content.Runs;
[3265]88        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[3226]89      }
90    }
[3904]91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
[3454]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();
[3367]100    }
[3226]101
102    protected override void OnClosed(FormClosedEventArgs e) {
[3265]103      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[3226]104      base.OnClosed(e);
105    }
106
107    #region Content Events
[3265]108    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
[3226]109      if (InvokeRequired)
[3265]110        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
[4070]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);
[3226]117      else {
[4070]118        ReadOnly = Locked = false;
[3454]119        SetEnabledStateOfExecutableButtons();
[3226]120      }
121    }
[4070]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    }
[3265]146    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3226]147      if (InvokeRequired)
[3265]148        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
149      else
[4216]150        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
[3226]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
[3758]156        ErrorHandling.ShowErrorDialog(this, e.Value);
[3226]157    }
[3265]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    }
[3226]165    private void Content_RepetitionsChanged(object sender, EventArgs e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
[3265]168      else
[3226]169        repetitionsNumericUpDown.Value = Content.Repetitions;
170    }
171    #endregion
172
173    #region Control events
[3265]174    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
175      if (repetitionsNumericUpDown.Text == string.Empty)
176        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
177    }
[3226]178    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
[4011]179      if (Content != null)
180        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
[3226]181    }
182    private void newAlgorithmButton_Click(object sender, EventArgs e) {
183      if (algorithmTypeSelectorDialog == null) {
184        algorithmTypeSelectorDialog = new TypeSelectorDialog();
185        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
[3407]186        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
[3588]187        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
[3226]188      }
189      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]190        try {
191          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
192        }
193        catch (Exception ex) {
[3758]194          ErrorHandling.ShowErrorDialog(this, ex);
[3407]195        }
[3226]196      }
197    }
198    private void openAlgorithmButton_Click(object sender, EventArgs e) {
199      openFileDialog.Title = "Open Algorithm";
200      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]201        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
[3226]202        algorithmViewHost.Enabled = false;
203
[3500]204        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[3226]205          try {
[3500]206            if (error != null) throw error;
207            IAlgorithm algorithm = content as IAlgorithm;
[3226]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;
[3500]212          }
213          catch (Exception ex) {
[3758]214            ErrorHandling.ShowErrorDialog(this, ex);
[3500]215          }
216          finally {
217            Invoke(new Action(delegate() {
218              algorithmViewHost.Enabled = true;
219              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
220            }));
221          }
222        });
[3226]223      }
224    }
225    private void startButton_Click(object sender, EventArgs e) {
226      Content.Start();
227    }
[3265]228    private void pauseButton_Click(object sender, EventArgs e) {
229      Content.Pause();
230    }
[3226]231    private void stopButton_Click(object sender, EventArgs e) {
232      Content.Stop();
233    }
234    private void resetButton_Click(object sender, EventArgs e) {
[3716]235      Content.Prepare(false);
[3226]236    }
[4522]237    private void algorithmTabPage_DragEnterOver(object sender, DragEventArgs e) {
[3299]238      e.Effect = DragDropEffects.None;
[3367]239      if (ReadOnly)
240        return;
[3299]241      Type type = e.Data.GetData("Type") as Type;
242      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
[3694]243        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]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;
[3526]247        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]248      }
249    }
[4522]250    private void algorithmTabPage_DragDrop(object sender, DragEventArgs e) {
[3299]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    }
[3226]257    #endregion
258
259    #region Helpers
[3454]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      }
[3226]269    }
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.