Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1762 implemented review comments:

  • removed self disposing. The progress view now reacts if a progress is set and Finish() is now called on the progress object and not the view.
  • Moved Cancel event from ProgressView to Progress
  • throw ArgumentNullException if the parent view is null
File size: 5.6 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        DeregisterProgressEvents();
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      this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2);
53      this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2);
54      this.Anchor = AnchorStyles.Left | AnchorStyles.Top;
55
56      LockBackground();
57
58      if (!parentView.Controls.Contains(this)) {
59        parentView.Controls.Add(this);
60      }
61
62      BringToFront();
63      Visible = true;
64    }
65
66    public ProgressView(ContentView parentView) {
67      InitializeComponent();
68      CancelEnabled = false;
69
70      if (parentView != null) {
71        this.parentView = parentView;
72      } else {
73        throw new ArgumentNullException("The parent view is null.");
74      }
75    }
76
77    private void RegisterProgressEvents() {
78      if (progress == null) return;
79      progress.Finished += new EventHandler(progress_Finished);
80      progress.StatusChanged += new EventHandler(progress_StatusChanged);
81      progress.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
82      progress.Canceled += new EventHandler(progress_Canceled);
83    }
84
85    private void DeregisterProgressEvents() {
86      if (progress == null) return;
87      progress.Finished -= new EventHandler(progress_Finished);
88      progress.StatusChanged -= new EventHandler(progress_StatusChanged);
89      progress.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
90      progress.Canceled -= new EventHandler(progress_Canceled);
91    }
92
93    private void progress_Finished(object sender, EventArgs e) {
94      Finish();
95    }
96
97    private void progress_StatusChanged(object sender, EventArgs e) {
98      UpdateProgressStatus();
99    }
100
101    private void progress_ProgressValueChanged(object sender, EventArgs e) {
102      UpdateProgressValue();
103    }
104
105    void progress_Canceled(object sender, EventArgs e) {
106      Finish();
107    }
108
109    private void LockBackground() {
110      if (InvokeRequired) {
111        Invoke((Action)LockBackground);
112      } else {
113        parentView.Locked = true;
114        parentView.ReadOnly = true;
115        foreach (Control c in this.parentView.Controls)
116          c.Enabled = false;
117        Enabled = true;
118        ReadOnly = false;
119      }
120    }
121
122    private void UpdateProgressValue() {
123      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
124      else {
125        double progressValue = progress.ProgressValue;
126        if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {
127          progressBar.Style = ProgressBarStyle.Marquee;
128          progressBar.Value = progressBar.Minimum;
129        } else {
130          progressBar.Style = ProgressBarStyle.Blocks;
131          progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
132        }
133      }
134    }
135
136    private void UpdateProgressStatus() {
137      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
138      else {
139        string status = progress.Status;
140        statusLabel.Text = progress.Status;
141      }
142    }
143
144    private void Finish() {
145      if (InvokeRequired) {
146        Invoke(new Action(Finish));
147      } else {
148        progressBar.Value = progressBar.Maximum;
149        parentView.Controls.Remove(this);
150        parentView.Locked = false;
151        parentView.ReadOnly = false;
152        foreach (Control c in this.parentView.Controls)
153          c.Enabled = true;
154        DeregisterProgressEvents();
155        progress = null;
156        this.Visible = false;
157      }
158    }
159
160    private void cancelButton_Click(object sender, EventArgs e) {
161      if (progress != null) {
162        progress.CancelRequested = true;
163        cancelButton.Enabled = false;
164      }
165    }
166
167    private void SetCancelButtonVisibility() {
168      if (InvokeRequired) {
169        Invoke((Action)SetCancelButtonVisibility);
170      } else {
171        cancelButton.Visible = cancelEnabled;
172      }
173    }
174
175    private void OnProgressChanged() {
176      UpdateProgressStatus();
177      UpdateProgressValue();
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.