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.Windows.Forms;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.MainForm.WindowsForms {
|
---|
26 | internal sealed partial class ProgressView : UserControl {
|
---|
27 | private readonly Control control;
|
---|
28 | public Control Control {
|
---|
29 | get { return control; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | private readonly IProgress content;
|
---|
33 | public IProgress Content {
|
---|
34 | get { return content; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public ProgressView(Control control, IProgress content)
|
---|
38 | : base() {
|
---|
39 | if (control == null) throw new ArgumentNullException("control", "The control is null.");
|
---|
40 | if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
|
---|
41 | InitializeComponent();
|
---|
42 |
|
---|
43 | this.control = control;
|
---|
44 | this.content = content;
|
---|
45 | if (content.ProgressState == ProgressState.Started)
|
---|
46 | ShowProgress();
|
---|
47 | RegisterContentEvents();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Clean up any resources being used.
|
---|
52 | /// </summary>
|
---|
53 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
54 | protected override void Dispose(bool disposing) {
|
---|
55 | DeregisterContentEvents();
|
---|
56 | HideProgress();
|
---|
57 |
|
---|
58 | if (disposing && (components != null)) {
|
---|
59 | components.Dispose();
|
---|
60 | }
|
---|
61 | base.Dispose(disposing);
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void RegisterContentEvents() {
|
---|
65 | content.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
66 | content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
|
---|
67 | content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
|
---|
68 | content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
|
---|
69 | }
|
---|
70 | private void DeregisterContentEvents() {
|
---|
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 | }
|
---|
76 |
|
---|
77 | private void ShowProgress() {
|
---|
78 | if (Control.InvokeRequired) {
|
---|
79 | Control.Invoke((Action)ShowProgress);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
|
---|
83 | Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
|
---|
84 | Anchor = AnchorStyles.None;
|
---|
85 |
|
---|
86 | control.Enabled = false;
|
---|
87 | Parent = Control.Parent;
|
---|
88 | BringToFront();
|
---|
89 |
|
---|
90 | UpdateProgressValue();
|
---|
91 | UpdateProgressStatus();
|
---|
92 | UpdateCancelButton();
|
---|
93 | Visible = true;
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void HideProgress() {
|
---|
97 | if (InvokeRequired) Invoke((Action)HideProgress);
|
---|
98 | else {
|
---|
99 | control.Enabled = true;
|
---|
100 | Parent = null;
|
---|
101 | Visible = false;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void progress_StatusChanged(object sender, EventArgs e) {
|
---|
106 | UpdateProgressStatus();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void progress_ProgressValueChanged(object sender, EventArgs e) {
|
---|
110 | UpdateProgressValue();
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void Content_ProgressStateChanged(object sender, EventArgs e) {
|
---|
114 | switch (content.ProgressState) {
|
---|
115 | case ProgressState.Finished: HideProgress(); break;
|
---|
116 | case ProgressState.Canceled: HideProgress(); break;
|
---|
117 | case ProgressState.Started: ShowProgress(); break;
|
---|
118 | default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
|
---|
123 | UpdateCancelButton();
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void UpdateCancelButton() {
|
---|
127 | cancelButton.Visible = content != null && content.CanBeCanceled;
|
---|
128 | cancelButton.Enabled = content != null && content.CanBeCanceled;
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void UpdateProgressValue() {
|
---|
132 | if (InvokeRequired) Invoke((Action)UpdateProgressValue);
|
---|
133 | else {
|
---|
134 | if (content != null) {
|
---|
135 | double progressValue = content.ProgressValue;
|
---|
136 | if (progressValue <= 0.0 || progressValue > 1.0) {
|
---|
137 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
138 | } else {
|
---|
139 | progressBar.Style = ProgressBarStyle.Blocks;
|
---|
140 | progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void UpdateProgressStatus() {
|
---|
147 | if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
|
---|
148 | else if (content != null)
|
---|
149 | statusLabel.Text = content.Status;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
153 | content.Cancel();
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|