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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using System.Threading.Tasks;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 | using HeuristicLab.MainForm.WindowsForms;
|
---|
33 | using HeuristicLab.Optimization;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using System.Collections.Generic;
|
---|
36 |
|
---|
37 | namespace 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 | SetEnabledStateOfExecutableButtons();
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | private void startButton_Click(object sender, EventArgs e) {
|
---|
399 | if (nameTextBox.Text.Trim() == string.Empty) {
|
---|
400 | MessageBox.Show("Please enter a name for the job before uploading it!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
401 | } else if (Content.Job.ProjectId == null || Content.Job.ProjectId == Guid.Empty) {
|
---|
402 | MessageBox.Show("Please select a project before uploading the job!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
403 | } else if (Content.Job.ResourceIds == null || !Content.Job.ResourceIds.Any()) {
|
---|
404 | MessageBox.Show("Please select resources before uploading the job!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
405 | } else if (Content.ExecutionState == ExecutionState.Paused) {
|
---|
406 | var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobAsync, Content);
|
---|
407 | task.ContinueWith((t) => {
|
---|
408 | Content.Progress.Finish();
|
---|
409 | MessageBox.Show("An error occured resuming the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
410 | Content.Log.LogException(t.Exception);
|
---|
411 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
412 | } else {
|
---|
413 | HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
|
---|
414 | UpdateSelectorDialog();
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | private void pauseButton_Click(object sender, EventArgs e) {
|
---|
419 | var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobAsync, Content);
|
---|
420 | task.ContinueWith((t) => {
|
---|
421 | Content.Progress.Finish();
|
---|
422 | MessageBox.Show("An error occured pausing the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
423 | Content.Log.LogException(t.Exception);
|
---|
424 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
425 | }
|
---|
426 |
|
---|
427 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
428 | var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobAsync, Content);
|
---|
429 | task.ContinueWith((t) => {
|
---|
430 | Content.Progress.Finish();
|
---|
431 | MessageBox.Show("An error occured stopping the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
432 | Content.Log.LogException(t.Exception);
|
---|
433 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
434 | }
|
---|
435 |
|
---|
436 | private void PauseJobAsync(object job) {
|
---|
437 | Content.Progress.Start("Pausing job...");
|
---|
438 | HiveClient.PauseJob((RefreshableJob)job);
|
---|
439 | Content.Progress.Finish();
|
---|
440 | }
|
---|
441 |
|
---|
442 | private void StopJobAsync(object job) {
|
---|
443 | Content.Progress.Start("Stopping job...");
|
---|
444 | HiveClient.StopJob((RefreshableJob)job);
|
---|
445 | Content.Progress.Finish();
|
---|
446 | }
|
---|
447 |
|
---|
448 | private void ResumeJobAsync(object job) {
|
---|
449 | Content.Progress.Start("Resuming job...");
|
---|
450 | HiveClient.ResumeJob((RefreshableJob)job);
|
---|
451 | Content.Progress.Finish();
|
---|
452 | }
|
---|
453 |
|
---|
454 | private void nameTextBox_Validated(object sender, EventArgs e) {
|
---|
455 | if (!SuppressEvents && Content.Job != null && Content.Job.Name != nameTextBox.Text)
|
---|
456 | Content.Job.Name = nameTextBox.Text;
|
---|
457 | }
|
---|
458 |
|
---|
459 | private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
|
---|
460 | //if (!SuppressEvents && Content.Job != null && Content.Job.ResourceNames != resourceNamesTextBox.Text)
|
---|
461 | // Content.Job.ResourceNames = resourceNamesTextBox.Text;
|
---|
462 | }
|
---|
463 |
|
---|
464 | private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
465 | if (Content != null && !SuppressEvents) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
|
---|
466 | }
|
---|
467 |
|
---|
468 | private void refreshButton_Click(object sender, EventArgs e) {
|
---|
469 | var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
|
---|
470 | invoker.BeginInvoke(Content, (ar) => {
|
---|
471 | try {
|
---|
472 | invoker.EndInvoke(ar);
|
---|
473 | } catch (Exception ex) {
|
---|
474 | ThreadPool.QueueUserWorkItem(delegate (object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
|
---|
475 | }
|
---|
476 | }, null);
|
---|
477 | UpdateSelectorDialog();
|
---|
478 | }
|
---|
479 |
|
---|
480 | private void updateButton_Click(object sender, EventArgs e) {
|
---|
481 | HiveClient.UpdateJob(
|
---|
482 | (Exception ex) => ErrorHandling.ShowErrorDialog(this, "Update failed.", ex),
|
---|
483 | Content,
|
---|
484 | new CancellationToken());
|
---|
485 | UpdateSelectorDialog();
|
---|
486 | }
|
---|
487 |
|
---|
488 | private void UnloadButton_Click(object sender, EventArgs e) {
|
---|
489 | Content.Unload();
|
---|
490 | runCollectionViewHost.Content = null;
|
---|
491 | stateLogViewHost.Content = null;
|
---|
492 | hiveExperimentPermissionListView.Content = null;
|
---|
493 | jobsTreeView.Content = null;
|
---|
494 |
|
---|
495 | SetEnabledStateOfControls();
|
---|
496 | }
|
---|
497 |
|
---|
498 | private void refreshPermissionsButton_Click(object sender, EventArgs e) {
|
---|
499 | if (this.Content.Job.Id == Guid.Empty) {
|
---|
500 | MessageBox.Show("You have to upload the Job first before you can share it.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
501 | } else {
|
---|
502 | hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | #endregion
|
---|
506 |
|
---|
507 | #region Helpers
|
---|
508 | private void SetEnabledStateOfExecutableButtons() {
|
---|
509 | if (Content == null) {
|
---|
510 | startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = false;
|
---|
511 | } else {
|
---|
512 | startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0
|
---|
513 | && Content.Job.ProjectId != null && Content.Job.ProjectId != Guid.Empty && Content.Job.ResourceIds != null && Content.Job.ResourceIds.Any()
|
---|
514 | && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
|
---|
515 | pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started && !Content.IsProgressing;
|
---|
516 | stopButton.Enabled = Content.IsControllable && (Content.ExecutionState == ExecutionState.Started || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | private Project GetProject(Guid projectId) {
|
---|
521 | return HiveServiceLocator.Instance.CallHiveService(s => s.GetProject(projectId));
|
---|
522 | }
|
---|
523 |
|
---|
524 | private void UpdateSelectorDialog() {
|
---|
525 | if(hiveResourceSelectorDialog != null) {
|
---|
526 | hiveResourceSelectorDialog = null;
|
---|
527 | //hiveResourceSelectorDialog.JobId = Content.Job.Id;
|
---|
528 | //hiveResourceSelectorDialog.SelectedProjectId = Content.Job.ProjectId;
|
---|
529 | //hiveResourceSelectorDialog.SelectedResourceIds = Content.Job.ResourceIds;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | #endregion
|
---|
533 |
|
---|
534 | #region Drag & Drop
|
---|
535 | private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
|
---|
536 | jobsTreeView_DragEnter(sender, e);
|
---|
537 | }
|
---|
538 |
|
---|
539 | private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
|
---|
540 | e.Effect = DragDropEffects.None;
|
---|
541 | var obj = (IDeepCloneable)e.Data.GetData(Constants.DragDropDataFormat);
|
---|
542 |
|
---|
543 | Type objType = obj.GetType();
|
---|
544 | if (ItemTask.IsTypeSupported(objType)) {
|
---|
545 | if (Content.Id != Guid.Empty) e.Effect = DragDropEffects.None;
|
---|
546 | else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
547 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
|
---|
552 | if (e.Effect != DragDropEffects.None) {
|
---|
553 | var obj = (IItem)e.Data.GetData(Constants.DragDropDataFormat);
|
---|
554 |
|
---|
555 | IItem newObj = null;
|
---|
556 | if (e.Effect.HasFlag(DragDropEffects.Copy)) {
|
---|
557 | newObj = (IItem)obj.Clone();
|
---|
558 | } else {
|
---|
559 | newObj = obj;
|
---|
560 | }
|
---|
561 |
|
---|
562 | //IOptimizer and IExecutables need some special care
|
---|
563 | if (newObj is IOptimizer) {
|
---|
564 | ((IOptimizer)newObj).Runs.Clear();
|
---|
565 | }
|
---|
566 | if (newObj is IExecutable) {
|
---|
567 | IExecutable exec = (IExecutable)newObj;
|
---|
568 | if (exec.ExecutionState != ExecutionState.Prepared) {
|
---|
569 | exec.Prepare();
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | ItemTask hiveTask = ItemTask.GetItemTaskForItem(newObj);
|
---|
574 | Content.HiveTasks.Add(hiveTask.CreateHiveTask());
|
---|
575 | }
|
---|
576 | }
|
---|
577 | #endregion
|
---|
578 |
|
---|
579 | private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
580 | if (tabControl.SelectedTab == permissionTabPage) {
|
---|
581 | if (!Content.IsSharable) {
|
---|
582 | MessageBox.Show("Unable to load permissions. You have insufficient access privileges.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
583 | tabControl.SelectedTab = tasksTabPage;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | private RunCollection GetAllRunsFromJob(RefreshableJob job) {
|
---|
589 | if (job != null) {
|
---|
590 | RunCollection runs = new RunCollection() { OptimizerName = job.ItemName };
|
---|
591 |
|
---|
592 | foreach (HiveTask subTask in job.HiveTasks) {
|
---|
593 | if (subTask is OptimizerHiveTask) {
|
---|
594 | OptimizerHiveTask ohTask = subTask as OptimizerHiveTask;
|
---|
595 | ohTask.ExecuteReadActionOnItemTask(new Action(delegate () {
|
---|
596 | runs.AddRange(ohTask.ItemTask.Item.Runs);
|
---|
597 | }));
|
---|
598 | }
|
---|
599 | }
|
---|
600 | return runs;
|
---|
601 | } else {
|
---|
602 | return null;
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | }
|
---|
607 | } |
---|