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 @ 6004

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

#1233

  • seperated ExperimentMangerClient (OKB-Style, contains business logic) and HiveExperiment (mainly only contains information)
  • fixed redundant cloning methods in dtos
  • added simple statistics in HiveExperiment which the user can see before downloading an experiment
  • added db-delete cascade for slaves and statelogs - now slaves can be safely deleted
File size: 14.3 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.ComponentModel;
24using System.Threading;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Clients.Hive.Views {
33  /// <summary>
34  /// The base class for visual representations of items.
35  /// </summary>
36  [View("Experiment View")]
37  [Content(typeof(HiveExperiment), true)]
38  public sealed partial class HiveExperimentView : ItemView {
39    private ProgressView progressView;
40
41    public new HiveExperiment Content {
42      get { return (HiveExperiment)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="ItemBaseView"/>.
48    /// </summary>
49    public HiveExperimentView() {
50      InitializeComponent();
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.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
62      Content.HiveJobChanged -= new EventHandler(Content_HiveJobChanged);
63      Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
64      Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
65      base.DeregisterContentEvents();
66    }
67
68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      //Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
71      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
72      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
73      //Content.Prepared += new EventHandler(Content_Prepared);
74      //Content.Started += new EventHandler(Content_Started);
75      //Content.Paused += new EventHandler(Content_Paused);
76      //Content.Stopped += new EventHandler(Content_Stopped);
77      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
78      Content.HiveJobChanged += new EventHandler(Content_HiveJobChanged);
79      Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
80      Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Content_PropertyChanged);
81    }
82
83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85      if (Content == null) {
86        nameTextBox.Text = string.Empty;
87        executionTimeTextBox.Text = string.Empty;
88        resourceNamesTextBox.Text = string.Empty;
89        useLocalPluginsCheckBox.Checked = false;
90        logView.Content = null;
91        //includeJobsCheckBox.Checked = false;
92        refreshAutomaticallyCheckBox.Checked = false;
93        jobsTextBox.Text = "0";
94        calculatingTextBox.Text = "0";
95        finishedTextBox.Text = "0";
96      } else {
97        nameTextBox.Text = Content.Name;
98        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
99        resourceNamesTextBox.Text = Content.ResourceNames;
100        useLocalPluginsCheckBox.Checked = Content.UseLocalPlugins;
101        //includeJobsCheckBox.Checked = Content.IncludeJobs;
102        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
103        jobsTextBox.Text = Content.JobCount.ToString();
104        calculatingTextBox.Text = Content.CalculatingCount.ToString();
105        finishedTextBox.Text = Content.FinishedCount.ToString();
106      }
107      Content_HiveJobChanged(this, EventArgs.Empty);
108      Content_IsProgressingChanged(this, EventArgs.Empty);
109      SetEnabledStateOfControls();
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      executionTimeTextBox.Enabled = Content != null;
115      experimentNamedItemView.ReadOnly = true;
116      jobsTextBox.ReadOnly = true;
117      calculatingTextBox.ReadOnly = true;
118      finishedTextBox.ReadOnly = true;
119
120      if (Content != null) {
121        bool alreadyUploaded = Content.RootJobId != null && Content.Id != Guid.Empty;
122        bool jobsLoaded = Content.HiveJob != null && Content.HiveJob.Job.Id != Guid.Empty;
123
124        this.nameTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
125        this.resourceNamesTextBox.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
126        this.jobsTreeView.ReadOnly = Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
127        this.useLocalPluginsCheckBox.Enabled = !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded);
128        this.refreshAutomaticallyCheckBox.Enabled = alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
129        this.viewExperimentButton.Enabled = Content.GetExperiment() != null;
130        this.openExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared;
131        this.newExperimentButton.Enabled = !alreadyUploaded && Content.ExecutionState == ExecutionState.Prepared;
132        this.refreshButton.Enabled = alreadyUploaded;
133
134        this.Locked = Content.ExecutionState == ExecutionState.Started;
135      }
136      SetEnabledStateOfExecutableButtons();
137    }
138
139    protected override void OnClosed(FormClosedEventArgs e) {
140      if (Content != null) {
141        if (Content.RefreshAutomatically)
142          Content.StopResultPolling();
143      }
144      base.OnClosed(e);
145    }
146
147    #region Content Events
148    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
149      if (InvokeRequired)
150        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
151      else
152        SetEnabledStateOfControls();
153    }
154    private void Content_Prepared(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_Prepared), sender, e);
157      else {
158        nameTextBox.Enabled = true;
159        Locked = false;
160        SetEnabledStateOfControls();
161      }
162    }
163    private void Content_Started(object sender, EventArgs e) {
164      if (InvokeRequired)
165        Invoke(new EventHandler(Content_Started), sender, e);
166      else {
167        nameTextBox.Enabled = false;
168        SetEnabledStateOfControls();
169      }
170    }
171    private void Content_Paused(object sender, EventArgs e) {
172      if (InvokeRequired)
173        Invoke(new EventHandler(Content_Paused), sender, e);
174      else {
175        nameTextBox.Enabled = true;
176        SetEnabledStateOfControls();
177      }
178    }
179    private void Content_Stopped(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_Stopped), sender, e);
182      else {
183        nameTextBox.Enabled = true;
184        Locked = false;
185        SetEnabledStateOfControls();
186      }
187    }
188    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
189      if (InvokeRequired)
190        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
191      else
192        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
193    }
194    private void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
195      if (InvokeRequired)
196        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
197      else
198        ErrorHandling.ShowErrorDialog(this, e.Value);
199    }
200    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
201      if (InvokeRequired)
202        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
203      else {
204        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
205        SetEnabledStateOfControls();
206      }
207    }
208    private void Content_HiveJobChanged(object sender, EventArgs e) {
209      if (InvokeRequired)
210        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
211      else {
212        if (Content != null) {
213          jobsTreeView.Content = Content.HiveJob;
214          experimentNamedItemView.Content = Content.GetExperiment();
215        } else {
216          jobsTreeView.Content = null;
217          experimentNamedItemView.Content = null;
218        }
219        SetEnabledStateOfControls();
220      }
221    }
222    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
223      if (InvokeRequired)
224        Invoke(new PropertyChangedEventHandler(Content_PropertyChanged), sender, e);
225      else {
226        jobsTextBox.Text = Content.JobCount.ToString();
227        calculatingTextBox.Text = Content.CalculatingCount.ToString();
228        finishedTextBox.Text = Content.FinishedCount.ToString();
229      }
230    }
231    #endregion
232
233    #region Control events
234    private void startButton_Click(object sender, EventArgs e) {
235      ExperimentManagerClient.StartExperiment(new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex)), Content);
236    }
237    private void pauseButton_Click(object sender, EventArgs e) {
238      ExperimentManagerClient.PauseExperiment(Content);
239    }
240    private void stopButton_Click(object sender, EventArgs e) {
241      ExperimentManagerClient.StopExperiment(Content);
242    }
243    private void resetButton_Click(object sender, EventArgs e) {  }
244
245    private void nameTextBox_Validated(object sender, EventArgs e) {
246      if (Content.Name != nameTextBox.Text)
247        Content.Name = nameTextBox.Text;
248    }
249
250    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
251      if (Content.ResourceNames != resourceNamesTextBox.Text)
252        Content.ResourceNames = resourceNamesTextBox.Text;
253    }
254
255    private void newExperimentButton_Click(object sender, EventArgs e) {
256      Content.SetExperiment(new HeuristicLab.Optimization.Experiment());
257    }
258
259    private void openExperimentButton_Click(object sender, EventArgs e) {
260      OpenFileDialog openFileDialog = new OpenFileDialog();
261      openFileDialog.Title = "Open Experimenter";
262      openFileDialog.FileName = "Item";
263      openFileDialog.Multiselect = false;
264      openFileDialog.DefaultExt = "hl";
265      openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
266
267      if (openFileDialog.ShowDialog() == DialogResult.OK) {
268        Content.SetExperiment((HeuristicLab.Optimization.Experiment)ContentManager.Load(openFileDialog.FileName));
269      }
270    }
271
272    private void showExperimentButton_Click(object sender, EventArgs e) {
273      MainFormManager.MainForm.ShowContent(Content.GetExperiment());
274    }
275
276    private void viewExperimentButton_Click(object sender, EventArgs e) {
277      MainFormManager.MainForm.ShowContent(Content.GetExperiment());
278    }
279
280    private void includeJobsCheckBox_CheckedChanged(object sender, EventArgs e) {
281      //if (Content != null) Content.IncludeJobs = includeJobsCheckBox.Checked;
282    }
283
284    private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
285      if (Content != null) {
286        Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
287      }
288    }
289
290    private void useLocalPluginsCheckBox_CheckedChanged(object sender, EventArgs e) {
291      if (Content != null) Content.UseLocalPlugins = useLocalPluginsCheckBox.Checked;
292    }
293
294    private void refreshButton_Click(object sender, EventArgs e) {
295      var invoker = new Action<HiveExperiment>(ExperimentManagerClient.LoadExperiment);
296      invoker.BeginInvoke(Content, (ar) => {
297        try {
298          invoker.EndInvoke(ar);
299        }
300        catch (Exception ex) {
301          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
302        }
303      }, null);
304    }
305    #endregion
306
307    #region Helpers
308    private void SetEnabledStateOfExecutableButtons() {
309      if (Content == null) {
310        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
311      } else {
312        startButton.Enabled = Content.GetExperiment() != null && Content.ExecutionState == ExecutionState.Prepared;
313        pauseButton.Enabled = Content.ExecutionState == ExecutionState.Started;
314        stopButton.Enabled = Content.ExecutionState == ExecutionState.Started;
315        resetButton.Enabled = false;
316      }
317    }
318    #endregion
319
320    #region Progress reporting
321    private void Content_IsProgressingChanged(object sender, EventArgs e) {
322      if (this.InvokeRequired) {
323        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
324      } else {
325        if (Content != null && Content.IsProgressing) {
326          SetProgressView();
327        } else {
328          FinishProgressView();
329        }
330      }
331    }
332
333    private void SetProgressView() {
334      if (progressView == null) {
335        progressView = new ProgressView(this, Content.Progress);
336      }
337      progressView.Progress = Content.Progress;
338    }
339
340    private void FinishProgressView() {
341      if (progressView != null) {
342        progressView.Finish();
343        progressView = null;
344        SetEnabledStateOfControls();
345      }
346    }
347    #endregion
348 
349  }
350}
Note: See TracBrowser for help on using the repository browser.