1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 | using HeuristicLab.MainForm.WindowsForms;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Clients.Hive.Views {
|
---|
27 | public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
|
---|
28 | private ContentView parentView;
|
---|
29 | private IProgress progress;
|
---|
30 | public IProgress Progress {
|
---|
31 | get {
|
---|
32 | return this.progress;
|
---|
33 | }
|
---|
34 | set {
|
---|
35 | if (this.progress != value) {
|
---|
36 | if (this.progress != null) {
|
---|
37 | DeregisterProgressEvents();
|
---|
38 | }
|
---|
39 | this.progress = value;
|
---|
40 | RegisterProgressEvents();
|
---|
41 | OnProgressChanged();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public bool CancelEnabled {
|
---|
47 | get {
|
---|
48 | return cancelButton.Visible;
|
---|
49 | }
|
---|
50 | set {
|
---|
51 | if (InvokeRequired) {
|
---|
52 | Invoke(new Action<bool>((ce) => { CancelEnabled = ce; }), value);
|
---|
53 | } else {
|
---|
54 | cancelButton.Visible = value;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /// <param name="parentView">This is the View which will be locked if lockParentView is true</param>
|
---|
60 | public ProgressView(ContentView parentView, IProgress progress) {
|
---|
61 | InitializeComponent();
|
---|
62 | this.parentView = parentView;
|
---|
63 | this.Progress = progress;
|
---|
64 |
|
---|
65 | this.CancelEnabled = false;
|
---|
66 |
|
---|
67 | progressBar.Style = ProgressBarStyle.Marquee;
|
---|
68 |
|
---|
69 | this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
|
---|
70 | this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
|
---|
71 | this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
|
---|
72 |
|
---|
73 | LockBackground();
|
---|
74 |
|
---|
75 | parentView.Controls.Add(this);
|
---|
76 | this.BringToFront();
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void RegisterProgressEvents() {
|
---|
80 | progress.Finished += new EventHandler(progress_Finished);
|
---|
81 | progress.StatusChanged += new EventHandler(progress_StatusChanged);
|
---|
82 | progress.ProgressValueChanged += new EventHandler(progress_ProgressChanged);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void DeregisterProgressEvents() {
|
---|
86 | progress.Finished -= new EventHandler(progress_Finished);
|
---|
87 | progress.StatusChanged -= new EventHandler(progress_StatusChanged);
|
---|
88 | progress.ProgressValueChanged -= new EventHandler(progress_ProgressChanged);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void progress_Finished(object sender, EventArgs e) {
|
---|
92 | Finish();
|
---|
93 | }
|
---|
94 |
|
---|
95 | void progress_ProgressChanged(object sender, EventArgs e) {
|
---|
96 | if (InvokeRequired) {
|
---|
97 | Invoke(new EventHandler(progress_ProgressChanged), sender, e);
|
---|
98 | } else {
|
---|
99 | progressBar.Style = ProgressBarStyle.Blocks;
|
---|
100 | this.progressBar.Value = Math.Min(this.progressBar.Maximum, (int)(progress.ProgressValue * progressBar.Maximum));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | void progress_StatusChanged(object sender, EventArgs e) {
|
---|
105 | if (InvokeRequired) {
|
---|
106 | Invoke(new EventHandler(progress_StatusChanged), sender, e);
|
---|
107 | } else {
|
---|
108 | statusLabel.Text = progress.Status;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private void LockBackground() {
|
---|
113 | if (InvokeRequired) {
|
---|
114 | Invoke(new Action(LockBackground));
|
---|
115 | } else {
|
---|
116 | this.parentView.Locked = true;
|
---|
117 | foreach (Control c in this.parentView.Controls) {
|
---|
118 | c.Enabled = false;
|
---|
119 | }
|
---|
120 | this.Enabled = true;
|
---|
121 | this.ReadOnly = false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void Finish() {
|
---|
126 | if (InvokeRequired) {
|
---|
127 | Invoke(new Action(Finish));
|
---|
128 | } else {
|
---|
129 | progressBar.Value = progressBar.Maximum;
|
---|
130 | parentView.Locked = false;
|
---|
131 | foreach (Control c in this.parentView.Controls) {
|
---|
132 | c.Enabled = true;
|
---|
133 | }
|
---|
134 | parentView.Controls.Remove(this);
|
---|
135 | this.Dispose();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void cancelButton_Click(object sender, EventArgs e) {
|
---|
140 | Finish();
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void OnProgressChanged() {
|
---|
144 | progress_StatusChanged(this, EventArgs.Empty);
|
---|
145 | progress_ProgressChanged(this, EventArgs.Empty);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler Canceled;
|
---|
149 | protected virtual void OnCanceled() {
|
---|
150 | var handler = Canceled;
|
---|
151 | if (handler != null) Canceled(this, EventArgs.Empty);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|