- Timestamp:
- 06/29/12 23:12:32 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs
r8159 r8165 21 21 22 22 using System; 23 using System.ComponentModel; 23 24 using System.Windows.Forms; 24 25 25 26 namespace HeuristicLab.MainForm.WindowsForms { 26 public partial class ProgressView : HeuristicLab.MainForm.WindowsForms.View { 27 [View("ProgressView")] 28 [Content(typeof(IProgress), true)] 29 public partial class ProgressView : AsynchronousContentView { 30 private const int DefaultCancelTimeoutMs = 3000; 27 31 private ContentView parentView; 28 32 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 } 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 public ProgressView() { 44 InitializeComponent(); 45 } 46 public ProgressView(IProgress progress) 47 : this() { 48 Content = progress; 49 } 50 public ProgressView(ContentView parentView) 51 : this() { 52 if (parentView == null) throw new ArgumentNullException("parentView", "The parent view is null."); 53 this.parentView = parentView; 54 } 55 public ProgressView(ContentView parentView, IProgress progress) 56 : this(parentView) { 57 Content = progress; 58 } 59 60 protected override void RegisterContentEvents() { 61 Content.StatusChanged += new EventHandler(progress_StatusChanged); 62 Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged); 63 Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged); 64 Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged); 65 base.RegisterContentEvents(); 66 } 67 68 protected override void DeregisterContentEvents() { 69 base.DeregisterContentEvents(); 70 Content.StatusChanged -= new EventHandler(progress_StatusChanged); 71 Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged); 72 Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged); 73 Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged); 74 } 75 76 protected override void OnContentChanged() { 77 base.OnContentChanged(); 78 if (Content == null) { 79 HideProgress(); 80 } else { 81 if (Content.ProgressState == ProgressState.Started) 82 ShowProgress(); 83 } 84 } 85 86 protected override void SetEnabledStateOfControls() { 87 base.SetEnabledStateOfControls(); 88 cancelButton.Visible = Content != null && Content.CanBeCanceled; 89 cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly; 49 90 } 50 91 51 92 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(); 93 if (InvokeRequired) Invoke((Action)ShowProgress); 94 else { 95 if (parentView != null) { 96 this.Left = (parentView.ClientRectangle.Width / 2) - (this.Width / 2); 97 this.Top = (parentView.ClientRectangle.Height / 2) - (this.Height / 2); 98 this.Anchor = AnchorStyles.None; 99 100 LockBackground(); 101 102 if (!parentView.Controls.Contains(this)) 103 parentView.Controls.Add(this); 104 105 BringToFront(); 106 } 107 UpdateProgressValue(); 108 UpdateProgressStatus(); 64 109 Visible = true; 65 110 } 66 111 } 67 112 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(); 113 private void HideProgress() { 114 if (InvokeRequired) Invoke((Action)HideProgress); 115 else { 116 if (parentView != null) { 117 if (parentView.Controls.Contains(this)) 118 parentView.Controls.Remove(this); 119 120 UnlockBackground(); 121 } 122 Visible = false; 123 } 97 124 } 98 125 … … 105 132 } 106 133 107 void progress_Canceled(object sender, EventArgs e) { 108 Finish(); 134 private void Content_ProgressStateChanged(object sender, EventArgs e) { 135 if (Content.ProgressState == ProgressState.Finished 136 || Content.ProgressState == ProgressState.Canceled) 137 HideProgress(); 138 if (Content.ProgressState == ProgressState.Started) 139 ShowProgress(); 140 } 141 142 private void Content_CanBeCanceledChanged(object sender, EventArgs e) { 143 SetEnabledStateOfControls(); 109 144 } 110 145 … … 115 150 parentView.Locked = true; 116 151 parentView.ReadOnly = true; 117 Enabled = true;152 Locked = false; 118 153 ReadOnly = false; 154 } 155 } 156 157 private void UnlockBackground() { 158 if (InvokeRequired) Invoke((Action)UnlockBackground); 159 else { 160 parentView.Locked = false; 161 parentView.ReadOnly = false; 162 Locked = true; 163 ReadOnly = true; 119 164 } 120 165 } … … 123 168 if (InvokeRequired) Invoke((Action)UpdateProgressValue); 124 169 else { 125 if ( progress!= null) {126 double progressValue = progress.ProgressValue;127 if (progressValue < progressBar.Minimum || progressValue > progressBar.Maximum) {128 progressBar.Style = ProgressBarStyle.Marquee;129 progressBar.Value = progressBar.Minimum;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; 130 175 } else { 131 progressBar.Style = ProgressBarStyle.Blocks; 176 if (progressBar.Style != ProgressBarStyle.Blocks) 177 progressBar.Style = ProgressBarStyle.Blocks; 132 178 progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum)); 133 179 } … … 138 184 private void UpdateProgressStatus() { 139 185 if (InvokeRequired) Invoke((Action)UpdateProgressStatus); 140 else { 141 if (progress != null) { 142 string status = progress.Status; 143 statusLabel.Text = progress.Status; 144 } 145 } 146 } 147 148 private void Finish() { 149 if (InvokeRequired) { 150 Invoke(new Action(Finish)); 151 } else { 152 progressBar.Value = progressBar.Maximum; 153 parentView.Controls.Remove(this); 154 parentView.Locked = false; 155 parentView.ReadOnly = false; 156 DeregisterProgressEvents(); 157 progress = null; 158 Visible = false; 159 } 186 else if (Content != null) 187 statusLabel.Text = Content.Status; 160 188 } 161 189 162 190 private void cancelButton_Click(object sender, EventArgs e) { 163 if (progress != null) { 164 progress.CancelRequested = true; 165 cancelButton.Enabled = false; 166 } 167 } 168 169 private void SetCancelButtonVisibility() { 170 if (InvokeRequired) { 171 Invoke((Action)SetCancelButtonVisibility); 172 } else { 173 cancelButton.Visible = cancelEnabled; 174 } 175 } 176 177 private void OnProgressChanged() { 178 UpdateProgressStatus(); 179 UpdateProgressValue(); 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; 180 206 } 181 207 }
Note: See TracChangeset
for help on using the changeset viewer.