UserFormVisitor.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:7k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : UserFormVisitor.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.Web.UI;
  11. namespace Itenso.WebUserForms.Controls
  12. {
  13. // ------------------------------------------------------------------------
  14. public abstract class UserFormVisitor
  15. {
  16. // ----------------------------------------------------------------------
  17. protected UserFormVisitor( Control startControl )
  18. {
  19. if ( startControl == null )
  20. {
  21. throw new ArgumentNullException( "startControl" );
  22. }
  23. this.startControl = startControl;
  24. } // UserFormVisitor
  25. // ----------------------------------------------------------------------
  26. public Control StartControl
  27. {
  28. get { return this.startControl; }
  29. } // StartControl
  30. // ----------------------------------------------------------------------
  31. protected void Start()
  32. {
  33. VisitForm( this.startControl, null );
  34. } // Start
  35. // ----------------------------------------------------------------------
  36. protected virtual void EnterForm( Control control )
  37. {
  38. } // EnterForm
  39. // ----------------------------------------------------------------------
  40. protected virtual void LeaveForm( Control control )
  41. {
  42. } // LeaveForm
  43. // ----------------------------------------------------------------------
  44. protected virtual void VisitControl( Control control )
  45. {
  46. } // VisitControl
  47. // ----------------------------------------------------------------------
  48. protected virtual void VisitHeader( Control control, 
  49. IUserFormHeader formHeader, IUserFormHeader parentFormHeader )
  50. {
  51. if ( parentFormHeader == null )
  52. {
  53. VisitMainHeader( control, formHeader );
  54. }
  55. else
  56. {
  57. VisitSubHeader( control, formHeader, parentFormHeader );
  58. }
  59. } // VisitFormHeader
  60. // ----------------------------------------------------------------------
  61. protected virtual void VisitMainHeader( Control control, 
  62. IUserFormHeader formHeader )
  63. {
  64. } // VisitMainHeader
  65. // ----------------------------------------------------------------------
  66. protected virtual void VisitSubHeader( Control control, 
  67. IUserFormHeader formHeader, IUserFormHeader parentFormHeader )
  68. {
  69. } // VisitSubHeader
  70. // ----------------------------------------------------------------------
  71. protected virtual void VisitFormField( Control control,
  72. IUserFormField formField, IUserFormHeader formHeader )
  73. {
  74. IListField listFormField = formField as IListField;
  75. if ( listFormField != null )
  76. {
  77. VisitListFormField( control, listFormField, formHeader );
  78. }
  79. ILookupField lookupField = formField as ILookupField;
  80. if ( lookupField != null )
  81. {
  82. VisitLookupFormField( control, lookupField, formHeader );
  83. }
  84. IExpressionField expressionField = formField as IExpressionField;
  85. if ( expressionField != null )
  86. {
  87. VisitExpresionFormField( control, expressionField, formHeader );
  88. }
  89. } // VisitFormField
  90. // ----------------------------------------------------------------------
  91. protected virtual void VisitListFormField( Control control,
  92. IListField listFormField, IUserFormHeader formHeader )
  93. {
  94. } // VisitListFormField
  95. // ----------------------------------------------------------------------
  96. protected virtual void VisitLookupFormField( Control control,
  97. ILookupField lookupField, IUserFormHeader formHeader )
  98. {
  99. } // VisitLookupFormField
  100. // ----------------------------------------------------------------------
  101. protected virtual void VisitExpresionFormField( Control control,
  102. IExpressionField expressionField, IUserFormHeader formHeader )
  103. {
  104. } // VisitExpresionFormField
  105. // ----------------------------------------------------------------------
  106. protected virtual void VisitFormCommand( Control control,
  107. IUserFormCommand formCommand, IUserFormHeader formHeader )
  108. {
  109. } // VisitFormCommand
  110. // ----------------------------------------------------------------------
  111. private void VisitForm( Control control, IUserFormHeader parentFormHeader )
  112. {
  113. VisitControl( control );
  114. if ( control.Controls.Count == 0 )
  115. {
  116. return;
  117. }
  118. IUserFormHeader formHeader = FindFormHeader( control.Controls );
  119. bool enterForm = false;
  120. if ( formHeader != null )
  121. {
  122. enterForm = true;
  123. EnterForm( control );
  124. VisitHeader( control, formHeader, parentFormHeader );
  125. }
  126. else
  127. {
  128. formHeader = parentFormHeader;
  129. }
  130. VisitFormControls( control.Controls, formHeader );
  131. int controlCount = control.Controls.Count;
  132. for ( int i = 0; i < controlCount; i++ )
  133. {
  134. Control subControl = control.Controls[ i ];
  135. VisitForm( subControl, formHeader );
  136. if ( control.Controls.Count != controlCount )
  137. {
  138. throw new InvalidOperationException();
  139. }
  140. }
  141. if ( enterForm )
  142. {
  143. LeaveForm( control );
  144. }
  145. } // VisitForm
  146. // ----------------------------------------------------------------------
  147. private static IUserFormHeader FindFormHeader( ControlCollection controls )
  148. {
  149. foreach ( Control control in controls )
  150. {
  151. IUserFormHeader formHeader = control as IUserFormHeader;
  152. if ( formHeader != null )
  153. {
  154. return formHeader;
  155. }
  156. }
  157. return null;
  158. } // FindFormHeader
  159. // ----------------------------------------------------------------------
  160. private void VisitFormControls( ControlCollection controls, IUserFormHeader formHeader )
  161. {
  162. int controlCount = controls.Count;
  163. for ( int i = 0; i < controlCount; i++ )
  164. {
  165. Control control = controls[ i ];
  166. IUserFormField formField = control as IUserFormField;
  167. if ( formField != null )
  168. {
  169. VisitFormField( control, formField, formHeader );
  170. if ( controls.Count != controlCount )
  171. {
  172. throw new InvalidOperationException();
  173. }
  174. }
  175. IUserFormCommand formCommand = control as IUserFormCommand;
  176. if ( formCommand != null )
  177. {
  178. VisitFormCommand( control, formCommand, formHeader );
  179. }
  180. }
  181. } // VisitFormControls
  182. // ----------------------------------------------------------------------
  183. // members
  184. private readonly Control startControl;
  185. } // class UserFormVisitor
  186. } // namespace Itenso.WebUserForms.Controls
  187. // -- EOF -------------------------------------------------------------------