Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs @ 9868

Last change on this file since 9868 was 9868, checked in by jkarder, 11 years ago

#1042: additional commit for r9867 (fix build fail)

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.ComponentModel;
24using System.Windows.Forms;
25
26namespace HeuristicLab.MainForm.WindowsForms {
27  [View("ProgressView")]
28  [Content(typeof(IProgress), true)]
29  public partial class ProgressView : AsynchronousContentView {
30    private const int DefaultCancelTimeoutMs = 3000;
31    private readonly IView view;
32
33    [Category("Custom"), Description("The time that the process is allowed to exit.")]
34    [DefaultValue(DefaultCancelTimeoutMs)]
35    public int CancelTimeoutMs { get; set; }
36    private bool ShouldSerializeCancelTimeoutMs() { return CancelTimeoutMs != DefaultCancelTimeoutMs; }
37
38    public new IProgress Content {
39      get { return (IProgress)base.Content; }
40      set { base.Content = value; }
41    }
42
43    private Control Control {
44      get { return (Control)view; }
45    }
46
47    public bool DisposeOnFinish { get; set; }
48
49    public ProgressView() {
50      InitializeComponent();
51    }
52
53    public ProgressView(IView view)
54      : this() {
55      if (view == null) throw new ArgumentNullException("view", "The view is null.");
56      if (!(view is Control)) throw new ArgumentException("The view is not a control.", "view");
57      this.view = view;
58    }
59    public ProgressView(IView view, IProgress progress)
60      : this(view) {
61      Content = progress;
62    }
63
64    public static ProgressView Attach(IView view, IProgress progress, bool disposeOnFinish = false) {
65      return new ProgressView(view, progress) {
66        DisposeOnFinish = disposeOnFinish
67      };
68    }
69
70    protected override void RegisterContentEvents() {
71      Content.StatusChanged += new EventHandler(progress_StatusChanged);
72      Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
73      Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
74      Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
75      base.RegisterContentEvents();
76    }
77
78    protected override void DeregisterContentEvents() {
79      base.DeregisterContentEvents();
80      Content.StatusChanged -= new EventHandler(progress_StatusChanged);
81      Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
82      Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
83      Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      if (Content == null) {
89        HideProgress();
90      } else {
91        if (Content.ProgressState == ProgressState.Started)
92          ShowProgress();
93      }
94    }
95
96    protected override void SetEnabledStateOfControls() {
97      base.SetEnabledStateOfControls();
98      cancelButton.Visible = Content != null && Content.CanBeCanceled;
99      cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
100    }
101
102    private void ShowProgress() {
103      if (InvokeRequired) Invoke((Action)ShowProgress);
104      else {
105        if (view != null) {
106          Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
107          Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
108          Anchor = AnchorStyles.None;
109
110          LockBackground();
111          Parent = Control.Parent;
112          BringToFront();
113        }
114        UpdateProgressValue();
115        UpdateProgressStatus();
116        Visible = true;
117      }
118    }
119
120    private void HideProgress() {
121      if (InvokeRequired) Invoke((Action)HideProgress);
122      else {
123        if (view != null) {
124          Parent = null;
125          UnlockBackground();
126        }
127        Visible = false;
128      }
129    }
130
131    private void progress_StatusChanged(object sender, EventArgs e) {
132      UpdateProgressStatus();
133    }
134
135    private void progress_ProgressValueChanged(object sender, EventArgs e) {
136      UpdateProgressValue();
137    }
138
139    private void Content_ProgressStateChanged(object sender, EventArgs e) {
140      switch (Content.ProgressState) {
141        case ProgressState.Finished:
142          HideProgress();
143          if (DisposeOnFinish) {
144            Content = null;
145            Dispose();
146          }
147          break;
148        case ProgressState.Canceled: HideProgress(); break;
149        case ProgressState.Started: ShowProgress(); break;
150      }
151    }
152
153    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
154      SetEnabledStateOfControls();
155    }
156
157    private void LockBackground() {
158      if (InvokeRequired) Invoke((Action)LockBackground);
159      else {
160        view.Enabled = false;
161      }
162    }
163
164    private void UnlockBackground() {
165      if (InvokeRequired) Invoke((Action)UnlockBackground);
166      else {
167        view.Enabled = true;
168      }
169    }
170
171    private void UpdateProgressValue() {
172      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
173      else {
174        if (Content != null) {
175          double progressValue = Content.ProgressValue;
176          if (progressValue <= 0.0 || progressValue > 1.0) {
177            if (progressBar.Style != ProgressBarStyle.Marquee)
178              progressBar.Style = ProgressBarStyle.Marquee;
179          } else {
180            if (progressBar.Style != ProgressBarStyle.Blocks)
181              progressBar.Style = ProgressBarStyle.Blocks;
182            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
183          }
184        }
185      }
186    }
187
188    private void UpdateProgressStatus() {
189      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
190      else if (Content != null)
191        statusLabel.Text = Content.Status;
192    }
193
194    private void cancelButton_Click(object sender, EventArgs e) {
195      if (Content != null) {
196        try {
197          Content.Cancel(CancelTimeoutMs);
198          ReadOnly = true;
199          cancelButtonTimer.Interval = CancelTimeoutMs;
200          cancelButtonTimer.Start();
201        } catch (NotSupportedException nse) {
202          PluginInfrastructure.ErrorHandling.ShowErrorDialog(nse);
203        }
204      }
205    }
206
207    private void cancelButtonTimer_Tick(object sender, EventArgs e) {
208      cancelButtonTimer.Stop();
209      if (Visible) ReadOnly = false;
210    }
211  }
212}
Note: See TracBrowser for help on using the repository browser.