Changeset 2233 for trunk/sources/HeuristicLab.MainForm/3.2/ViewBase.cs
- Timestamp:
- 08/04/09 16:52:49 (15 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm/3.2/ViewBase.cs
r2229 r2233 29 29 using System.Windows.Forms; 30 30 31 namespace HeuristicLab.Core { 32 /// <summary> 33 /// Base class for all visual representations. 34 /// </summary> 35 public partial class ViewBase : UserControl, IView { 36 private IItem myItem; 37 /// <summary> 38 /// Gets or sets the item to represent visually. 39 /// </summary> 40 /// <remarks>Calls <see cref="OnItemChanged"/>, <see cref="Refresh"/>, 41 /// <see cref="RemoveItemEvents"/> (if the current item is not null) and 42 /// <see cref="AddItemEvents"/> (if the new item is not null) in the setter.</remarks> 43 public IItem Item { 44 get { return myItem; } 45 protected set { 46 if (value != myItem) { 47 if (myItem != null) 48 RemoveItemEvents(); 49 myItem = value; 50 if (myItem != null) 51 AddItemEvents(); 52 OnItemChanged(); 53 Refresh(); 54 } 55 } 31 namespace HeuristicLab.MainForm { 32 public partial class ViewBase : UserControl { 33 public ViewBase() { 34 InitializeComponent(); 56 35 } 36 37 public ViewBase(IMainForm mainForm) 38 : this() { 39 this.mainForm = mainForm; 40 } 41 57 42 private string myCaption; 58 /// <summary>59 /// Gets or sets the caption of the current instance.60 /// </summary>61 /// <remarks>Call <see cref="OnCaptionChanged"/> in the setter if a new item is set.</remarks>62 43 public string Caption { 63 44 get { return myCaption; } … … 70 51 } 71 52 72 /// <summary>73 /// Initializes a new instance of <see cref="ViewBase"/> with the caption "View".74 /// </summary>75 public ViewBase() {76 InitializeComponent();77 Caption = "View";78 }79 80 /// <summary>81 /// Removes the eventhandlers from the current instance.82 /// </summary>83 protected virtual void RemoveItemEvents() { }84 /// <summary>85 /// Adds eventhandlers to the current instance.86 /// </summary>87 protected virtual void AddItemEvents() { }88 89 /// <summary>90 /// Refreshes the current view.91 /// </summary>92 /// <remarks>Creates a new <see cref="MethodInvoker"/> if an invoke is required93 /// (see <see cref="Control.InvokeRequired"/>.<br/>94 /// Otherwise calls <see cref="UpdateControls"/> and <see cref="Control.Refresh"/> of base class95 /// <see cref="System.Windows.Forms.UserControl"/>.</remarks>96 public override void Refresh() {97 if (InvokeRequired) {98 Invoke(new MethodInvoker(Refresh));99 } else {100 UpdateControls();101 base.Refresh();102 }103 }104 /// <summary>105 /// Updates the controls with the latest values of the model.106 /// </summary>107 protected virtual void UpdateControls() {108 if (Item == null)109 Caption = "View";110 else111 Caption = "View (" + Item.GetType().Name + ")";112 113 }114 115 /// <summary>116 /// Occurs when the current item was changed.117 /// </summary>118 public event EventHandler ItemChanged;119 /// <summary>120 /// Fires a new <c>ItemChanged</c> event.121 /// </summary>122 protected virtual void OnItemChanged() {123 if (ItemChanged != null)124 ItemChanged(this, new EventArgs());125 }126 /// <summary>127 /// Occurs when the current caption was changed.128 /// </summary>129 53 public event EventHandler CaptionChanged; 130 /// <summary>131 /// Fires a new <c>CaptionChanged</c> event.132 /// </summary>133 54 protected virtual void OnCaptionChanged() { 134 55 if (CaptionChanged != null) … … 136 57 } 137 58 138 /// <summary> 139 /// Asynchron call of GUI updating. 140 /// </summary> 141 /// <param name="method">The delegate to invoke.</param> 142 protected new void Invoke(Delegate method) { 143 // enforce context switch to improve GUI response time 144 System.Threading.Thread.Sleep(0); 59 private IMainForm mainForm; 60 public IMainForm MainForm { 61 get { return this.mainForm; } 62 set { this.mainForm = value; } 63 } 145 64 146 // prevent blocking of worker thread in Invoke, if the control is disposed147 IAsyncResult result = BeginInvoke(method);148 while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }149 if (!IsDisposed) EndInvoke(result);150 }151 /// <summary>152 /// Asynchron call of GUI updating.153 /// </summary>154 /// <param name="method">The delegate to invoke.</param>155 /// <param name="args">The invoke arguments.</param>156 protected new void Invoke(Delegate method, params object[] args) {157 // enforce context switch to improve GUI response time158 System.Threading.Thread.Sleep(0);159 160 // prevent blocking of worker thread in Invoke, if the control is disposed161 IAsyncResult result = BeginInvoke(method, args);162 while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }163 if (!IsDisposed) EndInvoke(result);164 }165 65 } 166 66 }
Note: See TracChangeset
for help on using the changeset viewer.