Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Optimization.Views/3.3/IOptimizerView.cs @ 15603

Last change on this file since 15603 was 15603, checked in by abeham, 6 years ago

#1614: Implemented changed behavior to measure execution time (cf. #2869)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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  [View("IOptimizer View")]
32  [Content(typeof(IOptimizer), false)]
33  public partial class IOptimizerView : NamedItemView {
34    public IOptimizerView() {
35      InitializeComponent();
36    }
37
38    public new IOptimizer Content {
39      get { return (IOptimizer)base.Content; }
40      set { base.Content = value; }
41    }
42
43    protected override void DeregisterContentEvents() {
44      Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
45      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
46      Content.Prepared -= new EventHandler(Content_Prepared);
47      Content.Started -= new EventHandler(Content_Started);
48      Content.Paused -= new EventHandler(Content_Paused);
49      Content.Stopped -= new EventHandler(Content_Stopped);
50      base.DeregisterContentEvents();
51    }
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
55      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
56      Content.Prepared += new EventHandler(Content_Prepared);
57      Content.Started += new EventHandler(Content_Started);
58      Content.Paused += new EventHandler(Content_Paused);
59      Content.Stopped += new EventHandler(Content_Stopped);
60    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content != null) {
65        Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
66      }
67      UpdateExecutionTime();
68    }
69
70    protected override void SetEnabledStateOfControls() {
71      base.SetEnabledStateOfControls();
72      executionTimeTextBox.Enabled = Content != null;
73      SetEnabledStateOfExecutableButtons();
74    }
75
76    #region Content Events
77    protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
78      if (InvokeRequired)
79        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
80      else
81        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
82    }
83    protected virtual void Content_Prepared(object sender, EventArgs e) {
84      if (InvokeRequired)
85        Invoke(new EventHandler(Content_Prepared), sender, e);
86      else {
87        nameTextBox.Enabled = infoLabel.Enabled = true;
88        UpdateExecutionTime();
89        ReadOnly = Locked = false;
90        SetEnabledStateOfExecutableButtons();
91      }
92    }
93    protected virtual void Content_Started(object sender, EventArgs e) {
94      if (InvokeRequired)
95        Invoke(new EventHandler(Content_Started), sender, e);
96      else {
97        executionTimer.Start();
98        nameTextBox.Enabled = infoLabel.Enabled = false;
99        ReadOnly = Locked = true;
100        SetEnabledStateOfExecutableButtons();
101      }
102    }
103    protected virtual void Content_Paused(object sender, EventArgs e) {
104      if (InvokeRequired)
105        Invoke(new EventHandler(Content_Paused), sender, e);
106      else {
107        executionTimer.Stop();
108        UpdateExecutionTime();
109        nameTextBox.Enabled = infoLabel.Enabled = true;
110        ReadOnly = Locked = false;
111        SetEnabledStateOfExecutableButtons();
112      }
113    }
114    protected virtual void Content_Stopped(object sender, EventArgs e) {
115      if (InvokeRequired)
116        Invoke(new EventHandler(Content_Stopped), sender, e);
117      else {
118        executionTimer.Stop();
119        UpdateExecutionTime();
120        nameTextBox.Enabled = infoLabel.Enabled = true;
121        ReadOnly = Locked = false;
122        SetEnabledStateOfExecutableButtons();
123      }
124    }
125    protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
126      if (InvokeRequired)
127        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
128      else {
129        executionTimer.Stop();
130        UpdateExecutionTime();
131        ErrorHandling.ShowErrorDialog(this, e.Value);
132      }
133    }
134    #endregion
135
136    #region Control events
137    protected virtual async void startButton_Click(object sender, EventArgs e) {
138      await Content.StartAsync();
139    }
140    protected virtual void pauseButton_Click(object sender, EventArgs e) {
141      Content.Pause();
142    }
143    protected virtual void stopButton_Click(object sender, EventArgs e) {
144      Content.Stop();
145    }
146    protected virtual void resetButton_Click(object sender, EventArgs e) {
147      Content.Prepare(false);
148    }
149    private void executionTimer_Tick(object sender, EventArgs e) {
150      UpdateExecutionTime();
151    }
152
153    private void UpdateExecutionTime() {
154      executionTimeTextBox.Text = (Content == null ? "-" : Content.ExecutionTime.ToString());
155    }
156    #endregion
157
158    #region Helpers
159    protected virtual void SetEnabledStateOfExecutableButtons() {
160      if (Content == null) {
161        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
162      } else {
163        startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
164        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
165        stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
166        resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
167      }
168    }
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.