PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Binding/Views/BinderViewBase.cs

https://bitbucket.org/VahidN/interlace
C# | 180 lines | 124 code | 31 blank | 25 comment | 12 complexity | 4f79d3d8427a3782583b0de025a978e6 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.Reflection;
  29. #endregion
  30. namespace Interlace.Binding.Views
  31. {
  32. public abstract class BinderViewBase
  33. {
  34. private BinderController _controller;
  35. private ViewConverterBase _converter;
  36. private bool _errorOccurred;
  37. private Exception _errorException;
  38. bool _viewToModelDisabled = false;
  39. bool _modelToViewDisabled = false;
  40. public BinderViewBase()
  41. {
  42. _converter = ViewConverterBase.Null;
  43. }
  44. public bool ViewToModelDisabled
  45. {
  46. get { return _viewToModelDisabled; }
  47. set { _viewToModelDisabled = value; }
  48. }
  49. public bool ModelToViewDisabled
  50. {
  51. get { return _modelToViewDisabled; }
  52. set { _modelToViewDisabled = value; }
  53. }
  54. public virtual int OrderingIndex
  55. {
  56. get { return int.MaxValue; }
  57. }
  58. public ViewConverterBase Converter
  59. {
  60. get { return _converter; }
  61. set
  62. {
  63. if (value == null)
  64. throw new ArgumentException("A non-null converter must be supplied.", "value");
  65. _converter = value;
  66. }
  67. }
  68. protected internal BinderController Controller
  69. {
  70. set { _controller = value; }
  71. }
  72. protected internal void SetValue(object value)
  73. {
  74. if (!_modelToViewDisabled)
  75. {
  76. object convertedValue;
  77. if (value == BinderNotBound.Value)
  78. {
  79. convertedValue = _converter.BinderNotBoundValue;
  80. }
  81. else if (value == BinderMissingProperty.Value)
  82. {
  83. convertedValue = _converter.BinderMissingValue;
  84. }
  85. else
  86. {
  87. convertedValue = _converter.ModelToView(value);
  88. }
  89. ClearError();
  90. OnModelChanged(convertedValue);
  91. }
  92. }
  93. protected abstract void OnModelChanged(object value);
  94. protected void ChangeModel(object value)
  95. {
  96. object valueForModel;
  97. try
  98. {
  99. if (_controller != null && !_viewToModelDisabled)
  100. {
  101. valueForModel = _converter.ViewToModel(value);
  102. _controller.OnViewModified(this, valueForModel);
  103. }
  104. }
  105. catch (TargetInvocationException ex)
  106. {
  107. SetError(ex.InnerException);
  108. return;
  109. }
  110. catch (FormatException ex)
  111. {
  112. SetError(ex);
  113. return;
  114. }
  115. }
  116. void SetError(Exception ex)
  117. {
  118. _errorOccurred = true;
  119. _errorException = ex;
  120. ShowError(ex.Message);
  121. }
  122. void ClearError()
  123. {
  124. _errorOccurred = false;
  125. _errorException = null;
  126. HideError();
  127. }
  128. protected internal virtual void ShowError(string message)
  129. {
  130. }
  131. protected internal virtual void HideError()
  132. {
  133. }
  134. protected internal virtual void HighlightError()
  135. {
  136. }
  137. internal BinderViewError FindFirstError()
  138. {
  139. if (_errorOccurred)
  140. {
  141. return new BinderViewError(_errorException.Message, this);
  142. }
  143. else
  144. {
  145. return null;
  146. }
  147. }
  148. }
  149. }