Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Core/ViewBase.cs @ 15033

Last change on this file since 15033 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 3.5 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;
30
31namespace HeuristicLab.Core {
32  public partial class ViewBase : UserControl, IView {
33    private IItem myItem;
34    public IItem Item {
35      get { return myItem; }
36      protected set {
37        if (value != myItem) {
38          if (myItem != null)
39            RemoveItemEvents();
40          myItem = value;
41          if (myItem != null)
42            AddItemEvents();
43          OnItemChanged();
44          Refresh();
45        }
46      }
47    }
48    private string myCaption;
49    public string Caption {
50      get { return myCaption; }
51      set {
52        if (value != myCaption) {
53          myCaption = value;
54          OnCaptionChanged();
55        }
56      }
57    }
58
59    public ViewBase() {
60      InitializeComponent();
61      Caption = "View";
62    }
63
64    protected virtual void RemoveItemEvents() { }
65    protected virtual void AddItemEvents() { }
66
67    public override void Refresh() {
68      if (InvokeRequired) {
69        Invoke(new MethodInvoker(Refresh));
70      } else {
71        UpdateControls();
72        base.Refresh();
73      }
74    }
75    protected virtual void UpdateControls() {
76      if (Item == null)
77        Caption = "View";
78      else
79        Caption = "View (" + Item.GetType().Name + ")";
80     
81    }
82
83    public event EventHandler ItemChanged;
84    protected virtual void OnItemChanged() {
85      if (ItemChanged != null)
86        ItemChanged(this, new EventArgs());
87    }
88    public event EventHandler CaptionChanged;
89    protected virtual void OnCaptionChanged() {
90      if (CaptionChanged != null)
91        CaptionChanged(this, new EventArgs());
92    }
93
94    protected new void Invoke(Delegate method) {
95      // enforce context switch to improve GUI response time
96      System.Threading.Thread.Sleep(0);
97
98      // prevent blocking of worker thread in Invoke, if the control is disposed
99      IAsyncResult result = BeginInvoke(method);
100      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
101      if (!IsDisposed) EndInvoke(result);
102    }
103    protected new void Invoke(Delegate method, params object[] args) {
104      // enforce context switch to improve GUI response time
105      System.Threading.Thread.Sleep(0);
106
107      // prevent blocking of worker thread in Invoke, if the control is disposed
108      IAsyncResult result = BeginInvoke(method, args);
109      while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
110      if (!IsDisposed) EndInvoke(result);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.