Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs @ 7615

Last change on this file since 7615 was 7615, checked in by gkronber, 13 years ago

#1081 merged r7462:7609 from trunk into time series branch

File size: 20.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
25using System.Threading;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32using HeuristicLab.Optimization;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.Hive.JobManager.Views {
36  /// <summary>
37  /// The base class for visual representations of items.
38  /// </summary>
39  [View("Hive Job View")]
40  [Content(typeof(RefreshableJob), true)]
41  public partial class RefreshableHiveJobView : HeuristicLab.Core.Views.ItemView {
42    private ProgressView progressView;
43
44    public new RefreshableJob Content {
45      get { return (RefreshableJob)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="ItemBaseView"/>.
51    /// </summary>
52    public RefreshableHiveJobView() {
53      InitializeComponent();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
59      Content.JobChanged += new EventHandler(Content_HiveExperimentChanged);
60      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
61      Content.JobStatisticsChanged += new EventHandler(Content_JobStatisticsChanged);
62      Content.ExceptionOccured += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
63      Content.StateLogListChanged += new EventHandler(Content_StateLogListChanged);
64      Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
65      Content.HiveTasksChanged += new EventHandler(Content_HiveTasksChanged);
66      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
67      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
68      Content.IsAllowedPrivilegedChanged += new EventHandler(Content_IsAllowedPrivilegedChanged);
69      Content.Loaded += new EventHandler(Content_Loaded);
70    }
71
72    protected override void DeregisterContentEvents() {
73      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
74      Content.JobChanged -= new EventHandler(Content_HiveExperimentChanged);
75      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
76      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
77      Content.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
78      Content.StateLogListChanged -= new EventHandler(Content_StateLogListChanged);
79      Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
80      Content.HiveTasksChanged -= new EventHandler(Content_HiveTasksChanged);
81      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
82      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
83      Content.Loaded -= new EventHandler(Content_Loaded);
84      base.DeregisterContentEvents();
85    }
86
87    private void RegisterHiveExperimentEvents() {
88      Content.Job.PropertyChanged += new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
89    }
90
91    private void DeregisterHiveExperimentEvents() {
92      Content.Job.PropertyChanged -= new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
93    }
94
95    private void RegisterHiveJobEvents() {
96      Content.HiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded);
97      Content.HiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved);
98      Content.HiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset);
99    }
100    private void DeregisterHiveJobEvents() {
101      Content.HiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded);
102      Content.HiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved);
103      Content.HiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset);
104    }
105
106    protected override void OnContentChanged() {
107      base.OnContentChanged();
108      if (Content == null) {
109        nameTextBox.Text = string.Empty;
110        executionTimeTextBox.Text = string.Empty;
111        resourceNamesTextBox.Text = string.Empty;
112        isPrivilegedCheckBox.Checked = false;
113        logView.Content = null;
114        refreshAutomaticallyCheckBox.Checked = false;
115        runCollectionViewHost.Content = null;
116      } else {
117        nameTextBox.Text = Content.Job.Name;
118        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
119        resourceNamesTextBox.Text = Content.Job.ResourceNames;
120        isPrivilegedCheckBox.Checked = Content.Job.IsPrivileged;
121        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
122        logView.Content = Content.Log;
123        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
124      }
125      hiveExperimentPermissionListView.Content = null; // has to be filled by refresh button
126      Content_JobStatisticsChanged(this, EventArgs.Empty);
127      Content_HiveExperimentChanged(this, EventArgs.Empty);
128      Content_HiveTasksChanged(this, EventArgs.Empty);
129      Content_IsProgressingChanged(this, EventArgs.Empty);
130      Content_StateLogListChanged(this, EventArgs.Empty);
131      HiveExperiment_PropertyChanged(this, new PropertyChangedEventArgs("Id"));
132      SetEnabledStateOfControls();
133    }
134
135    protected override void SetEnabledStateOfControls() {
136      base.SetEnabledStateOfControls();
137      executionTimeTextBox.Enabled = Content != null;
138      jobsTextBox.ReadOnly = true;
139      calculatingTextBox.ReadOnly = true;
140      finishedTextBox.ReadOnly = true;
141
142      if (Content != null) {
143        bool alreadyUploaded = Content.Id != Guid.Empty;
144        bool jobsLoaded = Content.HiveTasks != null && Content.HiveTasks.All(x => x.Task.Id != Guid.Empty);
145
146        this.nameTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
147        this.resourceNamesTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
148        this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
149
150        this.isPrivilegedCheckBox.Enabled = Content.IsAllowedPrivileged && Content.IsControllable && !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded); // TODO: check if user has the rights to do this       
151        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
152        this.refreshButton.Enabled = Content.IsDownloadable && alreadyUploaded;
153        this.Locked = !Content.IsControllable || Content.ExecutionState == ExecutionState.Started;
154      }
155      SetEnabledStateOfExecutableButtons();
156      tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
157    }
158
159    protected override void OnClosed(FormClosedEventArgs e) {
160      if (Content != null) {
161        if (Content.RefreshAutomatically)
162          Content.StopResultPolling();
163      }
164      base.OnClosed(e);
165    }
166
167    #region Content Events
168    private void HiveTasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
169      if (InvokeRequired)
170        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded), sender, e);
171      else {
172        foreach (HiveTask t in e.Items) {
173          t.TaskStateChanged += new EventHandler(HiveTask_TaskStateChanged);
174        }
175        SetEnabledStateOfControls();
176      }
177    }
178
179    private void HiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
180      if (InvokeRequired)
181        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved), sender, e);
182      else {
183        foreach (HiveTask t in e.Items) {
184          t.TaskStateChanged -= new EventHandler(HiveTask_TaskStateChanged);
185        }
186        SetEnabledStateOfControls();
187      }
188    }
189
190    private void HiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
191      if (InvokeRequired)
192        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset), sender, e);
193      else {
194        foreach (HiveTask t in e.Items) {
195          t.TaskStateChanged -= new EventHandler(HiveTask_TaskStateChanged);
196        }
197        SetEnabledStateOfControls();
198      }
199    }
200
201    void HiveTask_TaskStateChanged(object sender, EventArgs e) {
202      runCollectionViewHost.Content = GetAllRunsFromJob(Content);
203    }
204
205    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
206      if (InvokeRequired)
207        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
208      else
209        SetEnabledStateOfControls();
210    }
211    private void Content_Prepared(object sender, EventArgs e) {
212      if (InvokeRequired)
213        Invoke(new EventHandler(Content_Prepared), sender, e);
214      else {
215        nameTextBox.Enabled = true;
216        Locked = false;
217        SetEnabledStateOfControls();
218      }
219    }
220    private void Content_Started(object sender, EventArgs e) {
221      if (InvokeRequired)
222        Invoke(new EventHandler(Content_Started), sender, e);
223      else {
224        nameTextBox.Enabled = false;
225        SetEnabledStateOfControls();
226      }
227    }
228    private void Content_Paused(object sender, EventArgs e) {
229      if (InvokeRequired)
230        Invoke(new EventHandler(Content_Paused), sender, e);
231      else {
232        nameTextBox.Enabled = true;
233        SetEnabledStateOfControls();
234      }
235    }
236    private void Content_Stopped(object sender, EventArgs e) {
237      if (InvokeRequired)
238        Invoke(new EventHandler(Content_Stopped), sender, e);
239      else {
240        nameTextBox.Enabled = true;
241        Locked = false;
242        SetEnabledStateOfControls();
243      }
244    }
245    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
246      if (InvokeRequired)
247        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
248      else
249        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
250    }
251    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
252      if (InvokeRequired)
253        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
254      else {
255        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
256        SetEnabledStateOfControls();
257      }
258    }
259    private void Content_HiveTasksChanged(object sender, EventArgs e) {
260      if (InvokeRequired)
261        Invoke(new EventHandler(Content_HiveTasksChanged), sender, e);
262      else {
263        if (Content != null && Content.HiveTasks != null) {
264          jobsTreeView.Content = Content.HiveTasks;
265          RegisterHiveJobEvents();
266        } else {
267          jobsTreeView.Content = null;
268        }
269        SetEnabledStateOfControls();
270      }
271    }
272
273    void Content_Loaded(object sender, EventArgs e) {
274      runCollectionViewHost.Content = GetAllRunsFromJob(Content);
275    }
276
277    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
278      if (Content != null && Content.Job != null) {
279        RegisterHiveExperimentEvents();
280        Content_IsProgressingChanged(sender, e);
281      }
282    }
283    private void Content_IsControllableChanged(object sender, EventArgs e) {
284      SetEnabledStateOfControls();
285    }
286    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
287      if (InvokeRequired)
288        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
289      else {
290        if (Content != null) {
291          jobsTextBox.Text = (Content.Job.JobCount - Content.Job.CalculatingCount - Content.Job.FinishedCount).ToString();
292          calculatingTextBox.Text = Content.Job.CalculatingCount.ToString();
293          finishedTextBox.Text = Content.Job.FinishedCount.ToString();
294        } else {
295          jobsTextBox.Text = "0";
296          calculatingTextBox.Text = "0";
297          finishedTextBox.Text = "0";
298        }
299      }
300    }
301    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
302      if (InvokeRequired)
303        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured), sender, e);
304      else {
305        //don't show the error dialog when downloading tasks, the HiveClient will throw an exception and the dialog will be shown then
306        if (sender.GetType() != typeof(ConcurrentTaskDownloader<ItemTask>) && sender.GetType() != typeof(TaskDownloader)) {
307          ErrorHandling.ShowErrorDialog(this, e.Value);
308        }
309      }
310    }
311    private void Content_StateLogListChanged(object sender, EventArgs e) {
312      if (InvokeRequired)
313        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
314      else {
315        UpdateStateLogList();
316      }
317    }
318    private void Content_IsAllowedPrivilegedChanged(object sender, EventArgs e) {
319      if (InvokeRequired)
320        Invoke(new EventHandler(Content_IsAllowedPrivilegedChanged), sender, e);
321      else {
322        SetEnabledStateOfControls();
323      }
324    }
325
326
327    private void UpdateStateLogList() {
328      if (Content != null && this.Content.Job != null) {
329        stateLogViewHost.Content = this.Content.StateLogList;
330      } else {
331        stateLogViewHost.Content = null;
332      }
333    }
334
335    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
336      if (this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.Job.Id;
337    }
338    #endregion
339
340    #region Control events
341    private void startButton_Click(object sender, EventArgs e) {
342      if (nameTextBox.Text.Trim() == string.Empty) {
343        MessageBox.Show("Please enter a name for the job before uploading it!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
344      } else {
345        HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
346      }
347    }
348    private void pauseButton_Click(object sender, EventArgs e) {
349      HiveClient.PauseJob(Content);
350    }
351    private void stopButton_Click(object sender, EventArgs e) {
352      HiveClient.StopJob(Content);
353    }
354    private void resetButton_Click(object sender, EventArgs e) { }
355
356    private void nameTextBox_Validated(object sender, EventArgs e) {
357      if (Content.Job.Name != nameTextBox.Text)
358        Content.Job.Name = nameTextBox.Text;
359    }
360
361    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
362      if (Content.Job.ResourceNames != resourceNamesTextBox.Text)
363        Content.Job.ResourceNames = resourceNamesTextBox.Text;
364    }
365
366    private void refreshAutomaticallyCheckBox_Validated(object sender, EventArgs e) {
367      if (Content != null) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
368    }
369
370    private void isPrivilegedCheckBox_Validated(object sender, EventArgs e) {
371      if (Content != null) Content.Job.IsPrivileged = isPrivilegedCheckBox.Checked;
372    }
373
374    private void refreshButton_Click(object sender, EventArgs e) {
375      var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
376      invoker.BeginInvoke(Content, (ar) => {
377        try {
378          invoker.EndInvoke(ar);
379        }
380        catch (Exception ex) {
381          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
382        }
383      }, null);
384    }
385
386    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
387      if (this.Content.Job.Id == Guid.Empty) {
388        MessageBox.Show("You have to upload the Job first before you can share it.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
389      } else {
390        hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
391      }
392    }
393
394
395    #endregion
396
397    #region Helpers
398    private void SetEnabledStateOfExecutableButtons() {
399      if (Content == null) {
400        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
401      } else {
402        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0 && Content.ExecutionState == ExecutionState.Prepared;
403        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
404        stopButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
405        resetButton.Enabled = false;
406      }
407    }
408    #endregion
409
410    #region Progress reporting
411    private void Content_IsProgressingChanged(object sender, EventArgs e) {
412      if (this.InvokeRequired) {
413        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
414      } else {
415        if (Content != null && Content.IsProgressing) {
416          SetProgressView();
417        } else {
418          FinishProgressView();
419        }
420      }
421    }
422
423    private void SetProgressView() {
424      if (progressView == null) {
425        progressView = new ProgressView(this, Content.Progress);
426      }
427      progressView.Progress = Content.Progress;
428    }
429
430    private void FinishProgressView() {
431      if (progressView != null) {
432        progressView.Finish();
433        progressView = null;
434        SetEnabledStateOfControls();
435      }
436    }
437    #endregion
438
439    #region Drag & Drop
440    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
441      jobsTreeView_DragEnter(sender, e);
442    }
443
444    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
445      e.Effect = DragDropEffects.None;
446      var obj = e.Data.GetData(Constants.DragDropDataFormat);
447      if (obj is IOptimizer) {
448        if (Content.Id != Guid.Empty) e.Effect = DragDropEffects.None;
449        else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
450        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
451      }
452    }
453
454    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
455      if (e.Effect != DragDropEffects.None) {
456        var obj = e.Data.GetData(Constants.DragDropDataFormat);
457
458        var optimizer = obj as IOptimizer;
459        if (optimizer != null) {
460          IOptimizer newOptimizer = null;
461          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
462            newOptimizer = (IOptimizer)optimizer.Clone();
463          } else {
464            newOptimizer = optimizer;
465          }
466          if (newOptimizer.ExecutionState != ExecutionState.Prepared) {
467            newOptimizer.Prepare();
468          }
469
470          Content.HiveTasks.Add(new OptimizerHiveTask(newOptimizer));
471        }
472      }
473    }
474    #endregion
475
476    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
477      if (tabControl.SelectedTab == permissionTabPage) {
478        if (!Content.IsSharable) {
479          MessageBox.Show("Unable to load tab. You have insufficient access privileges.");
480          tabControl.SelectedTab = tasksTabPage;
481        }
482      }
483    }
484
485    private RunCollection GetAllRunsFromJob(RefreshableJob job) {
486      if (job != null) {
487        RunCollection runs = new RunCollection();
488
489        foreach (HiveTask subTask in job.HiveTasks) {
490          if (subTask is OptimizerHiveTask) {
491            OptimizerHiveTask ohTask = subTask as OptimizerHiveTask;
492            runs.AddRange(ohTask.ItemTask.Item.Runs);
493          }
494        }
495        return runs;
496      } else {
497        return null;
498      }
499    }
500  }
501}
Note: See TracBrowser for help on using the repository browser.