Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Optimization.Views/3.3/ExperimentView.cs @ 4656

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

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

File size: 7.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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("Experiment View")]
35  [Content(typeof(Experiment), true)]
36  public sealed partial class ExperimentView : NamedItemView {
37    public new Experiment Content {
38      get { return (Experiment)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="ItemBaseView"/>.
44    /// </summary>
45    public ExperimentView() {
46      InitializeComponent();
47    }
48
49    protected override void DeregisterContentEvents() {
50      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
51      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
52      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
53      Content.Prepared -= new EventHandler(Content_Prepared);
54      Content.Started -= new EventHandler(Content_Started);
55      Content.Paused -= new EventHandler(Content_Paused);
56      Content.Stopped -= new EventHandler(Content_Stopped);
57      base.DeregisterContentEvents();
58    }
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
62      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
63      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
64      Content.Prepared += new EventHandler(Content_Prepared);
65      Content.Started += new EventHandler(Content_Started);
66      Content.Paused += new EventHandler(Content_Paused);
67      Content.Stopped += new EventHandler(Content_Stopped);
68    }
69
70    protected override void OnContentChanged() {
71      base.OnContentChanged();
72      if (Content == null) {
73        optimizerListView.Content = null;
74        runsViewHost.Content = null;
75        executionTimeTextBox.Text = "-";
76      } else {
77        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
78        optimizerListView.Content = Content.Optimizers;
79        runsViewHost.Content = Content.Runs;
80        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
81      }
82    }
83
84    protected override void SetEnabledStateOfControls() {
85      base.SetEnabledStateOfControls();
86      optimizerListView.Enabled = Content != null;
87      runsViewHost.Enabled = Content != null;
88      executionTimeTextBox.Enabled = Content != null;
89      SetEnabledStateOfExecutableButtons();
90    }
91
92    protected override void OnClosed(FormClosedEventArgs e) {
93      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
94      base.OnClosed(e);
95    }
96
97    #region Content Events
98    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
99      if (InvokeRequired)
100        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
101      else
102        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
103    }
104    private void Content_Prepared(object sender, EventArgs e) {
105      if (InvokeRequired)
106        Invoke(new EventHandler(Content_Prepared), sender, e);
107      else {
108        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
109        ReadOnly = Locked = false;
110        SetEnabledStateOfExecutableButtons();
111      }
112    }
113    private void Content_Started(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_Started), sender, e);
116      else {
117        nameTextBox.Enabled = descriptionTextBox.Enabled = false;
118        ReadOnly = Locked = true;
119        SetEnabledStateOfExecutableButtons();
120      }
121    }
122    private void Content_Paused(object sender, EventArgs e) {
123      if (InvokeRequired)
124        Invoke(new EventHandler(Content_Paused), sender, e);
125      else {
126        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
127        ReadOnly = Locked = false;
128        SetEnabledStateOfExecutableButtons();
129      }
130    }
131    private void Content_Stopped(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_Stopped), sender, e);
134      else {
135        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
136        ReadOnly = Locked = false;
137        SetEnabledStateOfExecutableButtons();
138      }
139    }
140    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
141      if (InvokeRequired)
142        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
143      else
144        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
145    }
146    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
149      else
150        ErrorHandling.ShowErrorDialog(this, e.Value);
151    }
152    #endregion
153
154    #region Control events
155    private void startButton_Click(object sender, EventArgs e) {
156      Content.Start();
157    }
158    private void pauseButton_Click(object sender, EventArgs e) {
159      Content.Pause();
160    }
161    private void stopButton_Click(object sender, EventArgs e) {
162      Content.Stop();
163    }
164    private void resetButton_Click(object sender, EventArgs e) {
165      Content.Prepare(false);
166    }
167    #endregion
168
169    #region Helpers
170    private void SetEnabledStateOfExecutableButtons() {
171      if (Content == null) {
172        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
173      } else {
174        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
175        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
176        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
177        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
178      }
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.