Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3975 was 3904, checked in by mkommend, 14 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

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      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
143    }
144    private void newAlgorithmButton_Click(object sender, EventArgs e) {
145      if (algorithmTypeSelectorDialog == null) {
146        algorithmTypeSelectorDialog = new TypeSelectorDialog();
147        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
148        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
149        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
150      }
151      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
152        try {
153          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
154        }
155        catch (Exception ex) {
156          ErrorHandling.ShowErrorDialog(this, ex);
157        }
158      }
159    }
160    private void openAlgorithmButton_Click(object sender, EventArgs e) {
161      openFileDialog.Title = "Open Algorithm";
162      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
163        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
164        algorithmViewHost.Enabled = false;
165
166        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
167          try {
168            if (error != null) throw error;
169            IAlgorithm algorithm = content as IAlgorithm;
170            if (algorithm == null)
171              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
172            else
173              Content.Algorithm = algorithm;
174          }
175          catch (Exception ex) {
176            ErrorHandling.ShowErrorDialog(this, ex);
177          }
178          finally {
179            Invoke(new Action(delegate() {
180              algorithmViewHost.Enabled = true;
181              newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
182            }));
183          }
184        });
185      }
186    }
187    private void startButton_Click(object sender, EventArgs e) {
188      Content.Start();
189    }
190    private void pauseButton_Click(object sender, EventArgs e) {
191      Content.Pause();
192    }
193    private void stopButton_Click(object sender, EventArgs e) {
194      Content.Stop();
195    }
196    private void resetButton_Click(object sender, EventArgs e) {
197      Content.Prepare(false);
198    }
199    private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
200      e.Effect = DragDropEffects.None;
201      if (ReadOnly)
202        return;
203      Type type = e.Data.GetData("Type") as Type;
204      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
205        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
206        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
207        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
208        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
209        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
210      }
211    }
212    private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
213      if (e.Effect != DragDropEffects.None) {
214        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
215        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
216        Content.Algorithm = algorithm;
217      }
218    }
219    #endregion
220
221    #region Helpers
222    private void SetEnabledStateOfExecutableButtons() {
223      if (Content == null) {
224        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
225      } else {
226        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
227        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
228        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
229        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
230      }
231    }
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.