Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17092 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.MainForm {
27  public class Progress : IProgress {
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; }
42      set {
43        if (message != value) {
44          message = value;
45          OnMessageChanged();
46        }
47      }
48    }
49
50    private ProgressMode progressMode;
51    public ProgressMode ProgressMode {
52      get { return progressMode; }
53      set {
54        if (progressMode != value) {
55          progressMode = value;
56          OnProgressBarModeChanged();
57        }
58      }
59    }
60
61    private double progressValue;
62    public double ProgressValue {
63      get { return progressValue; }
64      set {
65        if (progressMode == ProgressMode.Indeterminate)
66          throw new InvalidOperationException("Cannot set ProgressValue while ProgressBar is in Indeterminate-Mode");
67        if (progressValue != value) {
68          progressValue = Math.Max(Math.Min(value, 1.0), 0.0);
69          OnProgressChanged();
70        }
71      }
72    }
73
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
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      }
94    }
95
96    public Progress() {
97      progressState = ProgressState.Finished;
98      canBeStopped = false;
99      canBeCanceled = false;
100      progressMode = ProgressMode.Indeterminate;
101      progressValue = 0.0;
102    }
103
104    public void Start(string message, ProgressMode mode = ProgressMode.Determinate) {
105      ProgressState = ProgressState.Started;
106      ProgressMode = mode;
107      if (mode == ProgressMode.Determinate)
108        ProgressValue = 0.0;
109      Message = message;
110    }
111
112    public void Finish() {
113      if (ProgressMode == ProgressMode.Determinate && ProgressValue != 1.0)
114        ProgressValue = 1.0;
115      ProgressState = ProgressState.Finished;
116    }
117
118    public void Stop() {
119      if (canBeStopped) {
120        ProgressState = ProgressState.StopRequested;
121        OnStopRequested();
122      } else throw new NotSupportedException("This progress cannot be stopped.");
123    }
124    public void Cancel() {
125      if (canBeCanceled) {
126        ProgressState = ProgressState.CancelRequested;
127        OnCancelRequested();
128      } else throw new NotSupportedException("This progress cannot be canceled.");
129    }
130
131    #region Show and Hide Progress
132    /// <summary>
133    /// Shows a started Progress on all Views of the specified content.
134    /// </summary>
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);
138      return progress;
139    }
140
141    /// <summary>
142    /// Shows a started Progress on the specified view.
143    /// </summary>
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);
147      return progress;
148    }
149    /// <summary>
150    /// Shows a started Progress on the specified control.
151    /// </summary>
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) {
160      var progress = new Progress();
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      }
169      progress.Start(progressMessage, mode);
170      return progress;
171    }
172
173    /// <summary>
174    /// Shows an existing progress on all Views of the specified content.
175    /// </summary>
176    public static IProgress Show(IContent content, IProgress progress, bool addToObjectGraphObjects = true) {
177      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToContent(content, progress, addToObjectGraphObjects);
178      return progress;
179    }
180    /// <summary>
181    /// Shows an existing progress on the specified View.
182    /// </summary>
183    public static IProgress Show(IView view, IProgress progress) {
184      return ShowOnControl((Control)view, progress);
185    }
186    /// <summary>
187    /// Shows an existing progress on the specified control.
188    /// </summary>
189    /// <remarks>For backwards compatibility. Use Progress.Show(IView, ...) if possible.</remarks>
190    public static IProgress ShowOnControl(Control control, IProgress progress) {
191      MainFormManager.GetMainForm<WindowsForms.MainForm>().AddProgressToControl(control, progress);
192      return progress;
193    }
194
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) {
199      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromContent(content, finishProgress);
200    }
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);
206    }
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) {
212      MainFormManager.GetMainForm<WindowsForms.MainForm>().RemoveProgressFromControl(control, finishProgress);
213    }
214    #endregion
215
216    #region Event Handler
217    public event EventHandler ProgressStateChanged;
218    private void OnProgressStateChanged() {
219      var handler = ProgressStateChanged;
220      if (handler != null) handler(this, EventArgs.Empty);
221    }
222
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
235    public event EventHandler ProgressValueChanged;
236    private void OnProgressChanged() {
237      var handler = ProgressValueChanged;
238      if (handler != null) handler(this, EventArgs.Empty);
239    }
240
241    public event EventHandler CanBeStoppedChanged;
242    private void OnCanBeStoppedChanged() {
243      var handler = CanBeStoppedChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
245    }
246
247    public event EventHandler CanBeCanceledChanged;
248    private void OnCanBeCanceledChanged() {
249      var handler = CanBeCanceledChanged;
250      if (handler != null) handler(this, EventArgs.Empty);
251    }
252
253    public event EventHandler StopRequested;
254    private void OnStopRequested() {
255      var handler = StopRequested;
256      if (handler != null) handler(this, EventArgs.Empty);
257    }
258
259    public event EventHandler CancelRequested;
260    private void OnCancelRequested() {
261      var handler = CancelRequested;
262      if (handler != null) handler(this, EventArgs.Empty);
263    }
264    #endregion
265  }
266}
Note: See TracBrowser for help on using the repository browser.