1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Core.Views;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Optimization.Views {
|
---|
29 | [View("IOptimizer View")]
|
---|
30 | [Content(typeof(IOptimizer), false)]
|
---|
31 | public partial class IOptimizerView : NamedItemView {
|
---|
32 | public IOptimizerView() {
|
---|
33 | InitializeComponent();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public new IOptimizer Content {
|
---|
37 | get { return (IOptimizer)base.Content; }
|
---|
38 | set { base.Content = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected override void DeregisterContentEvents() {
|
---|
42 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
43 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
44 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
45 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
46 | Content.Started -= new EventHandler(Content_Started);
|
---|
47 | Content.Paused -= new EventHandler(Content_Paused);
|
---|
48 | Content.Stopped -= new EventHandler(Content_Stopped);
|
---|
49 | base.DeregisterContentEvents();
|
---|
50 | }
|
---|
51 | protected override void RegisterContentEvents() {
|
---|
52 | base.RegisterContentEvents();
|
---|
53 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
54 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
55 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
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 | executionTimeTextBox.Text = "-";
|
---|
66 | } else {
|
---|
67 | Locked = ReadOnly = Content.ExecutionState == ExecutionState.Started;
|
---|
68 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override void SetEnabledStateOfControls() {
|
---|
73 | base.SetEnabledStateOfControls();
|
---|
74 | executionTimeTextBox.Enabled = Content != null;
|
---|
75 | SetEnabledStateOfExecutableButtons();
|
---|
76 | }
|
---|
77 |
|
---|
78 | #region Content Events
|
---|
79 | protected virtual void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
80 | if (InvokeRequired)
|
---|
81 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
82 | else
|
---|
83 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
84 | }
|
---|
85 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
86 | if (InvokeRequired)
|
---|
87 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
88 | else {
|
---|
89 | nameTextBox.Enabled = infoLabel.Enabled = true;
|
---|
90 | ReadOnly = Locked = false;
|
---|
91 | SetEnabledStateOfExecutableButtons();
|
---|
92 | }
|
---|
93 | }
|
---|
94 | protected virtual void Content_Started(object sender, EventArgs e) {
|
---|
95 | if (InvokeRequired)
|
---|
96 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
97 | else {
|
---|
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 | nameTextBox.Enabled = infoLabel.Enabled = true;
|
---|
108 | ReadOnly = Locked = false;
|
---|
109 | SetEnabledStateOfExecutableButtons();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | protected virtual void Content_Stopped(object sender, EventArgs e) {
|
---|
113 | if (InvokeRequired)
|
---|
114 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
115 | else {
|
---|
116 | nameTextBox.Enabled = infoLabel.Enabled = true;
|
---|
117 | ReadOnly = Locked = false;
|
---|
118 | SetEnabledStateOfExecutableButtons();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
122 | if (InvokeRequired)
|
---|
123 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
124 | else
|
---|
125 | executionTimeTextBox.Text = Content == null ? "-" : Content.ExecutionTime.ToString();
|
---|
126 | }
|
---|
127 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
130 | else
|
---|
131 | MainFormManager.MainForm.ShowError(e.Value.Message, e.Value);
|
---|
132 | }
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region Control events
|
---|
136 | protected virtual async void startButton_Click(object sender, EventArgs e) {
|
---|
137 | await Content.StartAsync();
|
---|
138 | }
|
---|
139 | protected virtual void pauseButton_Click(object sender, EventArgs e) {
|
---|
140 | Content.Pause();
|
---|
141 | }
|
---|
142 | protected virtual void stopButton_Click(object sender, EventArgs e) {
|
---|
143 | Content.Stop();
|
---|
144 | }
|
---|
145 | protected virtual void resetButton_Click(object sender, EventArgs e) {
|
---|
146 | Content.Prepare(false);
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | #region Helpers
|
---|
151 | protected virtual void SetEnabledStateOfExecutableButtons() {
|
---|
152 | if (Content == null) {
|
---|
153 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
|
---|
154 | } else {
|
---|
155 | startButton.Enabled = (Content.ExecutionState == ExecutionState.Prepared) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
156 | pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
|
---|
157 | stopButton.Enabled = (Content.ExecutionState == ExecutionState.Started) || (Content.ExecutionState == ExecutionState.Paused);
|
---|
158 | resetButton.Enabled = Content.ExecutionState != ExecutionState.Started;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 | }
|
---|
163 | }
|
---|