Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ObjectViewBase.cs @ 2655

Last change on this file since 2655 was 2655, checked in by swagner, 14 years ago

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Xml;
29using System.Windows.Forms;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Core.Views {
33  /// <summary>
34  /// Base class for all visual representations.
35  /// </summary>
36  public partial class ObjectViewBase : ViewBase, IObjectView {
37    private object obj;
38    /// <summary>
39    /// Gets or sets the item to represent visually.
40    /// </summary>
41    /// <remarks>Calls <see cref="OnItemChanged"/>, <see cref="Refresh"/>,
42    /// <see cref="RemoveItemEvents"/> (if the current item is not null) and
43    /// <see cref="AddItemEvents"/> (if the new item is not null) in the setter.</remarks>
44    public object Object {
45      get { return obj; }
46      protected set {
47        if (InvokeRequired) {
48          Invoke(new Action<object>(delegate(object o) { Object = o; }), value);
49        } else {
50          if (value != obj) {
51            if (obj != null)
52              DeregisterObjectEvents();
53            obj = value;
54            OnObjectChanged();
55            if (obj != null)
56              RegisterObjectEvents();
57          }
58        }
59      }
60    }
61
62    /// <summary>
63    /// Initializes a new instance of <see cref="ViewBase"/> with the caption "View".
64    /// </summary>
65    public ObjectViewBase() {
66      InitializeComponent();
67      Caption = "View";
68    }
69
70    /// <summary>
71    /// Removes the eventhandlers from the current instance.
72    /// </summary>
73    protected virtual void DeregisterObjectEvents() { }
74    /// <summary>
75    /// Adds eventhandlers to the current instance.
76    /// </summary>
77    protected virtual void RegisterObjectEvents() { }
78
79    /// <summary>
80    /// Occurs when the current item was changed.
81    /// </summary>
82    public event EventHandler ObjectChanged;
83    /// <summary>
84    /// Fires a new <c>ItemChanged</c> event.
85    /// </summary>
86    protected virtual void OnObjectChanged() {
87      Caption = "View";
88      if (ObjectChanged != null)
89        ObjectChanged(this, new EventArgs());
90    }
91
92    /// <summary>
93    /// Asynchron call of GUI updating.
94    /// </summary>
95    /// <param name="method">The delegate to invoke.</param>
96    protected new void Invoke(Delegate method) {
97      // enforce context switch to improve GUI response time
98      System.Threading.Thread.Sleep(0);
99
100      // prevent blocking of worker thread in Invoke, if the control is disposed
101      IAsyncResult result = BeginInvoke(method);
102      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
103      if (!IsDisposed) EndInvoke(result);
104    }
105    /// <summary>
106    /// Asynchron call of GUI updating.
107    /// </summary>
108    /// <param name="method">The delegate to invoke.</param>
109    /// <param name="args">The invoke arguments.</param>
110    protected new void Invoke(Delegate method, params object[] args) {
111      // enforce context switch to improve GUI response time
112      System.Threading.Thread.Sleep(0);
113
114      // prevent blocking of worker thread in Invoke, if the control is disposed
115      IAsyncResult result = BeginInvoke(method, args);
116      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
117      if (!IsDisposed) EndInvoke(result);
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.