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

Last change on this file since 6976 was 6976, checked in by ascheibe, 9 years ago

#1672 integrate the Hive client projects into trunk (Hive Job Manager and Administrator)

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