1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/External/Aga.Controls/Tree/NodeControls/BindableControl.cs Sun May 27 15:16:19 2012 +0000
1.3 @@ -0,0 +1,194 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Text;
1.7 +using System.Reflection;
1.8 +using System.ComponentModel;
1.9 +
1.10 +namespace Aga.Controls.Tree.NodeControls
1.11 +{
1.12 + public abstract class BindableControl : NodeControl
1.13 + {
1.14 + private struct MemberAdapter
1.15 + {
1.16 + private object _obj;
1.17 + private PropertyInfo _pi;
1.18 + private FieldInfo _fi;
1.19 +
1.20 + public static readonly MemberAdapter Empty = new MemberAdapter();
1.21 +
1.22 + public Type MemberType
1.23 + {
1.24 + get
1.25 + {
1.26 + if (_pi != null)
1.27 + return _pi.PropertyType;
1.28 + else if (_fi != null)
1.29 + return _fi.FieldType;
1.30 + else
1.31 + return null;
1.32 + }
1.33 + }
1.34 +
1.35 + public object Value
1.36 + {
1.37 + get
1.38 + {
1.39 + if (_pi != null && _pi.CanRead)
1.40 + return _pi.GetValue(_obj, null);
1.41 + else if (_fi != null)
1.42 + return _fi.GetValue(_obj);
1.43 + else
1.44 + return null;
1.45 + }
1.46 + set
1.47 + {
1.48 + if (_pi != null && _pi.CanWrite)
1.49 + _pi.SetValue(_obj, value, null);
1.50 + else if (_fi != null)
1.51 + _fi.SetValue(_obj, value);
1.52 + }
1.53 + }
1.54 +
1.55 + public MemberAdapter(object obj, PropertyInfo pi)
1.56 + {
1.57 + _obj = obj;
1.58 + _pi = pi;
1.59 + _fi = null;
1.60 + }
1.61 +
1.62 + public MemberAdapter(object obj, FieldInfo fi)
1.63 + {
1.64 + _obj = obj;
1.65 + _fi = fi;
1.66 + _pi = null;
1.67 + }
1.68 + }
1.69 +
1.70 + #region Properties
1.71 +
1.72 + private bool _virtualMode = false;
1.73 + [DefaultValue(false), Category("Data")]
1.74 + public bool VirtualMode
1.75 + {
1.76 + get { return _virtualMode; }
1.77 + set { _virtualMode = value; }
1.78 + }
1.79 +
1.80 + private string _propertyName = "";
1.81 + [DefaultValue(""), Category("Data")]
1.82 + public string DataPropertyName
1.83 + {
1.84 + get { return _propertyName; }
1.85 + set
1.86 + {
1.87 + if (_propertyName == null)
1.88 + _propertyName = string.Empty;
1.89 + _propertyName = value;
1.90 + }
1.91 + }
1.92 +
1.93 + private bool _incrementalSearchEnabled = false;
1.94 + [DefaultValue(false)]
1.95 + public bool IncrementalSearchEnabled
1.96 + {
1.97 + get { return _incrementalSearchEnabled; }
1.98 + set { _incrementalSearchEnabled = value; }
1.99 + }
1.100 +
1.101 + #endregion
1.102 +
1.103 + public virtual object GetValue(TreeNodeAdv node)
1.104 + {
1.105 + if (VirtualMode)
1.106 + {
1.107 + NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
1.108 + OnValueNeeded(args);
1.109 + return args.Value;
1.110 + }
1.111 + else
1.112 + {
1.113 + try
1.114 + {
1.115 + return GetMemberAdapter(node).Value;
1.116 + }
1.117 + catch (TargetInvocationException ex)
1.118 + {
1.119 + if (ex.InnerException != null)
1.120 + throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
1.121 + else
1.122 + throw new ArgumentException(ex.Message);
1.123 + }
1.124 + }
1.125 + }
1.126 +
1.127 + public virtual void SetValue(TreeNodeAdv node, object value)
1.128 + {
1.129 + if (VirtualMode)
1.130 + {
1.131 + NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
1.132 + args.Value = value;
1.133 + OnValuePushed(args);
1.134 + }
1.135 + else
1.136 + {
1.137 + try
1.138 + {
1.139 + MemberAdapter ma = GetMemberAdapter(node);
1.140 + ma.Value = value;
1.141 + }
1.142 + catch (TargetInvocationException ex)
1.143 + {
1.144 + if (ex.InnerException != null)
1.145 + throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
1.146 + else
1.147 + throw new ArgumentException(ex.Message);
1.148 + }
1.149 + }
1.150 + }
1.151 +
1.152 + public Type GetPropertyType(TreeNodeAdv node)
1.153 + {
1.154 + return GetMemberAdapter(node).MemberType;
1.155 + }
1.156 +
1.157 + private MemberAdapter GetMemberAdapter(TreeNodeAdv node)
1.158 + {
1.159 + if (node.Tag != null && !string.IsNullOrEmpty(DataPropertyName))
1.160 + {
1.161 + Type type = node.Tag.GetType();
1.162 + PropertyInfo pi = type.GetProperty(DataPropertyName);
1.163 + if (pi != null)
1.164 + return new MemberAdapter(node.Tag, pi);
1.165 + else
1.166 + {
1.167 + FieldInfo fi = type.GetField(DataPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1.168 + if (fi != null)
1.169 + return new MemberAdapter(node.Tag, fi);
1.170 + }
1.171 + }
1.172 + return MemberAdapter.Empty;
1.173 + }
1.174 +
1.175 + public override string ToString()
1.176 + {
1.177 + if (string.IsNullOrEmpty(DataPropertyName))
1.178 + return GetType().Name;
1.179 + else
1.180 + return string.Format("{0} ({1})", GetType().Name, DataPropertyName);
1.181 + }
1.182 +
1.183 + public event EventHandler<NodeControlValueEventArgs> ValueNeeded;
1.184 + private void OnValueNeeded(NodeControlValueEventArgs args)
1.185 + {
1.186 + if (ValueNeeded != null)
1.187 + ValueNeeded(this, args);
1.188 + }
1.189 +
1.190 + public event EventHandler<NodeControlValueEventArgs> ValuePushed;
1.191 + private void OnValuePushed(NodeControlValueEventArgs args)
1.192 + {
1.193 + if (ValuePushed != null)
1.194 + ValuePushed(this, args);
1.195 + }
1.196 + }
1.197 +}