Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.ExperimentManager.Views/3.3/HiveExperimentView.cs @ 5874

Last change on this file since 5874 was 5874, checked in by cneumuel, 13 years ago

#1260

  • added missing DisposableWrapper.cs
  • resolved broken compatibility with trunk (due to #1416)
  • updated binaries
File size: 11.5 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.Threading;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Hive.ExperimentManager.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("Experiment View")]
36  [Content(typeof(HiveExperiment), true)]
37  public sealed partial class HiveExperimentView : NamedItemView {
38    private ProgressView progressView;
39
40    public new HiveExperiment Content {
41      get { return (HiveExperiment)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.resourceIdsTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
104        this.hiveJobView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared;
105
106        viewExperimentButton.Enabled = Content.GetExperiment() != null;
107
108        this.Locked = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
109        downloadExperimentPanel.Visible = Content.HiveExperimentId != Guid.Empty && Content.GetExperiment() == null;
110      }
111      SetEnabledStateOfExecutableButtons();
112    }
113
114    protected override void OnClosed(FormClosedEventArgs e) {
115      if (Content != null) {
116        if (Content.IsPollingResults)
117          Content.StopResultPolling();
118      }
119      base.OnClosed(e);
120    }
121
122    #region Content Events
123    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
126      else
127        SetEnabledStateOfControls();
128    }
129    private void Content_Prepared(object sender, EventArgs e) {
130      if (InvokeRequired)
131        Invoke(new EventHandler(Content_Prepared), sender, e);
132      else {
133        nameTextBox.Enabled = true;
134        Locked = false;
135        SetEnabledStateOfControls();
136      }
137    }
138    private void Content_Started(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_Started), sender, e);
141      else {
142        nameTextBox.Enabled = false;
143        SetEnabledStateOfControls();
144      }
145    }
146    private void Content_Paused(object sender, EventArgs e) {
147      if (InvokeRequired)
148        Invoke(new EventHandler(Content_Paused), sender, e);
149      else {
150        nameTextBox.Enabled = true;
151        SetEnabledStateOfControls();
152      }
153    }
154    private void Content_Stopped(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_Stopped), sender, e);
157      else {
158        nameTextBox.Enabled = true;
159        Locked = false;
160        SetEnabledStateOfControls();
161      }
162    }
163    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
166      else
167        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
168    }
169    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
170      if (InvokeRequired)
171        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
172      else
173        ErrorHandling.ShowErrorDialog(this, e.Value);
174    }
175    private void Content_IsResultsPollingChanged(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Content_IsResultsPollingChanged), sender, e);
178      else {
179        SetEnabledStateOfControls();
180      }
181    }
182    void Content_HiveJobChanged(object sender, EventArgs e) {
183      if (InvokeRequired)
184        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
185      else {
186        if (Content != null) {
187          hiveJobView.Content = Content.HiveJob;
188          experimentNamedItemView.Content = Content.GetExperiment();
189        } else {
190          hiveJobView.Content = null;
191          experimentNamedItemView.Content = null;
192        }
193        SetEnabledStateOfControls();
194      }
195    }
196    #endregion
197
198    #region Control events
199    private void startButton_Click(object sender, EventArgs e) {
200      Content.Start();
201    }
202    private void pauseButton_Click(object sender, EventArgs e) {
203      Content.Pause();
204    }
205    private void stopButton_Click(object sender, EventArgs e) {
206      Content.Stop();
207    }
208    private void resetButton_Click(object sender, EventArgs e) {
209      Content.Prepare();
210    }
211
212    private void resourceIdsTextBox_Validated(object sender, EventArgs e) {
213      Content.ResourceIds = resourceIdsTextBox.Text;
214    }
215
216    private void newExperimentButton_Click(object sender, EventArgs e) {
217      Content.SetExperiment(new HeuristicLab.Optimization.Experiment());
218    }
219
220    private void openExperimentButton_Click(object sender, EventArgs e) {
221      OpenFileDialog openFileDialog = new OpenFileDialog();
222      openFileDialog.Title = "Open Experimenter";
223      openFileDialog.FileName = "Item";
224      openFileDialog.Multiselect = false;
225      openFileDialog.DefaultExt = "hl";
226      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
227
228      if (openFileDialog.ShowDialog() == DialogResult.OK) {
229        Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName));
230      }
231    }
232
233    private void showExperimentButton_Click(object sender, EventArgs e) {
234      MainFormManager.MainForm.ShowContent(Content.GetExperiment());
235    }
236
237    private void disconnectButton_Click(object sender, EventArgs e) {
238      if (Content != null) {
239        Content.StopResultPolling();
240        SetEnabledStateOfControls();
241      }
242    }
243
244    private void reconnectButton_Click(object sender, EventArgs e) {
245      if (Content != null) {
246        Content.StartResultPolling();
247        SetEnabledStateOfControls();
248      }
249    }
250    #endregion
251
252    #region Helpers
253    private void SetEnabledStateOfExecutableButtons() {
254      if (Content == null) {
255        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = reconnectButton.Enabled = disconnectButton.Enabled = false;
256      } else {
257        startButton.Enabled = Content.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared;
258        pauseButton.Enabled = false;
259        stopButton.Enabled = Content.ExecutionState == ExecutionState.Started && Content.IsPollingResults;
260        resetButton.Enabled = false;
261        reconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && !Content.IsPollingResults;
262        disconnectButton.Enabled = (Content.ExecutionState == ExecutionState.Started) && Content.IsPollingResults;
263      }
264    }
265    #endregion
266
267    private void downloadExperimentButton_Click(object sender, EventArgs e) {
268      var invoker = new MethodInvoker(Content.LoadHiveJob);
269      invoker.BeginInvoke((ar) => {
270        try {
271          invoker.EndInvoke(ar);
272        }
273        catch (Exception ex) {
274          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
275        }
276      }, null);
277    }
278
279    private void Content_IsProgressingChanged(object sender, EventArgs e) {
280      if (this.InvokeRequired) {
281        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
282      } else {
283        if (Content != null && Content.IsProgressing) {
284          SetProgressView();
285        } else {
286          FinishProgressView();
287        }
288      }
289    }
290
291    private void SetProgressView() {
292      if (progressView == null) {
293        progressView = new ProgressView(this, Content.Progress);
294      }
295      progressView.Progress = Content.Progress;
296    }
297
298    private void FinishProgressView() {
299      if (progressView != null) {
300        progressView.Finish();
301        progressView = null;
302        SetEnabledStateOfControls();
303      }
304    }
305  }
306}
Note: See TracBrowser for help on using the repository browser.