Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5751 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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