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

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

#1233

  • created user interface for experiment sharing
  • created UserManager which provides access to the users
  • inserted a lot of security and authorization checks serverside
  • minor fixes in experiment manager
File size: 17.5 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    }
66
67    protected override void DeregisterContentEvents() {
68      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
69      Content.HiveExperimentChanged -= 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      base.DeregisterContentEvents();
75    }
76
77    private void RegisterHiveExperimentEvents() {
78      Content.HiveExperiment.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
79      Content.HiveExperiment.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
80      Content.HiveExperiment.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
81      Content.HiveExperiment.IsProgressingChanged += new EventHandler(Content_IsProgressingChanged);
82      Content.HiveExperiment.PropertyChanged += new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
83    }
84   
85    private void DeregisterHiveExperimentEvents() {
86      Content.HiveExperiment.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
87      Content.HiveExperiment.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
88      Content.HiveExperiment.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
89      Content.HiveExperiment.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
90      Content.HiveExperiment.PropertyChanged -= new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
91    }
92
93    private void RegisterHiveJobEvents() {
94      Content.HiveExperiment.HiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
95      Content.HiveExperiment.HiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
96      Content.HiveExperiment.HiveJobs.CollectionReset += new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
97    }
98    private void DeregisterHiveJobEvents() {
99      Content.HiveExperiment.HiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
100      Content.HiveExperiment.HiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
101      Content.HiveExperiment.HiveJobs.CollectionReset -= new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset);
102    }
103
104    protected override void OnContentChanged() {
105      base.OnContentChanged();
106      if (Content == null) {
107        nameTextBox.Text = string.Empty;
108        executionTimeTextBox.Text = string.Empty;
109        resourceNamesTextBox.Text = string.Empty;
110        isPrivilegedCheckBox.Checked = false;
111        logView.Content = null;
112        refreshAutomaticallyCheckBox.Checked = false;
113        logView.Content = null;
114      } else {
115        nameTextBox.Text = Content.HiveExperiment.Name;
116        executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
117        resourceNamesTextBox.Text = Content.HiveExperiment.ResourceNames;
118        isPrivilegedCheckBox.Checked = Content.HiveExperiment.IsPrivileged;
119        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
120        logView.Content = Content.Log;
121      }
122      hiveExperimentPermissionListView.Content = null; // has to be filled by refresh button
123      Content_JobStatisticsChanged(this, EventArgs.Empty);
124      Content_HiveExperimentChanged(this, EventArgs.Empty);
125      Content_HiveJobChanged(this, EventArgs.Empty);
126      Content_IsProgressingChanged(this, EventArgs.Empty);
127      Content_StateLogListChanged(this, EventArgs.Empty);
128      HiveExperiment_PropertyChanged(this, new PropertyChangedEventArgs("Id"));
129      SetEnabledStateOfControls();
130    }
131
132    protected override void SetEnabledStateOfControls() {
133      base.SetEnabledStateOfControls();
134      executionTimeTextBox.Enabled = Content != null;
135      jobsTextBox.ReadOnly = true;
136      calculatingTextBox.ReadOnly = true;
137      finishedTextBox.ReadOnly = true;
138
139      if (Content != null) {
140        bool alreadyUploaded = Content.Id != Guid.Empty;
141        bool jobsLoaded = Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.All(x => x.Job.Id != Guid.Empty);
142
143        this.nameTextBox.ReadOnly = !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
144        this.resourceNamesTextBox.ReadOnly = !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
145        this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
146
147        this.isPrivilegedCheckBox.Enabled = Content.IsControllable && !(Content.HiveExperiment.ExecutionState != ExecutionState.Prepared || alreadyUploaded); // TODO: check if user has the rights to do this       
148        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
149        this.refreshButton.Enabled = Content.IsControllable && alreadyUploaded;
150        this.Locked = !Content.IsControllable || Content.HiveExperiment.ExecutionState == ExecutionState.Started;
151
152      }
153      SetEnabledStateOfExecutableButtons();
154    }
155
156    protected override void OnClosed(FormClosedEventArgs e) {
157      if (Content != null) {
158        if (Content.RefreshAutomatically)
159          Content.StopResultPolling();
160      }
161      base.OnClosed(e);
162    }
163
164    #region Content Events
165    private void HiveJobs_ItemsAdded_Removed_Reset(object sender, CollectionItemsChangedEventArgs<HiveJob> e) {
166      if (InvokeRequired)
167        Invoke(new CollectionItemsChangedEventHandler<HiveJob>(HiveJobs_ItemsAdded_Removed_Reset), sender, e);
168      else {
169        SetEnabledStateOfControls();
170      }
171    }
172    private void Content_ExecutionStateChanged(object sender, EventArgs e) {
173      if (InvokeRequired)
174        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
175      else
176        SetEnabledStateOfControls();
177    }
178    private void Content_Prepared(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Content_Prepared), sender, e);
181      else {
182        nameTextBox.Enabled = true;
183        Locked = false;
184        SetEnabledStateOfControls();
185      }
186    }
187    private void Content_Started(object sender, EventArgs e) {
188      if (InvokeRequired)
189        Invoke(new EventHandler(Content_Started), sender, e);
190      else {
191        nameTextBox.Enabled = false;
192        SetEnabledStateOfControls();
193      }
194    }
195    private void Content_Paused(object sender, EventArgs e) {
196      if (InvokeRequired)
197        Invoke(new EventHandler(Content_Paused), sender, e);
198      else {
199        nameTextBox.Enabled = true;
200        SetEnabledStateOfControls();
201      }
202    }
203    private void Content_Stopped(object sender, EventArgs e) {
204      if (InvokeRequired)
205        Invoke(new EventHandler(Content_Stopped), sender, e);
206      else {
207        nameTextBox.Enabled = true;
208        Locked = false;
209        SetEnabledStateOfControls();
210      }
211    }
212    private void Content_ExecutionTimeChanged(object sender, EventArgs e) {
213      if (InvokeRequired)
214        Invoke(new EventHandler(Content_ExecutionTimeChanged), sender, e);
215      else
216        executionTimeTextBox.Text = Content.HiveExperiment.ExecutionTime.ToString();
217    }
218    private void Content_RefreshAutomaticallyChanged(object sender, EventArgs e) {
219      if (InvokeRequired)
220        Invoke(new EventHandler(Content_RefreshAutomaticallyChanged), sender, e);
221      else {
222        refreshAutomaticallyCheckBox.Checked = Content.RefreshAutomatically;
223        SetEnabledStateOfControls();
224      }
225    }
226    private void Content_HiveJobChanged(object sender, EventArgs e) {
227      if (InvokeRequired)
228        Invoke(new EventHandler(Content_HiveJobChanged), sender, e);
229      else {
230        if (Content != null && Content.HiveExperiment.HiveJobs != null) {
231          jobsTreeView.Content = Content.HiveExperiment.HiveJobs;
232          RegisterHiveJobEvents();
233        } else {
234          jobsTreeView.Content = null;
235        }
236        SetEnabledStateOfControls();
237      }
238    }
239
240    private void Content_HiveExperimentChanged(object sender, EventArgs e) {
241      if (Content.HiveExperiment != null) {
242        RegisterHiveExperimentEvents();
243        Content_IsProgressingChanged(sender, e);
244      }
245    }
246    private void Content_IsControllableChanged(object sender, EventArgs e) {
247      SetEnabledStateOfControls();
248    }
249    private void Content_JobStatisticsChanged(object sender, EventArgs e) {
250      if (InvokeRequired)
251        Invoke(new EventHandler(Content_JobStatisticsChanged), sender, e);
252      else {
253        if (Content != null) {
254          jobsTextBox.Text = Content.HiveExperiment.JobCount.ToString();
255          calculatingTextBox.Text = Content.HiveExperiment.CalculatingCount.ToString();
256          finishedTextBox.Text = Content.HiveExperiment.FinishedCount.ToString();
257        } else {
258          jobsTextBox.Text = "0";
259          calculatingTextBox.Text = "0";
260          finishedTextBox.Text = "0";
261        }
262      }
263    }
264    private void Content_ExceptionOccured(object sender, EventArgs<Exception> e) {
265      if (Content.IsControllable) {
266        // show error dialog only if Controllable (otherwise it should continue trying without an error dialog (e.g. HiveEngine))
267        ErrorHandling.ShowErrorDialog(this, e.Value);
268      }
269    }
270    private void Content_StateLogListChanged(object sender, EventArgs e) {
271      if (InvokeRequired)
272        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
273      else {
274        UpdateStateLogList();
275      }
276    }
277
278    private void UpdateStateLogList() {
279      if (Content != null && this.Content.HiveExperiment != null) {
280        stateLogViewHost.Content = this.Content.HiveExperiment.StateLogList;
281      } else {
282        stateLogViewHost.Content = null;
283      }
284    }
285
286    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
287      if(this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.HiveExperiment.Id;
288    }
289    #endregion
290
291    #region Control events
292    private void startButton_Click(object sender, EventArgs e) {
293      HiveClient.StartExperiment((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
294    }
295    private void pauseButton_Click(object sender, EventArgs e) {
296      HiveClient.PauseExperiment(Content.HiveExperiment);
297    }
298    private void stopButton_Click(object sender, EventArgs e) {
299      HiveClient.StopExperiment(Content.HiveExperiment);
300    }
301    private void resetButton_Click(object sender, EventArgs e) { }
302
303    private void nameTextBox_Validated(object sender, EventArgs e) {
304      if (Content.HiveExperiment.Name != nameTextBox.Text)
305        Content.HiveExperiment.Name = nameTextBox.Text;
306    }
307
308    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
309      if (Content.HiveExperiment.ResourceNames != resourceNamesTextBox.Text)
310        Content.HiveExperiment.ResourceNames = resourceNamesTextBox.Text;
311    }
312
313    private void refreshAutomaticallyCheckBox_Validated(object sender, EventArgs e) {
314      if (Content != null) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
315    }
316
317    private void isPrivilegedCheckBox_Validated(object sender, EventArgs e) {
318      if (Content != null) Content.HiveExperiment.IsPrivileged = isPrivilegedCheckBox.Checked;
319    }
320
321    private void refreshButton_Click(object sender, EventArgs e) {
322      var invoker = new Action<HiveExperiment>(HiveClient.LoadExperiment);
323      invoker.BeginInvoke(Content.HiveExperiment, (ar) => {
324        try {
325          invoker.EndInvoke(ar);
326        }
327        catch (Exception ex) {
328          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
329        }
330      }, null);
331    }
332
333    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
334      hiveExperimentPermissionListView.Content = HiveClient.GetHiveExperimentPermissions(this.Content.HiveExperiment.Id);
335    }
336
337
338    #endregion
339
340    #region Helpers
341    private void SetEnabledStateOfExecutableButtons() {
342      if (Content == null) {
343        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
344      } else {
345        startButton.Enabled = Content.IsControllable && Content.HiveExperiment.HiveJobs != null && Content.HiveExperiment.HiveJobs.Count > 0 && Content.HiveExperiment.ExecutionState == ExecutionState.Prepared;
346        pauseButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
347        stopButton.Enabled = Content.IsControllable && Content.HiveExperiment.ExecutionState == ExecutionState.Started;
348        resetButton.Enabled = false;
349      }
350    }
351    #endregion
352
353    #region Progress reporting
354    private void Content_IsProgressingChanged(object sender, EventArgs e) {
355      if (this.InvokeRequired) {
356        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
357      } else {
358        if (Content != null && Content.HiveExperiment.IsProgressing) {
359          SetProgressView();
360        } else {
361          FinishProgressView();
362        }
363      }
364    }
365
366    private void SetProgressView() {
367      if (progressView == null) {
368        progressView = new ProgressView(this, Content.HiveExperiment.Progress);
369      }
370      progressView.Progress = Content.HiveExperiment.Progress;
371    }
372
373    private void FinishProgressView() {
374      if (progressView != null) {
375        progressView.Finish();
376        progressView = null;
377        SetEnabledStateOfControls();
378      }
379    }
380    #endregion
381
382    #region Drag & Drop
383    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
384      jobsTreeView_DragEnter(sender, e);
385    }
386
387    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
388      e.Effect = DragDropEffects.None;
389      var obj = e.Data.GetData(Constants.DragDropDataFormat);
390      if (obj is IOptimizer) {
391        e.Effect = DragDropEffects.Copy;
392      }
393    }
394
395    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
396      if (e.Effect != DragDropEffects.None) {
397        var obj = e.Data.GetData(Constants.DragDropDataFormat);
398
399        var optimizer = obj as IOptimizer;
400        if (optimizer != null) {
401          Content.HiveExperiment.HiveJobs.Add(new OptimizerHiveJob(optimizer));
402        }
403      }
404    }
405    #endregion
406  }
407}
Note: See TracBrowser for help on using the repository browser.