Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/12 23:12:32 (12 years ago)
Author:
abeham
Message:

#1762: Some changes to progress handling, see the ticket for more details

Location:
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.cs

    r8159 r8165  
    2121
    2222using System;
     23using System.ComponentModel;
    2324using System.Windows.Forms;
    2425
    2526namespace 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;
    2731    private ContentView parentView;
    2832
    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;
    4990    }
    5091
    5192    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();
    64109        Visible = true;
    65110      }
    66111    }
    67112
    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      }
    97124    }
    98125
     
    105132    }
    106133
    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();
    109144    }
    110145
     
    115150        parentView.Locked = true;
    116151        parentView.ReadOnly = true;
    117         Enabled = true;
     152        Locked = false;
    118153        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;
    119164      }
    120165    }
     
    123168      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
    124169      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;
    130175          } else {
    131             progressBar.Style = ProgressBarStyle.Blocks;
     176            if (progressBar.Style != ProgressBarStyle.Blocks)
     177              progressBar.Style = ProgressBarStyle.Blocks;
    132178            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    133179          }
     
    138184    private void UpdateProgressStatus() {
    139185      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;
    160188    }
    161189
    162190    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;
    180206    }
    181207  }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Views/ProgressView.designer.cs

    r8111 r8165  
    11
    22namespace HeuristicLab.MainForm.WindowsForms {
    3   partial class ProgressView : View {
     3  partial class ProgressView {
    44    /// <summary>
    55    /// Required designer variable.
     
    2525    /// </summary>
    2626    private void InitializeComponent() {
     27      this.components = new System.ComponentModel.Container();
    2728      this.progressBar = new System.Windows.Forms.ProgressBar();
    2829      this.statusLabel = new System.Windows.Forms.Label();
    2930      this.cancelButton = new System.Windows.Forms.Button();
    3031      this.panel = new System.Windows.Forms.Panel();
     32      this.cancelButtonTimer = new System.Windows.Forms.Timer(this.components);
    3133      this.panel.SuspendLayout();
    3234      this.SuspendLayout();
     
    7577      this.panel.TabIndex = 3;
    7678      //
     79      // cancelButtonTimer
     80      //
     81      this.cancelButtonTimer.Tick += new System.EventHandler(this.cancelButtonTimer_Tick);
     82      //
    7783      // ProgressView
    7884      //
    79       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    8085      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
    8186      this.Controls.Add(this.panel);
     
    9398    private System.Windows.Forms.Button cancelButton;
    9499    private System.Windows.Forms.Panel panel;
     100    private System.Windows.Forms.Timer cancelButtonTimer;
    95101  }
    96102}
Note: See TracChangeset for help on using the changeset viewer.