Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on OKB (#1174)

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