Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/HiveExperimentView.cs @ 5852

Last change on this file since 5852 was 5852, checked in by cneumuel, 14 years ago

#1233

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