#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Xml; using System.Windows.Forms; namespace HeuristicLab.Core { /// /// Base class for all visual representations. /// public partial class ViewBase : UserControl, IView { private IItem myItem; /// /// Gets or sets the item to represent visually. /// /// Calls , , /// (if the current item is not null) and /// (if the new item is not null) in the setter. public IItem Item { get { return myItem; } protected set { if (value != myItem) { if (myItem != null) RemoveItemEvents(); myItem = value; if (myItem != null) AddItemEvents(); OnItemChanged(); Refresh(); } } } private string myCaption; /// /// Gets or sets the caption of the current instance. /// /// Call in the setter if a new item is set. public string Caption { get { return myCaption; } set { if (value != myCaption) { myCaption = value; OnCaptionChanged(); } } } /// /// Initializes a new instance of with the caption "View". /// public ViewBase() { InitializeComponent(); Caption = "View"; } /// /// Removes the eventhandlers from the current instance. /// protected virtual void RemoveItemEvents() { } /// /// Adds eventhandlers to the current instance. /// protected virtual void AddItemEvents() { } /// /// Refreshes the current view. /// /// Creates a new if an invoke is required /// (see .
/// Otherwise calls and of base class /// .
public override void Refresh() { if (InvokeRequired) { Invoke(new MethodInvoker(Refresh)); } else { UpdateControls(); base.Refresh(); } } /// /// Updates the controls with the latest values of the model. /// protected virtual void UpdateControls() { if (Item == null) Caption = "View"; else Caption = "View (" + Item.GetType().Name + ")"; } /// /// Occurs when the current item was changed. /// public event EventHandler ItemChanged; /// /// Fires a new ItemChanged event. /// protected virtual void OnItemChanged() { if (ItemChanged != null) ItemChanged(this, new EventArgs()); } /// /// Occurs when the current caption was changed. /// public event EventHandler CaptionChanged; /// /// Fires a new CaptionChanged event. /// protected virtual void OnCaptionChanged() { if (CaptionChanged != null) CaptionChanged(this, new EventArgs()); } /// /// Asynchron call of GUI updating. /// /// The delegate to invoke. protected new void Invoke(Delegate method) { // enforce context switch to improve GUI response time System.Threading.Thread.Sleep(0); // prevent blocking of worker thread in Invoke, if the control is disposed IAsyncResult result = BeginInvoke(method); while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { } if (!IsDisposed) EndInvoke(result); } /// /// Asynchron call of GUI updating. /// /// The delegate to invoke. /// The invoke arguments. protected new void Invoke(Delegate method, params object[] args) { // enforce context switch to improve GUI response time System.Threading.Thread.Sleep(0); // prevent blocking of worker thread in Invoke, if the control is disposed IAsyncResult result = BeginInvoke(method, args); while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { } if (!IsDisposed) EndInvoke(result); } } }