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