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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Clients.Hive.Views {
|
---|
32 | /// <summary>
|
---|
33 | /// The base class for visual representations of items.
|
---|
34 | /// </summary>
|
---|
35 | [View("Experiment View")]
|
---|
36 | [Content(typeof(HiveExperimentClient), true)]
|
---|
37 | public sealed partial class HiveExperimentView : NamedItemView {
|
---|
38 | private ProgressView progressView;
|
---|
39 |
|
---|
40 | public new HiveExperimentClient Content {
|
---|
41 | get { return (HiveExperimentClient)base.Content; }
|
---|
42 | set { base.Content = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Initializes a new instance of <see cref="ItemBaseView"/>.
|
---|
47 | /// </summary>
|
---|
48 | public HiveExperimentView() {
|
---|
49 | InitializeComponent();
|
---|
50 | downloadExperimentPanel.Dock = DockStyle.Fill;
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void DeregisterContentEvents() {
|
---|
54 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
55 | Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
|
---|
56 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
57 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
58 | Content.Started -= new EventHandler(Content_Started);
|
---|
59 | Content.Paused -= new EventHandler(Content_Paused);
|
---|
60 | Content.Stopped -= new EventHandler(Content_Stopped);
|
---|
61 | Content.IsResultsPollingChanged -= new EventHandler(Content_IsResultsPollingChanged);
|
---|
62 | Content.HiveJobChanged -= new EventHandler(Content_HiveJobChanged);
|
---|
63 | Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
|
---|
64 | base.DeregisterContentEvents();
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override void RegisterContentEvents() {
|
---|
68 | base.RegisterContentEvents();
|
---|
69 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
70 | Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
|
---|
71 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
72 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
73 | Content.Started += new EventHandler(Content_Started);
|
---|
74 | Content.Paused += new EventHandler(Content_Paused);
|
---|
75 | Content.Stopped += new EventHandler(Content_Stopped);
|
---|
76 | Content.IsResultsPollingChanged += new EventHandler(Content_IsResultsPollingChanged);
|
---|
77 | Content.HiveJobChanged += new EventHandler(Content_HiveJobChanged);
|
---|
78 | Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override void OnContentChanged() {
|
---|
82 | base.OnContentChanged();
|
---|
83 | if (Content == null) {
|
---|
84 | executionTimeTextBox.Text = string.Empty;
|
---|
85 | resourceIdsTextBox.Text = string.Empty;
|
---|
86 | logView.Content = null;
|
---|
87 | } else {
|
---|
88 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
89 | resourceIdsTextBox.Text = Content.ResourceIds;
|
---|
90 | logView.Content = Content.Log;
|
---|
91 | }
|
---|
92 | Content_HiveJobChanged(this, EventArgs.Empty);
|
---|
93 | Content_IsProgressingChanged(this, EventArgs.Empty);
|
---|
94 | SetEnabledStateOfControls();
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override void SetEnabledStateOfControls() {
|
---|
98 | base.SetEnabledStateOfControls();
|
---|
99 | executionTimeTextBox.Enabled = Content != null;
|
---|
100 | experimentNamedItemView.ReadOnly = true;
|
---|
101 | if (Content != null) {
|
---|
102 | this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
103 | this.descriptionTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
104 | this.resourceIdsTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
105 | this.hiveJobView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
106 |
|
---|
107 | viewExperimentButton.Enabled = Content.GetExperiment() != null;
|
---|
108 |
|
---|
109 | this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
|
---|
110 | downloadExperimentPanel.Visible = Content.HiveExperimentId != Guid.Empty && Content.GetExperiment() == null;
|
---|
111 | }
|
---|
112 | SetEnabledStateOfExecutableButtons();
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
116 | if (Content != null) {
|
---|
117 | if (Content.IsPollingResults)
|
---|
118 | Content.StopResultPolling();
|
---|
119 | }
|
---|
120 | base.OnClosed(e);
|
---|
121 | }
|
---|
122 |
|
---|
123 | #region Content Events
|
---|
124 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
125 | if (InvokeRequired)
|
---|
126 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
127 | else
|
---|
128 | SetEnabledStateOfControls();
|
---|
129 | }
|
---|
130 | private void Content_Prepared(object sender, EventArgs e) {
|
---|
131 | if (InvokeRequired)
|
---|
132 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
133 | else {
|
---|
134 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
135 | Locked = false;
|
---|
136 | SetEnabledStateOfControls();
|
---|
137 | }
|
---|
138 | }
|
---|
139 | private void Content_Started(object sender, EventArgs e) {
|
---|
140 | if (InvokeRequired)
|
---|
141 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
142 | else {
|
---|
143 | nameTextBox.Enabled = descriptionTextBox.Enabled = false;
|
---|
144 | SetEnabledStateOfControls();
|
---|
145 | }
|
---|
146 | }
|
---|
147 | private void Content_Paused(object sender, EventArgs e) {
|
---|
148 | if (InvokeRequired)
|
---|
149 | Invoke(new EventHandler(Content_Paused), sender, e);
|
---|
150 | else {
|
---|
151 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
152 | SetEnabledStateOfControls();
|
---|
153 | }
|
---|
154 | }
|
---|
155 | private void Content_Stopped(object sender, EventArgs e) {
|
---|
156 | if (InvokeRequired)
|
---|
157 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
158 | else {
|
---|
159 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
160 | Locked = false;
|
---|
161 | SetEnabledStateOfControls();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
165 | if (InvokeRequired)
|
---|
166 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
167 | else
|
---|
168 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
169 | }
|
---|
170 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
171 | if (InvokeRequired)
|
---|
172 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
173 | else
|
---|
174 | ErrorHandling.ShowErrorDialog(this, e.Value);
|
---|
175 | }
|
---|
176 | private void Content_IsResultsPollingChanged(object sender, EventArgs e) {
|
---|
177 | if (InvokeRequired)
|
---|
178 | Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e);
|
---|
179 | else {
|
---|
180 | SetEnabledStateOfControls();
|
---|
181 | }
|
---|
182 | }
|
---|
183 | void Content_HiveJobChanged(object sender, EventArgs e) {
|
---|
184 | if (InvokeRequired)
|
---|
185 | Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
|
---|
186 | else {
|
---|
187 | if (Content != null) {
|
---|
188 | hiveJobView.Content = Content.HiveJob;
|
---|
189 | experimentNamedItemView.Content = Content.GetExperiment();
|
---|
190 | } else {
|
---|
191 | hiveJobView.Content = null;
|
---|
192 | experimentNamedItemView.Content = null;
|
---|
193 | }
|
---|
194 | SetEnabledStateOfControls();
|
---|
195 | }
|
---|
196 | }
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | #region Control events
|
---|
200 | private void startButton_Click(object sender, EventArgs e) {
|
---|
201 | Content.Start();
|
---|
202 | }
|
---|
203 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
204 | Content.Pause();
|
---|
205 | }
|
---|
206 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
207 | Content.Stop();
|
---|
208 | }
|
---|
209 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
210 | Content.Prepare();
|
---|
211 | }
|
---|
212 |
|
---|
213 | private void resourceIdsTextBox_Validated(object sender, EventArgs e) {
|
---|
214 | Content.ResourceIds = resourceIdsTextBox.Text;
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void newExperimentButton_Click(object sender, EventArgs e) {
|
---|
218 | Content.SetExperiment(new HeuristicLab.Optimization.Experiment());
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void openExperimentButton_Click(object sender, EventArgs e) {
|
---|
222 | OpenFileDialog openFileDialog = new OpenFileDialog();
|
---|
223 | openFileDialog.Title = "Open Experimenter";
|
---|
224 | openFileDialog.FileName = "Item";
|
---|
225 | openFileDialog.Multiselect = false;
|
---|
226 | openFileDialog.DefaultExt = "hl";
|
---|
227 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
228 |
|
---|
229 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
230 | Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName));
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void showExperimentButton_Click(object sender, EventArgs e) {
|
---|
235 | MainFormManager.MainForm.ShowContent(Content.GetExperiment());
|
---|
236 | }
|
---|
237 |
|
---|
238 | private void disconnectButton_Click(object sender, EventArgs e) {
|
---|
239 | if (Content != null) {
|
---|
240 | Content.StopResultPolling();
|
---|
241 | SetEnabledStateOfControls();
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void reconnectButton_Click(object sender, EventArgs e) {
|
---|
246 | if (Content != null) {
|
---|
247 | Content.StartResultPolling();
|
---|
248 | SetEnabledStateOfControls();
|
---|
249 | }
|
---|
250 | }
|
---|
251 | #endregion
|
---|
252 |
|
---|
253 | #region Helpers
|
---|
254 | private void SetEnabledStateOfExecutableButtons() {
|
---|
255 | if (Content == null) {
|
---|
256 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false;
|
---|
257 | } else {
|
---|
258 | startButton.Enabled = Content.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared;
|
---|
259 | pauseButton.Enabled = false;
|
---|
260 | stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
|
---|
261 | resetButton.Enabled = false;
|
---|
262 | reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults;
|
---|
263 | disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | #endregion
|
---|
267 |
|
---|
268 | private void downloadExperimentButton_Click(object sender, EventArgs e) {
|
---|
269 | var invoker = new MethodInvoker(Content.LoadHiveJob);
|
---|
270 | invoker.BeginInvoke((ar) => {
|
---|
271 | try {
|
---|
272 | invoker.EndInvoke(ar);
|
---|
273 | }
|
---|
274 | catch (Exception ex) {
|
---|
275 | ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
|
---|
276 | }
|
---|
277 | }, null);
|
---|
278 | }
|
---|
279 |
|
---|
280 | private void Content_IsProgressingChanged(object sender, EventArgs e) {
|
---|
281 | if (this.InvokeRequired) {
|
---|
282 | Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
|
---|
283 | } else {
|
---|
284 | if (Content != null && Content.IsProgressing) {
|
---|
285 | SetProgressView();
|
---|
286 | } else {
|
---|
287 | FinishProgressView();
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void SetProgressView() {
|
---|
293 | if (progressView == null) {
|
---|
294 | progressView = new ProgressView(this, Content.Progress);
|
---|
295 | }
|
---|
296 | progressView.Progress = Content.Progress;
|
---|
297 | }
|
---|
298 |
|
---|
299 | private void FinishProgressView() {
|
---|
300 | if (progressView != null) {
|
---|
301 | progressView.Finish();
|
---|
302 | progressView = null;
|
---|
303 | SetEnabledStateOfControls();
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|