Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs @ 15913

Last change on this file since 15913 was 15913, checked in by jzenisek, 6 years ago

#2839: updated genealogy computation for hive job administrator

File size: 26.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Threading.Tasks;
27using System.Windows.Forms;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.Optimization;
34using HeuristicLab.PluginInfrastructure;
35using System.Collections.Generic;
36
37namespace HeuristicLab.Clients.Hive.JobManager.Views {
38  /// <summary>
39  /// The base class for visual representations of items.
40  /// </summary>
41  [View("Hive Job View")]
42  [Content(typeof(RefreshableJob), true)]
43  public partial class RefreshableHiveJobView : HeuristicLab.Core.Views.ItemView {
44    private HiveResourceSelectorDialog hiveResourceSelectorDialog;
45    private bool SuppressEvents { get; set; }
46    private object runCollectionViewLocker = new object();
47    private Project selectedProject;
48    private Dictionary<Guid, Guid> originalJobProjectAssignment = new Dictionary<Guid, Guid>();
49
50    public new RefreshableJob Content {
51      get { return (RefreshableJob)base.Content; }
52      set {
53        base.Content = value;
54      }
55    }
56
57    /// <summary>
58    /// Initializes a new instance of <see cref="ItemBaseView"/>.
59    /// </summary>
60    public RefreshableHiveJobView() {
61      InitializeComponent();
62    }
63
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
67      Content.JobChanged += new EventHandler(Content_HiveExperimentChanged);
68      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
69      Content.JobStatisticsChanged += new EventHandler(Content_JobStatisticsChanged);
70      Content.ExceptionOccured += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
71      Content.StateLogListChanged += new EventHandler(Content_StateLogListChanged);
72      Content.HiveTasksChanged += new EventHandler(Content_HiveTasksChanged);
73      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
74      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
75      Content.Loaded += new EventHandler(Content_Loaded);
76      Content.TaskReceived += new EventHandler(Content_TaskReceived);
77      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, Content.Progress);
78    }
79
80    protected override void DeregisterContentEvents() {
81      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
82      Content.JobChanged -= new EventHandler(Content_HiveExperimentChanged);
83      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
84      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
85      Content.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
86      Content.StateLogListChanged -= new EventHandler(Content_StateLogListChanged);
87      Content.HiveTasksChanged -= new EventHandler(Content_HiveTasksChanged);
88      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
89      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
90      Content.Loaded -= new EventHandler(Content_Loaded);
91      Content.TaskReceived -= new EventHandler(Content_TaskReceived);
92      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this, false);
93      DeregisterHiveExperimentEvents();
94      DeregisterHiveTasksEvents();
95      base.DeregisterContentEvents();
96    }
97
98    private void RegisterHiveExperimentEvents() {
99      Content.Job.PropertyChanged += new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
100    }
101
102    private void DeregisterHiveExperimentEvents() {
103      Content.Job.PropertyChanged -= new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
104    }
105
106    private void RegisterHiveTasksEvents() {
107      Content.HiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded);
108      Content.HiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved);
109      Content.HiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset);
110    }
111    private void DeregisterHiveTasksEvents() {
112      Content.HiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded);
113      Content.HiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved);
114      Content.HiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset);
115    }
116
117    protected override void OnContentChanged() {
118      base.OnContentChanged();
119      SuppressEvents = true;
120      try {
121        if (Content == null) {
122          nameTextBox.Text = string.Empty;
123          executionTimeTextBox.Text = string.Empty;
124          projectNameTextBox.Text = string.Empty;
125          refreshAutomaticallyCheckBox.Checked = false;
126          lock (runCollectionViewLocker) {
127            runCollectionViewHost.Content = null;
128          }
129          logView.Content = null;
130          jobsTreeView.Content = null;
131          hiveExperimentPermissionListView.Content = null;
132          stateLogViewHost.Content = null;
133        } else {
134          if(Content.Job != null
135            && Content.Job.Id != Guid.Empty
136            && !originalJobProjectAssignment.ContainsKey(Content.Job.Id)) {
137            originalJobProjectAssignment.Add(Content.Job.Id, Content.Job.ProjectId);
138          }
139
140          nameTextBox.Text = Content.Job.Name;
141          executionTimeTextBox.Text = Content.ExecutionTime.ToString();
142          refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
143
144          // project look up
145          if(Content.Job.ProjectId != null && Content.Job.ProjectId != Guid.Empty) {
146            if(selectedProject == null || selectedProject.Id != Content.Job.ProjectId) {
147              selectedProject = GetProject(Content.Job.ProjectId);
148              if (selectedProject != null) {
149                projectNameTextBox.Text = selectedProject.Name;
150              } else {
151                projectNameTextBox.Text = string.Empty;
152              }
153            }
154          } else {
155            selectedProject = null;
156            projectNameTextBox.Text = string.Empty;
157            Content.Job.ResourceIds = null;
158          }
159         
160          logView.Content = Content.Log;
161          lock (runCollectionViewLocker) {
162            runCollectionViewHost.Content = GetAllRunsFromJob(Content);
163          }
164        }
165      } finally {
166        SuppressEvents = false;
167      }
168      hiveExperimentPermissionListView.Content = null; // has to be filled by refresh button
169      Content_JobStatisticsChanged(this, EventArgs.Empty);
170      Content_HiveExperimentChanged(this, EventArgs.Empty);
171      Content_HiveTasksChanged(this, EventArgs.Empty);
172      Content_StateLogListChanged(this, EventArgs.Empty);
173      HiveExperiment_PropertyChanged(this, new PropertyChangedEventArgs("Id"));
174      SetEnabledStateOfControls();
175    }
176
177    protected override void OnLockedChanged() {
178      base.OnLockedChanged();
179      executionTimeTextBox.Enabled = !Locked;
180      jobsTextBox.Enabled = !Locked;
181      calculatingTextBox.Enabled = !Locked;
182      finishedTextBox.Enabled = !Locked;
183      tabControl.Enabled = !Locked;
184      nameTextBox.Enabled = !Locked;
185      projectNameTextBox.Enabled = !Locked;
186      searchButton.Enabled = !Locked;
187      jobsTreeView.Enabled = !Locked;
188      refreshAutomaticallyCheckBox.Enabled = !Locked;
189      refreshButton.Enabled = !Locked;
190      UnloadButton.Enabled = !Locked;
191      startButton.Enabled = !Locked;
192      pauseButton.Enabled = !Locked;
193      stopButton.Enabled = !Locked;
194    }
195
196    protected override void SetEnabledStateOfControls() {
197      base.SetEnabledStateOfControls();
198      if (!Locked) {
199        executionTimeTextBox.Enabled = Content != null;
200        jobsTextBox.ReadOnly = true;
201        calculatingTextBox.ReadOnly = true;
202        finishedTextBox.ReadOnly = true;
203
204        if (Content != null) {
205          bool alreadyUploaded = Content.Id != Guid.Empty;
206          bool jobsLoaded = Content.HiveTasks != null && Content.HiveTasks.All(x => x.Task.Id != Guid.Empty);
207          tabControl.Enabled = !Content.IsProgressing;
208
209          this.nameTextBox.ReadOnly = Content.IsProgressing;
210          this.searchButton.Enabled = !Content.IsProgressing && Content.ExecutionState != ExecutionState.Stopped;
211          this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded || Content.IsProgressing;
212
213          this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && (Content.ExecutionState == ExecutionState.Started || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
214          this.refreshButton.Enabled = Content.IsDownloadable && alreadyUploaded && !Content.IsProgressing;
215          this.updateButton.Enabled = Content.ExecutionState != ExecutionState.Prepared && Content.ExecutionState != ExecutionState.Stopped && !Content.IsProgressing;
216
217          this.UnloadButton.Enabled = Content.HiveTasks != null && Content.HiveTasks.Count > 0 && alreadyUploaded && !Content.IsProgressing;
218        }
219        SetEnabledStateOfExecutableButtons();
220        tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
221      }
222    }
223
224    protected override void OnClosed(FormClosedEventArgs e) {
225      if (Content != null) {
226        if (Content.RefreshAutomatically)
227          Content.StopResultPolling();
228      }
229      base.OnClosed(e);
230    }
231
232    #region Content Events
233    void Content_TaskReceived(object sender, EventArgs e) {
234      lock (runCollectionViewLocker) {
235        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
236      }
237    }
238
239    private void HiveTasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
240      if (InvokeRequired)
241        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded), sender, e);
242      else {
243        SetEnabledStateOfControls();
244      }
245    }
246
247    private void HiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
248      if (InvokeRequired)
249        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved), sender, e);
250      else {
251        SetEnabledStateOfControls();
252      }
253    }
254
255    private void HiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
256      if (InvokeRequired)
257        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset), sender, e);
258      else {
259        SetEnabledStateOfControls();
260      }
261    }
262
263    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
264      if (InvokeRequired)
265        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
266      else
267        SetEnabledStateOfControls();
268    }
269
270    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
271      if (InvokeRequired)
272        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
273      else
274        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
275    }
276    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
277      if (InvokeRequired)
278        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
279      else {
280        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
281        SetEnabledStateOfControls();
282      }
283    }
284    private void Content_HiveTasksChanged(object sender, EventArgs e) {
285      if (InvokeRequired)
286        Invoke(new EventHandler(Content_HiveTasksChanged), sender, e);
287      else {
288        if (Content != null && Content.HiveTasks != null) {
289          jobsTreeView.Content = Content.HiveTasks;
290          RegisterHiveTasksEvents();
291        } else {
292          jobsTreeView.Content = null;
293        }
294        SetEnabledStateOfControls();
295      }
296    }
297
298    void Content_Loaded(object sender, EventArgs e) {
299      lock (runCollectionViewLocker) {
300        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
301      }
302    }
303
304    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
305      if (Content != null && Content.Job != null) {
306        RegisterHiveExperimentEvents();
307      }
308    }
309    private void Content_IsControllableChanged(object sender, EventArgs e) {
310      SetEnabledStateOfControls();
311    }
312    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
313      if (InvokeRequired)
314        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
315      else {
316        if (Content != null) {
317          jobsTextBox.Text = (Content.Job.JobCount - Content.Job.CalculatingCount - Content.Job.FinishedCount).ToString();
318          calculatingTextBox.Text = Content.Job.CalculatingCount.ToString();
319          finishedTextBox.Text = Content.Job.FinishedCount.ToString();
320        } else {
321          jobsTextBox.Text = "0";
322          calculatingTextBox.Text = "0";
323          finishedTextBox.Text = "0";
324        }
325      }
326    }
327    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
328      if (InvokeRequired)
329        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured), sender, e);
330      else {
331        //don't show the error dialog when downloading tasks, the HiveClient will throw an exception and the dialog will be shown then
332        if (sender.GetType() != typeof(ConcurrentTaskDownloader<ItemTask>) && sender.GetType() != typeof(TaskDownloader)) {
333          ErrorHandling.ShowErrorDialog(this, e.Value);
334        }
335      }
336    }
337    private void Content_StateLogListChanged(object sender, EventArgs e) {
338      if (InvokeRequired)
339        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
340      else {
341        UpdateStateLogList();
342      }
343    }
344
345    private void UpdateStateLogList() {
346      if (Content != null && this.Content.Job != null) {
347        stateLogViewHost.Content = this.Content.StateLogList;
348      } else {
349        stateLogViewHost.Content = null;
350      }
351    }
352
353    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
354      if (this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.Job.Id;
355    }
356    #endregion
357
358    #region Control events
359    private void searchButton_Click(object sender, EventArgs e) {
360      if (hiveResourceSelectorDialog == null) {
361        hiveResourceSelectorDialog = new HiveResourceSelectorDialog(Content.Job.Id, Content.Job.ProjectId);
362      } else if(hiveResourceSelectorDialog.JobId != Content.Job.Id) {
363        hiveResourceSelectorDialog.JobId = Content.Job.Id;
364        hiveResourceSelectorDialog.SelectedProjectId = Content.Job.ProjectId;
365        hiveResourceSelectorDialog.SelectedResourceIds = Content.Job.ResourceIds;
366
367        if (originalJobProjectAssignment.ContainsKey(Content.Job.Id)) {
368          hiveResourceSelectorDialog.ProjectId = originalJobProjectAssignment[Content.Job.Id];
369        } else {
370          hiveResourceSelectorDialog.ProjectId = Guid.Empty;
371        }
372      } else if(hiveResourceSelectorDialog.JobId == Guid.Empty && Content.Job.Id == Guid.Empty) {
373        hiveResourceSelectorDialog.JobId = Content.Job.Id;
374        hiveResourceSelectorDialog.ProjectId = Guid.Empty;
375        hiveResourceSelectorDialog.SelectedProjectId = Content.Job.ProjectId;
376        hiveResourceSelectorDialog.SelectedResourceIds = Content.Job.ResourceIds;
377      } else {
378        hiveResourceSelectorDialog.SelectedProjectId = Content.Job.ProjectId;
379        hiveResourceSelectorDialog.SelectedResourceIds = Content.Job.ResourceIds;
380      }
381
382      if (hiveResourceSelectorDialog.ShowDialog(this) == DialogResult.OK) {
383        selectedProject = hiveResourceSelectorDialog.SelectedProject;
384        if(selectedProject != null) {
385          projectNameTextBox.Text = selectedProject.Name;
386          Content.Job.ProjectId = selectedProject.Id;
387          Content.Job.ResourceIds = hiveResourceSelectorDialog.SelectedResources.Select(x => x.Id).ToList();
388        } else {
389          selectedProject = null;
390          projectNameTextBox.Text = string.Empty;
391          Content.Job.ProjectId = Guid.Empty;
392          Content.Job.ResourceIds = null;
393        }
394      }
395    }
396
397    private void startButton_Click(object sender, EventArgs e) {
398      if (nameTextBox.Text.Trim() == string.Empty) {
399        MessageBox.Show("Please enter a name for the job before uploading it!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
400      } else if (Content.Job.ProjectId == null || Content.Job.ProjectId == Guid.Empty) {
401        MessageBox.Show("Please select a project before uploading the job!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
402      } else if (Content.Job.ResourceIds == null || !Content.Job.ResourceIds.Any()) {
403        MessageBox.Show("Please select resources before uploading the job!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
404      } else if (Content.ExecutionState == ExecutionState.Paused) {
405        var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobAsync, Content);
406        task.ContinueWith((t) => {
407          Content.Progress.Finish();
408          MessageBox.Show("An error occured resuming the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
409          Content.Log.LogException(t.Exception);
410        }, TaskContinuationOptions.OnlyOnFaulted);
411      } else {
412        HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
413      }
414    }
415
416    private void pauseButton_Click(object sender, EventArgs e) {
417      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobAsync, Content);
418      task.ContinueWith((t) => {
419        Content.Progress.Finish();
420        MessageBox.Show("An error occured pausing the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
421        Content.Log.LogException(t.Exception);
422      }, TaskContinuationOptions.OnlyOnFaulted);
423    }
424
425    private void stopButton_Click(object sender, EventArgs e) {
426      var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobAsync, Content);
427      task.ContinueWith((t) => {
428        Content.Progress.Finish();
429        MessageBox.Show("An error occured stopping the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
430        Content.Log.LogException(t.Exception);
431      }, TaskContinuationOptions.OnlyOnFaulted);
432    }
433
434    private void PauseJobAsync(object job) {
435      Content.Progress.Start("Pausing job...");
436      HiveClient.PauseJob((RefreshableJob)job);
437      Content.Progress.Finish();
438    }
439
440    private void StopJobAsync(object job) {
441      Content.Progress.Start("Stopping job...");
442      HiveClient.StopJob((RefreshableJob)job);
443      Content.Progress.Finish();
444    }
445
446    private void ResumeJobAsync(object job) {
447      Content.Progress.Start("Resuming job...");
448      HiveClient.ResumeJob((RefreshableJob)job);
449      Content.Progress.Finish();
450    }
451
452    private void nameTextBox_Validated(object sender, EventArgs e) {
453      if (!SuppressEvents && Content.Job != null && Content.Job.Name != nameTextBox.Text)
454        Content.Job.Name = nameTextBox.Text;
455    }
456
457    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
458      //if (!SuppressEvents && Content.Job != null && Content.Job.ResourceNames != resourceNamesTextBox.Text)
459      //  Content.Job.ResourceNames = resourceNamesTextBox.Text;
460    }
461
462    private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
463      if (Content != null && !SuppressEvents) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
464    }
465
466    private void refreshButton_Click(object sender, EventArgs e) {
467      var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
468      invoker.BeginInvoke(Content, (ar) => {
469        try {
470          invoker.EndInvoke(ar);
471        } catch (Exception ex) {
472          ThreadPool.QueueUserWorkItem(delegate (object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
473        }
474      }, null);
475    }
476
477    private void updateButton_Click(object sender, EventArgs e) {
478      HiveClient.UpdateJob(
479        (Exception ex) => ErrorHandling.ShowErrorDialog(this, "Update failed.", ex),
480        Content,
481        new CancellationToken());
482    }
483
484    private void UnloadButton_Click(object sender, EventArgs e) {
485      Content.Unload();
486      runCollectionViewHost.Content = null;
487      stateLogViewHost.Content = null;
488      hiveExperimentPermissionListView.Content = null;
489      jobsTreeView.Content = null;
490
491      SetEnabledStateOfControls();
492    }
493
494    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
495      if (this.Content.Job.Id == Guid.Empty) {
496        MessageBox.Show("You have to upload the Job first before you can share it.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
497      } else {
498        hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
499      }
500    }
501    #endregion
502
503    #region Helpers
504    private void SetEnabledStateOfExecutableButtons() {
505      if (Content == null) {
506        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = false;
507      } else {
508        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0 && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
509        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started && !Content.IsProgressing;
510        stopButton.Enabled = Content.IsControllable && (Content.ExecutionState == ExecutionState.Started || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
511      }
512    }
513   
514    private Project GetProject(Guid projectId) {
515      return HiveServiceLocator.Instance.CallHiveService(s => s.GetProject(projectId));
516    }
517    #endregion
518
519    #region Drag & Drop
520    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
521      jobsTreeView_DragEnter(sender, e);
522    }
523
524    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
525      e.Effect = DragDropEffects.None;
526      var obj = (IDeepCloneable)e.Data.GetData(Constants.DragDropDataFormat);
527
528      Type objType = obj.GetType();
529      if (ItemTask.IsTypeSupported(objType)) {
530        if (Content.Id != Guid.Empty) e.Effect = DragDropEffects.None;
531        else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
532        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
533      }
534    }
535
536    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
537      if (e.Effect != DragDropEffects.None) {
538        var obj = (IItem)e.Data.GetData(Constants.DragDropDataFormat);
539
540        IItem newObj = null;
541        if (e.Effect.HasFlag(DragDropEffects.Copy)) {
542          newObj = (IItem)obj.Clone();
543        } else {
544          newObj = obj;
545        }
546
547        //IOptimizer and IExecutables need some special care
548        if (newObj is IOptimizer) {
549          ((IOptimizer)newObj).Runs.Clear();
550        }
551        if (newObj is IExecutable) {
552          IExecutable exec = (IExecutable)newObj;
553          if (exec.ExecutionState != ExecutionState.Prepared) {
554            exec.Prepare();
555          }
556        }
557
558        ItemTask hiveTask = ItemTask.GetItemTaskForItem(newObj);
559        Content.HiveTasks.Add(hiveTask.CreateHiveTask());
560      }
561    }
562    #endregion
563
564    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
565      if (tabControl.SelectedTab == permissionTabPage) {
566        if (!Content.IsSharable) {
567          MessageBox.Show("Unable to load permissions. You have insufficient access privileges.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
568          tabControl.SelectedTab = tasksTabPage;
569        }
570      }
571    }
572
573    private RunCollection GetAllRunsFromJob(RefreshableJob job) {
574      if (job != null) {
575        RunCollection runs = new RunCollection() { OptimizerName = job.ItemName };
576
577        foreach (HiveTask subTask in job.HiveTasks) {
578          if (subTask is OptimizerHiveTask) {
579            OptimizerHiveTask ohTask = subTask as OptimizerHiveTask;
580            ohTask.ExecuteReadActionOnItemTask(new Action(delegate () {
581              runs.AddRange(ohTask.ItemTask.Item.Runs);
582            }));
583          }
584        }
585        return runs;
586      } else {
587        return null;
588      }
589    }
590
591  }
592}
Note: See TracBrowser for help on using the repository browser.