Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted HL.Optimization.Views to new ReadOnly mechanism (ticket #973)

File size: 12.3 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;
29using HeuristicLab.Persistence.Default.Xml;
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    /// <summary>
53    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
54    /// </summary>
55    /// <param name="item">The item that should be displayed.</param>
56    public AlgorithmView(IAlgorithm content)
57      : this() {
58      Content = content;
59    }
60
[2916]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
[2851]75    protected override void DeregisterContentEvents() {
76      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]77      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
[2851]78      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
79      Content.Prepared -= new EventHandler(Content_Prepared);
80      Content.ProblemChanged -= new EventHandler(Content_ProblemChanged);
81      base.DeregisterContentEvents();
82    }
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
[3262]86      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
[2851]87      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
88      Content.Prepared += new EventHandler(Content_Prepared);
89      Content.ProblemChanged += new EventHandler(Content_ProblemChanged);
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;
[2851]99        tabControl.Enabled = false;
[3262]100        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
[2851]101        executionTimeTextBox.Text = "-";
102        executionTimeTextBox.Enabled = false;
103      } else {
104        parameterCollectionView.Content = Content.Parameters;
105        saveProblemButton.Enabled = Content.Problem != null;
[2949]106        problemViewHost.ViewType = null;
[2851]107        problemViewHost.Content = Content.Problem;
[3226]108        resultsView.Content = Content.Results.AsReadOnly();
[3275]109        runsView.Content = Content.Runs;
[2851]110        tabControl.Enabled = true;
[3262]111        EnableDisableButtons();
112        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2851]113        executionTimeTextBox.Enabled = true;
114      }
[3367]115      SetEnableStateOfControls();
[2851]116    }
117
[3367]118    protected override void OnReadOnlyChanged() {
119      base.OnReadOnlyChanged();
120      SetEnableStateOfControls();
121    }
122    private void SetEnableStateOfControls() {
123      parameterCollectionView.ReadOnly = ReadOnly;
124      problemViewHost.ReadOnly = ReadOnly;
125      resultsView.ReadOnly = ReadOnly;
126      runsView.ReadOnly = ReadOnly;
127    }
128
[2955]129    protected override void OnClosed(FormClosedEventArgs e) {
[3262]130      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
[2955]131      base.OnClosed(e);
132    }
133
[2851]134    #region Content Events
[2998]135    protected virtual void Content_ProblemChanged(object sender, EventArgs e) {
[2851]136      if (InvokeRequired)
137        Invoke(new EventHandler(Content_ProblemChanged), sender, e);
138      else {
[2949]139        problemViewHost.ViewType = null;
[2851]140        problemViewHost.Content = Content.Problem;
141        saveProblemButton.Enabled = Content.Problem != null;
142      }
143    }
[3262]144    protected virtual void Content_Prepared(object sender, EventArgs e) {
[2851]145      if (InvokeRequired)
[3262]146        Invoke(new EventHandler(Content_Prepared), sender, e);
147      else
148        resultsView.Content = Content.Results.AsReadOnly();
149    }
150    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
[2851]153      else {
[3367]154        this.ReadOnly = Content.ExecutionState == ExecutionState.Started;
[3262]155        SaveEnabled = Content.ExecutionState != ExecutionState.Started;
156        EnableDisableButtons();
[2851]157      }
158    }
159    protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
[3262]160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
162      else
163        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
[2851]164    }
165    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
168      else
169        Auxiliary.ShowErrorMessageBox(e.Value);
170    }
171    #endregion
172
[3299]173    #region Control Events
[2998]174    protected virtual void newProblemButton_Click(object sender, EventArgs e) {
[2851]175      if (problemTypeSelectorDialog == null) {
176        problemTypeSelectorDialog = new TypeSelectorDialog();
177        problemTypeSelectorDialog.Caption = "Select Problem";
[2852]178        problemTypeSelectorDialog.TypeSelector.Configure(Content.ProblemType, false, false);
[2851]179      }
180      if (problemTypeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
181        Content.Problem = (IProblem)problemTypeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
182      }
183    }
[2998]184    protected virtual void openProblemButton_Click(object sender, EventArgs e) {
[2851]185      openFileDialog.Title = "Open Problem";
186      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
[2954]187        this.Cursor = Cursors.AppStarting;
188        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
[3177]189        problemViewHost.Enabled = false;
[2954]190
191        var call = new Func<string, object>(XmlParser.Deserialize);
192        call.BeginInvoke(openFileDialog.FileName, delegate(IAsyncResult a) {
193          IProblem problem = null;
194          try {
195            problem = call.EndInvoke(a) as IProblem;
196          } catch (Exception ex) {
197            Auxiliary.ShowErrorMessageBox(ex);
198          }
199          Invoke(new Action(delegate() {
200            if (problem == null)
201              MessageBox.Show(this, "The selected file does not contain a problem.", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
202            else if (!Content.ProblemType.IsInstanceOfType(problem))
203              MessageBox.Show(this, "The selected file contains a problem type which is not supported by this algorithm.", "Invalid Problem Type", MessageBoxButtons.OK, MessageBoxIcon.Error);
204            else
205              Content.Problem = problem;
[3177]206            problemViewHost.Enabled = true;
[2954]207            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
208            this.Cursor = Cursors.Default;
209          }));
210        }, null);
[2851]211      }
212    }
[2998]213    protected virtual void saveProblemButton_Click(object sender, EventArgs e) {
[2851]214      saveFileDialog.Title = "Save Problem";
215      if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
[2954]216        this.Cursor = Cursors.AppStarting;
217        newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = false;
[3177]218        problemViewHost.Enabled = false;
[2954]219
220        var call = new Action<IProblem, string, int>(XmlGenerator.Serialize);
221        int compression = 9;
222        if (saveFileDialog.FilterIndex == 1) compression = 0;
223        call.BeginInvoke(Content.Problem, saveFileDialog.FileName, compression, delegate(IAsyncResult a) {
224          try {
225            call.EndInvoke(a);
226          }
227          catch (Exception ex) {
228            Auxiliary.ShowErrorMessageBox(ex);
229          }
230          Invoke(new Action(delegate() {
[3177]231            problemViewHost.Enabled = true;
[2954]232            newProblemButton.Enabled = openProblemButton.Enabled = saveProblemButton.Enabled = true;
233            this.Cursor = Cursors.Default;
234          }));
235        }, null);
[2851]236      }
237    }
238    protected virtual void startButton_Click(object sender, EventArgs e) {
239      Content.Start();
240    }
[3262]241    protected virtual void pauseButton_Click(object sender, EventArgs e) {
242      Content.Pause();
243    }
[2851]244    protected virtual void stopButton_Click(object sender, EventArgs e) {
245      Content.Stop();
246    }
247    protected virtual void resetButton_Click(object sender, EventArgs e) {
[3275]248      if (Content.Runs.Count > 0) {
249        if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
[3274]250          Content.Prepare(true);
251        else
252          Content.Prepare(false);
253      } else {
254        Content.Prepare();
255      }
[2851]256    }
[3299]257    protected virtual void problemPanel_DragEnterOver(object sender, DragEventArgs e) {
258      e.Effect = DragDropEffects.None;
259      Type type = e.Data.GetData("Type") as Type;
260      if ((type != null) && (Content.ProblemType.IsAssignableFrom(type))) {
261        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
262        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
263        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
264        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
265        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
266      }
267    }
268    protected virtual void problemPanel_DragDrop(object sender, DragEventArgs e) {
269      if (e.Effect != DragDropEffects.None) {
270        IProblem problem = e.Data.GetData("Value") as IProblem;
271        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) problem = (IProblem)problem.Clone();
272        Content.Problem = problem;
273      }
274    }
[2851]275    #endregion
276
277    #region Helpers
[3262]278    private void EnableDisableButtons() {
279      startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
280      pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
281      stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
282      resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
[2851]283    }
284    #endregion
285  }
286}
Note: See TracBrowser for help on using the repository browser.