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 | resourceNamesTextBox.Text = string.Empty;
|
---|
86 | useLocalPluginsCheckBox.Checked = false;
|
---|
87 | logView.Content = null;
|
---|
88 | } else {
|
---|
89 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
90 | resourceNamesTextBox.Text = Content.ResourceNames;
|
---|
91 | useLocalPluginsCheckBox.Checked = Content.UseLocalPlugins;
|
---|
92 | logView.Content = Content.Log;
|
---|
93 | }
|
---|
94 | Content_HiveJobChanged(this, EventArgs.Empty);
|
---|
95 | Content_IsProgressingChanged(this, EventArgs.Empty);
|
---|
96 | SetEnabledStateOfControls();
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override void SetEnabledStateOfControls() {
|
---|
100 | base.SetEnabledStateOfControls();
|
---|
101 | executionTimeTextBox.Enabled = Content != null;
|
---|
102 | experimentNamedItemView.ReadOnly = true;
|
---|
103 | if (Content != null) {
|
---|
104 | this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
105 | this.descriptionTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
106 | this.resourceNamesTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
107 | this.hiveJobView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
|
---|
108 | this.useLocalPluginsCheckBox.Enabled = Content.ExecutionState == ExecutionState.Prepared;
|
---|
109 |
|
---|
110 | viewExperimentButton.Enabled = Content.GetExperiment() != null;
|
---|
111 |
|
---|
112 | this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
|
---|
113 | downloadExperimentPanel.Visible = Content.HiveExperimentId != Guid.Empty && Content.GetExperiment() == null;
|
---|
114 | }
|
---|
115 | SetEnabledStateOfExecutableButtons();
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected override void OnClosed(FormClosedEventArgs e) {
|
---|
119 | if (Content != null) {
|
---|
120 | if (Content.IsPollingResults)
|
---|
121 | Content.StopResultPolling();
|
---|
122 | }
|
---|
123 | base.OnClosed(e);
|
---|
124 | }
|
---|
125 |
|
---|
126 | #region Content Events
|
---|
127 | private void Content_ExecutionStateChanged(object sender, EventArgs e) {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
|
---|
130 | else
|
---|
131 | SetEnabledStateOfControls();
|
---|
132 | }
|
---|
133 | private void Content_Prepared(object sender, EventArgs e) {
|
---|
134 | if (InvokeRequired)
|
---|
135 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
136 | else {
|
---|
137 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
138 | Locked = false;
|
---|
139 | SetEnabledStateOfControls();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | private void Content_Started(object sender, EventArgs e) {
|
---|
143 | if (InvokeRequired)
|
---|
144 | Invoke(new EventHandler(Content_Started), sender, e);
|
---|
145 | else {
|
---|
146 | nameTextBox.Enabled = descriptionTextBox.Enabled = false;
|
---|
147 | SetEnabledStateOfControls();
|
---|
148 | }
|
---|
149 | }
|
---|
150 | private void Content_Paused(object sender, EventArgs e) {
|
---|
151 | if (InvokeRequired)
|
---|
152 | Invoke(new EventHandler(Content_Paused), sender, e);
|
---|
153 | else {
|
---|
154 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
155 | SetEnabledStateOfControls();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | private void Content_Stopped(object sender, EventArgs e) {
|
---|
159 | if (InvokeRequired)
|
---|
160 | Invoke(new EventHandler(Content_Stopped), sender, e);
|
---|
161 | else {
|
---|
162 | nameTextBox.Enabled = descriptionTextBox.Enabled = true;
|
---|
163 | Locked = false;
|
---|
164 | SetEnabledStateOfControls();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
168 | if (InvokeRequired)
|
---|
169 | Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
|
---|
170 | else
|
---|
171 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
172 | }
|
---|
173 | private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
174 | if (InvokeRequired)
|
---|
175 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
176 | else
|
---|
177 | ErrorHandling.ShowErrorDialog(this, e.Value);
|
---|
178 | }
|
---|
179 | private void Content_IsResultsPollingChanged(object sender, EventArgs e) {
|
---|
180 | if (InvokeRequired)
|
---|
181 | Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e);
|
---|
182 | else {
|
---|
183 | SetEnabledStateOfControls();
|
---|
184 | }
|
---|
185 | }
|
---|
186 | void Content_HiveJobChanged(object sender, EventArgs e) {
|
---|
187 | if (InvokeRequired)
|
---|
188 | Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
|
---|
189 | else {
|
---|
190 | if (Content != null) {
|
---|
191 | hiveJobView.Content = Content.HiveJob;
|
---|
192 | experimentNamedItemView.Content = Content.GetExperiment();
|
---|
193 | } else {
|
---|
194 | hiveJobView.Content = null;
|
---|
195 | experimentNamedItemView.Content = null;
|
---|
196 | }
|
---|
197 | SetEnabledStateOfControls();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | #endregion
|
---|
201 |
|
---|
202 | #region Control events
|
---|
203 | private void startButton_Click(object sender, EventArgs e) {
|
---|
204 | Content.Start();
|
---|
205 | }
|
---|
206 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
207 | Content.Pause();
|
---|
208 | }
|
---|
209 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
210 | Content.Stop();
|
---|
211 | }
|
---|
212 | private void resetButton_Click(object sender, EventArgs e) {
|
---|
213 | Content.Prepare();
|
---|
214 | }
|
---|
215 |
|
---|
216 | private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
|
---|
217 | Content.ResourceNames = resourceNamesTextBox.Text;
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void newExperimentButton_Click(object sender, EventArgs e) {
|
---|
221 | Content.SetExperiment(new HeuristicLab.Optimization.Experiment());
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void openExperimentButton_Click(object sender, EventArgs e) {
|
---|
225 | OpenFileDialog openFileDialog = new OpenFileDialog();
|
---|
226 | openFileDialog.Title = "Open Experimenter";
|
---|
227 | openFileDialog.FileName = "Item";
|
---|
228 | openFileDialog.Multiselect = false;
|
---|
229 | openFileDialog.DefaultExt = "hl";
|
---|
230 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
231 |
|
---|
232 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
233 | Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName));
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | private void showExperimentButton_Click(object sender, EventArgs e) {
|
---|
238 | MainFormManager.MainForm.ShowContent(Content.GetExperiment());
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void disconnectButton_Click(object sender, EventArgs e) {
|
---|
242 | if (Content != null) {
|
---|
243 | Content.StopResultPolling();
|
---|
244 | SetEnabledStateOfControls();
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void reconnectButton_Click(object sender, EventArgs e) {
|
---|
249 | if (Content != null) {
|
---|
250 | Content.StartResultPolling();
|
---|
251 | SetEnabledStateOfControls();
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | private void useLocalPluginsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
256 | if (Content != null) {
|
---|
257 | Content.UseLocalPlugins = useLocalPluginsCheckBox.Checked;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | #endregion
|
---|
261 |
|
---|
262 | #region Helpers
|
---|
263 | private void SetEnabledStateOfExecutableButtons() {
|
---|
264 | if (Content == null) {
|
---|
265 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false;
|
---|
266 | } else {
|
---|
267 | startButton.Enabled = Content.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared;
|
---|
268 | pauseButton.Enabled = false;
|
---|
269 | stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
|
---|
270 | resetButton.Enabled = false;
|
---|
271 | reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults;
|
---|
272 | disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | #endregion
|
---|
276 |
|
---|
277 | private void downloadExperimentButton_Click(object sender, EventArgs e) {
|
---|
278 | var invoker = new MethodInvoker(Content.LoadHiveJob);
|
---|
279 | invoker.BeginInvoke((ar) => {
|
---|
280 | try {
|
---|
281 | invoker.EndInvoke(ar);
|
---|
282 | }
|
---|
283 | catch (Exception ex) {
|
---|
284 | ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
|
---|
285 | }
|
---|
286 | }, null);
|
---|
287 | }
|
---|
288 |
|
---|
289 | private void Content_IsProgressingChanged(object sender, EventArgs e) {
|
---|
290 | if (this.InvokeRequired) {
|
---|
291 | Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
|
---|
292 | } else {
|
---|
293 | if (Content != null && Content.IsProgressing) {
|
---|
294 | SetProgressView();
|
---|
295 | } else {
|
---|
296 | FinishProgressView();
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | private void SetProgressView() {
|
---|
302 | if (progressView == null) {
|
---|
303 | progressView = new ProgressView(this, Content.Progress);
|
---|
304 | }
|
---|
305 | progressView.Progress = Content.Progress;
|
---|
306 | }
|
---|
307 |
|
---|
308 | private void FinishProgressView() {
|
---|
309 | if (progressView != null) {
|
---|
310 | progressView.Finish();
|
---|
311 | progressView = null;
|
---|
312 | SetEnabledStateOfControls();
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|