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

Last change on this file since 9894 was 9894, checked in by ascheibe, 8 years ago

#1042

  • changed Hive views to use MainForm for progress handling
  • removed Cancel timeout
  • setter for ProgressState is now private
  • added Start methods to Progress
  • removed unused methods from RefreshableHiveJobView
File size: 5.2 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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;
[7582]23using System.Threading.Tasks;
[6976]24using System.Windows.Forms;
25using HeuristicLab.MainForm;
[7582]26using HeuristicLab.MainForm.WindowsForms;
[7156]27using HeuristicLab.PluginInfrastructure;
[6976]28
29namespace HeuristicLab.Clients.Hive.Views {
30  [View("OptimizerHiveTask View")]
31  [Content(typeof(OptimizerHiveTask), true)]
32  public partial class OptimizerHiveTaskView : HiveTaskView {
[8165]33    private Progress progress;
[9894]34   
[6976]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();
[9893]46      progress = new Progress();
[6976]47    }
48
49    protected override void Job_ItemChanged(object sender, EventArgs e) {
50      if (Content != null && Content.Task != null && Content.ItemTask.Item != null) {
[8939]51        Content.ExecuteReadActionOnItemTask(new Action(delegate() {
52          runCollectionViewHost.Content = Content.ItemTask.Item.Runs;
53        }));
[6976]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);
[9894]62      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress);
[6976]63    }
64
65    protected override void DeregisterContentEvents() {
66      Content.IsControllableChanged -= new EventHandler(Content_IsControllableChanged);
[9894]67      MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this, false);
[6976]68      base.DeregisterContentEvents();
69    }
70
71    protected virtual void Content_IsControllableChanged(object sender, EventArgs e) {
72      SetEnabledStateOfControls();
73    }
74
75    #endregion
76
77    #region Child Control Events
78    private void restartButton_Click(object sender, EventArgs e) {
[7156]79      var task = System.Threading.Tasks.Task.Factory.StartNew(ResumeTaskAsync);
80      task.ContinueWith((t) => {
[8165]81        progress.Finish();
[7156]82        ErrorHandling.ShowErrorDialog(this, "An error occured while resuming the task.", t.Exception);
[7582]83      }, TaskContinuationOptions.OnlyOnFaulted);
[6976]84    }
85
86    private void pauseButton_Click(object sender, EventArgs e) {
[7156]87      var task = System.Threading.Tasks.Task.Factory.StartNew(PauseTaskAsync);
88      task.ContinueWith((t) => {
[8165]89        progress.Finish();
[7156]90        ErrorHandling.ShowErrorDialog(this, "An error occured while pausing the task.", t.Exception);
91      }, TaskContinuationOptions.OnlyOnFaulted);
[6976]92    }
93
94    private void stopButton_Click(object sender, EventArgs e) {
[7156]95      var task = System.Threading.Tasks.Task.Factory.StartNew(StopTaskAsync);
96      task.ContinueWith((t) => {
[8165]97        progress.Finish();
[7156]98        ErrorHandling.ShowErrorDialog(this, "An error occured while stopping the task.", t.Exception);
99      }, TaskContinuationOptions.OnlyOnFaulted);
[6976]100    }
101    #endregion
102
[7156]103    private void PauseTaskAsync() {
[9893]104      progress.Start("Pausing task. Please be patient for the command to take effect.");
[8181]105      Content.Pause();
[8186]106      progress.Finish();
[7156]107    }
108
109    private void StopTaskAsync() {
[9893]110      progress.Start("Stopping task. Please be patient for the command to take effect.");
[8181]111      Content.Stop();
[8186]112      progress.Finish();
[7156]113    }
114
115    private void ResumeTaskAsync() {
[9893]116      progress.Start("Resuming task. Please be patient for the command to take effect.");
[8181]117      Content.Restart();
[8186]118      progress.Finish();
[7156]119    }
120
[6976]121    protected override void SetEnabledStateOfControls() {
122      base.SetEnabledStateOfControls();
123
124      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);
125      this.pauseButton.Enabled = Content != null && Content.IsControllable && !Content.Task.Command.HasValue && Content.Task.State == TaskState.Calculating;
126      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);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.