Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

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