Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemViewBase.cs @ 2636

Last change on this file since 2636 was 2555, checked in by swagner, 15 years ago

Continued work on Optimizer (#770)

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