Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ViewBase.cs @ 1548

Last change on this file since 1548 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 5.5 KB
RevLine 
[2]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;
30
31namespace HeuristicLab.Core {
[776]32  /// <summary>
33  /// Base class for all visual representations.
34  /// </summary>
[2]35  public partial class ViewBase : UserControl, IView {
36    private IItem myItem;
[776]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>
[2]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      }
56    }
57    private string myCaption;
[776]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>
[2]62    public string Caption {
63      get { return myCaption; }
64      set {
65        if (value != myCaption) {
66          myCaption = value;
67          OnCaptionChanged();
68        }
69      }
70    }
71
[776]72    /// <summary>
73    /// Initializes a new instance of <see cref="ViewBase"/> with the caption "View".
74    /// </summary>
[2]75    public ViewBase() {
76      InitializeComponent();
77      Caption = "View";
78    }
79
[776]80    /// <summary>
81    /// Removes the eventhandlers from the current instance.
82    /// </summary>
[2]83    protected virtual void RemoveItemEvents() { }
[776]84    /// <summary>
85    /// Adds eventhandlers to the current instance.
86    /// </summary>
[2]87    protected virtual void AddItemEvents() { }
88
[776]89    /// <summary>
90    /// Refreshes the current view.
91    /// </summary>
92    /// <remarks>Creates a new <see cref="MethodInvoker"/> if an invoke is required
93    /// (see <see cref="Control.InvokeRequired"/>.<br/>
94    /// Otherwise calls <see cref="UpdateControls"/> and <see cref="Control.Refresh"/> of base class
95    /// <see cref="System.Windows.Forms.UserControl"/>.</remarks>
[2]96    public override void Refresh() {
97      if (InvokeRequired) {
98        Invoke(new MethodInvoker(Refresh));
99      } else {
100        UpdateControls();
101        base.Refresh();
102      }
103    }
[776]104    /// <summary>
105    /// Updates the controls with the latest values of the model.
106    /// </summary>
[2]107    protected virtual void UpdateControls() {
108      if (Item == null)
109        Caption = "View";
110      else
111        Caption = "View (" + Item.GetType().Name + ")";
112     
113    }
114
[776]115    /// <summary>
116    /// Occurs when the current item was changed.
117    /// </summary>
[2]118    public event EventHandler ItemChanged;
[776]119    /// <summary>
120    /// Fires a new <c>ItemChanged</c> event.
121    /// </summary>
[2]122    protected virtual void OnItemChanged() {
123      if (ItemChanged != null)
124        ItemChanged(this, new EventArgs());
125    }
[776]126    /// <summary>
127    /// Occurs when the current caption was changed.
128    /// </summary>
[2]129    public event EventHandler CaptionChanged;
[776]130    /// <summary>
131    /// Fires a new <c>CaptionChanged</c> event.
132    /// </summary>
[2]133    protected virtual void OnCaptionChanged() {
134      if (CaptionChanged != null)
135        CaptionChanged(this, new EventArgs());
136    }
137
[776]138    /// <summary>
139    /// Asynchron call of GUI updating.
140    /// </summary>
141    /// <param name="method">The delegate to invoke.</param>
[2]142    protected new void Invoke(Delegate method) {
143      // enforce context switch to improve GUI response time
144      System.Threading.Thread.Sleep(0);
145
146      // prevent blocking of worker thread in Invoke, if the control is disposed
147      IAsyncResult result = BeginInvoke(method);
148      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
149      if (!IsDisposed) EndInvoke(result);
150    }
[776]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>
[2]156    protected new void Invoke(Delegate method, params object[] args) {
157      // enforce context switch to improve GUI response time
158      System.Threading.Thread.Sleep(0);
159
160      // prevent blocking of worker thread in Invoke, if the control is disposed
161      IAsyncResult result = BeginInvoke(method, args);
162      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
163      if (!IsDisposed) EndInvoke(result);
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.