#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;
using System.Windows.Forms;
using HeuristicLab.Common;
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;
}
public void Start(string message, double progressValue = 0) {
ProgressState = ProgressState.Started;
ProgressBarMode = ProgressBarMode.Continuous;
ProgressValue = progressValue;
Message = message;
}
public void StartMarquee(string message) {
ProgressState = ProgressState.Started;
ProgressBarMode = ProgressBarMode.Marquee;
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 Show/Hide Progress
///
/// Shows a started Progress in Continuous-mode on all Views of the specified content.
///
public static IProgress Show(IContent content, string progressMessage, double initialProgressValue = 0, bool addToObjectGraphObjects = true) {
var progress = new Progress();
progress.Start(progressMessage, initialProgressValue);
MainFormManager.GetMainForm().AddProgressToContent(content, progress, addToObjectGraphObjects);
return progress;
}
///
/// Shows a started Progress in Marquee-mode on all Views of the specified content.
///
public static IProgress ShowMarquee(IContent content, string progressMessage, bool addToObjectGraphObjects = true) {
var progress = new Progress();
progress.StartMarquee(progressMessage);
MainFormManager.GetMainForm().AddProgressToContent(content, progress, addToObjectGraphObjects);
return progress;
}
///
/// Shows a started Progress in Continuous-mode on the specified view.
///
public static IProgress Show(Control control, string progressMessage, double initialProgressValue = 0) {
var progress = new Progress();
progress.Start(progressMessage, initialProgressValue);
MainFormManager.GetMainForm().AddProgressToView(control, progress);
return progress;
}
///
/// Shows a started Progress in Marquee-mode on the specified view.
///
public static IProgress ShowMarquee(Control control, string progressMessage) {
var progress = new Progress();
progress.StartMarquee(progressMessage);
MainFormManager.GetMainForm().AddProgressToView(control, progress);
return progress;
}
///
/// Hides the Progress fom all Views of the specified content.
///
public static void Hide(IContent content) {
MainFormManager.GetMainForm().RemoveProgressFromContent(content);
}
///
/// Hides the Progress fom the specified view.
///
public static void Hide(Control control) {
MainFormManager.GetMainForm().RemoveProgressFromView(control);
}
#endregion
#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
}
}