BindableControl.cs :  » GUI » TreeViewAdv » Aga » Controls » Tree » NodeControls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » TreeViewAdv 
TreeViewAdv » Aga » Controls » Tree » NodeControls » BindableControl.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace Aga.Controls.Tree.NodeControls{
  public abstract class BindableControl : NodeControl
  {
    private struct MemberAdapter
    {
      private object _obj;
      private PropertyInfo _pi;
      private FieldInfo _fi;

      public static readonly MemberAdapter Empty = new MemberAdapter();

      public Type MemberType
      {
        get
        {
          if (_pi != null)
            return _pi.PropertyType;
          else if (_fi != null)
            return _fi.FieldType;
          else
            return null;
        }
      }

      public object Value
      {
        get
        {
          if (_pi != null && _pi.CanRead)
            return _pi.GetValue(_obj, null);
          else if (_fi != null)
            return _fi.GetValue(_obj);
          else
            return null;
        }
        set
        {
          if (_pi != null && _pi.CanWrite)
            _pi.SetValue(_obj, value, null);
          else if (_fi != null)
            _fi.SetValue(_obj, value);
        }
      }

      public MemberAdapter(object obj, PropertyInfo pi)
      {
        _obj = obj;
        _pi = pi;
        _fi = null;
      }

      public MemberAdapter(object obj, FieldInfo fi)
      {
        _obj = obj;
        _fi = fi;
        _pi = null;
      }
    }

    #region Properties

    private bool _virtualMode = false;
    [DefaultValue(false), Category("Data")]
    public bool VirtualMode
    {
      get { return _virtualMode; }
      set { _virtualMode = value; }
    }

    private string _propertyName = "";
    [DefaultValue(""), Category("Data")]
    public string DataPropertyName
    {
      get { return _propertyName; }
      set 
      {
        if (_propertyName == null)
          _propertyName = string.Empty;
        _propertyName = value; 
      }
    }

    private bool _incrementalSearchEnabled = false;
    [DefaultValue(false)]
    public bool IncrementalSearchEnabled
    {
      get { return _incrementalSearchEnabled; }
      set { _incrementalSearchEnabled = value; }
    }

    #endregion

    public virtual object GetValue(TreeNodeAdv node)
    {
      if (VirtualMode)
      {
        NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
        OnValueNeeded(args);
        return args.Value;
      }
      else
      {
        try
        {
          return GetMemberAdapter(node).Value;
        }
        catch (TargetInvocationException ex)
        {
          if (ex.InnerException != null)
            throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
          else
            throw new ArgumentException(ex.Message);
        }
      }
    }

    public virtual void SetValue(TreeNodeAdv node, object value)
    {
      if (VirtualMode)
      {
        NodeControlValueEventArgs args = new NodeControlValueEventArgs(node);
        args.Value = value;
        OnValuePushed(args);
      }
      else
      {
        try
        {
          MemberAdapter ma = GetMemberAdapter(node);
          ma.Value = value;
        }
        catch (TargetInvocationException ex)
        {
          if (ex.InnerException != null)
            throw new ArgumentException(ex.InnerException.Message, ex.InnerException);
          else
            throw new ArgumentException(ex.Message);
        }
      }
    }

    public Type GetPropertyType(TreeNodeAdv node)
    {
      return GetMemberAdapter(node).MemberType;
    }

    private MemberAdapter GetMemberAdapter(TreeNodeAdv node)
    {
      if (node.Tag != null && !string.IsNullOrEmpty(DataPropertyName))
      {
        Type type = node.Tag.GetType();
        PropertyInfo pi = type.GetProperty(DataPropertyName);
        if (pi != null)
          return new MemberAdapter(node.Tag, pi);
        else
        {
          FieldInfo fi = type.GetField(DataPropertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
          if (fi != null)
            return new MemberAdapter(node.Tag, fi);
        }
      }
      return MemberAdapter.Empty;
    }

    public override string ToString()
    {
      if (string.IsNullOrEmpty(DataPropertyName))
        return GetType().Name;
      else
        return string.Format("{0} ({1})", GetType().Name, DataPropertyName);
    }

    public event EventHandler<NodeControlValueEventArgs> ValueNeeded;
    private void OnValueNeeded(NodeControlValueEventArgs args)
    {
      if (ValueNeeded != null)
        ValueNeeded(this, args);
    }

    public event EventHandler<NodeControlValueEventArgs> ValuePushed;
    private void OnValuePushed(NodeControlValueEventArgs args)
    {
      if (ValuePushed != null)
        ValuePushed(this, args);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.