#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;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Core.Views {
///
/// Base class for all visual representations.
///
public partial class ItemViewBase : ViewBase, IItemView {
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();
}
}
}
///
/// Initializes a new instance of with the caption "View".
///
public ItemViewBase() {
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 Action(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 = Item.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());
}
///
/// 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);
}
}
}