source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.ExperimentManager/3.4/Views/RefreshableHiveExperimentView.cs @ 6479

Last change on this file since 6479 was 6479, checked in by cneumuel, 8 years ago

#1233

  • finished experiment sharing
  • added role for executing privileged jobs
  • refreshing experiments in experimentManager does not delete already downloaded jobs
  • moved some properties from HiveExperiment into RefreshableHiveExperiment
File size: 17.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Threading;
26using System.Windows.Forms;
27using HeuristicLab.Clients.Hive.Views;
28using HeuristicLab.Collections;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33using HeuristicLab.Optimization;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Clients.Hive.ExperimentManager.Views {
37  /// <summary>
38  /// The base class for visual representations of items.
39  /// </summary>
40  [View("Hive Experiment View")]
41  [Content(typeof(RefreshableHiveExperiment), true)]
42  public sealed partial class RefreshableHiveExperimentView : ItemView {
43    private ProgressView progressView;
44
45    public new RefreshableHiveExperiment Content {
46      get { return (RefreshableHiveExperiment)base.Content; }
47      set { base.Content = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="ItemBaseView"/>.
52    /// </summary>
53    public RefreshableHiveExperimentView() {
54      InitializeComponent();
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.RefreshAutomaticallyChanged += new EventHandler(Content_RefreshAutomaticallyChanged);
60      Content.HiveExperimentChanged += new EventHandler(Content_HiveExperimentChanged);
61      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
62      Content.JobStatisticsChanged += new EventHandler(Content_JobStatisticsChanged);
63      Content.ExceptionOccured += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
64      Content.StateLogListChanged += new EventHandler(Content_StateLogListChanged);
65      Content.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
66      Content.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
67      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
68      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
69      Content.IsAllowedPrivilegedChanged += new EventHandler(Content_IsAllowedPrivilegedChanged);
70    }
71
72    protected override void DeregisterContentEvents() {
73      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
74      Content.HiveExperimentChanged -= new EventHandler(Content_HiveExperimentChanged);
75      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
76      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
77      Content.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
78      Content.StateLogListChanged -= new EventHandler(Content_StateLogListChanged);
79      Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
80      Content.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
81      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
82      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
83      base.DeregisterContentEvents();
84    }
85
86    private void RegisterHiveExperimentEvents() {
87      Content.HiveExperiment.PropertyChanged += new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
88    }
89   
90    private void DeregisterHiveExperimentEvents() {
91      Content.HiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
92    }
93
94    private void RegisterHiveJobEvents() {
95      Content.HiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
96      Content.HiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
97      Content.HiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
98    }
99    private void DeregisterHiveJobEvents() {
100      Content.HiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
101      Content.HiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
102      Content.HiveJobs.CollectionReset -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
103    }
104
105    protected override void OnContentChanged() {
106      base.OnContentChanged();
107      if (Content == null) {
108        nameTextBox.Text = string.Empty;
109        executionTimeTextBox.Text = string.Empty;
110        resourceNamesTextBox.Text = string.Empty;
111        isPrivilegedCheckBox.Checked = false;
112        logView.Content = null;
113        refreshAutomaticallyCheckBox.Checked = false;
114        logView.Content = null;
115      } else {
116        nameTextBox.Text = Content.HiveExperiment.Name;
117        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
118        resourceNamesTextBox.Text = Content.HiveExperiment.ResourceNames;
119        isPrivilegedCheckBox.Checked = Content.HiveExperiment.IsPrivileged;
120        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
121        logView.Content = Content.Log;
122      }
123      hiveExperimentPermissionListView.Content = null; // has to be filled by refresh button
124      Content_JobStatisticsChanged(this, EventArgs.Empty);
125      Content_HiveExperimentChanged(this, EventArgs.Empty);
126      Content_HiveJobChanged(this, EventArgs.Empty);
127      Content_IsProgressingChanged(this, EventArgs.Empty);
128      Content_StateLogListChanged(this, EventArgs.Empty);
129      HiveExperiment_PropertyChanged(this, new PropertyChangedEventArgs("Id"));
130      SetEnabledStateOfControls();
131    }
132
133    protected override void SetEnabledStateOfControls() {
134      base.SetEnabledStateOfControls();
135      executionTimeTextBox.Enabled = Content != null;
136      jobsTextBox.ReadOnly = true;
137      calculatingTextBox.ReadOnly = true;
138      finishedTextBox.ReadOnly = true;
139
140      if (Content != null) {
141        bool alreadyUploaded = Content.Id != Guid.Empty;
142        bool jobsLoaded = Content.HiveJobs != null && Content.HiveJobs.All(x => x.Job.Id != Guid.Empty);
143
144        this.nameTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
145        this.resourceNamesTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
146        this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
147
148        this.isPrivilegedCheckBox.Enabled = Content.IsAllowedPrivileged && Content.IsControllable && !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded); // TODO: check if user has the rights to do this       
149        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
150        this.refreshButton.Enabled = Content.IsDownloadable && alreadyUploaded;
151        this.Locked = !Content.IsControllable || Content.ExecutionState == ExecutionState.Started;
152      }
153      SetEnabledStateOfExecutableButtons();
154      tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
155    }
156
157    protected override void OnClosed(FormClosedEventArgs e) {
158      if (Content != null) {
159        if (Content.RefreshAutomatically)
160          Content.StopResultPolling();
161      }
162      base.OnClosed(e);
163    }
164
165    #region Content Events
166    private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
167      if (InvokeRequired)
168        Invoke(new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset), sender, e);
169      else {
170        SetEnabledStateOfControls();
171      }
172    }
173    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
176      else
177        SetEnabledStateOfControls();
178    }
179    private void Content_Prepared(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_Prepared), sender, e);
182      else {
183        nameTextBox.Enabled = true;
184        Locked = false;
185        SetEnabledStateOfControls();
186      }
187    }
188    private void Content_Started(object sender, EventArgs e) {
189      if (InvokeRequired)
190        Invoke(new EventHandler(Content_Started), sender, e);
191      else {
192        nameTextBox.Enabled = false;
193        SetEnabledStateOfControls();
194      }
195    }
196    private void Content_Paused(object sender, EventArgs e) {
197      if (InvokeRequired)
198        Invoke(new EventHandler(Content_Paused), sender, e);
199      else {
200        nameTextBox.Enabled = true;
201        SetEnabledStateOfControls();
202      }
203    }
204    private void Content_Stopped(object sender, EventArgs e) {
205      if (InvokeRequired)
206        Invoke(new EventHandler(Content_Stopped), sender, e);
207      else {
208        nameTextBox.Enabled = true;
209        Locked = false;
210        SetEnabledStateOfControls();
211      }
212    }
213    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
214      if (InvokeRequired)
215        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
216      else
217        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
218    }
219    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
220      if (InvokeRequired)
221        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
222      else {
223        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
224        SetEnabledStateOfControls();
225      }
226    }
227    private void Content_HiveJobChanged(object sender, EventArgs e) {
228      if (InvokeRequired)
229        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
230      else {
231        if (Content != null && Content.HiveJobs != null) {
232          jobsTreeView.Content = Content.HiveJobs;
233          RegisterHiveJobEvents();
234        } else {
235          jobsTreeView.Content = null;
236        }
237        SetEnabledStateOfControls();
238      }
239    }
240
241    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
242      if (Content.HiveExperiment != null) {
243        RegisterHiveExperimentEvents();
244        Content_IsProgressingChanged(sender, e);
245      }
246    }
247    private void Content_IsControllableChanged(object sender, EventArgs e) {
248      SetEnabledStateOfControls();
249    }
250    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
251      if (InvokeRequired)
252        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
253      else {
254        if (Content != null) {
255          jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
256          calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
257          finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
258        } else {
259          jobsTextBox.Text = "0";
260          calculatingTextBox.Text = "0";
261          finishedTextBox.Text = "0";
262        }
263      }
264    }
265    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
266      // don't show exception, it is logged anyway
267    }
268    private void Content_StateLogListChanged(object sender, EventArgs e) {
269      if (InvokeRequired)
270        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
271      else {
272        UpdateStateLogList();
273      }
274    }
275    private void Content_IsAllowedPrivilegedChanged(object sender, EventArgs e) {
276      if (InvokeRequired)
277        Invoke(new EventHandler(Content_IsAllowedPrivilegedChanged), sender, e);
278      else {
279        SetEnabledStateOfControls();
280      }
281    }
282
283
284    private void UpdateStateLogList() {
285      if (Content != null && this.Content.HiveExperiment != null) {
286        stateLogViewHost.Content = this.Content.StateLogList;
287      } else {
288        stateLogViewHost.Content = null;
289      }
290    }
291
292    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
293      if(this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.HiveExperiment.Id;
294    }
295    #endregion
296
297    #region Control events
298    private void startButton_Click(object sender, EventArgs e) {
299      HiveClient.StartExperiment((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
300    }
301    private void pauseButton_Click(object sender, EventArgs e) {
302      HiveClient.PauseExperiment(Content);
303    }
304    private void stopButton_Click(object sender, EventArgs e) {
305      HiveClient.StopExperiment(Content);
306    }
307    private void resetButton_Click(object sender, EventArgs e) { }
308
309    private void nameTextBox_Validated(object sender, EventArgs e) {
310      if (Content.HiveExperiment.Name != nameTextBox.Text)
311        Content.HiveExperiment.Name = nameTextBox.Text;
312    }
313
314    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
315      if (Content.HiveExperiment.ResourceNames != resourceNamesTextBox.Text)
316        Content.HiveExperiment.ResourceNames = resourceNamesTextBox.Text;
317    }
318
319    private void refreshAutomaticallyCheckBox_Validated(object sender, EventArgs e) {
320      if (Content != null) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
321    }
322
323    private void isPrivilegedCheckBox_Validated(object sender, EventArgs e) {
324      if (Content != null) Content.HiveExperiment.IsPrivileged = isPrivilegedCheckBox.Checked;
325    }
326
327    private void refreshButton_Click(object sender, EventArgs e) {
328      var invoker = new Action<RefreshableHiveExperiment>(HiveClient.LoadExperiment);
329      invoker.BeginInvoke(Content, (ar) => {
330        try {
331          invoker.EndInvoke(ar);
332        }
333        catch (Exception ex) {
334          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
335        }
336      }, null);
337    }
338
339    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
340      hiveExperimentPermissionListView.Content = HiveClient.GetHiveExperimentPermissions(this.Content.HiveExperiment.Id);
341    }
342
343
344    #endregion
345
346    #region Helpers
347    private void SetEnabledStateOfExecutableButtons() {
348      if (Content == null) {
349        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
350      } else {
351        startButton.Enabled = Content.IsControllable && Content.HiveJobs != null && Content.HiveJobs.Count > 0 && Content.ExecutionState == ExecutionState.Prepared;
352        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
353        stopButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
354        resetButton.Enabled = false;
355      }
356    }
357    #endregion
358
359    #region Progress reporting
360    private void Content_IsProgressingChanged(object sender, EventArgs e) {
361      if (this.InvokeRequired) {
362        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
363      } else {
364        if (Content != null && Content.IsProgressing) {
365          SetProgressView();
366        } else {
367          FinishProgressView();
368        }
369      }
370    }
371
372    private void SetProgressView() {
373      if (progressView == null) {
374        progressView = new ProgressView(this, Content.Progress);
375      }
376      progressView.Progress = Content.Progress;
377    }
378
379    private void FinishProgressView() {
380      if (progressView != null) {
381        progressView.Finish();
382        progressView = null;
383        SetEnabledStateOfControls();
384      }
385    }
386    #endregion
387
388    #region Drag & Drop
389    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
390      jobsTreeView_DragEnter(sender, e);
391    }
392
393    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
394      e.Effect = DragDropEffects.None;
395      var obj = e.Data.GetData(Constants.DragDropDataFormat);
396      if (obj is IOptimizer) {
397        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
398        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
399      }
400    }
401
402    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
403      if (e.Effect != DragDropEffects.None) {
404        var obj = e.Data.GetData(Constants.DragDropDataFormat);
405
406        var optimizer = obj as IOptimizer;
407        if (optimizer != null) {
408          Content.HiveJobs.Add(new OptimizerHiveJob(e.Effect.HasFlag(DragDropEffects.Copy) ? (IOptimizer)optimizer.Clone() : optimizer));
409        }
410      }
411    }
412    #endregion
413
414    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
415      if (tabControl.SelectedTab == sharingTabPage) {
416        if (!Content.IsSharable) {
417          MessageBox.Show("Unable to load tab. You have insufficient access privileges.");
418          tabControl.SelectedTab = jobsTabPage;
419        }
420      }
421    }
422  }
423}
Note: See TracBrowser for help on using the repository browser.