Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adapted views according the new read-only property (#973)

File size: 6.2 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;
28
29namespace HeuristicLab.Optimization.Views {
30  /// <summary>
31  /// The base class for visual representations of items.
32  /// </summary>
33  [View("Experiment View")]
34  [Content(typeof(Experiment), true)]
35  public sealed partial class ExperimentView : NamedItemView {
36    public new Experiment Content {
37      get { return (Experiment)base.Content; }
38      set { base.Content = value; }
39    }
40
41    /// <summary>
42    /// Initializes a new instance of <see cref="ItemBaseView"/>.
43    /// </summary>
44    public ExperimentView() {
45      InitializeComponent();
46    }
47    /// <summary>
48    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
49    /// </summary>
50    /// <param name="item">The item that should be displayed.</param>
51    public ExperimentView(Experiment content)
52      : this() {
53      Content = content;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
58      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
59      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
60      base.DeregisterContentEvents();
61    }
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
65      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
66      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        optimizerListView.Content = null;
73        runsViewHost.Content = null;
74        executionTimeTextBox.Text = "-";
75      } else {
76        optimizerListView.Content = Content.Optimizers;
77        runsViewHost.Content = Content.Runs;
78        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
79      }
80      SetEnableStateOfControls();
81    }
82
83    protected override void OnReadOnlyChanged() {
84      base.OnReadOnlyChanged();
85      SetEnableStateOfControls();
86    }
87    private void SetEnableStateOfControls() {
88      optimizerListView.Enabled = Content != null;
89      optimizerListView.ReadOnly = ReadOnly;
90      runsViewHost.Enabled = Content != null;
91      runsViewHost.ReadOnly = ReadOnly;
92      executionTimeTextBox.Enabled = Content != null;
93      SetEnabledStateOfExecutableButtons();
94    }
95
96    protected override void OnClosed(FormClosedEventArgs e) {
97      if ((Content != null) && (Content.ExecutionState == ExecutionState.Started)) Content.Stop();
98      base.OnClosed(e);
99    }
100
101    #region Content Events
102    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
103      if (InvokeRequired)
104        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
105      else {
106        nameTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
107        descriptionTextBox.Enabled = Content.ExecutionState != ExecutionState.Started;
108        Locked = Content.ExecutionState == ExecutionState.Started;
109        SetEnabledStateOfExecutableButtons();
110      }
111    }
112    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
113      if (InvokeRequired)
114        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
115      else
116        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
117    }
118    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
121      else
122        Auxiliary.ShowErrorMessageBox(e.Value);
123    }
124    #endregion
125
126    #region Control events
127    private void startButton_Click(object sender, EventArgs e) {
128      Content.Start();
129    }
130    private void pauseButton_Click(object sender, EventArgs e) {
131      Content.Pause();
132    }
133    private void stopButton_Click(object sender, EventArgs e) {
134      Content.Stop();
135    }
136    private void resetButton_Click(object sender, EventArgs e) {
137      if (Content.Runs.Count > 0) {
138        if (MessageBox.Show(this, "Clear all runs executed so far?", "Clear All Runs?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
139          Content.Prepare(true);
140        else
141          Content.Prepare(false);
142      } else {
143        Content.Prepare();
144      }
145    }
146    #endregion
147
148    #region Helpers
149    private void SetEnabledStateOfExecutableButtons() {
150      if (Content == null) {
151        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
152      } else {
153        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
154        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
155        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
156        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
157      }
158    }
159    #endregion
160  }
161}
Note: See TracBrowser for help on using the repository browser.