PageRenderTime 228ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Binding/CountBinderModel.cs

https://bitbucket.org/VahidN/interlace
C# | 143 lines | 92 code | 25 blank | 26 comment | 15 complexity | e776199e0329d9cefad8813abee97e03 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. using System.Text;
  33. #endregion
  34. namespace Interlace.Binding
  35. {
  36. public class CountBinderModel : IBinderModel
  37. {
  38. ICollection _boundTo;
  39. Type _boundToType;
  40. private BinderController _controller;
  41. public CountBinderModel()
  42. {
  43. _boundTo = null;
  44. }
  45. public BinderController Controller
  46. {
  47. set { _controller = value; }
  48. }
  49. public string GetDescriptionForTracing()
  50. {
  51. if (_boundTo == null) return "Count of null";
  52. return string.Format("Count of \"{0}\", type \"{1}\"",
  53. _boundTo, _boundToType.Name);
  54. }
  55. private void ConnectEvents()
  56. {
  57. EventInfo changeEvent = _boundToType.GetEvent("ListChanged");
  58. if (changeEvent != null)
  59. {
  60. changeEvent.AddEventHandler(_boundTo, new ListChangedEventHandler(ListChangedEventHandler));
  61. }
  62. }
  63. private void DisconnectEvents()
  64. {
  65. EventInfo changeEvent = _boundToType.GetEvent("ListChanged");
  66. if (changeEvent != null)
  67. {
  68. changeEvent.RemoveEventHandler(_boundTo, new ListChangedEventHandler(ListChangedEventHandler));
  69. }
  70. }
  71. public void ConnectBoundToObject(object boundTo)
  72. {
  73. if (_boundTo != null) DisconnectEvents();
  74. _boundTo = boundTo as ICollection;
  75. if (_boundTo != null)
  76. {
  77. _boundToType = _boundTo.GetType();
  78. ConnectEvents();
  79. }
  80. _controller.OnModelModified();
  81. }
  82. public void DisconnectBoundToObject()
  83. {
  84. if (_boundTo != null)
  85. {
  86. DisconnectEvents();
  87. }
  88. _boundTo = null;
  89. _boundToType = null;
  90. _controller.OnModelModified();
  91. }
  92. private void ListChangedEventHandler(object sender, ListChangedEventArgs e)
  93. {
  94. switch (e.ListChangedType)
  95. {
  96. case ListChangedType.ItemChanged:
  97. case ListChangedType.ItemMoved:
  98. case ListChangedType.PropertyDescriptorAdded:
  99. case ListChangedType.PropertyDescriptorChanged:
  100. case ListChangedType.PropertyDescriptorDeleted:
  101. // Ignore these events for the count.
  102. return;
  103. default:
  104. _controller.OnModelModified();
  105. break;
  106. }
  107. }
  108. public object GetValue()
  109. {
  110. if (_boundTo == null) return BinderNotBound.Value;
  111. return _boundTo.Count;
  112. }
  113. public void SetValue(object value)
  114. {
  115. return;
  116. }
  117. }
  118. }