Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs @ 8156

Last change on this file since 8156 was 8156, checked in by ascheibe, 12 years ago

#1762 Removed IProgressReporter and changed the Hive Job Manager accordingly.

File size: 5.7 KB
Line 
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
22using System;
23using System.Windows.Forms;
24
25namespace HeuristicLab.MainForm.WindowsForms {
26  public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View {
27    private ContentView parentView;
28
29    private IProgress progress;
30    public IProgress Progress {
31      get { return progress; }
32      set {
33        if (progress == value) return;
34        Finish();
35        progress = value;
36        RegisterProgressEvents();
37        ShowProgress();
38        OnProgressChanged();
39      }
40    }
41
42    private bool cancelEnabled;
43    public bool CancelEnabled {
44      get { return cancelEnabled; }
45      set {
46        cancelEnabled = value;
47        SetCancelButtonVisibility();
48      }
49    }
50
51    private void ShowProgress() {
52      if (progress != null) {
53        this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
54        this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
55        this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
56
57        LockBackground();
58
59        if (!parentView.Controls.Contains(this)) {
60          parentView.Controls.Add(this);
61        }
62
63        BringToFront();
64        Visible = true;
65      }
66    }
67
68    public ProgressView(ContentView parentView) {
69      InitializeComponent();
70      CancelEnabled = false;
71
72      if (parentView != null) {
73        this.parentView = parentView;
74      } else {
75        throw new ArgumentNullException("The parent view is null.");
76      }
77    }
78
79    private void RegisterProgressEvents() {
80      if (progress == null) return;
81      progress.Finished += new EventHandler(progress_Finished);
82      progress.StatusChanged += new EventHandler(progress_StatusChanged);
83      progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
84      progress.Canceled += new EventHandler(progress_Canceled);
85    }
86
87    private void DeregisterProgressEvents() {
88      if (progress == null) return;
89      progress.Finished -= new EventHandler(progress_Finished);
90      progress.StatusChanged -= new EventHandler(progress_StatusChanged);
91      progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
92      progress.Canceled -= new EventHandler(progress_Canceled);
93    }
94
95    private void progress_Finished(object sender, EventArgs e) {
96      Finish();
97    }
98
99    private void progress_StatusChanged(object sender, EventArgs e) {
100      UpdateProgressStatus();
101    }
102
103    private void progress_ProgressValueChanged(object sender, EventArgs e) {
104      UpdateProgressValue();
105    }
106
107    void progress_Canceled(object sender, EventArgs e) {
108      Finish();
109    }
110
111    private void LockBackground() {
112      if (InvokeRequired) {
113        Invoke((Action)LockBackground);
114      } else {
115        parentView.Locked = true;
116        parentView.ReadOnly = true;
117        foreach (Control c in this.parentView.Controls)
118          c.Enabled = false;
119        Enabled = true;
120        ReadOnly = false;
121      }
122    }
123
124    private void UpdateProgressValue() {
125      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
126      else {
127        if (progress != null) {
128          double progressValue = progress.ProgressValue;
129          if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
130            progressBar.Style = ProgressBarStyle.Marquee;
131            progressBar.Value = progressBar.Minimum;
132          } else {
133            progressBar.Style = ProgressBarStyle.Blocks;
134            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
135          }
136        }
137      }
138    }
139
140    private void UpdateProgressStatus() {
141      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
142      else {
143        if (progress != null) {
144          string status = progress.Status;
145          statusLabel.Text = progress.Status;
146        }
147      }
148    }
149
150    private void Finish() {
151      if (InvokeRequired) {
152        Invoke(new Action(Finish));
153      } else {
154        progressBar.Value = progressBar.Maximum;
155        parentView.Controls.Remove(this);
156        parentView.Locked = false;
157        parentView.ReadOnly = false;
158        foreach (Control c in this.parentView.Controls)
159          c.Enabled = true;
160        DeregisterProgressEvents();
161        progress = null;
162        this.Visible = false;
163      }
164    }
165
166    private void cancelButton_Click(object sender, EventArgs e) {
167      if (progress != null) {
168        progress.CancelRequested = true;
169        cancelButton.Enabled = false;
170      }
171    }
172
173    private void SetCancelButtonVisibility() {
174      if (InvokeRequired) {
175        Invoke((Action)SetCancelButtonVisibility);
176      } else {
177        cancelButton.Visible = cancelEnabled;
178      }
179    }
180
181    private void OnProgressChanged() {
182      UpdateProgressStatus();
183      UpdateProgressValue();
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.