Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/03/13 15:55:36 (11 years ago)
Author:
ascheibe
Message:

#1042 merged r9849, r9851, r9865, r9867, r9868, r9893, r9894, r9895, r9896, r9900, r9901, r9905, r9907 into stable branch

Location:
stable
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.cs

    r9456 r9933  
    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 ContentView parentView;
     26  internal sealed partial class ProgressView : UserControl {
     27    private const int defaultControlHeight = 88;
     28    private const int collapsedControlHeight = 55;
    3229
    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; }
     30    private readonly Control control;
     31    public Control Control {
     32      get { return control; }
    4133    }
    4234
    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;
     35    private readonly IProgress content;
     36    public IProgress Content {
     37      get { return content; }
    5838    }
    5939
    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();
     40    public ProgressView(Control control, IProgress content)
     41      : base() {
     42      if (control == null) throw new ArgumentNullException("control", "The control is null.");
     43      if (content == null) throw new ArgumentNullException("content", "The passed progress is null.");
     44      InitializeComponent();
     45
     46      this.control = control;
     47      this.content = content;
     48      if (content.ProgressState == ProgressState.Started)
     49        ShowProgress();
     50      RegisterContentEvents();
    6651    }
    6752
    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);
     53    /// <summary>
     54    /// Clean up any resources being used.
     55    /// </summary>
     56    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
     57    protected override void Dispose(bool disposing) {
     58      DeregisterContentEvents();
     59      HideProgress();
     60
     61      if (disposing && (components != null)) {
     62        components.Dispose();
     63      }
     64      base.Dispose(disposing);
    7465    }
    7566
    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       }
     67    private void RegisterContentEvents() {
     68      content.StatusChanged += new EventHandler(progress_StatusChanged);
     69      content.ProgressValueChanged += new EventHandler(progress_ProgressValueChanged);
     70      content.ProgressStateChanged += new EventHandler(Content_ProgressStateChanged);
     71      content.CanBeCanceledChanged += new EventHandler(Content_CanBeCanceledChanged);
    8472    }
    85 
    86     protected override void SetEnabledStateOfControls() {
    87       base.SetEnabledStateOfControls();
    88       cancelButton.Visible = Content != null && Content.CanBeCanceled;
    89       cancelButton.Enabled = Content != null && Content.CanBeCanceled && !ReadOnly;
     73    private void DeregisterContentEvents() {
     74      content.StatusChanged -= new EventHandler(progress_StatusChanged);
     75      content.ProgressValueChanged -= new EventHandler(progress_ProgressValueChanged);
     76      content.ProgressStateChanged -= new EventHandler(Content_ProgressStateChanged);
     77      content.CanBeCanceledChanged -= new EventHandler(Content_CanBeCanceledChanged);
    9078    }
    9179
    9280    private void ShowProgress() {
    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;
     81      if (Control.InvokeRequired) {
     82        Control.Invoke((Action)ShowProgress);
     83        return;
     84      }
     85      int height = Content.CanBeCanceled ? Height : collapsedControlHeight;
    9986
    100           LockBackground();
     87      Left = (Control.ClientRectangle.Width / 2) - (Width / 2);
     88      Top = (Control.ClientRectangle.Height / 2) - (height / 2);
     89      Anchor = AnchorStyles.None;
    10190
    102           if (!parentView.Controls.Contains(this))
    103             parentView.Controls.Add(this);
     91      control.Enabled = false;
     92      Parent = Control.Parent;
     93      BringToFront();
    10494
    105           BringToFront();
    106         }
    107         UpdateProgressValue();
    108         UpdateProgressStatus();
    109         Visible = true;
    110       }
     95      UpdateProgressValue();
     96      UpdateProgressStatus();
     97      UpdateCancelButton();
     98      Visible = true;
    11199    }
    112100
     
    114102      if (InvokeRequired) Invoke((Action)HideProgress);
    115103      else {
    116         if (parentView != null) {
    117           if (parentView.Controls.Contains(this))
    118             parentView.Controls.Remove(this);
    119 
    120           UnlockBackground();
    121         }
     104        control.Enabled = true;
     105        Parent = null;
    122106        Visible = false;
    123107      }
     
    133117
    134118    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();
     119      switch (content.ProgressState) {
     120        case ProgressState.Finished: HideProgress(); break;
     121        case ProgressState.Canceled: HideProgress(); break;
     122        case ProgressState.Started: ShowProgress(); break;
     123        default: throw new NotSupportedException("The progress state " + content.ProgressState + " is not supported by the ProgressView.");
     124      }
    140125    }
    141126
    142127    private void Content_CanBeCanceledChanged(object sender, EventArgs e) {
    143       SetEnabledStateOfControls();
     128      UpdateCancelButton();
    144129    }
    145130
    146     private void LockBackground() {
    147       if (InvokeRequired) {
    148         Invoke((Action)LockBackground);
    149       } else {
    150         parentView.Locked = true;
    151         parentView.ReadOnly = true;
    152         Locked = false;
    153         ReadOnly = false;
    154       }
    155     }
     131    private void UpdateCancelButton() {
     132      cancelButton.Visible = content != null && content.CanBeCanceled;
     133      cancelButton.Enabled = content != null && content.CanBeCanceled;
    156134
    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;
     135      if (content != null && content.CanBeCanceled) {
     136        Height = defaultControlHeight;
     137      } else if (content != null && !content.CanBeCanceled) {
     138        Height = collapsedControlHeight;
    164139      }
    165140    }
     
    168143      if (InvokeRequired) Invoke((Action)UpdateProgressValue);
    169144      else {
    170         if (Content != null) {
    171           double progressValue = Content.ProgressValue;
     145        if (content != null) {
     146          double progressValue = content.ProgressValue;
    172147          if (progressValue <= 0.0 || progressValue > 1.0) {
    173             if (progressBar.Style != ProgressBarStyle.Marquee)
    174               progressBar.Style = ProgressBarStyle.Marquee;
     148            progressBar.Style = ProgressBarStyle.Marquee;
    175149          } else {
    176             if (progressBar.Style != ProgressBarStyle.Blocks)
    177               progressBar.Style = ProgressBarStyle.Blocks;
     150            progressBar.Style = ProgressBarStyle.Blocks;
    178151            progressBar.Value = (int)Math.Round(progressBar.Minimum + progressValue * (progressBar.Maximum - progressBar.Minimum));
    179152          }
     
    184157    private void UpdateProgressStatus() {
    185158      if (InvokeRequired) Invoke((Action)UpdateProgressStatus);
    186       else if (Content != null)
    187         statusLabel.Text = Content.Status;
     159      else if (content != null)
     160        statusLabel.Text = content.Status;
    188161    }
    189162
    190163    private void cancelButton_Click(object sender, EventArgs e) {
    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;
     164      content.Cancel();
    206165    }
    207166  }
  • stable/HeuristicLab.MainForm.WindowsForms/3.3/Controls/ProgressView.designer.cs

    r9456 r9933  
    2727    private System.ComponentModel.IContainer components = null;
    2828
    29     /// <summary>
    30     /// Clean up any resources being used.
    31     /// </summary>
    32     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    33     protected override void Dispose(bool disposing) {
    34       if (disposing && (components != null)) {
    35         components.Dispose();
    36       }
    37       base.Dispose(disposing);
    38     }
    39 
     29   
    4030    #region Component Designer generated code
    4131
     
    4535    /// </summary>
    4636    private void InitializeComponent() {
    47       this.components = new System.ComponentModel.Container();
    4837      this.progressBar = new System.Windows.Forms.ProgressBar();
    4938      this.statusLabel = new System.Windows.Forms.Label();
    5039      this.cancelButton = new System.Windows.Forms.Button();
    5140      this.panel = new System.Windows.Forms.Panel();
    52       this.cancelButtonTimer = new System.Windows.Forms.Timer(this.components);
    5341      this.panel.SuspendLayout();
    5442      this.SuspendLayout();
     
    5644      // progressBar
    5745      //
    58       this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    59                   | System.Windows.Forms.AnchorStyles.Right)));
     46      this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
     47            | System.Windows.Forms.AnchorStyles.Right)));
    6048      this.progressBar.Location = new System.Drawing.Point(3, 3);
    6149      this.progressBar.Name = "progressBar";
     
    6654      // statusLabel
    6755      //
    68       this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    69                   | System.Windows.Forms.AnchorStyles.Right)));
     56      this.statusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
     57            | System.Windows.Forms.AnchorStyles.Right)));
    7058      this.statusLabel.Location = new System.Drawing.Point(3, 33);
    7159      this.statusLabel.Name = "statusLabel";
     
    8674      // panel
    8775      //
    88       this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    89                   | System.Windows.Forms.AnchorStyles.Right)));
     76      this.panel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     77            | System.Windows.Forms.AnchorStyles.Left)
     78            | System.Windows.Forms.AnchorStyles.Right)));
    9079      this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    9180      this.panel.Controls.Add(this.progressBar);
     
    9685      this.panel.Size = new System.Drawing.Size(360, 88);
    9786      this.panel.TabIndex = 3;
    98       //
    99       // cancelButtonTimer
    100       //
    101       this.cancelButtonTimer.Tick += new System.EventHandler(this.cancelButtonTimer_Tick);
    10287      //
    10388      // ProgressView
     
    118103    private System.Windows.Forms.Button cancelButton;
    119104    private System.Windows.Forms.Panel panel;
    120     private System.Windows.Forms.Timer cancelButtonTimer;
    121105  }
    122106}
  • stable/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs

    r9931 r9933  
    345345    #endregion
    346346
     347    #region progress views
     348    private readonly Dictionary<IContent, IProgress> contentProgressLookup = new Dictionary<IContent, IProgress>();
     349    private readonly Dictionary<Control, IProgress> viewProgressLookup = new Dictionary<Control, IProgress>();
     350    private readonly List<ProgressView> progressViews = new List<ProgressView>();
     351
     352    /// <summary>
     353    /// Adds a <see cref="ProgressView"/> to the <see cref="ContentView"/>s showing the specified content.
     354    /// </summary>
     355    public IProgress AddOperationProgressToContent(IContent content, string progressMessage, bool addToObjectGraphObjects = true) {
     356      if (InvokeRequired) {
     357        IProgress result = (IProgress)Invoke((Func<IContent, string, bool, IProgress>)AddOperationProgressToContent, content, progressMessage, addToObjectGraphObjects);
     358        return result;
     359      }
     360      if (contentProgressLookup.ContainsKey(content))
     361        throw new ArgumentException("A progress is already registered for the specified content.", "content");
     362
     363      var contentViews = views.Keys.OfType<ContentView>();
     364      if (!contentViews.Any(v => v.Content == content))
     365        throw new ArgumentException("The content is not displayed in a top-level view", "content");
     366
     367      if (addToObjectGraphObjects) {
     368        var containedObjects = content.GetObjectGraphObjects();
     369        contentViews = contentViews.Where(v => containedObjects.Contains(v.Content));
     370      } else
     371        contentViews = contentViews.Where(v => v.Content == content);
     372
     373      var progress = new Progress(progressMessage, ProgressState.Started);
     374      foreach (var contentView in contentViews) {
     375        progressViews.Add(new ProgressView(contentView, progress));
     376      }
     377
     378      contentProgressLookup[content] = progress;
     379      return progress;
     380    }
     381
     382    /// <summary>
     383    /// Adds a <see cref="ProgressView"/> to the specified view.
     384    /// </summary>
     385    public IProgress AddOperationProgressToView(Control control, string progressMessage) {
     386      var progress = new Progress(progressMessage, ProgressState.Started);
     387      AddOperationProgressToView(control, progress);
     388      return progress;
     389    }
     390
     391    public void AddOperationProgressToView(Control control, IProgress progress) {
     392      if (InvokeRequired) {
     393        Invoke((Action<Control, IProgress>)AddOperationProgressToView, control, progress);
     394        return;
     395      }
     396      if (control == null) throw new ArgumentNullException("control", "The view must not be null.");
     397      if (progress == null) throw new ArgumentNullException("progress", "The progress must not be null.");
     398
     399      IProgress oldProgress;
     400      if (viewProgressLookup.TryGetValue(control, out oldProgress)) {
     401        foreach (var progressView in progressViews.Where(v => v.Content == oldProgress).ToList()) {
     402          progressView.Dispose();
     403          progressViews.Remove(progressView);
     404        }
     405        viewProgressLookup.Remove(control);
     406      }
     407
     408      progressViews.Add(new ProgressView(control, progress));
     409      viewProgressLookup[control] = progress;
     410    }
     411
     412    /// <summary>
     413    /// Removes an existing <see cref="ProgressView"/> from the <see cref="ContentView"/>s showing the specified content.
     414    /// </summary>
     415    public void RemoveOperationProgressFromContent(IContent content, bool finishProgress = true) {
     416      if (InvokeRequired) {
     417        Invoke((Action<IContent, bool>)RemoveOperationProgressFromContent, content, finishProgress);
     418        return;
     419      }
     420
     421      IProgress progress;
     422      if (!contentProgressLookup.TryGetValue(content, out progress))
     423        throw new ArgumentException("No progress is registered for the specified content.", "content");
     424
     425      if (finishProgress) progress.Finish();
     426      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
     427        progressView.Dispose();
     428        progressViews.Remove(progressView);
     429      }
     430      contentProgressLookup.Remove(content);
     431
     432    }
     433
     434    /// <summary>
     435    /// Removes an existing <see cref="ProgressView"/> from the specified view.
     436    /// </summary>
     437    public void RemoveOperationProgressFromView(Control control, bool finishProgress = true) {
     438      IProgress progress;
     439      if (!viewProgressLookup.TryGetValue(control, out progress))
     440        throw new ArgumentException("No progress is registered for the specified control.", "control");
     441
     442      if (finishProgress) progress.Finish();
     443      foreach (var progressView in progressViews.Where(v => v.Content == progress).ToList()) {
     444        progressView.Dispose();
     445        progressViews.Remove(progressView);
     446      }
     447      viewProgressLookup.Remove(control);
     448    }
     449    #endregion
     450
    347451    #region create menu and toolbar
    348452    private void CreateGUI() {
Note: See TracChangeset for help on using the changeset viewer.