Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.Views/3.3/HiveTasks/OptimizerHiveTaskView.cs @ 8145

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

#1762 implemented review comments:

  • removed self disposing. The progress view now reacts if a progress is set and Finish() is now called on the progress object and not the view.
  • Moved Cancel event from ProgressView to Progress
  • throw ArgumentNullException if the parent view is null
File size: 5.5 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.Threading.Tasks;
24using System.Windows.Forms;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Clients.Hive.Views {
30  [View("OptimizerHiveTask View")]
31  [Content(typeof(OptimizerHiveTask), true)]
32  public partial class OptimizerHiveTaskView : HiveTaskView {
33    private ProgressView progressView;
34    private IProgress progress;
35
36    public new OptimizerHiveTask Content {
37      get { return (OptimizerHiveTask)base.Content; }
38      set {
39        if (base.Content != value) {
40          base.Content = value;
41        }
42      }
43    }
44
45    public OptimizerHiveTaskView() {
46      InitializeComponent();
47      progressView = new ProgressView(this);
48    }
49
50    protected override void Job_ItemChanged(object sender, EventArgs e) {
51      if (Content != null && Content.Task != null && Content.ItemTask.Item != null) {
52        runCollectionViewHost.Content = Content.ItemTask.Item.Runs;
53      } else {
54        runCollectionViewHost.Content = null;
55      }
56    }
57    #region Content Events
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
61    }
62
63    protected override void DeregisterContentEvents() {
64      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
65      base.DeregisterContentEvents();
66    }
67
68    protected virtual void Content_IsControllableChanged(object sender, EventArgs e) {
69      SetEnabledStateOfControls();
70    }
71
72    #endregion
73
74    #region Child Control Events
75    private void restartButton_Click(object sender, EventArgs e) {
76      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
77      task.ContinueWith((t) => {
78        FinishProgressView();
79        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
80      }, TaskContinuationOptions.OnlyOnFaulted);
81    }
82
83    private void pauseButton_Click(object sender, EventArgs e) {
84      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
85      task.ContinueWith((t) => {
86        FinishProgressView();
87        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
88      }, TaskContinuationOptions.OnlyOnFaulted);
89    }
90
91    private void stopButton_Click(object sender, EventArgs e) {
92      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
93      task.ContinueWith((t) => {
94        FinishProgressView();
95        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
96      }, TaskContinuationOptions.OnlyOnFaulted);
97    }
98    #endregion
99
100    private void PauseTaskAsync() {
101      progress = new Progress();
102      progress.Status = "Pausing task. Please be patient for the command to take effect.";
103      SetProgressView(progress);
104      Content.Pause();
105      FinishProgressView();
106    }
107
108    private void StopTaskAsync() {
109      progress = new Progress();
110      progress.Status = "Stopping task. Please be patient for the command to take effect.";
111      SetProgressView(progress);
112      Content.Stop();
113      FinishProgressView();
114    }
115
116    private void ResumeTaskAsync() {
117      progress = new Progress();
118      progress.Status = "Resuming task. Please be patient for the command to take effect.";
119      SetProgressView(progress);
120      Content.Restart();
121      FinishProgressView();
122    }
123
124    private void SetProgressView(IProgress progress) {
125      if (InvokeRequired) {
126        Invoke(new Action<IProgress>(SetProgressView), progress);
127      } else {
128        progressView.Progress = progress;
129      }
130    }
131
132    private void FinishProgressView() {
133      if (InvokeRequired) {
134        Invoke(new Action(FinishProgressView));
135      } else {
136        progress.Finish();
137        SetEnabledStateOfControls();
138      }
139    }
140
141    protected override void SetEnabledStateOfControls() {
142      base.SetEnabledStateOfControls();
143
144      this.restartButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && (Content.Task.State == TaskState.Paused || Content.Task.State == TaskState.Failed || Content.Task.State == TaskState.Aborted);
145      this.pauseButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && Content.Task.State == TaskState.Calculating;
146      this.stopButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && (Content.Task.State == TaskState.Calculating || Content.Task.State == TaskState.Waiting || Content.Task.State == TaskState.Paused);
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.