[2655] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Xml;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Core.Views {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Base class for all visual representations.
|
---|
| 35 | /// </summary>
|
---|
[2664] | 36 | public partial class ObjectView : ViewBase, IObjectView {
|
---|
[2655] | 37 | private object obj;
|
---|
| 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 object Object {
|
---|
| 45 | get { return obj; }
|
---|
| 46 | protected set {
|
---|
| 47 | if (InvokeRequired) {
|
---|
| 48 | Invoke(new Action<object>(delegate(object o) { Object = o; }), value);
|
---|
| 49 | } else {
|
---|
| 50 | if (value != obj) {
|
---|
| 51 | if (obj != null)
|
---|
| 52 | DeregisterObjectEvents();
|
---|
| 53 | obj = value;
|
---|
| 54 | OnObjectChanged();
|
---|
| 55 | if (obj != null)
|
---|
| 56 | RegisterObjectEvents();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// Initializes a new instance of <see cref="ViewBase"/> with the caption "View".
|
---|
| 64 | /// </summary>
|
---|
[2664] | 65 | public ObjectView() {
|
---|
[2655] | 66 | InitializeComponent();
|
---|
| 67 | Caption = "View";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /// <summary>
|
---|
| 71 | /// Removes the eventhandlers from the current instance.
|
---|
| 72 | /// </summary>
|
---|
| 73 | protected virtual void DeregisterObjectEvents() { }
|
---|
| 74 | /// <summary>
|
---|
| 75 | /// Adds eventhandlers to the current instance.
|
---|
| 76 | /// </summary>
|
---|
| 77 | protected virtual void RegisterObjectEvents() { }
|
---|
| 78 |
|
---|
| 79 | /// <summary>
|
---|
| 80 | /// Occurs when the current item was changed.
|
---|
| 81 | /// </summary>
|
---|
| 82 | public event EventHandler ObjectChanged;
|
---|
| 83 | /// <summary>
|
---|
| 84 | /// Fires a new <c>ItemChanged</c> event.
|
---|
| 85 | /// </summary>
|
---|
| 86 | protected virtual void OnObjectChanged() {
|
---|
| 87 | Caption = "View";
|
---|
| 88 | if (ObjectChanged != null)
|
---|
| 89 | ObjectChanged(this, new EventArgs());
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /// <summary>
|
---|
| 93 | /// Asynchron call of GUI updating.
|
---|
| 94 | /// </summary>
|
---|
| 95 | /// <param name="method">The delegate to invoke.</param>
|
---|
| 96 | protected new void Invoke(Delegate method) {
|
---|
| 97 | // enforce context switch to improve GUI response time
|
---|
| 98 | System.Threading.Thread.Sleep(0);
|
---|
| 99 |
|
---|
| 100 | // prevent blocking of worker thread in Invoke, if the control is disposed
|
---|
| 101 | IAsyncResult result = BeginInvoke(method);
|
---|
| 102 | while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
|
---|
| 103 | if (!IsDisposed) EndInvoke(result);
|
---|
| 104 | }
|
---|
| 105 | /// <summary>
|
---|
| 106 | /// Asynchron call of GUI updating.
|
---|
| 107 | /// </summary>
|
---|
| 108 | /// <param name="method">The delegate to invoke.</param>
|
---|
| 109 | /// <param name="args">The invoke arguments.</param>
|
---|
| 110 | protected new void Invoke(Delegate method, params object[] args) {
|
---|
| 111 | // enforce context switch to improve GUI response time
|
---|
| 112 | System.Threading.Thread.Sleep(0);
|
---|
| 113 |
|
---|
| 114 | // prevent blocking of worker thread in Invoke, if the control is disposed
|
---|
| 115 | IAsyncResult result = BeginInvoke(method, args);
|
---|
| 116 | while ((!result.AsyncWaitHandle.WaitOne(100, false)) && (!IsDisposed)) { }
|
---|
| 117 | if (!IsDisposed) EndInvoke(result);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|