Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.Azure/HeuristicLab.Clients.Hive.Views/3.3/HiveTasks/OptimizerHiveTaskView.cs @ 7669

Last change on this file since 7669 was 7669, checked in by spimming, 12 years ago

#1680: merged changes from trunk into branch

File size: 5.7 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
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    }
47
48    protected override void Job_ItemChanged(object sender, EventArgs e) {
49      if (Content != null && Content.Task != null && Content.ItemTask.Item != null) {
50        runCollectionViewHost.Content = Content.ItemTask.Item.Runs;
51      } else {
52        runCollectionViewHost.Content = null;
53      }
54    }
55    #region Content Events
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.IsControllableChanged += new EventHandler(Content_IsControllableChanged);
59    }
60
61    protected override void DeregisterContentEvents() {
62      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
63      base.DeregisterContentEvents();
64    }
65
66    protected virtual void Content_IsControllableChanged(object sender, EventArgs e) {
67      SetEnabledStateOfControls();
68    }
69
70    #endregion
71
72    #region Child Control Events
73    private void restartButton_Click(object sender, EventArgs e) {
74      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
75      task.ContinueWith((t) => {
76        FinishProgressView();
77        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
78      }, TaskContinuationOptions.OnlyOnFaulted);
79    }
80
81    private void pauseButton_Click(object sender, EventArgs e) {
82      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
83      task.ContinueWith((t) => {
84        FinishProgressView();
85        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
86      }, TaskContinuationOptions.OnlyOnFaulted);
87    }
88
89    private void stopButton_Click(object sender, EventArgs e) {
90      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
91      task.ContinueWith((t) => {
92        FinishProgressView();
93        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
94      }, TaskContinuationOptions.OnlyOnFaulted);
95    }
96    #endregion
97
98    private void PauseTaskAsync() {
99      IProgress prog = new Progress();
100      prog.Status = "Pausing task. Please be patient for the command to take effect.";
101      SetProgressView(prog);
102      Content.Pause();
103      FinishProgressView();
104    }
105
106    private void StopTaskAsync() {
107      IProgress prog = new Progress();
108      prog.Status = "Stopping task. Please be patient for the command to take effect.";
109      SetProgressView(prog);
110      Content.Stop();
111      FinishProgressView();
112    }
113
114    private void ResumeTaskAsync() {
115      IProgress prog = new Progress();
116      prog.Status = "Resuming task. Please be patient for the command to take effect.";
117      SetProgressView(prog);
118      Content.Restart();
119      FinishProgressView();
120    }
121
122    private void SetProgressView(IProgress progress) {
123      if (InvokeRequired) {
124        Invoke(new Action<IProgress>(SetProgressView), progress);
125      } else {
126        if (progressView == null) {
127          progressView = new ProgressView(this, progress);
128        } else {
129          progressView.Progress = progress;
130        }
131      }
132    }
133
134    private void FinishProgressView() {
135      if (InvokeRequired) {
136        Invoke(new Action(FinishProgressView));
137      } else {
138        if (progressView != null) {
139          progressView.Finish();
140          progressView = null;
141          SetEnabledStateOfControls();
142        }
143      }
144    }
145
146    protected override void SetEnabledStateOfControls() {
147      base.SetEnabledStateOfControls();
148
149      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);
150      this.pauseButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && Content.Task.State == TaskState.Calculating;
151      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);
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.