source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.JobManager/3.3/MenuItems/RunInHiveMenuItem.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 15 months ago

#2839:

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Core;
26using HeuristicLab.MainForm;
27using HeuristicLab.Optimization;
28using HeuristicLab.Optimizer;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Clients.Hive.JobManager.Views;
31using System.Windows.Forms;
32using System.Linq;
33
34namespace HeuristicLab.Clients.Hive.JobManager {
35  public class RunInHiveMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
36    public override string Name {
37      get { return "&Run In Hive"; }
38    }
39    public override IEnumerable<string> Structure {
40      get { return new string[] { "&Services", "&Hive" }; }
41    }
42
43    public override int Position {
44      get { return 10002; }
45    }
46
47    protected override void OnActiveViewChanged(object sender, EventArgs e) {
48      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
49
50      if (activeView != null && activeView.Content != null && !activeView.Locked) {
51        var content = activeView.Content as IItem;
52        if (content != null) {
53          Type contentType = content.GetType();
54          ToolStripItem.Enabled = ItemTask.IsTypeSupported(contentType);
55          return;
56        }
57      }
58      ToolStripItem.Enabled = false;
59    }
60
61    private IProgress progress;
62    private IItem content;
63    public override void Execute() {
64      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
65      content = activeView.Content as IItem;
66
67      //IOptimizer and IExecutables need some special care
68      if (content is IOptimizer) {
69        ((IOptimizer)content).Runs.Clear();
70      }
71      if (content is IExecutable) {
72        IExecutable exec = content as IExecutable;
73        if (exec.ExecutionState != ExecutionState.Prepared) {
74          exec.Prepare();
75        }
76      }
77
78      HiveClient.Instance.Refresh();
79
80      ItemTask hiveTask = ItemTask.GetItemTaskForItem(content);
81      HiveTask task = hiveTask.CreateHiveTask();
82      RefreshableJob rJob = new RefreshableJob();
83      rJob.Job.Name = content.ToString();
84      rJob.HiveTasks.Add(task);
85      task.ItemTask.ComputeInParallel = content is Experiment || content is BatchRun;
86
87
88      var hiveResourceSelectorDialog = new HiveResourceSelectorDialog(rJob.Job.Id, rJob.Job.ProjectId);
89      if (hiveResourceSelectorDialog.ShowDialog((UserControl)activeView) == DialogResult.OK) {
90        var selectedProject = hiveResourceSelectorDialog.SelectedProject;
91        if (selectedProject != null) {
92          rJob.Job.ProjectId = selectedProject.Id;
93          rJob.Job.ResourceIds = hiveResourceSelectorDialog.SelectedResources.Select(x => x.Id).ToList();
94
95          progress = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToContent(this.content, "Uploading to Hive...");
96          rJob.Progress = progress;
97          progress.ProgressStateChanged += progress_ProgressStateChanged;
98
99          HiveClient.StartJob(new Action<Exception>(HandleEx), rJob, new CancellationToken());
100        }
101      }
102    }
103
104    private void progress_ProgressStateChanged(object sender, EventArgs e) {
105      if (progress.ProgressState != ProgressState.Started) {
106        MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromContent(content);
107        progress.ProgressStateChanged -= progress_ProgressStateChanged;
108      }
109    }
110
111    private void HandleEx(Exception ex) {
112      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromContent(content);
113      progress.ProgressStateChanged -= progress_ProgressStateChanged;
114      ErrorHandling.ShowErrorDialog("Error uploading tasks", ex);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.