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

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : UserFormCommandManager.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.Collections.Generic;
  11. using System.Web.UI;
  12. using System.Web.UI.WebControls;
  13. namespace Itenso.WebUserForms.Controls
  14. {
  15. // ------------------------------------------------------------------------
  16. public class UserFormCommandManager
  17. {
  18. // ----------------------------------------------------------------------
  19. public event CommandEventHandler Command;
  20. // ----------------------------------------------------------------------
  21. public UserFormCommandManager( UserControl userControl )
  22. {
  23. if ( userControl == null )
  24. {
  25. throw new ArgumentNullException( "userControl" );
  26. }
  27. this.userControl = userControl;
  28. RegisterEvents();
  29. } // UserFormCommandManager
  30. // ----------------------------------------------------------------------
  31. public UserControl UserControl
  32. {
  33. get { return this.userControl; }
  34. } // UserControl
  35. // ----------------------------------------------------------------------
  36. public bool HasCommand( string commandName )
  37. {
  38. if ( string.IsNullOrEmpty( commandName) )
  39. {
  40. throw new ArgumentNullException( "commandName" );
  41. }
  42. if ( this.formCommands == null || this.formCommands.Count == 0 )
  43. {
  44. return false;
  45. }
  46. foreach ( IUserFormCommand formCommand in formCommands )
  47. {
  48. if ( commandName.Equals( formCommand.CommandName ) )
  49. {
  50. return true;
  51. }
  52. }
  53. return false;
  54. } // HasCommand
  55. // ----------------------------------------------------------------------
  56. public void EnableCommand( string commandName, bool enabled )
  57. {
  58. if ( string.IsNullOrEmpty( commandName ) )
  59. {
  60. throw new ArgumentNullException( "commandName" );
  61. }
  62. if ( this.formCommands == null || this.formCommands.Count == 0 )
  63. {
  64. return;
  65. }
  66. commandName = commandName.ToLower();
  67. foreach ( IUserFormCommand formCommand in formCommands )
  68. {
  69. if ( commandName.Equals( formCommand.CommandName.ToLower() ) )
  70. {
  71. formCommand.Enabled = enabled;
  72. }
  73. }
  74. } // EnableCommand
  75. // ----------------------------------------------------------------------
  76. protected virtual void OnCommand( IUserFormCommand formCommand, CommandEventArgs e )
  77. {
  78. if ( Command != null )
  79. {
  80. Command( formCommand, e );
  81. }
  82. } // OnCommand
  83. // ----------------------------------------------------------------------
  84. private void RegisterEvents()
  85. {
  86. this.formCommands = CommandCollector.Collect( this.userControl );
  87. if ( this.formCommands == null || this.formCommands.Count == 0 )
  88. {
  89. return;
  90. }
  91. foreach ( IUserFormCommand formCommand in formCommands )
  92. {
  93. formCommand.Command += new CommandEventHandler(FormCommandCommand);
  94. }
  95. } // RegisterEvents
  96. // ----------------------------------------------------------------------
  97. private void UnregisterEvents()
  98. {
  99. if ( this.formCommands == null || this.formCommands.Count == 0 )
  100. {
  101. return;
  102. }
  103. foreach ( IUserFormCommand formCommand in formCommands )
  104. {
  105. formCommand.Command -= new CommandEventHandler( FormCommandCommand );
  106. }
  107. } // UnregisterEvents
  108. // ----------------------------------------------------------------------
  109. private void FormCommandCommand( object sender, CommandEventArgs e )
  110. {
  111. IUserFormCommand formCommand = sender as IUserFormCommand;
  112. if ( formCommand == null )
  113. {
  114. return;
  115. }
  116. OnCommand( formCommand, e );
  117. } // RegisterEvents
  118. // ----------------------------------------------------------------------
  119. // members
  120. private readonly UserControl userControl;
  121. private List<IUserFormCommand> formCommands;
  122. } // class UserFormCommandManager
  123. } // namespace Itenso.WebUserForms.Controls
  124. // -- EOF -------------------------------------------------------------------