Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2117

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