Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9219 was 9219, checked in by ascheibe, 12 years ago

#2005 merged Hive Job Unloading branch back into trunk

File size: 25.2 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        this.UnloadButton.Enabled = Content.HiveTasks != null && Content.HiveTasks.Count > 0 && alreadyUploaded && !Content.IsProgressing;
191      }
192      SetEnabledStateOfExecutableButtons();
193      tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
194    }
195
196    protected override void OnClosed(FormClosedEventArgs e) {
197      if (Content != null) {
198        if (Content.RefreshAutomatically)
199          Content.StopResultPolling();
200      }
201      base.OnClosed(e);
202    }
203
204    #region Content Events
205    void Content_TaskReceived(object sender, EventArgs e) {
206      lock (runCollectionViewLocker) {
207        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
208      }
209    }
210
211    private void HiveTasks_ItemsAdded(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
212      if (InvokeRequired)
213        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsAdded), sender, e);
214      else {
215        SetEnabledStateOfControls();
216      }
217    }
218
219    private void HiveTasks_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
220      if (InvokeRequired)
221        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_ItemsRemoved), sender, e);
222      else {
223        SetEnabledStateOfControls();
224      }
225    }
226
227    private void HiveTasks_CollectionReset(object sender, CollectionItemsChangedEventArgs<HiveTask> e) {
228      if (InvokeRequired)
229        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(HiveTasks_CollectionReset), sender, e);
230      else {
231        SetEnabledStateOfControls();
232      }
233    }
234
235    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
236      if (InvokeRequired)
237        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
238      else
239        SetEnabledStateOfControls();
240    }
241    private void Content_Prepared(object sender, EventArgs e) {
242      if (InvokeRequired)
243        Invoke(new EventHandler(Content_Prepared), sender, e);
244      else {
245        nameTextBox.Enabled = true;
246        Locked = false;
247        SetEnabledStateOfControls();
248      }
249    }
250    private void Content_Started(object sender, EventArgs e) {
251      if (InvokeRequired)
252        Invoke(new EventHandler(Content_Started), sender, e);
253      else {
254        nameTextBox.Enabled = false;
255        SetEnabledStateOfControls();
256      }
257    }
258    private void Content_Paused(object sender, EventArgs e) {
259      if (InvokeRequired)
260        Invoke(new EventHandler(Content_Paused), sender, e);
261      else {
262        nameTextBox.Enabled = true;
263        SetEnabledStateOfControls();
264      }
265    }
266    private void Content_Stopped(object sender, EventArgs e) {
267      if (InvokeRequired)
268        Invoke(new EventHandler(Content_Stopped), sender, e);
269      else {
270        nameTextBox.Enabled = true;
271        Locked = false;
272        SetEnabledStateOfControls();
273      }
274    }
275    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
276      if (InvokeRequired)
277        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
278      else
279        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
280    }
281    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
282      if (InvokeRequired)
283        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
284      else {
285        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
286        SetEnabledStateOfControls();
287      }
288    }
289    private void Content_HiveTasksChanged(object sender, EventArgs e) {
290      if (InvokeRequired)
291        Invoke(new EventHandler(Content_HiveTasksChanged), sender, e);
292      else {
293        if (Content != null && Content.HiveTasks != null) {
294          jobsTreeView.Content = Content.HiveTasks;
295          RegisterHiveTasksEvents();
296        } else {
297          jobsTreeView.Content = null;
298        }
299        SetEnabledStateOfControls();
300      }
301    }
302
303    void Content_Loaded(object sender, EventArgs e) {
304      lock (runCollectionViewLocker) {
305        runCollectionViewHost.Content = GetAllRunsFromJob(Content);
306      }
307    }
308
309    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
310      if (Content != null && Content.Job != null) {
311        RegisterHiveExperimentEvents();
312        Content_IsProgressingChanged(sender, e);
313      }
314    }
315    private void Content_IsControllableChanged(object sender, EventArgs e) {
316      SetEnabledStateOfControls();
317    }
318    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
319      if (InvokeRequired)
320        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
321      else {
322        if (Content != null) {
323          jobsTextBox.Text = (Content.Job.JobCount - Content.Job.CalculatingCount - Content.Job.FinishedCount).ToString();
324          calculatingTextBox.Text = Content.Job.CalculatingCount.ToString();
325          finishedTextBox.Text = Content.Job.FinishedCount.ToString();
326        } else {
327          jobsTextBox.Text = "0";
328          calculatingTextBox.Text = "0";
329          finishedTextBox.Text = "0";
330        }
331      }
332    }
333    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
334      if (InvokeRequired)
335        Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured), sender, e);
336      else {
337        //don't show the error dialog when downloading tasks, the HiveClient will throw an exception and the dialog will be shown then
338        if (sender.GetType() != typeof(ConcurrentTaskDownloader<ItemTask>) && sender.GetType() != typeof(TaskDownloader)) {
339          ErrorHandling.ShowErrorDialog(this, e.Value);
340        }
341      }
342    }
343    private void Content_StateLogListChanged(object sender, EventArgs e) {
344      if (InvokeRequired)
345        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
346      else {
347        UpdateStateLogList();
348      }
349    }
350    private void Content_IsAllowedPrivilegedChanged(object sender, EventArgs e) {
351      if (InvokeRequired)
352        Invoke(new EventHandler(Content_IsAllowedPrivilegedChanged), sender, e);
353      else {
354        isPrivilegedCheckBox.Checked = Content.IsAllowedPrivileged;
355        SetEnabledStateOfControls();
356      }
357    }
358
359    private void UpdateStateLogList() {
360      if (Content != null && this.Content.Job != null) {
361        stateLogViewHost.Content = this.Content.StateLogList;
362      } else {
363        stateLogViewHost.Content = null;
364      }
365    }
366
367    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
368      if (this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.Job.Id;
369    }
370    #endregion
371
372    #region Control events
373    private void searchButton_Click(object sender, EventArgs e) {
374      if (hiveResourceSelectorDialog == null)
375        hiveResourceSelectorDialog = new HiveResourceSelectorDialog();
376      if (hiveResourceSelectorDialog.ShowDialog(this) == DialogResult.OK) {
377        StringBuilder sb = new StringBuilder();
378        foreach (Resource resource in hiveResourceSelectorDialog.GetSelectedResources()) {
379          sb.Append(resource.Name);
380          sb.Append(";");
381        }
382        resourceNamesTextBox.Text = sb.ToString();
383        if (Content.Job.ResourceNames != resourceNamesTextBox.Text)
384          Content.Job.ResourceNames = resourceNamesTextBox.Text;
385      }
386    }
387
388    private void startButton_Click(object sender, EventArgs e) {
389      if (nameTextBox.Text.Trim() == string.Empty) {
390        MessageBox.Show("Please enter a name for the job before uploading it!", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
391      } else if (Content.ExecutionState == ExecutionState.Paused) {
392        var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeJobAsync, Content);
393        task.ContinueWith((t) => {
394          progress.Finish();
395          MessageBox.Show("An error occured resuming the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
396          Content.Log.LogException(t.Exception);
397        }, TaskContinuationOptions.OnlyOnFaulted);
398      } else {
399        HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
400      }
401    }
402
403    private void pauseButton_Click(object sender, EventArgs e) {
404      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseJobAsync, Content);
405      task.ContinueWith((t) => {
406        progress.Finish();
407        MessageBox.Show("An error occured pausing the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
408        Content.Log.LogException(t.Exception);
409      }, TaskContinuationOptions.OnlyOnFaulted);
410    }
411
412    private void stopButton_Click(object sender, EventArgs e) {
413      var task = System.Threading.Tasks.Task.Factory.StartNew(StopJobAsync, Content);
414      task.ContinueWith((t) => {
415        progress.Finish();
416        MessageBox.Show("An error occured stopping the job. See the log for more information.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
417        Content.Log.LogException(t.Exception);
418      }, TaskContinuationOptions.OnlyOnFaulted);
419    }
420    private void resetButton_Click(object sender, EventArgs e) { }
421
422    private void PauseJobAsync(object job) {
423      progress.Status = "Pausing job...";
424      progress.ProgressState = ProgressState.Started;
425      HiveClient.PauseJob((RefreshableJob)job);
426      progress.Finish();
427    }
428
429    private void StopJobAsync(object job) {
430      progress.Status = "Stopping job...";
431      progress.ProgressState = ProgressState.Started;
432      HiveClient.StopJob((RefreshableJob)job);
433      progress.Finish();
434    }
435
436    private void ResumeJobAsync(object job) {
437      progress.Status = "Resuming job...";
438      progress.ProgressState = ProgressState.Started;
439      HiveClient.ResumeJob((RefreshableJob)job);
440      progress.Finish();
441    }
442
443    private void nameTextBox_Validated(object sender, EventArgs e) {
444      if (!SuppressEvents && Content.Job != null && Content.Job.Name != nameTextBox.Text)
445        Content.Job.Name = nameTextBox.Text;
446    }
447
448    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
449      if (!SuppressEvents && Content.Job != null && Content.Job.ResourceNames != resourceNamesTextBox.Text)
450        Content.Job.ResourceNames = resourceNamesTextBox.Text;
451    }
452
453    private void refreshAutomaticallyCheckBox_CheckedChanged(object sender, EventArgs e) {
454      if (Content != null && !SuppressEvents) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
455    }
456
457    private void isPrivilegedCheckBox_CheckChanged(object sender, EventArgs e) {
458      if (Content != null && !SuppressEvents) Content.IsAllowedPrivileged = isPrivilegedCheckBox.Checked;
459    }
460
461    private void refreshButton_Click(object sender, EventArgs e) {
462      var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
463      invoker.BeginInvoke(Content, (ar) => {
464        try {
465          invoker.EndInvoke(ar);
466        }
467        catch (Exception ex) {
468          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
469        }
470      }, null);
471    }
472
473    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
474      if (this.Content.Job.Id == Guid.Empty) {
475        MessageBox.Show("You have to upload the Job first before you can share it.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
476      } else {
477        hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
478      }
479    }
480    #endregion
481
482    #region Helpers
483    private void SetEnabledStateOfExecutableButtons() {
484      if (Content == null) {
485        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
486      } else {
487        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0 && (Content.ExecutionState == ExecutionState.Prepared || Content.ExecutionState == ExecutionState.Paused) && !Content.IsProgressing;
488        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started && !Content.IsProgressing;
489        stopButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started && !Content.IsProgressing;
490        resetButton.Enabled = false;
491      }
492    }
493    #endregion
494
495    #region Progress reporting
496    private void Content_IsProgressingChanged(object sender, EventArgs e) {
497      if (this.InvokeRequired) {
498        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
499      } else {
500        if (Content != null && Content.Progress != null && Content.IsProgressing) {
501          progressView.Content = Content.Progress;
502        } else if (Content != null) {
503          progressView.Content = progress;
504        }
505      }
506    }
507    #endregion
508
509    #region Drag & Drop
510    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
511      jobsTreeView_DragEnter(sender, e);
512    }
513
514    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
515      e.Effect = DragDropEffects.None;
516      var obj = e.Data.GetData(Constants.DragDropDataFormat);
517      if (obj is IOptimizer) {
518        if (Content.Id != Guid.Empty) e.Effect = DragDropEffects.None;
519        else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
520        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
521      }
522    }
523
524    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
525      if (e.Effect != DragDropEffects.None) {
526        var obj = e.Data.GetData(Constants.DragDropDataFormat);
527
528        var optimizer = obj as IOptimizer;
529        if (optimizer != null) {
530          IOptimizer newOptimizer = null;
531          if (e.Effect.HasFlag(DragDropEffects.Copy)) {
532            newOptimizer = (IOptimizer)optimizer.Clone();
533            newOptimizer.Runs.Clear();
534          } else {
535            newOptimizer = optimizer;
536          }
537          if (newOptimizer.ExecutionState != ExecutionState.Prepared) {
538            newOptimizer.Prepare();
539          }
540
541          Content.HiveTasks.Add(new OptimizerHiveTask(newOptimizer));
542        }
543      }
544    }
545    #endregion
546
547    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
548      if (tabControl.SelectedTab == permissionTabPage) {
549        if (!Content.IsSharable) {
550          MessageBox.Show("Unable to load permissions. You have insufficient access privileges.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
551          tabControl.SelectedTab = tasksTabPage;
552        }
553      }
554    }
555
556    private RunCollection GetAllRunsFromJob(RefreshableJob job) {
557      if (job != null) {
558        RunCollection runs = new RunCollection() { OptimizerName = job.ItemName };
559
560        foreach (HiveTask subTask in job.HiveTasks) {
561          if (subTask is OptimizerHiveTask) {
562            OptimizerHiveTask ohTask = subTask as OptimizerHiveTask;
563            ohTask.ExecuteReadActionOnItemTask(new Action(delegate() {
564              runs.AddRange(ohTask.ItemTask.Item.Runs);
565            }));
566          }
567        }
568        return runs;
569      } else {
570        return null;
571      }
572    }
573
574    private void UnloadButton_Click(object sender, EventArgs e) {
575      Content.Unload();
576      runCollectionViewHost.Content = null;
577      stateLogViewHost.Content = null;
578      hiveExperimentPermissionListView.Content = null;
579      jobsTreeView.Content = null;
580
581      SetEnabledStateOfControls();
582    }
583  }
584}
Note: See TracBrowser for help on using the repository browser.