EventBindingService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // System.ComponentModel.Design.EventBindingService
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. namespace System.ComponentModel.Design
  34. {
  35. public abstract class EventBindingService : IEventBindingService
  36. {
  37. private IServiceProvider _provider;
  38. protected EventBindingService (IServiceProvider provider)
  39. {
  40. if (provider == null)
  41. throw new ArgumentNullException ("provider");
  42. _provider = provider;
  43. }
  44. protected abstract bool ShowCode (IComponent component, EventDescriptor e, string methodName);
  45. protected abstract bool ShowCode (int lineNumber);
  46. protected abstract bool ShowCode ();
  47. protected abstract string CreateUniqueMethodName (IComponent component, EventDescriptor eventDescriptor);
  48. protected abstract ICollection GetCompatibleMethods (EventDescriptor eventDescriptor);
  49. protected virtual void FreeMethod (IComponent component, EventDescriptor e, string methodName)
  50. {
  51. }
  52. protected virtual void UseMethod (IComponent component, EventDescriptor e, string methodName)
  53. {
  54. }
  55. protected virtual void ValidateMethodName (string methodName)
  56. {
  57. }
  58. protected object GetService (Type service)
  59. {
  60. if (_provider != null)
  61. return _provider.GetService (service);
  62. return null;
  63. }
  64. #region IEventBindingService implementation
  65. string IEventBindingService.CreateUniqueMethodName (IComponent component, EventDescriptor eventDescriptor)
  66. {
  67. if (eventDescriptor == null)
  68. throw new ArgumentNullException ("eventDescriptor");
  69. if (component == null)
  70. throw new ArgumentNullException ("component");
  71. return this.CreateUniqueMethodName (component, eventDescriptor);
  72. }
  73. ICollection IEventBindingService.GetCompatibleMethods (EventDescriptor eventDescriptor)
  74. {
  75. if (eventDescriptor == null)
  76. throw new ArgumentNullException ("eventDescriptor");
  77. return this.GetCompatibleMethods (eventDescriptor);
  78. }
  79. EventDescriptor IEventBindingService.GetEvent (PropertyDescriptor property)
  80. {
  81. if (property == null)
  82. throw new ArgumentNullException ("property");
  83. EventPropertyDescriptor eventPropDescriptor = property as EventPropertyDescriptor;
  84. if (eventPropDescriptor == null)
  85. return null;
  86. return eventPropDescriptor.InternalEventDescriptor;
  87. }
  88. PropertyDescriptorCollection IEventBindingService.GetEventProperties (EventDescriptorCollection events)
  89. {
  90. if (events == null)
  91. throw new ArgumentNullException ("events");
  92. List<PropertyDescriptor> properties = new List <PropertyDescriptor>();
  93. foreach (EventDescriptor eventDescriptor in events)
  94. properties.Add (((IEventBindingService)this).GetEventProperty (eventDescriptor));
  95. return new PropertyDescriptorCollection (properties.ToArray ());
  96. }
  97. PropertyDescriptor IEventBindingService.GetEventProperty (EventDescriptor eventDescriptor)
  98. {
  99. if (eventDescriptor == null)
  100. throw new ArgumentNullException ("eventDescriptor");
  101. return new EventPropertyDescriptor (eventDescriptor);
  102. }
  103. bool IEventBindingService.ShowCode (IComponent component, EventDescriptor eventDescriptor)
  104. {
  105. if (component == null)
  106. throw new ArgumentNullException ("component");
  107. if (eventDescriptor == null)
  108. throw new ArgumentNullException ("eventDescriptor");
  109. return this.ShowCode (component, eventDescriptor, (string) ((IEventBindingService)this).GetEventProperty (eventDescriptor).GetValue (component));
  110. }
  111. bool IEventBindingService.ShowCode (int lineNumber)
  112. {
  113. return this.ShowCode (lineNumber);
  114. }
  115. bool IEventBindingService.ShowCode ()
  116. {
  117. return this.ShowCode ();
  118. }
  119. #endregion
  120. }
  121. internal class EventPropertyDescriptor : PropertyDescriptor
  122. {
  123. private EventDescriptor _eventDescriptor;
  124. public EventPropertyDescriptor (EventDescriptor eventDescriptor)
  125. : base (eventDescriptor)
  126. {
  127. if (eventDescriptor == null)
  128. throw new ArgumentNullException ("eventDescriptor");
  129. _eventDescriptor = eventDescriptor;
  130. }
  131. public override bool CanResetValue (object component)
  132. {
  133. return true;
  134. }
  135. public override Type ComponentType {
  136. get { return _eventDescriptor.ComponentType; }
  137. }
  138. public override bool IsReadOnly {
  139. get { return false; }
  140. }
  141. public override Type PropertyType {
  142. get { return _eventDescriptor.EventType; }
  143. }
  144. public override void ResetValue (object component)
  145. {
  146. this.SetValue (component, null);
  147. }
  148. public override object GetValue (object component)
  149. {
  150. if (component is IComponent && ((IComponent)component).Site != null) {
  151. IDictionaryService dictionary = ((IComponent)component).Site.GetService (typeof (IDictionaryService)) as IDictionaryService;
  152. if (dictionary != null)
  153. return dictionary.GetValue (base.Name);
  154. }
  155. return null;
  156. }
  157. public override void SetValue (object component, object value)
  158. {
  159. if (component is IComponent && ((IComponent)component).Site != null) {
  160. IDictionaryService dictionary = ((IComponent)component).Site.GetService (typeof (IDictionaryService)) as IDictionaryService;
  161. if (dictionary != null)
  162. dictionary.SetValue (base.Name, value);
  163. }
  164. }
  165. public override bool ShouldSerializeValue (object component)
  166. {
  167. if (this.GetValue (component) != null)
  168. return true;
  169. return false;
  170. }
  171. public override TypeConverter Converter {
  172. get { return TypeDescriptor.GetConverter (String.Empty); }
  173. }
  174. internal EventDescriptor InternalEventDescriptor {
  175. get { return _eventDescriptor; }
  176. }
  177. }
  178. }
  179. #endif