Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8165 was 8165, checked in by abeham, 12 years ago

#1762: Some changes to progress handling, see the ticket for more details

File size: 5.2 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 Progress progress;
34
35    public new OptimizerHiveTask Content {
36      get { return (OptimizerHiveTask)base.Content; }
37      set {
38        if (base.Content != value) {
39          base.Content = value;
40        }
41      }
42    }
43
44    public OptimizerHiveTaskView() {
45      InitializeComponent();
46      progress = new Progress() {
47        CanBeCanceled = false
48      };
49    }
50
51    protected override void Job_ItemChanged(object sender, EventArgs e) {
52      if (Content != null && Content.Task != null && Content.ItemTask.Item != null) {
53        runCollectionViewHost.Content = Content.ItemTask.Item.Runs;
54      } else {
55        runCollectionViewHost.Content = null;
56      }
57    }
58    #region Content Events
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
62    }
63
64    protected override void DeregisterContentEvents() {
65      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
66      base.DeregisterContentEvents();
67    }
68
69    protected virtual void Content_IsControllableChanged(object sender, EventArgs e) {
70      SetEnabledStateOfControls();
71    }
72
73    #endregion
74
75    #region Child Control Events
76    private void restartButton_Click(object sender, EventArgs e) {
77      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
78      task.ContinueWith((t) => {
79        progress.Finish();
80        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
81      }, TaskContinuationOptions.OnlyOnFaulted);
82    }
83
84    private void pauseButton_Click(object sender, EventArgs e) {
85      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
86      task.ContinueWith((t) => {
87        progress.Finish();
88        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
89      }, TaskContinuationOptions.OnlyOnFaulted);
90    }
91
92    private void stopButton_Click(object sender, EventArgs e) {
93      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
94      task.ContinueWith((t) => {
95        progress.Finish();
96        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
97      }, TaskContinuationOptions.OnlyOnFaulted);
98    }
99    #endregion
100
101    private void PauseTaskAsync() {
102      progress.Status = "Pausing task. Please be patient for the command to take effect.";
103      progress.ProgressState = ProgressState.Started;
104      using (var view = new ProgressView(this, progress)) {
105        Content.Pause();
106      }
107    }
108
109    private void StopTaskAsync() {
110      progress.Status = "Stopping task. Please be patient for the command to take effect.";
111      progress.ProgressState = ProgressState.Started;
112      using (var view = new ProgressView(this, progress)) {
113        Content.Stop();
114      }
115    }
116
117    private void ResumeTaskAsync() {
118      progress.Status = "Resuming task. Please be patient for the command to take effect.";
119      progress.ProgressState = ProgressState.Started;
120      using (var view = new ProgressView(this, progress)) {
121        Content.Restart();
122      }
123    }
124
125    protected override void SetEnabledStateOfControls() {
126      base.SetEnabledStateOfControls();
127
128      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);
129      this.pauseButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && Content.Task.State == TaskState.Calculating;
130      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);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.