Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/Views/OKBAlgorithmView.cs @ 5660

Last change on this file since 5660 was 5660, checked in by swagner, 13 years ago

Worked on OKB (#1174)

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