source: trunk/sources/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs @ 7068

Last change on this file since 7068 was 7068, checked in by ascheibe, 9 years ago

#1672

  • added a default job name
  • added check that a job name is set before upload
File size: 20.3 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.Clients.Hive.Views;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.MainForm;
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        logView.Content = null;
116        runCollectionViewHost.Content = null;
117      } else {
118        nameTextBox.Text = Content.Job.Name;
119        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
120        resourceNamesTextBox.Text = Content.Job.ResourceNames;
121        isPrivilegedCheckBox.Checked = Content.Job.IsPrivileged;
122        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
123        logView.Content = Content.Log;
124        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
125      }
126      hiveExperimentPermissionListView.Content = null; // has to be filled by refresh button
127      Content_JobStatisticsChanged(this, EventArgs.Empty);
128      Content_HiveExperimentChanged(this, EventArgs.Empty);
129      Content_HiveTasksChanged(this, EventArgs.Empty);
130      Content_IsProgressingChanged(this, EventArgs.Empty);
131      Content_StateLogListChanged(this, EventArgs.Empty);
132      HiveExperiment_PropertyChanged(this, new PropertyChangedEventArgs("Id"));
133      SetEnabledStateOfControls();
134    }
135
136    protected override void SetEnabledStateOfControls() {
137      base.SetEnabledStateOfControls();
138      executionTimeTextBox.Enabled = Content != null;
139      jobsTextBox.ReadOnly = true;
140      calculatingTextBox.ReadOnly = true;
141      finishedTextBox.ReadOnly = true;
142
143      if (Content != null) {
144        bool alreadyUploaded = Content.Id != Guid.Empty;
145        bool jobsLoaded = Content.HiveTasks != null && Content.HiveTasks.All(x => x.Task.Id != Guid.Empty);
146
147        this.nameTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
148        this.resourceNamesTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
149        this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
150
151        this.isPrivilegedCheckBox.Enabled = Content.IsAllowedPrivileged && Content.IsControllable && !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded); // TODO: check if user has the rights to do this       
152        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
153        this.refreshButton.Enabled = Content.IsDownloadable && alreadyUploaded;
154        this.Locked = !Content.IsControllable || Content.ExecutionState == ExecutionState.Started;
155      }
156      SetEnabledStateOfExecutableButtons();
157      tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
158    }
159
160    protected override void OnClosed(FormClosedEventArgs e) {
161      if (Content != null) {
162        if (Content.RefreshAutomatically)
163          Content.StopResultPolling();
164      }
165      base.OnClosed(e);
166    }
167
168    #region Content Events
169    private void HiveTasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
170      if (InvokeRequired)
171        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded), sender, e);
172      else {
173        foreach (HiveTask t in e.Items) {
174          t.TaskStateChanged += new EventHandler(HiveTask_TaskStateChanged);
175        }
176        SetEnabledStateOfControls();
177      }
178    }
179
180    private void HiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
181      if (InvokeRequired)
182        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved), sender, e);
183      else {
184        foreach (HiveTask t in e.Items) {
185          t.TaskStateChanged -= new EventHandler(HiveTask_TaskStateChanged);
186        }
187        SetEnabledStateOfControls();
188      }
189    }
190
191    private void HiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
192      if (InvokeRequired)
193        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset), sender, e);
194      else {
195        foreach (HiveTask t in e.Items) {
196          t.TaskStateChanged -= new EventHandler(HiveTask_TaskStateChanged);
197        }
198        SetEnabledStateOfControls();
199      }
200    }
201
202    void HiveTask_TaskStateChanged(object sender, EventArgs e) {
203      runCollectionViewHost.Content = GetAllRunsFromJob(Content);
204    }
205
206    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
207      if (InvokeRequired)
208        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
209      else
210        SetEnabledStateOfControls();
211    }
212    private void Content_Prepared(object sender, EventArgs e) {
213      if (InvokeRequired)
214        Invoke(new EventHandler(Content_Prepared), sender, e);
215      else {
216        nameTextBox.Enabled = true;
217        Locked = false;
218        SetEnabledStateOfControls();
219      }
220    }
221    private void Content_Started(object sender, EventArgs e) {
222      if (InvokeRequired)
223        Invoke(new EventHandler(Content_Started), sender, e);
224      else {
225        nameTextBox.Enabled = false;
226        SetEnabledStateOfControls();
227      }
228    }
229    private void Content_Paused(object sender, EventArgs e) {
230      if (InvokeRequired)
231        Invoke(new EventHandler(Content_Paused), sender, e);
232      else {
233        nameTextBox.Enabled = true;
234        SetEnabledStateOfControls();
235      }
236    }
237    private void Content_Stopped(object sender, EventArgs e) {
238      if (InvokeRequired)
239        Invoke(new EventHandler(Content_Stopped), sender, e);
240      else {
241        nameTextBox.Enabled = true;
242        Locked = false;
243        SetEnabledStateOfControls();
244      }
245    }
246    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
247      if (InvokeRequired)
248        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
249      else
250        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
251    }
252    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
253      if (InvokeRequired)
254        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
255      else {
256        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
257        SetEnabledStateOfControls();
258      }
259    }
260    private void Content_HiveTasksChanged(object sender, EventArgs e) {
261      if (InvokeRequired)
262        Invoke(new EventHandler(Content_HiveTasksChanged), sender, e);
263      else {
264        if (Content != null && Content.HiveTasks != null) {
265          jobsTreeView.Content = Content.HiveTasks;
266          RegisterHiveJobEvents();
267        } else {
268          jobsTreeView.Content = null;
269        }
270        SetEnabledStateOfControls();
271      }
272    }
273
274    void Content_Loaded(object sender, EventArgs e) {
275      runCollectionViewHost.Content = GetAllRunsFromJob(Content);
276    }
277
278    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
279      if (Content != null && Content.Job != null) {
280        RegisterHiveExperimentEvents();
281        Content_IsProgressingChanged(sender, e);
282      }
283    }
284    private void Content_IsControllableChanged(object sender, EventArgs e) {
285      SetEnabledStateOfControls();
286    }
287    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
288      if (InvokeRequired)
289        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
290      else {
291        if (Content != null) {
292          jobsTextBox.Text = (Content.Job.JobCount - Content.Job.CalculatingCount - Content.Job.FinishedCount).ToString();
293          calculatingTextBox.Text = Content.Job.CalculatingCount.ToString();
294          finishedTextBox.Text = Content.Job.FinishedCount.ToString();
295        } else {
296          jobsTextBox.Text = "0";
297          calculatingTextBox.Text = "0";
298          finishedTextBox.Text = "0";
299        }
300      }
301    }
302    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
303      // don't show exception, it is logged anyway     
304    }
305    private void Content_StateLogListChanged(object sender, EventArgs e) {
306      if (InvokeRequired)
307        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
308      else {
309        UpdateStateLogList();
310      }
311    }
312    private void Content_IsAllowedPrivilegedChanged(object sender, EventArgs e) {
313      if (InvokeRequired)
314        Invoke(new EventHandler(Content_IsAllowedPrivilegedChanged), sender, e);
315      else {
316        SetEnabledStateOfControls();
317      }
318    }
319
320
321    private void UpdateStateLogList() {
322      if (Content != null && this.Content.Job != null) {
323        stateLogViewHost.Content = this.Content.StateLogList;
324      } else {
325        stateLogViewHost.Content = null;
326      }
327    }
328
329    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
330      if (this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.Job.Id;
331    }
332    #endregion
333
334    #region Control events
335    private void startButton_Click(object sender, EventArgs e) {
336      if (nameTextBox.Text.Trim() == string.Empty) {
337        MessageBox.Show("Please enter a name for the job before uploading it!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
338      } else {
339        HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
340      }
341    }
342    private void pauseButton_Click(object sender, EventArgs e) {
343      HiveClient.PauseJob(Content);
344    }
345    private void stopButton_Click(object sender, EventArgs e) {
346      HiveClient.StopJob(Content);
347    }
348    private void resetButton_Click(object sender, EventArgs e) { }
349
350    private void nameTextBox_Validated(object sender, EventArgs e) {
351      if (Content.Job.Name != nameTextBox.Text)
352        Content.Job.Name = nameTextBox.Text;
353    }
354
355    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
356      if (Content.Job.ResourceNames != resourceNamesTextBox.Text)
357        Content.Job.ResourceNames = resourceNamesTextBox.Text;
358    }
359
360    private void refreshAutomaticallyCheckBox_Validated(object sender, EventArgs e) {
361      if (Content != null) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
362    }
363
364    private void isPrivilegedCheckBox_Validated(object sender, EventArgs e) {
365      if (Content != null) Content.Job.IsPrivileged = isPrivilegedCheckBox.Checked;
366    }
367
368    private void refreshButton_Click(object sender, EventArgs e) {
369      var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
370      invoker.BeginInvoke(Content, (ar) => {
371        try {
372          invoker.EndInvoke(ar);
373        }
374        catch (Exception ex) {
375          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
376        }
377      }, null);
378    }
379
380    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
381      if (this.Content.Job.Id == Guid.Empty) {
382        MessageBox.Show("You have to upload the Job first before you can share it.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
383      } else {
384        hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
385      }
386    }
387
388
389    #endregion
390
391    #region Helpers
392    private void SetEnabledStateOfExecutableButtons() {
393      if (Content == null) {
394        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
395      } else {
396        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0 && Content.ExecutionState == ExecutionState.Prepared;
397        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
398        stopButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
399        resetButton.Enabled = false;
400      }
401    }
402    #endregion
403
404    #region Progress reporting
405    private void Content_IsProgressingChanged(object sender, EventArgs e) {
406      if (this.InvokeRequired) {
407        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
408      } else {
409        if (Content != null && Content.IsProgressing) {
410          SetProgressView();
411        } else {
412          FinishProgressView();
413        }
414      }
415    }
416
417    private void SetProgressView() {
418      if (progressView == null) {
419        progressView = new ProgressView(this, Content.Progress);
420      }
421      progressView.Progress = Content.Progress;
422    }
423
424    private void FinishProgressView() {
425      if (progressView != null) {
426        progressView.Finish();
427        progressView = null;
428        SetEnabledStateOfControls();
429      }
430    }
431    #endregion
432
433    #region Drag & Drop
434    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
435      jobsTreeView_DragEnter(sender, e);
436    }
437
438    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
439      e.Effect = DragDropEffects.None;
440      var obj = e.Data.GetData(Constants.DragDropDataFormat);
441      if (obj is IOptimizer) {
442        if (Content.Id != Guid.Empty) e.Effect = DragDropEffects.None;
443        else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
444        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
445      }
446    }
447
448    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
449      if (e.Effect != DragDropEffects.None) {
450        var obj = e.Data.GetData(Constants.DragDropDataFormat);
451
452        var optimizer = obj as IOptimizer;
453        if (optimizer != null) {
454          IOptimizer newOptimizer = null;
455          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
456            newOptimizer = (IOptimizer)optimizer.Clone();
457          } else {
458            newOptimizer = optimizer;
459          }
460          if (newOptimizer.ExecutionState != ExecutionState.Prepared) {
461            newOptimizer.Prepare();
462          }
463
464          Content.HiveTasks.Add(new OptimizerHiveTask(newOptimizer));
465        }
466      }
467    }
468    #endregion
469
470    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
471      if (tabControl.SelectedTab == permissionTabPage) {
472        if (!Content.IsSharable) {
473          MessageBox.Show("Unable to load tab. You have insufficient access privileges.");
474          tabControl.SelectedTab = tasksTabPage;
475        }
476      }
477    }
478
479    private RunCollection GetAllRunsFromJob(RefreshableJob job) {
480      if (job != null) {
481        RunCollection runs = new RunCollection();
482
483        foreach (HiveTask subTask in job.HiveTasks) {
484          if (subTask is OptimizerHiveTask) {
485            OptimizerHiveTask ohTask = subTask as OptimizerHiveTask;
486            runs.AddRange(ohTask.ItemTask.Item.Runs);
487          }
488        }
489        return runs;
490      } else {
491        return null;
492      }
493    }
494  }
495}
Note: See TracBrowser for help on using the repository browser.