Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3362 was 3299, checked in by swagner, 15 years ago

Enabled dragging and dropping of problems and algorithms in AlgorithmView and BatchRunView (#965).

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