External/Aga.Controls/Tree/NodeControls/BindableControl.cs
author sl
Thu, 01 Jan 2015 23:35:49 +0100
changeset 405 5715aefd2bcc
permissions -rw-r--r--
SharpDisplay: Migrating to new robust client scheme.
moel@345
     1
using System;
moel@345
     2
using System.Collections.Generic;
moel@345
     3
using System.Text;
moel@345
     4
using System.Reflection;
moel@345
     5
using System.ComponentModel;
moel@345
     6
moel@345
     7
namespace Aga.Controls.Tree.NodeControls
moel@345
     8
{
moel@345
     9
	public abstract class BindableControl : NodeControl
moel@345
    10
	{
moel@345
    11
		private struct MemberAdapter
moel@345
    12
		{
moel@345
    13
			private object _obj;
moel@345
    14
			private PropertyInfo _pi;
moel@345
    15
			private FieldInfo _fi;
moel@345
    16
moel@345
    17
			public static readonly MemberAdapter Empty = new MemberAdapter();
moel@345
    18
moel@345
    19
			public Type MemberType
moel@345
    20
			{
moel@345
    21
				get
moel@345
    22
				{
moel@345
    23
					if (_pi != null)
moel@345
    24
						return _pi.PropertyType;
moel@345
    25
					else if (_fi != null)
moel@345
    26
						return _fi.FieldType;
moel@345
    27
					else
moel@345
    28
						return null;
moel@345
    29
				}
moel@345
    30
			}
moel@345
    31
moel@345
    32
			public object Value
moel@345
    33
			{
moel@345
    34
				get
moel@345
    35
				{
moel@345
    36
					if (_pi != null && _pi.CanRead)
moel@345
    37
						return _pi.GetValue(_obj, null);
moel@345
    38
					else if (_fi != null)
moel@345
    39
						return _fi.GetValue(_obj);
moel@345
    40
					else
moel@345
    41
						return null;
moel@345
    42
				}
moel@345
    43
				set
moel@345
    44
				{
moel@345
    45
					if (_pi != null && _pi.CanWrite)
moel@345
    46
						_pi.SetValue(_obj, value, null);
moel@345
    47
					else if (_fi != null)
moel@345
    48
						_fi.SetValue(_obj, value);
moel@345
    49
				}
moel@345
    50
			}
moel@345
    51
moel@345
    52
			public MemberAdapter(object obj, PropertyInfo pi)
moel@345
    53
			{
moel@345
    54
				_obj = obj;
moel@345
    55
				_pi = pi;
moel@345
    56
				_fi = null;
moel@345
    57
			}
moel@345
    58
moel@345
    59
			public MemberAdapter(object obj, FieldInfo fi)
moel@345
    60
			{
moel@345
    61
				_obj = obj;
moel@345
    62
				_fi = fi;
moel@345
    63
				_pi = null;
moel@345
    64
			}
moel@345
    65
		}
moel@345
    66
moel@345
    67
		#region Properties
moel@345
    68
moel@345
    69
		private bool _virtualMode = false;
moel@345
    70
		[DefaultValue(false), Category("Data")]
moel@345
    71
		public bool VirtualMode
moel@345
    72
		{
moel@345
    73
			get { return _virtualMode; }
moel@345
    74
			set { _virtualMode = value; }
moel@345
    75
		}
moel@345
    76
moel@345
    77
		private string _propertyName = "";
moel@345
    78
		[DefaultValue(""), Category("Data")]
moel@345
    79
		public string DataPropertyName
moel@345
    80
		{
moel@345
    81
			get { return _propertyName; }
moel@345
    82
			set 
moel@345
    83
			{
moel@345
    84
				if (_propertyName == null)
moel@345
    85
					_propertyName = string.Empty;
moel@345
    86
				_propertyName = value; 
moel@345
    87
			}
moel@345
    88
		}
moel@345
    89
moel@345
    90
		private bool _incrementalSearchEnabled = false;
moel@345
    91
		[DefaultValue(false)]
moel@345
    92
		public bool IncrementalSearchEnabled
moel@345
    93
		{
moel@345
    94
			get { return _incrementalSearchEnabled; }
moel@345
    95
			set { _incrementalSearchEnabled = value; }
moel@345
    96
		}
moel@345
    97
moel@345
    98
		#endregion
moel@345
    99
moel@345
   100
		public virtual object GetValue(TreeNodeAdv node)
moel@345
   101
		{
moel@345
   102
			if (VirtualMode)
moel@345
   103
			{
moel@345
   104
				NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
moel@345
   105
				OnValueNeeded(args);
moel@345
   106
				return args.Value;
moel@345
   107
			}
moel@345
   108
			else
moel@345
   109
			{
moel@345
   110
				try
moel@345
   111
				{
moel@345
   112
					return GetMemberAdapter(node).Value;
moel@345
   113
				}
moel@345
   114
				catch (TargetInvocationException ex)
moel@345
   115
				{
moel@345
   116
					if (ex.InnerException != null)
moel@345
   117
						throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
moel@345
   118
					else
moel@345
   119
						throw new ArgumentException(ex.Message);
moel@345
   120
				}
moel@345
   121
			}
moel@345
   122
		}
moel@345
   123
moel@345
   124
		public virtual void SetValue(TreeNodeAdv node, object value)
moel@345
   125
		{
moel@345
   126
			if (VirtualMode)
moel@345
   127
			{
moel@345
   128
				NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
moel@345
   129
				args.Value = value;
moel@345
   130
				OnValuePushed(args);
moel@345
   131
			}
moel@345
   132
			else
moel@345
   133
			{
moel@345
   134
				try
moel@345
   135
				{
moel@345
   136
					MemberAdapter ma = GetMemberAdapter(node);
moel@345
   137
					ma.Value = value;
moel@345
   138
				}
moel@345
   139
				catch (TargetInvocationException ex)
moel@345
   140
				{
moel@345
   141
					if (ex.InnerException != null)
moel@345
   142
						throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
moel@345
   143
					else
moel@345
   144
						throw new ArgumentException(ex.Message);
moel@345
   145
				}
moel@345
   146
			}
moel@345
   147
		}
moel@345
   148
moel@345
   149
		public Type GetPropertyType(TreeNodeAdv node)
moel@345
   150
		{
moel@345
   151
			return GetMemberAdapter(node).MemberType;
moel@345
   152
		}
moel@345
   153
moel@345
   154
		private MemberAdapter GetMemberAdapter(TreeNodeAdv node)
moel@345
   155
		{
moel@345
   156
			if (node.Tag != null && !string.IsNullOrEmpty(DataPropertyName))
moel@345
   157
			{
moel@345
   158
				Type type = node.Tag.GetType();
moel@345
   159
				PropertyInfo pi = type.GetProperty(DataPropertyName);
moel@345
   160
				if (pi != null)
moel@345
   161
					return new MemberAdapter(node.Tag, pi);
moel@345
   162
				else
moel@345
   163
				{
moel@345
   164
					FieldInfo fi = type.GetField(DataPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
moel@345
   165
					if (fi != null)
moel@345
   166
						return new MemberAdapter(node.Tag, fi);
moel@345
   167
				}
moel@345
   168
			}
moel@345
   169
			return MemberAdapter.Empty;
moel@345
   170
		}
moel@345
   171
moel@345
   172
		public override string ToString()
moel@345
   173
		{
moel@345
   174
			if (string.IsNullOrEmpty(DataPropertyName))
moel@345
   175
				return GetType().Name;
moel@345
   176
			else
moel@345
   177
				return string.Format("{0} ({1})", GetType().Name, DataPropertyName);
moel@345
   178
		}
moel@345
   179
moel@345
   180
		public event EventHandler<NodeControlValueEventArgs> ValueNeeded;
moel@345
   181
		private void OnValueNeeded(NodeControlValueEventArgs args)
moel@345
   182
		{
moel@345
   183
			if (ValueNeeded != null)
moel@345
   184
				ValueNeeded(this, args);
moel@345
   185
		}
moel@345
   186
moel@345
   187
		public event EventHandler<NodeControlValueEventArgs> ValuePushed;
moel@345
   188
		private void OnValuePushed(NodeControlValueEventArgs args)
moel@345
   189
		{
moel@345
   190
			if (ValuePushed != null)
moel@345
   191
				ValuePushed(this, args);
moel@345
   192
		}
moel@345
   193
	}
moel@345
   194
}