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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Windows.Forms;
|
---|
25 |
|
---|
26 | namespace 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(IView view) {
|
---|
50 | InitializeComponent();
|
---|
51 | if (view == null) throw new ArgumentNullException("view", "The view is null.");
|
---|
52 | if (!(view is Control)) throw new ArgumentException("The view is not a control.", "view");
|
---|
53 | this.view = view;
|
---|
54 | }
|
---|
55 | public ProgressView(IView view, IProgress progress)
|
---|
56 | : this(view) {
|
---|
57 | Content = progress;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static ProgressView Attach(IView view, IProgress progress, bool disposeOnFinish = false) {
|
---|
61 | return new ProgressView(view, progress) {
|
---|
62 | DisposeOnFinish = disposeOnFinish
|
---|
63 | };
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected override void RegisterContentEvents() {
|
---|
67 | Content.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
68 | Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
|
---|
69 | Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
|
---|
70 | Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
|
---|
71 | base.RegisterContentEvents();
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void DeregisterContentEvents() {
|
---|
75 | base.DeregisterContentEvents();
|
---|
76 | Content.StatusChanged -= new EventHandler(progress_StatusChanged);
|
---|
77 | Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
|
---|
78 | Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
|
---|
79 | Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override void OnContentChanged() {
|
---|
83 | base.OnContentChanged();
|
---|
84 | if (Content == null) {
|
---|
85 | HideProgress();
|
---|
86 | } else {
|
---|
87 | if (Content.ProgressState == ProgressState.Started)
|
---|
88 | ShowProgress();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override void SetEnabledStateOfControls() {
|
---|
93 | base.SetEnabledStateOfControls();
|
---|
94 | cancelButton.Visible = Content != null && Content.CanBeCanceled;
|
---|
95 | cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void ShowProgress() {
|
---|
99 | if (InvokeRequired) Invoke((Action)ShowProgress);
|
---|
100 | else {
|
---|
101 | if (view != null) {
|
---|
102 | Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
|
---|
103 | Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
|
---|
104 | Anchor = AnchorStyles.None;
|
---|
105 |
|
---|
106 | LockBackground();
|
---|
107 | Parent = Control.Parent;
|
---|
108 | BringToFront();
|
---|
109 | }
|
---|
110 | UpdateProgressValue();
|
---|
111 | UpdateProgressStatus();
|
---|
112 | Visible = true;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | private void HideProgress() {
|
---|
117 | if (InvokeRequired) Invoke((Action)HideProgress);
|
---|
118 | else {
|
---|
119 | if (view != null) {
|
---|
120 | Parent = null;
|
---|
121 | UnlockBackground();
|
---|
122 | }
|
---|
123 | Visible = false;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void progress_StatusChanged(object sender, EventArgs e) {
|
---|
128 | UpdateProgressStatus();
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
132 | UpdateProgressValue();
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void Content_ProgressStateChanged(object sender, EventArgs e) {
|
---|
136 | switch (Content.ProgressState) {
|
---|
137 | case ProgressState.Finished:
|
---|
138 | HideProgress();
|
---|
139 | if (DisposeOnFinish) {
|
---|
140 | Content = null;
|
---|
141 | Dispose();
|
---|
142 | }
|
---|
143 | break;
|
---|
144 | case ProgressState.Canceled: HideProgress(); break;
|
---|
145 | case ProgressState.Started: ShowProgress(); break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
|
---|
150 | SetEnabledStateOfControls();
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void LockBackground() {
|
---|
154 | if (InvokeRequired) Invoke((Action)LockBackground);
|
---|
155 | else {
|
---|
156 | view.Enabled = false;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void UnlockBackground() {
|
---|
161 | if (InvokeRequired) Invoke((Action)UnlockBackground);
|
---|
162 | else {
|
---|
163 | view.Enabled = true;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | private void UpdateProgressValue() {
|
---|
168 | if (InvokeRequired) Invoke((Action)UpdateProgressValue);
|
---|
169 | else {
|
---|
170 | if (Content != null) {
|
---|
171 | double progressValue = Content.ProgressValue;
|
---|
172 | if (progressValue <= 0.0 || progressValue > 1.0) {
|
---|
173 | if (progressBar.Style != ProgressBarStyle.Marquee)
|
---|
174 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
175 | } else {
|
---|
176 | if (progressBar.Style != ProgressBarStyle.Blocks)
|
---|
177 | progressBar.Style = ProgressBarStyle.Blocks;
|
---|
178 | progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void UpdateProgressStatus() {
|
---|
185 | if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
|
---|
186 | else if (Content != null)
|
---|
187 | statusLabel.Text = Content.Status;
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
191 | if (Content != null) {
|
---|
192 | try {
|
---|
193 | Content.Cancel(CancelTimeoutMs);
|
---|
194 | ReadOnly = true;
|
---|
195 | cancelButtonTimer.Interval = CancelTimeoutMs;
|
---|
196 | cancelButtonTimer.Start();
|
---|
197 | } catch (NotSupportedException nse) {
|
---|
198 | PluginInfrastructure.ErrorHandling.ShowErrorDialog(nse);
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void cancelButtonTimer_Tick(object sender, EventArgs e) {
|
---|
204 | cancelButtonTimer.Stop();
|
---|
205 | if (Visible) ReadOnly = false;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|