Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/ExperimentView.cs @ 4526

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

Corrected locked and readonly state in ExperimentView (ticket #1155).

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        optimizerListView.Content = Content.Optimizers;
78        runsViewHost.Content = Content.Runs;
79        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      optimizerListView.Enabled = Content != null;
86      runsViewHost.Enabled = Content != null;
87      executionTimeTextBox.Enabled = Content != null;
88      SetEnabledStateOfExecutableButtons();
89    }
90
91    protected override void OnClosed(FormClosedEventArgs e) {
92      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
93      base.OnClosed(e);
94    }
95
96    #region Content Events
97    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
98      if (InvokeRequired)
99        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
100      else
101        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
102    }
103    private void Content_Prepared(object sender, EventArgs e) {
104      if (InvokeRequired)
105        Invoke(new EventHandler(Content_Prepared), sender, e);
106      else {
107        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
108        ReadOnly = Locked = false;
109        SetEnabledStateOfExecutableButtons();
110      }
111    }
112    private void Content_Started(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_Started), sender, e);
115      else {
116        nameTextBox.Enabled = descriptionTextBox.Enabled = false;
117        ReadOnly = Locked = true;
118        SetEnabledStateOfExecutableButtons();
119      }
120    }
121    private void Content_Paused(object sender, EventArgs e) {
122      if (InvokeRequired)
123        Invoke(new EventHandler(Content_Paused), sender, e);
124      else {
125        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
126        ReadOnly = Locked = false;
127        SetEnabledStateOfExecutableButtons();
128      }
129    }
130    private void Content_Stopped(object sender, EventArgs e) {
131      if (InvokeRequired)
132        Invoke(new EventHandler(Content_Stopped), sender, e);
133      else {
134        nameTextBox.Enabled = descriptionTextBox.Enabled = true;
135        ReadOnly = Locked = false;
136        SetEnabledStateOfExecutableButtons();
137      }
138    }
139    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
140      if (InvokeRequired)
141        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
142      else
143        executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
144    }
145    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
148      else
149        ErrorHandling.ShowErrorDialog(this, e.Value);
150    }
151    #endregion
152
153    #region Control events
154    private void startButton_Click(object sender, EventArgs e) {
155      Content.Start();
156    }
157    private void pauseButton_Click(object sender, EventArgs e) {
158      Content.Pause();
159    }
160    private void stopButton_Click(object sender, EventArgs e) {
161      Content.Stop();
162    }
163    private void resetButton_Click(object sender, EventArgs e) {
164      Content.Prepare(false);
165    }
166    #endregion
167
168    #region Helpers
169    private void SetEnabledStateOfExecutableButtons() {
170      if (Content == null) {
171        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
172      } else {
173        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
174        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
175        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
176        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
177      }
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.