Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/21/13 23:08:47 (11 years ago)
Author:
ascheibe
Message:

#1042

  • applied mkommends progress view patch
  • adapted Hive views accordingly
  • made some minor improvements (more to come...)
Location:
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3
Files:
3 edited

Legend:

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

    r9868 r9893  
    2121
    2222using System;
    23 using System.ComponentModel;
    2423using System.Windows.Forms;
    2524
    2625namespace HeuristicLab.MainForm.WindowsForms {
    27   [View("ProgressView")]
    28   [Content(typeof(IProgress), true)]
    29   public partial class ProgressView : AsynchronousContentView {
    30     private const int DefaultCancelTimeoutMs = 3000;
    31     private readonly IView view;
    32 
    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; }
     26  public sealed partial class ProgressView : UserControl {
     27    private readonly Control control;
     28    public Control Control {
     29      get { return control; }
    4130    }
    4231
    43     private Control Control {
    44       get { return (Control)view; }
     32    private readonly IProgress content;
     33    public IProgress Content {
     34      get { return content; }
    4535    }
    4636
    47     public bool DisposeOnFinish { get; set; }
     37    public ProgressView(Control control, IProgress content)
     38      : base() {
     39      if (control == null) throw new ArgumentNullException("control", "The control is null.");
     40      if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
     41      InitializeComponent();
    4842
    49     public ProgressView() {
    50       InitializeComponent();
     43      this.control = control;
     44      this.content = content;
     45      if (content.ProgressState == ProgressState.Started)
     46        ShowProgress();
     47      RegisterContentEvents();
    5148    }
    5249
    53     public ProgressView(IView view)
    54       : this() {
    55       if (view == null) throw new ArgumentNullException("view", "The view is null.");
    56       if (!(view is Control)) throw new ArgumentException("The view is not a control.", "view");
    57       this.view = view;
     50    private void RegisterContentEvents() {
     51      content.StatusChanged += new EventHandler(progress_StatusChanged);
     52      content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
     53      content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
     54      content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
    5855    }
    59     public ProgressView(IView view, IProgress progress)
    60       : this(view) {
    61       Content = progress;
    62     }
    63 
    64     public static ProgressView Attach(IView view, IProgress progress, bool disposeOnFinish = false) {
    65       return new ProgressView(view, progress) {
    66         DisposeOnFinish = disposeOnFinish
    67       };
    68     }
    69 
    70     protected override void RegisterContentEvents() {
    71       Content.StatusChanged += new EventHandler(progress_StatusChanged);
    72       Content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
    73       Content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
    74       Content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
    75       base.RegisterContentEvents();
    76     }
    77 
    78     protected override void DeregisterContentEvents() {
    79       base.DeregisterContentEvents();
    80       Content.StatusChanged -= new EventHandler(progress_StatusChanged);
    81       Content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
    82       Content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
    83       Content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
    84     }
    85 
    86     protected override void OnContentChanged() {
    87       base.OnContentChanged();
    88       if (Content == null) {
    89         HideProgress();
    90       } else {
    91         if (Content.ProgressState == ProgressState.Started)
    92           ShowProgress();
    93       }
    94     }
    95 
    96     protected override void SetEnabledStateOfControls() {
    97       base.SetEnabledStateOfControls();
    98       cancelButton.Visible = Content != null && Content.CanBeCanceled;
    99       cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
     56    private void DeregisterContentEvents() {
     57      content.StatusChanged -= new EventHandler(progress_StatusChanged);
     58      content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
     59      content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
     60      content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
    10061    }
    10162
    10263    private void ShowProgress() {
    103       if (InvokeRequired) Invoke((Action)ShowProgress);
    104       else {
    105         if (view != null) {
    106           Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
    107           Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
    108           Anchor = AnchorStyles.None;
     64      if (Control.InvokeRequired) {
     65        Control.Invoke((Action)ShowProgress);
     66        return;
     67      }
     68      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
     69      Top = (Control.ClientRectangle.Height / 2) - (Height / 2);
     70      Anchor = AnchorStyles.None;
    10971
    110           LockBackground();
    111           Parent = Control.Parent;
    112           BringToFront();
    113         }
    114         UpdateProgressValue();
    115         UpdateProgressStatus();
    116         Visible = true;
    117       }
     72      control.Enabled = false;
     73      Parent = Control.Parent;
     74      BringToFront();
     75
     76      UpdateProgressValue();
     77      UpdateProgressStatus();
     78      UpdateCancelButton();
     79      Visible = true;
    11880    }
    11981
     
    12183      if (InvokeRequired) Invoke((Action)HideProgress);
    12284      else {
    123         if (view != null) {
    124           Parent = null;
    125           UnlockBackground();
    126         }
     85        control.Enabled = true;
     86        Parent = null;
    12787        Visible = false;
    12888      }
     
    13898
    13999    private void Content_ProgressStateChanged(object sender, EventArgs e) {
    140       switch (Content.ProgressState) {
    141         case ProgressState.Finished:
    142           HideProgress();
    143           if (DisposeOnFinish) {
    144             Content = null;
    145             Dispose();
    146           }
    147           break;
     100      switch (content.ProgressState) {
     101        case ProgressState.Finished: HideProgress(); break;
    148102        case ProgressState.Canceled: HideProgress(); break;
    149103        case ProgressState.Started: ShowProgress(); break;
     104        default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
    150105      }
    151106    }
    152107
    153108    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
    154       SetEnabledStateOfControls();
     109      UpdateCancelButton();
    155110    }
    156111
    157     private void LockBackground() {
    158       if (InvokeRequired) Invoke((Action)LockBackground);
    159       else {
    160         view.Enabled = false;
    161       }
    162     }
    163 
    164     private void UnlockBackground() {
    165       if (InvokeRequired) Invoke((Action)UnlockBackground);
    166       else {
    167         view.Enabled = true;
    168       }
     112    private void UpdateCancelButton() {
     113      cancelButton.Visible = content != null && content.CanBeCanceled;
     114      cancelButton.Enabled = content != null && content.CanBeCanceled;
    169115    }
    170116
     
    172118      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
    173119      else {
    174         if (Content != null) {
    175           double progressValue = Content.ProgressValue;
     120        if (content != null) {
     121          double progressValue = content.ProgressValue;
    176122          if (progressValue <= 0.0 || progressValue > 1.0) {
    177             if (progressBar.Style != ProgressBarStyle.Marquee)
    178               progressBar.Style = ProgressBarStyle.Marquee;
     123            progressBar.Style = ProgressBarStyle.Marquee;
    179124          } else {
    180             if (progressBar.Style != ProgressBarStyle.Blocks)
    181               progressBar.Style = ProgressBarStyle.Blocks;
     125            progressBar.Style = ProgressBarStyle.Blocks;
    182126            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    183127          }
     
    188132    private void UpdateProgressStatus() {
    189133      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
    190       else if (Content != null)
    191         statusLabel.Text = Content.Status;
     134      else if (content != null)
     135        statusLabel.Text = content.Status;
    192136    }
    193137
    194138    private void cancelButton_Click(object sender, EventArgs e) {
    195       if (Content != null) {
    196         try {
    197           Content.Cancel(CancelTimeoutMs);
    198           ReadOnly = true;
    199           cancelButtonTimer.Interval = CancelTimeoutMs;
    200           cancelButtonTimer.Start();
    201         } catch (NotSupportedException nse) {
    202           PluginInfrastructure.ErrorHandling.ShowErrorDialog(nse);
    203         }
    204       }
    205     }
    206 
    207     private void cancelButtonTimer_Tick(object sender, EventArgs e) {
    208       cancelButtonTimer.Stop();
    209       if (Visible) ReadOnly = false;
     139      content.Cancel();
    210140    }
    211141  }
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.designer.cs

    r9456 r9893  
    3232    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    3333    protected override void Dispose(bool disposing) {
     34      DeregisterContentEvents();
    3435      if (disposing && (components != null)) {
    3536        components.Dispose();
     
    5051      this.cancelButton = new System.Windows.Forms.Button();
    5152      this.panel = new System.Windows.Forms.Panel();
    52       this.cancelButtonTimer = new System.Windows.Forms.Timer(this.components);
    5353      this.panel.SuspendLayout();
    5454      this.SuspendLayout();
     
    9797      this.panel.TabIndex = 3;
    9898      //
    99       // cancelButtonTimer
    100       //
    101       this.cancelButtonTimer.Tick += new System.EventHandler(this.cancelButtonTimer_Tick);
    102       //
    10399      // ProgressView
    104100      //
     
    109105      this.panel.ResumeLayout(false);
    110106      this.ResumeLayout(false);
    111 
    112107    }
    113108
     
    118113    private System.Windows.Forms.Button cancelButton;
    119114    private System.Windows.Forms.Panel panel;
    120     private System.Windows.Forms.Timer cancelButtonTimer;
    121115  }
    122116}
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs

    r9865 r9893  
    3030namespace HeuristicLab.MainForm.WindowsForms {
    3131  public partial class MainForm : Form, IMainForm {
    32     private readonly Dictionary<IContent, IProgress> contentProgressLookup;
    33     private readonly Dictionary<IView, IProgress> viewProgressLookup;
    3432    private bool initialized;
    3533    private int appStartingCursors;
     
    4139      this.views = new Dictionary<IView, Form>();
    4240      this.userInterfaceItems = new List<IUserInterfaceItem>();
    43       this.contentProgressLookup = new Dictionary<IContent, IProgress>();
    44       this.viewProgressLookup = new Dictionary<IView, IProgress>();
    4541      this.initialized = false;
    4642      this.showContentInViewHost = false;
     
    347343        CloseView(view, closeReason);
    348344    }
     345    #endregion
     346
     347    #region progress views
     348    private readonly Dictionary<IContent, IProgress> contentProgressLookup = new Dictionary<IContent, IProgress>();
     349    private readonly Dictionary<IView, IProgress> viewProgressLookup = new Dictionary<IView, IProgress>();
     350    private readonly List<ProgressView> progressViews = new List<ProgressView>();
    349351
    350352    /// <summary>
     
    355357        throw new ArgumentException("A progress is already registered for the specified content.", "content");
    356358
    357       var contentViews = Enumerable.Empty<IContentView>();
     359      var contentViews = views.Keys.OfType<ContentView>();
     360      if (!contentViews.Any(v => v.Content == content))
     361        throw new ArgumentException("The content is not displayed in a top-level view", "content");
     362
    358363      if (addToObjectGraphObjects) {
    359364        var containedObjects = content.GetObjectGraphObjects();
    360         contentViews = views.Keys.OfType<IContentView>().Where(v => containedObjects.Contains(v.Content));
     365        contentViews = contentViews.Where(v => containedObjects.Contains(v.Content));
    361366      } else
    362         contentViews = views.Keys.OfType<IContentView>().Where(v => v.Content == content);
    363 
    364       var progress = new Progress(progressMessage);
    365       foreach (var contentView in contentViews)
    366         ProgressView.Attach(contentView, progress, true);
     367        contentViews = contentViews.Where(v => v.Content == content);
     368
     369      var progress = new Progress(progressMessage, ProgressState.Started);
     370      foreach (var contentView in contentViews) {
     371        progressViews.Add(new ProgressView((Control)contentView, progress));
     372      }
    367373
    368374      contentProgressLookup[content] = progress;
     
    376382        throw new ArgumentException("A progress is already registered for the specified view.", "view");
    377383
    378       var progress = new Progress(progressMessage);
    379       ProgressView.Attach(view, progress, true);
     384      var control = view as Control;
     385      if (control == null) throw new ArgumentException("The passed view must be a control.", "view");
     386
     387      var progress = new Progress(progressMessage, ProgressState.Started);
     388      progressViews.Add(new ProgressView(control, progress));
    380389      viewProgressLookup[view] = progress;
    381390    }
     
    390399
    391400      progress.Finish();
     401      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
     402        progressView.Dispose();
     403        progressViews.Remove(progressView);
     404      }
    392405      contentProgressLookup.Remove(content);
    393406    }
     
    402415
    403416      progress.Finish();
     417      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
     418        progressView.Dispose();
     419        progressViews.Remove(progressView);
     420      }
    404421      viewProgressLookup.Remove(view);
    405422    }
Note: See TracChangeset for help on using the changeset viewer.