#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
namespace HeuristicLab.MainForm {
public class Progress : IProgress {
private ProgressState progressState;
public ProgressState ProgressState {
get { return progressState; }
private set {
if (progressState != value) {
progressState = value;
OnProgressStateChanged();
}
}
}
private string message;
public string Message {
get { return message; }
set {
if (message != value) {
message = value;
OnMessageChanged();
}
}
}
private ProgressBarMode progressBarMode;
public ProgressBarMode ProgressBarMode {
get { return progressBarMode; }
set {
if (progressBarMode != value) {
progressBarMode = value;
OnProgressBarModeChanged();
}
}
}
private double progressValue;
public double ProgressValue {
get { return progressValue; }
set {
if (progressBarMode == ProgressBarMode.Marquee)
throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Marquee-Mode");
if (progressValue != value) {
progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
OnProgressChanged();
}
}
}
private bool canBeStopped;
public bool CanBeStopped {
get { return canBeStopped; }
set {
if (canBeStopped != value) {
canBeStopped = value;
OnCanBeStoppedChanged();
}
}
}
private bool canBeCanceled;
public bool CanBeCanceled {
get { return canBeCanceled; }
set {
if (canBeCanceled != value) {
canBeCanceled = value;
OnCanBeCanceledChanged();
}
}
}
public Progress() {
progressState = ProgressState.Finished;
canBeStopped = false;
canBeCanceled = false;
progressBarMode = ProgressBarMode.Marquee;
progressValue = 0.0;
}
///
/// Starts or restarts an "undefined" progress in marquee-mode
///
public void Start(string message) {
ProgressState = ProgressState.Started;
ProgressBarMode = ProgressBarMode.Marquee;
Message = message;
}
///
/// Starts or restarts a progress in continuous-mode
///
public void Start(string message, double progressValue) {
ProgressState = ProgressState.Started;
ProgressBarMode = ProgressBarMode.Continuous;
ProgressValue = progressValue;
Message = message;
}
public void Finish() {
if (ProgressBarMode == ProgressBarMode.Continuous && ProgressValue != 1.0)
ProgressValue = 1.0;
ProgressState = ProgressState.Finished;
}
public void Stop() {
if (canBeStopped) {
ProgressState = ProgressState.StopRequested;
OnStopRequested();
} else throw new NotSupportedException("This progress cannot be stopped.");
}
public void Cancel() {
if (canBeCanceled) {
ProgressState = ProgressState.CancelRequested;
OnCancelRequested();
} else throw new NotSupportedException("This progress cannot be canceled.");
}
#region Event Handler
public event EventHandler ProgressStateChanged;
private void OnProgressStateChanged() {
var handler = ProgressStateChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler MessageChanged;
private void OnMessageChanged() {
var handler = MessageChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler ProgressBarModeChanged;
private void OnProgressBarModeChanged() {
var handler = ProgressBarModeChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler ProgressValueChanged;
private void OnProgressChanged() {
var handler = ProgressValueChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler CanBeStoppedChanged;
private void OnCanBeStoppedChanged() {
var handler = CanBeStoppedChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler CanBeCanceledChanged;
private void OnCanBeCanceledChanged() {
var handler = CanBeCanceledChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler StopRequested;
private void OnStopRequested() {
var handler = StopRequested;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler CancelRequested;
private void OnCancelRequested() {
var handler = CancelRequested;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
}
}