Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/AlgorithmView.cs @ 4682

Last change on this file since 4682 was 4540, checked in by mkommend, 14 years ago

Corrected the status of executable views in the OnContentChanged method (ticket #1225).

File size: 12.9 KB
RevLine 
[2851]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[2916]23using System.Collections.Generic;
[2851]24using System.Windows.Forms;
25using HeuristicLab.Common;
[3262]26using HeuristicLab.Core;
[2851]27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
[3758]29using HeuristicLab.PluginInfrastructure;
[2851]30
[2852]31namespace HeuristicLab.Optimization.Views {
[2851]32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
[2917]35  [View("Algorithm View")]
[2851]36  [Content(typeof(Algorithm), true)]
37  [Content(typeof(IAlgorithm), false)]
38  public partial class AlgorithmView : NamedItemView {
39    private TypeSelectorDialog problemTypeSelectorDialog;
40
41    public new IAlgorithm Content {
42      get { return (IAlgorithm)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="ItemBaseView"/>.
48    /// </summary>
49    public AlgorithmView() {
50      InitializeComponent();
51    }
52
[2916]53    protected override void OnInitialized(EventArgs e) {
54      // Set order of tab pages according to z order.
55      // NOTE: This is required due to a bug in the VS designer.
56      List<Control> tabPages = new List<Control>();
57      for (int i = 0; i < tabControl.Controls.Count; i++) {
58        tabPages.Add(tabControl.Controls[i]);
59      }
60      tabControl.Controls.Clear();
61      foreach (Control control in tabPages)
62        tabControl.Controls.Add(control);
63
64      base.OnInitialized(e);
65    }
66
[2851]67    protected override void DeregisterContentEvents() {
68      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]69      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[2851]70      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
71      Content.Prepared -= new EventHandler(Content_Prepared);
[4070]72      Content.Started -= new EventHandler(Content_Started);
73      Content.Paused -= new EventHandler(Content_Paused);
74      Content.Stopped -= new EventHandler(Content_Stopped);
[2851]75      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
[4102]76      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
[2851]77      base.DeregisterContentEvents();
78    }
79    protected override void RegisterContentEvents() {
80      base.RegisterContentEvents();
81      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]82      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[2851]83      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
84      Content.Prepared += new EventHandler(Content_Prepared);
[4070]85      Content.Started += new EventHandler(Content_Started);
86      Content.Paused += new EventHandler(Content_Paused);
87      Content.Stopped += new EventHandler(Content_Stopped);
[2851]88      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
[4102]89      Content.StoreAlgorithmInEachRunChanged += new EventHandler(Content_StoreAlgorithmInEachRunChanged);
[2851]90    }
91
92    protected override void OnContentChanged() {
93      base.OnContentChanged();
94      if (Content == null) {
95        parameterCollectionView.Content = null;
96        problemViewHost.Content = null;
[2882]97        resultsView.Content = null;
[3275]98        runsView.Content = null;
[4102]99        storeAlgorithmInEachRunCheckBox.Checked = true;
[2851]100        executionTimeTextBox.Text = "-";
101      } else {
[4540]102        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
[2851]103        parameterCollectionView.Content = Content.Parameters;
[2949]104        problemViewHost.ViewType = null;
[2851]105        problemViewHost.Content = Content.Problem;
[3226]106        resultsView.Content = Content.Results.AsReadOnly();
[3275]107        runsView.Content = Content.Runs;
[4102]108        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
[3262]109        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2851]110      }
111    }
112
[3904]113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
[3454]115      parameterCollectionView.Enabled = Content != null;
116      newProblemButton.Enabled = Content != null && !ReadOnly;
117      openProblemButton.Enabled = Content != null && !ReadOnly;
118      problemViewHost.Enabled = Content != null;
119      resultsView.Enabled = Content != null;
120      runsView.Enabled = Content != null;
[4102]121      storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
[3454]122      executionTimeTextBox.Enabled = Content != null;
123      SetEnabledStateOfExecutableButtons();
[3367]124    }
125
[2955]126    protected override void OnClosed(FormClosedEventArgs e) {
[3262]127      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[2955]128      base.OnClosed(e);
129    }
130
[2851]131    #region Content Events
[2998]132    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
[2851]133      if (InvokeRequired)
134        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
135      else {
[2949]136        problemViewHost.ViewType = null;
[2851]137        problemViewHost.Content = Content.Problem;
138      }
139    }
[4070]140    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
143      else
144        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
145    }
[4102]146    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
149      else
[4216]150        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
[4102]151    }
152    protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
153      if (InvokeRequired)
154        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
155      else
156        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
157    }
[3262]158    protected virtual void Content_Prepared(object sender, EventArgs e) {
[2851]159      if (InvokeRequired)
[3262]160        Invoke(new EventHandler(Content_Prepared), sender, e);
[4070]161      else {
[3262]162        resultsView.Content = Content.Results.AsReadOnly();
[4070]163        ReadOnly = Locked = false;
164        SetEnabledStateOfExecutableButtons();
165      }
[3262]166    }
[4070]167    protected virtual void Content_Started(object sender, EventArgs e) {
[3262]168      if (InvokeRequired)
[4070]169        Invoke(new EventHandler(Content_Started), sender, e);
[2851]170      else {
[4070]171        ReadOnly = Locked = true;
[3454]172        SetEnabledStateOfExecutableButtons();
[2851]173      }
174    }
[4070]175    protected virtual void Content_Paused(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Content_Paused), sender, e);
178      else {
179        ReadOnly = Locked = false;
180        SetEnabledStateOfExecutableButtons();
181      }
182    }
183    protected virtual void Content_Stopped(object sender, EventArgs e) {
184      if (InvokeRequired)
185        Invoke(new EventHandler(Content_Stopped), sender, e);
186      else {
187        ReadOnly = Locked = false;
188        SetEnabledStateOfExecutableButtons();
189      }
190    }
[2851]191    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
192      if (InvokeRequired)
193        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
194      else
[3758]195        ErrorHandling.ShowErrorDialog(this, e.Value);
[2851]196    }
197    #endregion
198
[3299]199    #region Control Events
[2998]200    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
[2851]201      if (problemTypeSelectorDialog == null) {
202        problemTypeSelectorDialog = new TypeSelectorDialog();
203        problemTypeSelectorDialog.Caption = "Select Problem";
[3407]204        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
[3588]205        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
[2851]206      }
207      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]208        try {
209          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
210        }
211        catch (Exception ex) {
[3758]212          ErrorHandling.ShowErrorDialog(this, ex);
[3407]213        }
[2851]214      }
215    }
[2998]216    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
[2851]217      openFileDialog.Title = "Open Problem";
218      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[3454]219        newProblemButton.Enabled = openProblemButton.Enabled = false;
[3177]220        problemViewHost.Enabled = false;
[2954]221
[3500]222        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
[2954]223          try {
[3500]224            if (error != null) throw error;
225            IProblem problem = content as IProblem;
[2954]226            if (problem == null)
[4068]227              Invoke(new Action(() =>
[3548]228                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]229            else if (!Content.ProblemType.IsInstanceOfType(problem))
[3548]230              Invoke(new Action(() =>
231                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
[2954]232            else
233              Content.Problem = problem;
[3500]234          }
235          catch (Exception ex) {
[3758]236            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
[3500]237          }
238          finally {
239            Invoke(new Action(delegate() {
240              problemViewHost.Enabled = true;
241              newProblemButton.Enabled = openProblemButton.Enabled = true;
242            }));
243          }
244        });
[2851]245      }
246    }
[4102]247    protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
248      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
249    }
[2851]250    protected virtual void startButton_Click(object sender, EventArgs e) {
251      Content.Start();
252    }
[3262]253    protected virtual void pauseButton_Click(object sender, EventArgs e) {
254      Content.Pause();
255    }
[2851]256    protected virtual void stopButton_Click(object sender, EventArgs e) {
257      Content.Stop();
258    }
259    protected virtual void resetButton_Click(object sender, EventArgs e) {
[3716]260      Content.Prepare(false);
[2851]261    }
[4522]262    protected virtual void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
[3299]263      e.Effect = DragDropEffects.None;
264      Type type = e.Data.GetData("Type") as Type;
265      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
[3694]266        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3299]267        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
268        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
269        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]270        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[3299]271      }
272    }
[4522]273    protected virtual void problemTabPage_DragDrop(object sender, DragEventArgs e) {
[3299]274      if (e.Effect != DragDropEffects.None) {
275        IProblem problem = e.Data.GetData("Value") as IProblem;
276        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
277        Content.Problem = problem;
278      }
279    }
[2851]280    #endregion
281
282    #region Helpers
[3454]283    private void SetEnabledStateOfExecutableButtons() {
284      if (Content == null) {
285        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
286      } else {
287        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
288        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
289        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
290        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
291      }
[2851]292    }
293    #endregion
294  }
295}
Note: See TracBrowser for help on using the repository browser.