Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.ExperimentManager/3.3/Views/RefreshableHiveJobView.cs @ 6792

Last change on this file since 6792 was 6792, checked in by ascheibe, 13 years ago

#1233 Hive Experiment Manager is now called Hive Job Manager

File size: 17.6 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.HiveJobsChanged += new EventHandler(Content_HiveJobChanged);
66      Content.ExecutionStateChanged += new EventHandler(Content_ExecutionStateChanged);
67      Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
68      Content.IsAllowedPrivilegedChanged += new EventHandler(Content_IsAllowedPrivilegedChanged);
69    }
70
71    protected override void DeregisterContentEvents() {
72      Content.RefreshAutomaticallyChanged -= new EventHandler(Content_RefreshAutomaticallyChanged);
73      Content.JobChanged -= new EventHandler(Content_HiveExperimentChanged);
74      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
75      Content.JobStatisticsChanged -= new EventHandler(Content_JobStatisticsChanged);
76      Content.ExceptionOccured -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccured);
77      Content.StateLogListChanged -= new EventHandler(Content_StateLogListChanged);
78      Content.IsProgressingChanged -= new EventHandler(Content_IsProgressingChanged);
79      Content.HiveJobsChanged -= new EventHandler(Content_HiveJobChanged);
80      Content.ExecutionStateChanged -= new EventHandler(Content_ExecutionStateChanged);
81      Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
82      base.DeregisterContentEvents();
83    }
84
85    private void RegisterHiveExperimentEvents() {
86      Content.Job.PropertyChanged += new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
87    }
88
89    private void DeregisterHiveExperimentEvents() {
90      Content.Job.PropertyChanged -= new PropertyChangedEventHandler(HiveExperiment_PropertyChanged);
91    }
92
93    private void RegisterHiveJobEvents() {
94      Content.HiveTasks.ItemsAdded += new CollectionItemsChangedEventHandler<HiveTask>(HiveJobs_ItemsAdded_Removed_Reset);
95      Content.HiveTasks.ItemsRemoved += new CollectionItemsChangedEventHandler<HiveTask>(HiveJobs_ItemsAdded_Removed_Reset);
96      Content.HiveTasks.CollectionReset += new CollectionItemsChangedEventHandler<HiveTask>(HiveJobs_ItemsAdded_Removed_Reset);
97    }
98    private void DeregisterHiveJobEvents() {
99      Content.HiveTasks.ItemsAdded -= new CollectionItemsChangedEventHandler<HiveTask>(HiveJobs_ItemsAdded_Removed_Reset);
100      Content.HiveTasks.ItemsRemoved -= new CollectionItemsChangedEventHandler<HiveTask>(HiveJobs_ItemsAdded_Removed_Reset);
101      Content.HiveTasks.CollectionReset -= new CollectionItemsChangedEventHandler<HiveTask>(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.Job.Name;
116        executionTimeTextBox.Text = Content.ExecutionTime.ToString();
117        resourceNamesTextBox.Text = Content.Job.ResourceNames;
118        isPrivilegedCheckBox.Checked = Content.Job.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.HiveTasks != null && Content.HiveTasks.All(x => x.Task.Id != Guid.Empty);
142
143        this.nameTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
144        this.resourceNamesTextBox.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
145        this.jobsTreeView.ReadOnly = !Content.IsControllable || Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded;
146
147        this.isPrivilegedCheckBox.Enabled = Content.IsAllowedPrivileged && Content.IsControllable && !(Content.ExecutionState != ExecutionState.Prepared || alreadyUploaded); // TODO: check if user has the rights to do this       
148        this.refreshAutomaticallyCheckBox.Enabled = Content.IsControllable && alreadyUploaded && jobsLoaded && Content.ExecutionState == ExecutionState.Started;
149        this.refreshButton.Enabled = Content.IsDownloadable && alreadyUploaded;
150        this.Locked = !Content.IsControllable || Content.ExecutionState == ExecutionState.Started;
151      }
152      SetEnabledStateOfExecutableButtons();
153      tabControl_SelectedIndexChanged(this, EventArgs.Empty); // ensure sharing tabpage is disabled
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<HiveTask> e) {
166      if (InvokeRequired)
167        Invoke(new CollectionItemsChangedEventHandler<HiveTask>(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.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.HiveTasks != null) {
231          jobsTreeView.Content = Content.HiveTasks;
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.Job != 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.Job.JobCount - Content.Job.CalculatingCount - Content.Job.FinishedCount).ToString();
255          calculatingTextBox.Text = Content.Job.CalculatingCount.ToString();
256          finishedTextBox.Text = Content.Job.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      // don't show exception, it is logged anyway
266    }
267    private void Content_StateLogListChanged(object sender, EventArgs e) {
268      if (InvokeRequired)
269        Invoke(new EventHandler(Content_StateLogListChanged), sender, e);
270      else {
271        UpdateStateLogList();
272      }
273    }
274    private void Content_IsAllowedPrivilegedChanged(object sender, EventArgs e) {
275      if (InvokeRequired)
276        Invoke(new EventHandler(Content_IsAllowedPrivilegedChanged), sender, e);
277      else {
278        SetEnabledStateOfControls();
279      }
280    }
281
282
283    private void UpdateStateLogList() {
284      if (Content != null && this.Content.Job != null) {
285        stateLogViewHost.Content = this.Content.StateLogList;
286      } else {
287        stateLogViewHost.Content = null;
288      }
289    }
290
291    private void HiveExperiment_PropertyChanged(object sender, PropertyChangedEventArgs e) {
292      if (this.Content != null && e.PropertyName == "Id") this.hiveExperimentPermissionListView.HiveExperimentId = this.Content.Job.Id;
293    }
294    #endregion
295
296    #region Control events
297    private void startButton_Click(object sender, EventArgs e) {
298      HiveClient.StartJob((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Start failed.", ex), Content, new CancellationToken());
299    }
300    private void pauseButton_Click(object sender, EventArgs e) {
301      HiveClient.PauseJob(Content);
302    }
303    private void stopButton_Click(object sender, EventArgs e) {
304      HiveClient.StopJob(Content);
305    }
306    private void resetButton_Click(object sender, EventArgs e) { }
307
308    private void nameTextBox_Validated(object sender, EventArgs e) {
309      if (Content.Job.Name != nameTextBox.Text)
310        Content.Job.Name = nameTextBox.Text;
311    }
312
313    private void resourceNamesTextBox_Validated(object sender, EventArgs e) {
314      if (Content.Job.ResourceNames != resourceNamesTextBox.Text)
315        Content.Job.ResourceNames = resourceNamesTextBox.Text;
316    }
317
318    private void refreshAutomaticallyCheckBox_Validated(object sender, EventArgs e) {
319      if (Content != null) Content.RefreshAutomatically = refreshAutomaticallyCheckBox.Checked;
320    }
321
322    private void isPrivilegedCheckBox_Validated(object sender, EventArgs e) {
323      if (Content != null) Content.Job.IsPrivileged = isPrivilegedCheckBox.Checked;
324    }
325
326    private void refreshButton_Click(object sender, EventArgs e) {
327      var invoker = new Action<RefreshableJob>(HiveClient.LoadJob);
328      invoker.BeginInvoke(Content, (ar) => {
329        try {
330          invoker.EndInvoke(ar);
331        }
332        catch (Exception ex) {
333          ThreadPool.QueueUserWorkItem(delegate(object exception) { ErrorHandling.ShowErrorDialog(this, (Exception)exception); }, ex);
334        }
335      }, null);
336    }
337
338    private void refreshPermissionsButton_Click(object sender, EventArgs e) {
339      hiveExperimentPermissionListView.Content = HiveClient.GetJobPermissions(this.Content.Job.Id);
340    }
341
342
343    #endregion
344
345    #region Helpers
346    private void SetEnabledStateOfExecutableButtons() {
347      if (Content == null) {
348        startButton.Enabled = pauseButton.Enabled = stopButton.Enabled = resetButton.Enabled = false;
349      } else {
350        startButton.Enabled = Content.IsControllable && Content.HiveTasks != null && Content.HiveTasks.Count > 0 && Content.ExecutionState == ExecutionState.Prepared;
351        pauseButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
352        stopButton.Enabled = Content.IsControllable && Content.ExecutionState == ExecutionState.Started;
353        resetButton.Enabled = false;
354      }
355    }
356    #endregion
357
358    #region Progress reporting
359    private void Content_IsProgressingChanged(object sender, EventArgs e) {
360      if (this.InvokeRequired) {
361        Invoke(new EventHandler(Content_IsProgressingChanged), sender, e);
362      } else {
363        if (Content != null && Content.IsProgressing) {
364          SetProgressView();
365        } else {
366          FinishProgressView();
367        }
368      }
369    }
370
371    private void SetProgressView() {
372      if (progressView == null) {
373        progressView = new ProgressView(this, Content.Progress);
374      }
375      progressView.Progress = Content.Progress;
376    }
377
378    private void FinishProgressView() {
379      if (progressView != null) {
380        progressView.Finish();
381        progressView = null;
382        SetEnabledStateOfControls();
383      }
384    }
385    #endregion
386
387    #region Drag & Drop
388    private void jobsTreeView_DragOver(object sender, DragEventArgs e) {
389      jobsTreeView_DragEnter(sender, e);
390    }
391
392    private void jobsTreeView_DragEnter(object sender, DragEventArgs e) {
393      e.Effect = DragDropEffects.None;
394      var obj = e.Data.GetData(Constants.DragDropDataFormat);
395      if (obj is IOptimizer) {
396        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
397        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
398      }
399    }
400
401    private void jobsTreeView_DragDrop(object sender, DragEventArgs e) {
402      if (e.Effect != DragDropEffects.None) {
403        var obj = e.Data.GetData(Constants.DragDropDataFormat);
404
405        var optimizer = obj as IOptimizer;
406        if (optimizer != null) {
407          Content.HiveTasks.Add(new OptimizerHiveTask(e.Effect.HasFlag(DragDropEffects.Copy) ? (IOptimizer)optimizer.Clone() : optimizer));
408        }
409      }
410    }
411    #endregion
412
413    private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
414      if (tabControl.SelectedTab == permissionTabPage) {
415        if (!Content.IsSharable) {
416          MessageBox.Show("Unable to load tab. You have insufficient access privileges.");
417          tabControl.SelectedTab = tasksTabPage;
418        }
419      }
420    }
421  }
422}
Note: See TracBrowser for help on using the repository browser.