Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UnloadJobs/HeuristicLab.Clients.Hive.JobManager/3.3/Views/RefreshableHiveJobView.cs @ 9173

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

#2005

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