Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 5.4 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      base.DeregisterContentEvents();
54    }
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
58      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
59      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) {
65        optimizerListView.Content = null;
66        runsViewHost.Content = null;
67        executionTimeTextBox.Text = "-";
68      } else {
69        optimizerListView.Content = Content.Optimizers;
70        runsViewHost.Content = Content.Runs;
71        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
72      }
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77      optimizerListView.Enabled = Content != null;
78      runsViewHost.Enabled = Content != null;
79      executionTimeTextBox.Enabled = Content != null;
80      SetEnabledStateOfExecutableButtons();
81    }
82
83    protected override void OnClosed(FormClosedEventArgs e) {
84      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
85      base.OnClosed(e);
86    }
87
88    #region Content Events
89    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
90      if (InvokeRequired)
91        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
92      else {
93        nameTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
94        descriptionTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
95        Locked = Content.ExecutionState == ExecutionState.Started;
96        SetEnabledStateOfExecutableButtons();
97      }
98    }
99    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
100      if (InvokeRequired)
101        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
102      else
103        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
104    }
105    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
106      if (InvokeRequired)
107        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
108      else
109        ErrorHandling.ShowErrorDialog(this, e.Value);
110    }
111    #endregion
112
113    #region Control events
114    private void startButton_Click(object sender, EventArgs e) {
115      Content.Start();
116    }
117    private void pauseButton_Click(object sender, EventArgs e) {
118      Content.Pause();
119    }
120    private void stopButton_Click(object sender, EventArgs e) {
121      Content.Stop();
122    }
123    private void resetButton_Click(object sender, EventArgs e) {
124      Content.Prepare(false);
125    }
126    #endregion
127
128    #region Helpers
129    private void SetEnabledStateOfExecutableButtons() {
130      if (Content == null) {
131        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
132      } else {
133        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
134        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
135        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
136        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
137      }
138    }
139    #endregion
140  }
141}
Note: See TracBrowser for help on using the repository browser.