Free cookie consent management tool by TermsFeed Policy Generator

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

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

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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