Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9710 was 9710, checked in by ascheibe, 11 years ago

#2085 fixed InvalidOperationException in Hive Job Manager

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