Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.MainForm.WindowsForms/3.3/Progress.cs @ 17607

Last change on this file since 17607 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 9.5 KB
RevLine 
[7582]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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
22using System;
[15477]23using System.Windows.Forms;
24using HeuristicLab.Common;
[7582]25
26namespace HeuristicLab.MainForm {
27  public class Progress : IProgress {
[15415]28    private ProgressState progressState;
29    public ProgressState ProgressState {
30      get { return progressState; }
31      private set {
32        if (progressState != value) {
33          progressState = value;
34          OnProgressStateChanged();
35        }
36      }
37    }
38
39    private string message;
40    public string Message {
41      get { return message; }
[7582]42      set {
[15415]43        if (message != value) {
44          message = value;
45          OnMessageChanged();
[7582]46        }
47      }
48    }
49
[16317]50    private ProgressMode progressMode;
51    public ProgressMode ProgressMode {
52      get { return progressMode; }
[15415]53      set {
[16317]54        if (progressMode != value) {
55          progressMode = value;
[15415]56          OnProgressBarModeChanged();
57        }
58      }
59    }
60
[7582]61    private double progressValue;
62    public double ProgressValue {
[8165]63      get { return progressValue; }
[7582]64      set {
[16317]65        if (progressMode == ProgressMode.Indeterminate)
66          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Indeterminate-Mode");
[8165]67        if (progressValue != value) {
[15415]68          progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
[7582]69          OnProgressChanged();
70        }
71      }
72    }
73
[15415]74    private bool canBeStopped;
75    public bool CanBeStopped {
76      get { return canBeStopped; }
77      set {
78        if (canBeStopped != value) {
79          canBeStopped = value;
80          OnCanBeStoppedChanged();
81        }
82      }
83    }
84
[8165]85    private bool canBeCanceled;
86    public bool CanBeCanceled {
87      get { return canBeCanceled; }
88      set {
89        if (canBeCanceled != value) {
90          canBeCanceled = value;
91          OnCanBeCanceledChanged();
92        }
93      }
[7582]94    }
95
[8165]96    public Progress() {
[9893]97      progressState = ProgressState.Finished;
[15415]98      canBeStopped = false;
[9893]99      canBeCanceled = false;
[16317]100      progressMode = ProgressMode.Indeterminate;
[15415]101      progressValue = 0.0;
[8145]102    }
[15415]103
[16317]104    public void Start(string message, ProgressMode mode = ProgressMode.Determinate) {
[15415]105      ProgressState = ProgressState.Started;
[16317]106      ProgressMode = mode;
107      if (mode == ProgressMode.Determinate)
108        ProgressValue = 0.0;
[15415]109      Message = message;
[15400]110    }
[8145]111
[8165]112    public void Finish() {
[16317]113      if (ProgressMode == ProgressMode.Determinate && ProgressValue != 1.0)
[15415]114        ProgressValue = 1.0;
[8165]115      ProgressState = ProgressState.Finished;
[7582]116    }
117
[15415]118    public void Stop() {
119      if (canBeStopped) {
[15417]120        ProgressState = ProgressState.StopRequested;
[15415]121        OnStopRequested();
122      } else throw new NotSupportedException("This progress cannot be stopped.");
[9893]123    }
[15415]124    public void Cancel() {
125      if (canBeCanceled) {
[15417]126        ProgressState = ProgressState.CancelRequested;
[15415]127        OnCancelRequested();
128      } else throw new NotSupportedException("This progress cannot be canceled.");
[9893]129    }
130
[16318]131    #region Show and Hide Progress
[15477]132    /// <summary>
[16317]133    /// Shows a started Progress on all Views of the specified content.
[15477]134    /// </summary>
[16318]135    public static IProgress Show(IContent content, string progressMessage, ProgressMode mode = ProgressMode.Determinate, Action stopRequestHandler = null, Action cancelRequestHandler = null, bool addToObjectGraphObjects = true) {
136      var progress = CreateAndStartProgress(progressMessage, mode, stopRequestHandler, cancelRequestHandler);
137      Show(content, progress, addToObjectGraphObjects);
[15477]138      return progress;
139    }
140
141    /// <summary>
[16317]142    /// Shows a started Progress on the specified view.
[15477]143    /// </summary>
[16318]144    public static IProgress Show(IView view, string progressMessage, ProgressMode mode = ProgressMode.Determinate, Action stopRequestHandler = null, Action cancelRequestHandler = null) {
145      var progress = CreateAndStartProgress(progressMessage, mode, stopRequestHandler, cancelRequestHandler);
146      Show(view, progress);
[15477]147      return progress;
148    }
149    /// <summary>
[16317]150    /// Shows a started Progress on the specified control.
[15477]151    /// </summary>
[16318]152    /// <remarks>For backwards compatibility. Use Progress.Show(IView, ...) if possible.</remarks>
153    public static IProgress ShowOnControl(Control control, string progressMessage, ProgressMode mode = ProgressMode.Determinate, Action stopRequestHandler = null, Action cancelRequestHandler = null) {
154      var progress = CreateAndStartProgress(progressMessage, mode, stopRequestHandler, cancelRequestHandler);
155      ShowOnControl(control, progress);
156      return progress;
157    }
158
159    private static IProgress CreateAndStartProgress(string progressMessage, ProgressMode mode, Action stopRequestHandler, Action cancelRequestHandler) {
[15477]160      var progress = new Progress();
[16318]161      if (stopRequestHandler != null) {
162        progress.CanBeStopped = true;
163        progress.StopRequested += (s, a) => stopRequestHandler();
164      }
165      if (cancelRequestHandler != null) {
166        progress.CanBeCanceled = true;
167        progress.CancelRequested += (s, a) => cancelRequestHandler();
168      }
[16317]169      progress.Start(progressMessage, mode);
[15477]170      return progress;
171    }
172
173    /// <summary>
[16318]174    /// Shows an existing progress on all Views of the specified content.
[15477]175    /// </summary>
[16318]176    public static IProgress Show(IContent content, IProgress progress, bool addToObjectGraphObjects = true) {
177      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToContent(content, progress, addToObjectGraphObjects);
178      return progress;
[15477]179    }
180    /// <summary>
[16318]181    /// Shows an existing progress on the specified View.
[15477]182    /// </summary>
[16318]183    public static IProgress Show(IView view, IProgress progress) {
184      return ShowOnControl((Control)view, progress);
[15477]185    }
[16317]186    /// <summary>
[16318]187    /// Shows an existing progress on the specified control.
[16317]188    /// </summary>
[16318]189    /// <remarks>For backwards compatibility. Use Progress.Show(IView, ...) if possible.</remarks>
190    public static IProgress ShowOnControl(Control control, IProgress progress) {
[16317]191      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToControl(control, progress);
[16314]192      return progress;
193    }
194
[16318]195    /// <summary>
196    /// Hides the Progress from all Views of the specified content.
197    /// </summary>
198    public static void Hide(IContent content, bool finishProgress = true) {
[16314]199      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromContent(content, finishProgress);
200    }
[16318]201    /// <summary>
202    /// Hides the Progress from the specified view.
203    /// </summary>
204    public static void Hide(IView view, bool finishProgress = true) {
205      HideFromControl((Control)view, finishProgress);
[16314]206    }
[16318]207    /// <summary>
208    /// Hides the Progress from the specified control.
209    /// </summary>
210    /// <remarks>For backwards compatibility. Use Progress.Hide(IView) if possible.</remarks>
211    public static void HideFromControl(Control control, bool finishProgress = true) {
[16317]212      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromControl(control, finishProgress);
213    }
[16314]214    #endregion
215
[8165]216    #region Event Handler
[15415]217    public event EventHandler ProgressStateChanged;
218    private void OnProgressStateChanged() {
219      var handler = ProgressStateChanged;
[9893]220      if (handler != null) handler(this, EventArgs.Empty);
[7582]221    }
222
[15415]223    public event EventHandler MessageChanged;
224    private void OnMessageChanged() {
225      var handler = MessageChanged;
226      if (handler != null) handler(this, EventArgs.Empty);
227    }
228
229    public event EventHandler ProgressBarModeChanged;
230    private void OnProgressBarModeChanged() {
231      var handler = ProgressBarModeChanged;
232      if (handler != null) handler(this, EventArgs.Empty);
233    }
234
[7582]235    public event EventHandler ProgressValueChanged;
236    private void OnProgressChanged() {
237      var handler = ProgressValueChanged;
[9893]238      if (handler != null) handler(this, EventArgs.Empty);
[7582]239    }
[8165]240
[15415]241    public event EventHandler CanBeStoppedChanged;
242    private void OnCanBeStoppedChanged() {
243      var handler = CanBeStoppedChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
245    }
246
[8165]247    public event EventHandler CanBeCanceledChanged;
248    private void OnCanBeCanceledChanged() {
249      var handler = CanBeCanceledChanged;
[9893]250      if (handler != null) handler(this, EventArgs.Empty);
[8165]251    }
252
[15415]253    public event EventHandler StopRequested;
254    private void OnStopRequested() {
255      var handler = StopRequested;
256      if (handler != null) handler(this, EventArgs.Empty);
257    }
258
[9894]259    public event EventHandler CancelRequested;
260    private void OnCancelRequested() {
[8165]261      var handler = CancelRequested;
[15415]262      if (handler != null) handler(this, EventArgs.Empty);
[8165]263    }
[7582]264    #endregion
265  }
266}
Note: See TracBrowser for help on using the repository browser.