Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive.JobManager/3.3/MenuItems/RunInHiveMenuItem.cs @ 11303

Last change on this file since 11303 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

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