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

Last change on this file since 3454 was 3454, checked in by swagner, 12 years ago

Adapted views according the new read-only property (#973)

File size: 10.9 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    /// <summary>
51    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
52    /// </summary>
53    /// <param name="item">The item that should be displayed.</param>
54    public BatchRunView(BatchRun content)
55      : this() {
56      Content = content;
57    }
58
59    protected override void DeregisterContentEvents() {
60      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
61      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
62      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
63      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
64      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
65      base.DeregisterContentEvents();
66    }
67    protected override void RegisterContentEvents() {
68      base.RegisterContentEvents();
69      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
70      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
71      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
72      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
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;
81        runsView.Content = null;
82        executionTimeTextBox.Text = "-";
83      } else {
84        repetitionsNumericUpDown.Value = Content.Repetitions;
85        algorithmViewHost.ViewType = null;
86        algorithmViewHost.Content = Content.Algorithm;
87        runsView.Content = Content.Runs;
88        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
89      }
90      SetEnableStateOfControls();
91    }
92    protected override void OnReadOnlyChanged() {
93      base.OnReadOnlyChanged();
94      SetEnableStateOfControls();
95    }
96    private void SetEnableStateOfControls() {
97      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
98      newAlgorithmButton.Enabled = Content != null && !ReadOnly;
99      openAlgorithmButton.Enabled = Content != null && !ReadOnly;
100      algorithmViewHost.Enabled = Content != null;
101      algorithmViewHost.ReadOnly = ReadOnly;
102      runsView.Enabled = Content != null;
103      runsView.ReadOnly = ReadOnly;
104      executionTimeTextBox.Enabled = Content != null;
105      SetEnabledStateOfExecutableButtons();
106    }
107
108    protected override void OnClosed(FormClosedEventArgs e) {
109      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
110      base.OnClosed(e);
111    }
112
113    #region Content Events
114    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
117      else {
118        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
119        Locked = Content.ExecutionState == ExecutionState.Started;
120        SetEnabledStateOfExecutableButtons();
121      }
122    }
123    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
126      else
127        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
128    }
129    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
130      if (InvokeRequired)
131        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
132      else
133        Auxiliary.ShowErrorMessageBox(e.Value);
134    }
135    private void Content_AlgorithmChanged(object sender, EventArgs e) {
136      if (InvokeRequired)
137        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
138      else {
139        algorithmViewHost.ViewType = null;
140        algorithmViewHost.Content = Content.Algorithm;
141      }
142    }
143    private void Content_RepetitionsChanged(object sender, EventArgs e) {
144      if (InvokeRequired)
145        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
146      else
147        repetitionsNumericUpDown.Value = Content.Repetitions;
148    }
149    #endregion
150
151    #region Control events
152    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
153      if (repetitionsNumericUpDown.Text == string.Empty)
154        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
155    }
156    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
157      Content.Repetitions = (int)repetitionsNumericUpDown.Value;
158    }
159    private void newAlgorithmButton_Click(object sender, EventArgs e) {
160      if (algorithmTypeSelectorDialog == null) {
161        algorithmTypeSelectorDialog = new TypeSelectorDialog();
162        algorithmTypeSelectorDialog.Caption = "Select Algorithm";
163        algorithmTypeSelectorDialog.TypeSelector.Caption = "Available Algorithms";
164        algorithmTypeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, false);
165      }
166      if (algorithmTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
167        try {
168          Content.Algorithm = (IAlgorithm)algorithmTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
169        }
170        catch (Exception ex) {
171          Auxiliary.ShowErrorMessageBox(ex);
172        }
173      }
174    }
175    private void openAlgorithmButton_Click(object sender, EventArgs e) {
176      openFileDialog.Title = "Open Algorithm";
177      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
178        this.Cursor = Cursors.AppStarting;
179        newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = false;
180        algorithmViewHost.Enabled = false;
181
182        var call = new Func<string, object>(XmlParser.Deserialize);
183        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
184          IAlgorithm algorithm = null;
185          try {
186            algorithm = call.EndInvoke(a) as IAlgorithm;
187          } catch (Exception ex) {
188            Auxiliary.ShowErrorMessageBox(ex);
189          }
190          Invoke(new Action(delegate() {
191            if (algorithm == null)
192              MessageBox.Show(this, "The selected file does not contain an algorithm.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
193            else
194              Content.Algorithm = algorithm;
195            algorithmViewHost.Enabled = true;
196            newAlgorithmButton.Enabled = openAlgorithmButton.Enabled = true;
197            this.Cursor = Cursors.Default;
198          }));
199        }, null);
200      }
201    }
202    private void startButton_Click(object sender, EventArgs e) {
203      Content.Start();
204    }
205    private void pauseButton_Click(object sender, EventArgs e) {
206      Content.Pause();
207    }
208    private void stopButton_Click(object sender, EventArgs e) {
209      Content.Stop();
210    }
211    private void resetButton_Click(object sender, EventArgs e) {
212      if (Content.Runs.Count > 0) {
213        if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
214          Content.Prepare(true);
215        else
216          Content.Prepare(false);
217      } else {
218        Content.Prepare();
219      }
220    }
221    private void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
222      e.Effect = DragDropEffects.None;
223      if (ReadOnly)
224        return;
225      Type type = e.Data.GetData("Type") as Type;
226      if ((type != null) && (typeof(IAlgorithm).IsAssignableFrom(type))) {
227        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
228        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
229        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
230        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
231        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
232      }
233    }
234    private void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
235      if (e.Effect != DragDropEffects.None) {
236        IAlgorithm algorithm = e.Data.GetData("Value") as IAlgorithm;
237        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) algorithm = (IAlgorithm)algorithm.Clone();
238        Content.Algorithm = algorithm;
239      }
240    }
241    #endregion
242
243    #region Helpers
244    private void SetEnabledStateOfExecutableButtons() {
245      if (Content == null) {
246        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
247      } else {
248        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
249        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
250        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
251        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
252      }
253    }
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.