Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

File size: 16.0 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.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      RunCreationClient.Instance.Refreshing += new EventHandler(RunCreationClient_Refreshing);
69      RunCreationClient.Instance.Refreshed += new EventHandler(RunCreationClient_Refreshed);
70      PopulateComboBox();
71    }
72
73    protected override void DeregisterContentEvents() {
74      Content.AlgorithmChanged -= new EventHandler(Content_AlgorithmChanged);
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);
84      base.DeregisterContentEvents();
85    }
86    protected override void RegisterContentEvents() {
87      base.RegisterContentEvents();
88      Content.AlgorithmChanged += new EventHandler(Content_AlgorithmChanged);
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);
98    }
99
100    protected override void OnContentChanged() {
101      base.OnContentChanged();
102      if (Content == null) {
103        algorithmComboBox.SelectedIndex = -1;
104        parameterCollectionView.Content = null;
105        problemViewHost.Content = null;
106        resultsView.Content = null;
107        runsView.Content = null;
108        storeAlgorithmInEachRunCheckBox.Checked = true;
109        executionTimeTextBox.Text = "-";
110      } else {
111        algorithmComboBox.SelectedItem = RunCreationClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId);
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();
120      }
121    }
122
123    protected override void SetEnabledStateOfControls() {
124      base.SetEnabledStateOfControls();
125      algorithmComboBox.Enabled = (Content != null) && !ReadOnly && !Locked && (algorithmComboBox.Items.Count > 0);
126      cloneAlgorithmButton.Enabled = (Content != null) && (Content.AlgorithmId != -1) && !ReadOnly && !Locked;
127      refreshButton.Enabled = (Content != null) && !ReadOnly && !Locked;
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();
137    }
138
139    protected override void OnClosed(FormClosedEventArgs e) {
140      RunCreationClient.Instance.Refreshing -= new EventHandler(RunCreationClient_Refreshing);
141      RunCreationClient.Instance.Refreshed -= new EventHandler(RunCreationClient_Refreshed);
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      }
150      base.OnClosed(e);
151    }
152
153    private void RunCreationClient_Refreshing(object sender, EventArgs e) {
154      if (InvokeRequired) {
155        Invoke(new EventHandler(RunCreationClient_Refreshing), sender, e);
156      } else {
157        Cursor = Cursors.AppStarting;
158        Enabled = false;
159      }
160    }
161    private void RunCreationClient_Refreshed(object sender, EventArgs e) {
162      if (InvokeRequired) {
163        Invoke(new EventHandler(RunCreationClient_Refreshed), sender, e);
164      } else {
165        PopulateComboBox();
166        Enabled = true;
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
177        OnContentChanged();
178    }
179    private void Content_ProblemChanged(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
182      else {
183        problemViewHost.ViewType = null;
184        problemViewHost.Content = Content.Problem;
185      }
186    }
187    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
188      if (InvokeRequired)
189        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
190      else
191        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
192    }
193    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
194      if (InvokeRequired)
195        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
196      else
197        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
198    }
199    private void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
200      if (InvokeRequired)
201        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
202      else
203        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
204    }
205    private void Content_Prepared(object sender, EventArgs e) {
206      if (InvokeRequired)
207        Invoke(new EventHandler(Content_Prepared), sender, e);
208      else {
209        resultsView.Content = Content.Results.AsReadOnly();
210        ReadOnly = Locked = false;
211        SetEnabledStateOfExecutableButtons();
212      }
213    }
214    private void Content_Started(object sender, EventArgs e) {
215      if (InvokeRequired)
216        Invoke(new EventHandler(Content_Started), sender, e);
217      else {
218        ReadOnly = Locked = true;
219        SetEnabledStateOfExecutableButtons();
220      }
221    }
222    private void Content_Paused(object sender, EventArgs e) {
223      if (InvokeRequired)
224        Invoke(new EventHandler(Content_Paused), sender, e);
225      else {
226        ReadOnly = Locked = false;
227        SetEnabledStateOfExecutableButtons();
228      }
229    }
230    private void Content_Stopped(object sender, EventArgs e) {
231      if (InvokeRequired)
232        Invoke(new EventHandler(Content_Stopped), sender, e);
233      else {
234        ReadOnly = Locked = false;
235        SetEnabledStateOfExecutableButtons();
236      }
237    }
238    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
239      if (InvokeRequired)
240        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
241      else
242        ErrorHandling.ShowErrorDialog(this, e.Value);
243    }
244    #endregion
245
246    #region Control Events
247    private void cloneAlgorithmButton_Click(object sender, EventArgs e) {
248      MainFormManager.MainForm.ShowContent(Content.CloneAlgorithm());
249    }
250    private void refreshButton_Click(object sender, System.EventArgs e) {
251      RunCreationClient.Instance.Refresh();
252    }
253    private void algorithmComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
254      Algorithm algorithm = algorithmComboBox.SelectedValue as Algorithm;
255      if ((algorithm != null) && (Content != null)) {
256        Content.Load(algorithm.Id);
257        if (Content.AlgorithmId != algorithm.Id)  // reset selected item if load was not successful
258          algorithmComboBox.SelectedItem = RunCreationClient.Instance.Algorithms.FirstOrDefault(x => x.Id == Content.AlgorithmId);
259      }
260    }
261    private void newProblemButton_Click(object sender, EventArgs e) {
262      if (problemTypeSelectorDialog == null) {
263        problemTypeSelectorDialog = new TypeSelectorDialog();
264        problemTypeSelectorDialog.Caption = "Select Problem";
265        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
266        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
267      }
268      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
269        try {
270          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
271        }
272        catch (Exception ex) {
273          ErrorHandling.ShowErrorDialog(this, ex);
274        }
275      }
276    }
277    private void openProblemButton_Click(object sender, EventArgs e) {
278      openFileDialog.Title = "Open Problem";
279      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
280        newProblemButton.Enabled = openProblemButton.Enabled = false;
281        problemViewHost.Enabled = false;
282
283        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
284          try {
285            if (error != null) throw error;
286            IProblem problem = content as IProblem;
287            if (problem == null)
288              Invoke(new Action(() =>
289                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
290            else if (!Content.ProblemType.IsInstanceOfType(problem))
291              Invoke(new Action(() =>
292                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
293            else
294              Content.Problem = problem;
295          }
296          catch (Exception ex) {
297            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
298          }
299          finally {
300            Invoke(new Action(delegate() {
301              problemViewHost.Enabled = true;
302              newProblemButton.Enabled = openProblemButton.Enabled = true;
303            }));
304          }
305        });
306      }
307    }
308    private void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
309      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
310    }
311    private void startButton_Click(object sender, EventArgs e) {
312      Content.Start();
313    }
314    private void pauseButton_Click(object sender, EventArgs e) {
315      Content.Pause();
316    }
317    private void stopButton_Click(object sender, EventArgs e) {
318      Content.Stop();
319    }
320    private void resetButton_Click(object sender, EventArgs e) {
321      Content.Prepare(false);
322    }
323    private void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
324      e.Effect = DragDropEffects.None;
325      Type type = e.Data.GetData("Type") as Type;
326      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
327        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
328        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
329        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
330        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
331        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
332      }
333    }
334    private void problemTabPage_DragDrop(object sender, DragEventArgs e) {
335      if (e.Effect != DragDropEffects.None) {
336        IProblem problem = e.Data.GetData("Value") as IProblem;
337        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
338        Content.Problem = problem;
339      }
340    }
341    #endregion
342
343    #region Helpers
344    private void PopulateComboBox() {
345      algorithmComboBox.DataSource = null;
346      algorithmComboBox.DataSource = RunCreationClient.Instance.Algorithms.ToList();
347      algorithmComboBox.DisplayMember = "Name";
348    }
349    private void SetEnabledStateOfExecutableButtons() {
350      if (Content == null) {
351        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
352      } else {
353        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
354        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
355        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
356        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
357      }
358    }
359    #endregion
360  }
361}
Note: See TracBrowser for help on using the repository browser.