Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.OKB.Views/3.3/RunCreation/Views/OKBAlgorithmView.cs @ 10121

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

Updated copyright year and added some missing license headers (#1889)

File size: 17.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Clients.OKB.RunCreation {
34  [View("OKBAlgorithm View")]
35  [Content(typeof(OKBAlgorithm), true)]
36  public sealed partial class OKBAlgorithmView : NamedItemView {
37    private TypeSelectorDialog problemTypeSelectorDialog;
38
39    public new OKBAlgorithm Content {
40      get { return (OKBAlgorithm)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public OKBAlgorithmView() {
45      InitializeComponent();
46    }
47
48    protected override void Dispose(bool disposing) {
49      if (disposing) {
50        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
51        if (components != null) components.Dispose();
52      }
53      base.Dispose(disposing);
54    }
55
56    protected override void OnInitialized(EventArgs e) {
57      // Set order of tab pages according to z order.
58      // NOTE: This is required due to a bug in the VS designer.
59      List<Control> tabPages = new List<Control>();
60      for (int i = 0; i < tabControl.Controls.Count; i++) {
61        tabPages.Add(tabControl.Controls[i]);
62      }
63      tabControl.Controls.Clear();
64      foreach (Control control in tabPages)
65        tabControl.Controls.Add(control);
66
67      base.OnInitialized(e);
68      //TODO: deregistration of eventhandlers is missing?
69      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
70      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
71      PopulateComboBox();
72    }
73
74    protected override void DeregisterContentEvents() {
75      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
77      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
78      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
79      Content.Prepared -= new EventHandler(Content_Prepared);
80      Content.Started -= new EventHandler(Content_Started);
81      Content.Paused -= new EventHandler(Content_Paused);
82      Content.Stopped -= new EventHandler(Content_Stopped);
83      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
84      Content.StoreRunsAutomaticallyChanged -= new EventHandler(Content_StoreRunsAutomaticallyChanged);
85      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
86      base.DeregisterContentEvents();
87    }
88    protected override void RegisterContentEvents() {
89      base.RegisterContentEvents();
90      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
91      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
92      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
93      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
94      Content.Prepared += new EventHandler(Content_Prepared);
95      Content.Started += new EventHandler(Content_Started);
96      Content.Paused += new EventHandler(Content_Paused);
97      Content.Stopped += new EventHandler(Content_Stopped);
98      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
99      Content.StoreRunsAutomaticallyChanged += new EventHandler(Content_StoreRunsAutomaticallyChanged);
100      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
101    }
102
103    protected override void OnContentChanged() {
104      base.OnContentChanged();
105      if (Content == null) {
106        algorithmComboBox.SelectedIndex = -1;
107        parameterCollectionView.Content = null;
108        problemViewHost.Content = null;
109        resultsView.Content = null;
110        runsView.Content = null;
111        storeRunsAutomaticallyCheckBox.Checked = true;
112        storeAlgorithmInEachRunCheckBox.Checked = true;
113        executionTimeTextBox.Text = "-";
114      } else {
115        algorithmComboBox.SelectedItem = RunCreationClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId);
116        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
117        parameterCollectionView.Content = Content.Parameters;
118        problemViewHost.ViewType = null;
119        problemViewHost.Content = Content.Problem;
120        resultsView.Content = Content.Results.AsReadOnly();
121        runsView.Content = Content.Runs;
122        storeRunsAutomaticallyCheckBox.Checked = Content.StoreRunsAutomatically;
123        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
124        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
125      }
126    }
127
128    protected override void SetEnabledStateOfControls() {
129      base.SetEnabledStateOfControls();
130      algorithmComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (algorithmComboBox.Items.Count > 0);
131      cloneAlgorithmButton.Enabled = (Content != null) && (Content.AlgorithmId != -1) && !ReadOnly && !Locked;
132      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
133      parameterCollectionView.Enabled = Content != null;
134      newProblemButton.Enabled = Content != null && !ReadOnly;
135      openProblemButton.Enabled = Content != null && !ReadOnly;
136      problemViewHost.Enabled = Content != null;
137      resultsView.Enabled = Content != null;
138      runsView.Enabled = Content != null;
139      storeRunsAutomaticallyCheckBox.Enabled = Content != null && !ReadOnly;
140      storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
141      executionTimeTextBox.Enabled = Content != null;
142      SetEnabledStateOfExecutableButtons();
143    }
144
145    protected override void OnClosed(FormClosedEventArgs e) {
146      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
147      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
148      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) {
149        //The content must be stopped if no other view showing the content is available
150        var optimizers = MainFormManager.MainForm.Views.OfType<IContentView>().Where(v => v != this).Select(v => v.Content).OfType<IOptimizer>();
151        if (!optimizers.Contains(Content)) {
152          var nestedOptimizers = optimizers.SelectMany(opt => opt.NestedOptimizers);
153          if (!nestedOptimizers.Contains(Content)) Content.Stop();
154        }
155      }
156      base.OnClosed(e);
157    }
158
159    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
160      if (InvokeRequired) {
161        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
162      } else {
163        Cursor = Cursors.AppStarting;
164        Enabled = false;
165      }
166    }
167    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
168      if (InvokeRequired) {
169        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
170      } else {
171        PopulateComboBox();
172        Enabled = true;
173        SetEnabledStateOfControls();
174        Cursor = Cursors.Default;
175      }
176    }
177
178    #region Content Events
179    private void Content_AlgorithmChanged(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
182      else
183        OnContentChanged();
184    }
185    private void Content_ProblemChanged(object sender, EventArgs e) {
186      if (InvokeRequired)
187        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
188      else {
189        problemViewHost.ViewType = null;
190        problemViewHost.Content = Content.Problem;
191      }
192    }
193    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
194      if (InvokeRequired)
195        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
196      else
197        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
198    }
199    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
200      if (InvokeRequired)
201        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
202      else
203        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
204    }
205    private void Content_StoreRunsAutomaticallyChanged(object sender, EventArgs e) {
206      if (InvokeRequired)
207        Invoke(new EventHandler(Content_StoreRunsAutomaticallyChanged), sender, e);
208      else
209        storeRunsAutomaticallyCheckBox.Checked = Content.StoreRunsAutomatically;
210    }
211    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
212      if (InvokeRequired)
213        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
214      else
215        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
216    }
217    private void Content_Prepared(object sender, EventArgs e) {
218      if (InvokeRequired)
219        Invoke(new EventHandler(Content_Prepared), sender, e);
220      else {
221        resultsView.Content = Content.Results.AsReadOnly();
222        ReadOnly = Locked = false;
223        SetEnabledStateOfExecutableButtons();
224      }
225    }
226    private void Content_Started(object sender, EventArgs e) {
227      if (InvokeRequired)
228        Invoke(new EventHandler(Content_Started), sender, e);
229      else {
230        ReadOnly = Locked = true;
231        SetEnabledStateOfExecutableButtons();
232      }
233    }
234    private void Content_Paused(object sender, EventArgs e) {
235      if (InvokeRequired)
236        Invoke(new EventHandler(Content_Paused), sender, e);
237      else {
238        ReadOnly = Locked = false;
239        SetEnabledStateOfExecutableButtons();
240      }
241    }
242    private void Content_Stopped(object sender, EventArgs e) {
243      if (InvokeRequired)
244        Invoke(new EventHandler(Content_Stopped), sender, e);
245      else {
246        ReadOnly = Locked = false;
247        SetEnabledStateOfExecutableButtons();
248      }
249    }
250    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
251      if (InvokeRequired)
252        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
253      else
254        ErrorHandling.ShowErrorDialog(this, e.Value);
255    }
256    #endregion
257
258    #region Control Events
259    private void cloneAlgorithmButton_Click(object sender, EventArgs e) {
260      MainFormManager.MainForm.ShowContent(Content.CloneAlgorithm());
261    }
262    private void refreshButton_Click(object sender, System.EventArgs e) {
263      RunCreationClient.Instance.Refresh();
264    }
265    private void algorithmComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
266      Algorithm algorithm = algorithmComboBox.SelectedValue as Algorithm;
267      if ((algorithm != null) && (Content != null)) {
268        Content.Load(algorithm.Id);
269        if (Content.AlgorithmId != algorithm.Id)  // reset selected item if load was not successful
270          algorithmComboBox.SelectedItem = RunCreationClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId);
271      }
272    }
273    private void newProblemButton_Click(object sender, EventArgs e) {
274      if (problemTypeSelectorDialog == null) {
275        problemTypeSelectorDialog = new TypeSelectorDialog();
276        problemTypeSelectorDialog.Caption = "Select Problem";
277        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
278        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
279      }
280      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
281        try {
282          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
283        }
284        catch (Exception ex) {
285          ErrorHandling.ShowErrorDialog(this, ex);
286        }
287      }
288    }
289    private void openProblemButton_Click(object sender, EventArgs e) {
290      openFileDialog.Title = "Open Problem";
291      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
292        newProblemButton.Enabled = openProblemButton.Enabled = false;
293        problemViewHost.Enabled = false;
294
295        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
296          try {
297            if (error != null) throw error;
298            IProblem problem = content as IProblem;
299            if (problem == null)
300              Invoke(new Action(() =>
301                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
302            else if (!Content.ProblemType.IsInstanceOfType(problem))
303              Invoke(new Action(() =>
304                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
305            else
306              Content.Problem = problem;
307          }
308          catch (Exception ex) {
309            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
310          }
311          finally {
312            Invoke(new Action(delegate() {
313              problemViewHost.Enabled = true;
314              newProblemButton.Enabled = openProblemButton.Enabled = true;
315            }));
316          }
317        });
318      }
319    }
320    private void storeRunsAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
321      if (Content != null) Content.StoreRunsAutomatically = storeRunsAutomaticallyCheckBox.Checked;
322    }
323    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
324      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
325    }
326    private void startButton_Click(object sender, EventArgs e) {
327      Content.Start();
328    }
329    private void pauseButton_Click(object sender, EventArgs e) {
330      Content.Pause();
331    }
332    private void stopButton_Click(object sender, EventArgs e) {
333      Content.Stop();
334    }
335    private void resetButton_Click(object sender, EventArgs e) {
336      Content.Prepare(false);
337    }
338    private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
339      e.Effect = DragDropEffects.None;
340      Type type = e.Data.GetData("Type") as Type;
341      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
342        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
343        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
344        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
345        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
346        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
347      }
348    }
349    private void problemTabPage_DragDrop(object sender, DragEventArgs e) {
350      if (e.Effect != DragDropEffects.None) {
351        IProblem problem = e.Data.GetData("Value") as IProblem;
352        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
353        Content.Problem = problem;
354      }
355    }
356    #endregion
357
358    #region Helpers
359    private void PopulateComboBox() {
360      algorithmComboBox.DataSource = null;
361      algorithmComboBox.DataSource = RunCreationClient.Instance.Algorithms.ToList();
362      algorithmComboBox.DisplayMember = "Name";
363    }
364    private void SetEnabledStateOfExecutableButtons() {
365      if (Content == null) {
366        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
367      } else {
368        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
369        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
370        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
371        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
372      }
373    }
374    #endregion
375  }
376}
Note: See TracBrowser for help on using the repository browser.