[7582] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7582] | 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;
|
---|
[8165] | 23 | using HeuristicLab.Common;
|
---|
[7582] | 24 |
|
---|
| 25 | namespace HeuristicLab.MainForm {
|
---|
| 26 | public class Progress : IProgress {
|
---|
| 27 | private string status;
|
---|
| 28 | public string Status {
|
---|
[8165] | 29 | get { return status; }
|
---|
[7582] | 30 | set {
|
---|
[8165] | 31 | if (status != value) {
|
---|
| 32 | status = value;
|
---|
[7582] | 33 | OnStatusChanged();
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | private double progressValue;
|
---|
| 39 | public double ProgressValue {
|
---|
[8165] | 40 | get { return progressValue; }
|
---|
[7582] | 41 | set {
|
---|
[8165] | 42 | if (progressValue != value) {
|
---|
| 43 | progressValue = value;
|
---|
[7582] | 44 | OnProgressChanged();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[8165] | 49 | private ProgressState progressState;
|
---|
| 50 | public ProgressState ProgressState {
|
---|
| 51 | get { return progressState; }
|
---|
| 52 | set {
|
---|
| 53 | if (progressState != value) {
|
---|
| 54 | progressState = value;
|
---|
| 55 | OnProgressStateChanged();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[7582] | 58 | }
|
---|
| 59 |
|
---|
[8165] | 60 | private bool canBeCanceled;
|
---|
| 61 | public bool CanBeCanceled {
|
---|
| 62 | get { return canBeCanceled; }
|
---|
| 63 | set {
|
---|
| 64 | if (canBeCanceled != value) {
|
---|
| 65 | canBeCanceled = value;
|
---|
| 66 | OnCanBeCanceledChanged();
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
[7582] | 69 | }
|
---|
| 70 |
|
---|
[8165] | 71 | public Progress() {
|
---|
[9893] | 72 | progressState = ProgressState.Finished;
|
---|
| 73 | canBeCanceled = false;
|
---|
[8145] | 74 | }
|
---|
[8165] | 75 | public Progress(string status)
|
---|
| 76 | : this() {
|
---|
| 77 | this.status = status;
|
---|
| 78 | }
|
---|
[9893] | 79 | public Progress(string status, ProgressState state)
|
---|
| 80 | : this() {
|
---|
| 81 | this.status = status;
|
---|
| 82 | this.progressState = state;
|
---|
[8165] | 83 | }
|
---|
[8145] | 84 |
|
---|
[9893] | 85 | public void Cancel(int timeoutMs = 0) {
|
---|
[8165] | 86 | if (canBeCanceled)
|
---|
| 87 | OnCancelRequested(timeoutMs);
|
---|
[8145] | 88 | }
|
---|
| 89 |
|
---|
[8165] | 90 | public void Finish() {
|
---|
| 91 | if (ProgressValue != 1.0) ProgressValue = 1.0;
|
---|
| 92 | ProgressState = ProgressState.Finished;
|
---|
[7582] | 93 | }
|
---|
| 94 |
|
---|
[9893] | 95 | public void Start() {
|
---|
| 96 | ProgressValue = 0.0;
|
---|
| 97 | ProgressState = ProgressState.Started;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void Start(string status) {
|
---|
| 101 | Start();
|
---|
| 102 | Status = status;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[8165] | 105 | #region Event Handler
|
---|
[7582] | 106 | public event EventHandler StatusChanged;
|
---|
| 107 | private void OnStatusChanged() {
|
---|
| 108 | var handler = StatusChanged;
|
---|
[9893] | 109 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[7582] | 110 | }
|
---|
| 111 |
|
---|
| 112 | public event EventHandler ProgressValueChanged;
|
---|
| 113 | private void OnProgressChanged() {
|
---|
| 114 | var handler = ProgressValueChanged;
|
---|
[9893] | 115 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[7582] | 116 | }
|
---|
[8165] | 117 |
|
---|
| 118 | public event EventHandler ProgressStateChanged;
|
---|
| 119 | private void OnProgressStateChanged() {
|
---|
| 120 | var handler = ProgressStateChanged;
|
---|
[9893] | 121 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[8165] | 122 | }
|
---|
| 123 |
|
---|
| 124 | public event EventHandler CanBeCanceledChanged;
|
---|
| 125 | private void OnCanBeCanceledChanged() {
|
---|
| 126 | var handler = CanBeCanceledChanged;
|
---|
[9893] | 127 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 128 |
|
---|
[8165] | 129 | }
|
---|
| 130 |
|
---|
| 131 | public event EventHandler<EventArgs<int>> CancelRequested;
|
---|
| 132 | private void OnCancelRequested(int timeoutMs) {
|
---|
| 133 | var handler = CancelRequested;
|
---|
[9893] | 134 | if (handler != null) throw new NotSupportedException("Cancel request was ignored.");
|
---|
| 135 | else handler(this, new EventArgs<int>(timeoutMs));
|
---|
[8165] | 136 | }
|
---|
[7582] | 137 | #endregion
|
---|
| 138 | }
|
---|
| 139 | }
|
---|