Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5751 was 5744, checked in by swagner, 14 years ago

Implemented dragging of multiple items in all collection views and refactored drag & drop code (#1112)

File size: 12.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
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 optimizerTypeSelectorDialog;
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 Dispose(bool disposing) {
53      if (disposing) {
54        if (optimizerTypeSelectorDialog != null) optimizerTypeSelectorDialog.Dispose();
55        if (components != null) components.Dispose();
56      }
57      base.Dispose(disposing);
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.OptimizerChanged -= new EventHandler(Content_OptimizerChanged);
62      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
63      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
64      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
65      Content.Prepared -= new EventHandler(Content_Prepared);
66      Content.Started -= new EventHandler(Content_Started);
67      Content.Paused -= new EventHandler(Content_Paused);
68      Content.Stopped -= new EventHandler(Content_Stopped);
69      Content.RepetitionsChanged -= new EventHandler(Content_RepetitionsChanged);
70      base.DeregisterContentEvents();
71    }
72    protected override void RegisterContentEvents() {
73      base.RegisterContentEvents();
74      Content.OptimizerChanged += new EventHandler(Content_OptimizerChanged);
75      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
76      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
77      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
78      Content.Prepared += new EventHandler(Content_Prepared);
79      Content.Started += new EventHandler(Content_Started);
80      Content.Paused += new EventHandler(Content_Paused);
81      Content.Stopped += new EventHandler(Content_Stopped);
82      Content.RepetitionsChanged += new EventHandler(Content_RepetitionsChanged);
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      if (Content == null) {
88        repetitionsNumericUpDown.Value = 1;
89        optimizerViewHost.Content = null;
90        runsView.Content = null;
91        executionTimeTextBox.Text = "-";
92      } else {
93        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
94        repetitionsNumericUpDown.Value = Content.Repetitions;
95        optimizerViewHost.Content = Content.Optimizer;
96        runsView.Content = Content.Runs;
97        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
98      }
99    }
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      repetitionsNumericUpDown.Enabled = Content != null && !ReadOnly;
103      newOptimizerButton.Enabled = Content != null && !ReadOnly;
104      openOptimizerButton.Enabled = Content != null && !ReadOnly;
105      optimizerViewHost.Enabled = Content != null;
106      runsView.Enabled = Content != null;
107      executionTimeTextBox.Enabled = Content != null;
108      SetEnabledStateOfExecutableButtons();
109    }
110
111    protected override void OnClosed(FormClosedEventArgs e) {
112      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
113        //The content must be stopped if no other view showing the content is available
114        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
115        if (!optimizers.Contains(Content)) {
116          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
117          if (!nestedOptimizers.Contains(Content)) Content.Stop();
118        }
119      }
120      base.OnClosed(e);
121    }
122
123    #region Content Events
124    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
125      if (InvokeRequired)
126        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
127      else
128        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
129    }
130    private void Content_Prepared(object sender, EventArgs e) {
131      if (InvokeRequired)
132        Invoke(new EventHandler(Content_Prepared), sender, e);
133      else {
134        ReadOnly = Locked = false;
135        SetEnabledStateOfExecutableButtons();
136      }
137    }
138    private void Content_Started(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_Started), sender, e);
141      else {
142        ReadOnly = Locked = true;
143        SetEnabledStateOfExecutableButtons();
144      }
145    }
146    private void Content_Paused(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_Paused), sender, e);
149      else {
150        ReadOnly = Locked = false;
151        SetEnabledStateOfExecutableButtons();
152      }
153    }
154    private void Content_Stopped(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_Stopped), sender, e);
157      else {
158        ReadOnly = Locked = false;
159        SetEnabledStateOfExecutableButtons();
160      }
161    }
162    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
163      if (InvokeRequired)
164        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
165      else
166        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
167    }
168    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
169      if (InvokeRequired)
170        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
171      else
172        ErrorHandling.ShowErrorDialog(this, e.Value);
173    }
174    private void Content_OptimizerChanged(object sender, EventArgs e) {
175      if (InvokeRequired)
176        Invoke(new EventHandler(Content_OptimizerChanged), sender, e);
177      else {
178        optimizerViewHost.Content = Content.Optimizer;
179      }
180    }
181    private void Content_RepetitionsChanged(object sender, EventArgs e) {
182      if (InvokeRequired)
183        Invoke(new EventHandler(Content_RepetitionsChanged), sender, e);
184      else
185        repetitionsNumericUpDown.Value = Content.Repetitions;
186    }
187    #endregion
188
189    #region Control events
190    private void repetitionsNumericUpDown_Validated(object sender, System.EventArgs e) {
191      if (repetitionsNumericUpDown.Text == string.Empty)
192        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
193    }
194    private void repetitionsNumericUpDown_ValueChanged(object sender, EventArgs e) {
195      if (Content != null)
196        Content.Repetitions = (int)repetitionsNumericUpDown.Value;
197    }
198    private void newOptimizerButton_Click(object sender, EventArgs e) {
199      if (optimizerTypeSelectorDialog == null) {
200        optimizerTypeSelectorDialog = new TypeSelectorDialog();
201        optimizerTypeSelectorDialog.Caption = "Select Optimizer";
202        optimizerTypeSelectorDialog.TypeSelector.Caption = "Available Optimizers";
203        optimizerTypeSelectorDialog.TypeSelector.Configure(typeof(IOptimizer), false, true);
204      }
205      if (optimizerTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
206        try {
207          Content.Optimizer = (IOptimizer)optimizerTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
208        }
209        catch (Exception ex) {
210          ErrorHandling.ShowErrorDialog(this, ex);
211        }
212      }
213    }
214    private void openOptimizerButton_Click(object sender, EventArgs e) {
215      openFileDialog.Title = "Open Optimizer";
216      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
217        newOptimizerButton.Enabled = openOptimizerButton.Enabled = false;
218        optimizerViewHost.Enabled = false;
219
220        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
221          try {
222            if (error != null) throw error;
223            IOptimizer optimizer = content as IOptimizer;
224            if (optimizer == null)
225              MessageBox.Show(this, "The selected file does not contain an optimizer.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
226            else
227              Content.Optimizer = optimizer;
228          }
229          catch (Exception ex) {
230            ErrorHandling.ShowErrorDialog(this, ex);
231          }
232          finally {
233            Invoke(new Action(delegate() {
234              optimizerViewHost.Enabled = true;
235              newOptimizerButton.Enabled = openOptimizerButton.Enabled = true;
236            }));
237          }
238        });
239      }
240    }
241    private void startButton_Click(object sender, EventArgs e) {
242      Content.Start();
243    }
244    private void pauseButton_Click(object sender, EventArgs e) {
245      Content.Pause();
246    }
247    private void stopButton_Click(object sender, EventArgs e) {
248      Content.Stop();
249    }
250    private void resetButton_Click(object sender, EventArgs e) {
251      Content.Prepare(false);
252    }
253    private void optimizerTabPage_DragEnterOver(object sender, DragEventArgs e) {
254      e.Effect = DragDropEffects.None;
255      if (!ReadOnly && (e.Data.GetData("HeuristicLab") is IOptimizer)) {
256        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
257        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
258        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
259        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
260        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
261      }
262    }
263    private void optimizerTabPage_DragDrop(object sender, DragEventArgs e) {
264      if (e.Effect != DragDropEffects.None) {
265        IOptimizer optimizer = e.Data.GetData("HeuristicLab") as IOptimizer;
266        if (e.Effect.HasFlag(DragDropEffects.Copy)) optimizer = (IOptimizer)optimizer.Clone();
267        Content.Optimizer = optimizer;
268      }
269    }
270    #endregion
271
272    #region Helpers
273    private void SetEnabledStateOfExecutableButtons() {
274      if (Content == null) {
275        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
276      } else {
277        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
278        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
279        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
280        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
281      }
282    }
283    #endregion
284  }
285}
Note: See TracBrowser for help on using the repository browser.