Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/10 21:00:30 (14 years ago)
Author:
mkommend
Message:

implemented ContentViews and propagation of view state changes (ticket #982)

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/AsynchronousStorableContentView.cs

    r3410 r3416  
    2828using System.Text;
    2929using System.Windows.Forms;
     30using HeuristicLab.Common;
     31using System.Threading;
    3032
    3133namespace HeuristicLab.MainForm.WindowsForms {
    32   public partial class ContentView : View, IContentView {
    33     private object content;
    34     public object Content {
    35       get { return content; }
    36       set {
    37         if ((value != null) && (!MainFormManager.ViewCanViewObject(this, value)))
    38           throw new ArgumentException(string.Format("View \"{0}\" cannot view object \"{1}\".", this.GetType().Name, value.GetType().Name));
    39         if (InvokeRequired) {
    40           Invoke(new Action<object>(delegate(object o) { this.Content = o; }), value);
    41         } else {
    42           if (this.content != value) {
    43             if (this.content != null) this.DeregisterContentEvents();
    44             this.content = value;
    45             if (this.content != null) this.RegisterContentEvents();
    46             this.OnContentChanged();
    47           }
    48         }
    49       }
     34  public partial class AsynchronousStorableContentView : StorableContentView {
     35    public AsynchronousStorableContentView()
     36      : base() {
     37      InitializeComponent();
    5038    }
    51     private bool saveEnabled;
    52     public bool SaveEnabled {
    53       get { return saveEnabled; }
    54       protected set {
    55         if (value != saveEnabled) {
    56           saveEnabled = value;
    57           OnChanged();
    58         }
     39    public AsynchronousStorableContentView(IStorableContent content)
     40      : base() {
     41      this.Content = content;
     42    }
     43
     44    /// <summary>
     45    /// Asynchronous call of GUI updating.
     46    /// </summary>
     47    /// <param name="method">The delegate to invoke.</param>
     48    protected new void Invoke(Delegate method) {
     49      // prevents blocking of worker thread in Invoke, if the control is disposed
     50      IAsyncResult result = BeginInvoke(method);
     51      result.AsyncWaitHandle.WaitOne(1000, false);
     52      if (result.IsCompleted) try { EndInvoke(result); }
     53        catch (ObjectDisposedException) { } else {
     54        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
     55          new WaitOrTimerCallback((x, b) => { try { EndInvoke(result); } catch (ObjectDisposedException) { } }),
     56          null, -1, true);
    5957      }
    6058    }
    6159
    62     public ContentView()
    63       : base() {
    64       InitializeComponent();
    65       saveEnabled = true;
    66     }
    67     public ContentView(object content)
    68       : this() {
    69       this.content = content;
    70     }
    71 
    7260    /// <summary>
    73     /// Adds eventhandlers to the current instance.
     61    /// Asynchronous call of GUI updating.
    7462    /// </summary>
    75     protected virtual void RegisterContentEvents() {
    76     }
    77 
    78     /// <summary>
    79     /// Removes the eventhandlers from the current instance.
    80     /// </summary>
    81     protected virtual void DeregisterContentEvents() {
    82     }
    83 
    84     /// <summary>
    85     /// Is called when the content property changes.
    86     /// </summary>
    87     protected virtual void OnContentChanged() {
     63    /// <param name="method">The delegate to invoke.</param>
     64    /// <param name="args">The invoke arguments.</param>
     65    protected new void Invoke(Delegate method, params object[] args) {
     66      // prevents blocking of worker thread in Invoke, if the control is disposed
     67      IAsyncResult result = BeginInvoke(method, args);
     68      result.AsyncWaitHandle.WaitOne(1000, false);
     69      if (result.IsCompleted) try { EndInvoke(result); }
     70        catch (ObjectDisposedException) { } else {
     71        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
     72          new WaitOrTimerCallback((x, b) => { try { EndInvoke(result); } catch (ObjectDisposedException) { } }),
     73          null, -1, true);
     74      }
    8875    }
    8976  }
Note: See TracChangeset for help on using the changeset viewer.