Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ObjectView.cs @ 2676

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

Operator architecture refactoring (#95)

  • continued work on adapting and refactoring HeuristicLab.Data according to the changes in HeuristicLab.Core
  • unified visual appearance of views
File size: 4.4 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;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.Core.Views {
34  /// <summary>
35  /// Base class for all visual representations.
36  /// </summary>
37  public partial class ObjectView : ViewBase, IObjectView {
38    private object obj;
39    /// <summary>
40    /// Gets or sets the item to represent visually.
41    /// </summary>
42    /// <remarks>Calls <see cref="OnItemChanged"/>, <see cref="Refresh"/>,
43    /// <see cref="RemoveItemEvents"/> (if the current item is not null) and
44    /// <see cref="AddItemEvents"/> (if the new item is not null) in the setter.</remarks>
45    public object Object {
46      get { return obj; }
47      set {
48        if ((value != null) && (!MainFormManager.ViewCanViewObject(this, value)))
49          throw new ArgumentException(string.Format("View \"{0}\" cannot view object \"{1}\".", this.GetType().Name, value.GetType().Name));
50        if (InvokeRequired) {
51          Invoke(new Action<object>(delegate(object o) { Object = o; }), value);
52        } else {
53          if (value != obj) {
54            if (obj != null)
55              DeregisterObjectEvents();
56            obj = value;
57            OnObjectChanged();
58            if (obj != null)
59              RegisterObjectEvents();
60          }
61        }
62      }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="ViewBase"/> with the caption "View".
67    /// </summary>
68    public ObjectView() {
69      InitializeComponent();
70      Caption = "View";
71    }
72
73    /// <summary>
74    /// Removes the eventhandlers from the current instance.
75    /// </summary>
76    protected virtual void DeregisterObjectEvents() { }
77    /// <summary>
78    /// Adds eventhandlers to the current instance.
79    /// </summary>
80    protected virtual void RegisterObjectEvents() { }
81
82    /// <summary>
83    /// Occurs when the current item was changed.
84    /// </summary>
85    public event EventHandler ObjectChanged;
86    /// <summary>
87    /// Fires a new <c>ItemChanged</c> event.
88    /// </summary>
89    protected virtual void OnObjectChanged() {
90      Caption = "View";
91      if (ObjectChanged != null)
92        ObjectChanged(this, new EventArgs());
93    }
94
95    /// <summary>
96    /// Asynchron call of GUI updating.
97    /// </summary>
98    /// <param name="method">The delegate to invoke.</param>
99    protected new void Invoke(Delegate method) {
100      // enforce context switch to improve GUI response time
101      System.Threading.Thread.Sleep(0);
102
103      // prevent blocking of worker thread in Invoke, if the control is disposed
104      IAsyncResult result = BeginInvoke(method);
105      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
106      if (!IsDisposed) EndInvoke(result);
107    }
108    /// <summary>
109    /// Asynchron call of GUI updating.
110    /// </summary>
111    /// <param name="method">The delegate to invoke.</param>
112    /// <param name="args">The invoke arguments.</param>
113    protected new void Invoke(Delegate method, params object[] args) {
114      // enforce context switch to improve GUI response time
115      System.Threading.Thread.Sleep(0);
116
117      // prevent blocking of worker thread in Invoke, if the control is disposed
118      IAsyncResult result = BeginInvoke(method, args);
119      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
120      if (!IsDisposed) EndInvoke(result);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.