Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5304 was 5237, checked in by swagner, 14 years ago

#1324:

  • Added performance optimization in all other item collection views
  • Checked and refactored item event registration/deregistration
  • Enabled null items in item collection views
  • Moved non-default Dispose methods from designer files into user files
File size: 13.1 KB
Line 
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;
23using System.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Optimization.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("Algorithm View")]
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
53    protected override void Dispose(bool disposing) {
54      if (disposing) {
55        if (problemTypeSelectorDialog != null) problemTypeSelectorDialog.Dispose();
56        if (components != null) components.Dispose();
57      }
58      base.Dispose(disposing);
59    }
60
61    protected override void OnInitialized(EventArgs e) {
62      // Set order of tab pages according to z order.
63      // NOTE: This is required due to a bug in the VS designer.
64      List<Control> tabPages = new List<Control>();
65      for (int i = 0; i < tabControl.Controls.Count; i++) {
66        tabPages.Add(tabControl.Controls[i]);
67      }
68      tabControl.Controls.Clear();
69      foreach (Control control in tabPages)
70        tabControl.Controls.Add(control);
71
72      base.OnInitialized(e);
73    }
74
75    protected override void DeregisterContentEvents() {
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
77      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
78      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
79      Content.Prepared -= new EventHandler(Content_Prepared);
80      Content.Started -= new EventHandler(Content_Started);
81      Content.Paused -= new EventHandler(Content_Paused);
82      Content.Stopped -= new EventHandler(Content_Stopped);
83      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
84      Content.StoreAlgorithmInEachRunChanged -= new EventHandler(Content_StoreAlgorithmInEachRunChanged);
85      base.DeregisterContentEvents();
86    }
87    protected override void RegisterContentEvents() {
88      base.RegisterContentEvents();
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        parameterCollectionView.Content = null;
104        problemViewHost.Content = null;
105        resultsView.Content = null;
106        runsView.Content = null;
107        storeAlgorithmInEachRunCheckBox.Checked = true;
108        executionTimeTextBox.Text = "-";
109      } else {
110        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
111        parameterCollectionView.Content = Content.Parameters;
112        problemViewHost.ViewType = null;
113        problemViewHost.Content = Content.Problem;
114        resultsView.Content = Content.Results.AsReadOnly();
115        runsView.Content = Content.Runs;
116        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
117        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
118      }
119    }
120
121    protected override void SetEnabledStateOfControls() {
122      base.SetEnabledStateOfControls();
123      parameterCollectionView.Enabled = Content != null;
124      newProblemButton.Enabled = Content != null && !ReadOnly;
125      openProblemButton.Enabled = Content != null && !ReadOnly;
126      problemViewHost.Enabled = Content != null;
127      resultsView.Enabled = Content != null;
128      runsView.Enabled = Content != null;
129      storeAlgorithmInEachRunCheckBox.Enabled = Content != null && !ReadOnly;
130      executionTimeTextBox.Enabled = Content != null;
131      SetEnabledStateOfExecutableButtons();
132    }
133
134    protected override void OnClosed(FormClosedEventArgs e) {
135      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
136      base.OnClosed(e);
137    }
138
139    #region Content Events
140    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
143      else {
144        problemViewHost.ViewType = null;
145        problemViewHost.Content = Content.Problem;
146      }
147    }
148    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
149      if (InvokeRequired)
150        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
151      else
152        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
153    }
154    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
157      else
158        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
159    }
160    protected virtual void Content_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
161      if (InvokeRequired)
162        Invoke(new EventHandler(Content_StoreAlgorithmInEachRunChanged), sender, e);
163      else
164        storeAlgorithmInEachRunCheckBox.Checked = Content.StoreAlgorithmInEachRun;
165    }
166    protected virtual void Content_Prepared(object sender, EventArgs e) {
167      if (InvokeRequired)
168        Invoke(new EventHandler(Content_Prepared), sender, e);
169      else {
170        resultsView.Content = Content.Results.AsReadOnly();
171        ReadOnly = Locked = false;
172        SetEnabledStateOfExecutableButtons();
173      }
174    }
175    protected virtual void Content_Started(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Content_Started), sender, e);
178      else {
179        ReadOnly = Locked = true;
180        SetEnabledStateOfExecutableButtons();
181      }
182    }
183    protected virtual void Content_Paused(object sender, EventArgs e) {
184      if (InvokeRequired)
185        Invoke(new EventHandler(Content_Paused), sender, e);
186      else {
187        ReadOnly = Locked = false;
188        SetEnabledStateOfExecutableButtons();
189      }
190    }
191    protected virtual void Content_Stopped(object sender, EventArgs e) {
192      if (InvokeRequired)
193        Invoke(new EventHandler(Content_Stopped), sender, e);
194      else {
195        ReadOnly = Locked = false;
196        SetEnabledStateOfExecutableButtons();
197      }
198    }
199    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
200      if (InvokeRequired)
201        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
202      else
203        ErrorHandling.ShowErrorDialog(this, e.Value);
204    }
205    #endregion
206
207    #region Control Events
208    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
209      if (problemTypeSelectorDialog == null) {
210        problemTypeSelectorDialog = new TypeSelectorDialog();
211        problemTypeSelectorDialog.Caption = "Select Problem";
212        problemTypeSelectorDialog.TypeSelector.Caption = "Available Problems";
213        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, true);
214      }
215      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
216        try {
217          Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
218        }
219        catch (Exception ex) {
220          ErrorHandling.ShowErrorDialog(this, ex);
221        }
222      }
223    }
224    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
225      openFileDialog.Title = "Open Problem";
226      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
227        newProblemButton.Enabled = openProblemButton.Enabled = false;
228        problemViewHost.Enabled = false;
229
230        ContentManager.LoadAsync(openFileDialog.FileName, delegate(IStorableContent content, Exception error) {
231          try {
232            if (error != null) throw error;
233            IProblem problem = content as IProblem;
234            if (problem == null)
235              Invoke(new Action(() =>
236                MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error)));
237            else if (!Content.ProblemType.IsInstanceOfType(problem))
238              Invoke(new Action(() =>
239                MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error)));
240            else
241              Content.Problem = problem;
242          }
243          catch (Exception ex) {
244            Invoke(new Action(() => ErrorHandling.ShowErrorDialog(this, ex)));
245          }
246          finally {
247            Invoke(new Action(delegate() {
248              problemViewHost.Enabled = true;
249              newProblemButton.Enabled = openProblemButton.Enabled = true;
250            }));
251          }
252        });
253      }
254    }
255    protected virtual void storeAlgorithmInEachRunCheckBox_CheckedChanged(object sender, EventArgs e) {
256      if (Content != null) Content.StoreAlgorithmInEachRun = storeAlgorithmInEachRunCheckBox.Checked;
257    }
258    protected virtual void startButton_Click(object sender, EventArgs e) {
259      Content.Start();
260    }
261    protected virtual void pauseButton_Click(object sender, EventArgs e) {
262      Content.Pause();
263    }
264    protected virtual void stopButton_Click(object sender, EventArgs e) {
265      Content.Stop();
266    }
267    protected virtual void resetButton_Click(object sender, EventArgs e) {
268      Content.Prepare(false);
269    }
270    protected virtual void problemTabPage_DragEnterOver(object sender, DragEventArgs e) {
271      e.Effect = DragDropEffects.None;
272      Type type = e.Data.GetData("Type") as Type;
273      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
274        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
275        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
276        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
277        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
278        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
279      }
280    }
281    protected virtual void problemTabPage_DragDrop(object sender, DragEventArgs e) {
282      if (e.Effect != DragDropEffects.None) {
283        IProblem problem = e.Data.GetData("Value") as IProblem;
284        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
285        Content.Problem = problem;
286      }
287    }
288    #endregion
289
290    #region Helpers
291    private void SetEnabledStateOfExecutableButtons() {
292      if (Content == null) {
293        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
294      } else {
295        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
296        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
297        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
298        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
299      }
300    }
301    #endregion
302  }
303}
Note: See TracBrowser for help on using the repository browser.