DesignModeSite.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.ComponentModel.Design.DesignModeSite
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2006-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;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design.Serialization;
  33. namespace System.ComponentModel.Design
  34. {
  35. internal class DesignModeSite : ISite, IDictionaryService, IServiceProvider, IServiceContainer
  36. {
  37. // The DesignModeSite:
  38. // * offers the IDictionaryService and INestedContaineroffers site-specific services
  39. // * implements the IServiceContainer interface, but according to the tests it:
  40. // - does *NOT* offer IServiceContainer as a site-specific service
  41. // - offers the added site-specific services via IServiceProvider
  42. private IServiceProvider _serviceProvider;
  43. private IComponent _component;
  44. private IContainer _container;
  45. private string _componentName;
  46. private NestedContainer _nestedContainer;
  47. public DesignModeSite (IComponent component, string name, IContainer container, IServiceProvider serviceProvider)
  48. {
  49. _component = component;
  50. _container = container;
  51. _componentName = name;
  52. _serviceProvider = serviceProvider;
  53. }
  54. public IComponent Component {
  55. get { return _component; }
  56. }
  57. public IContainer Container {
  58. get { return _container; }
  59. }
  60. // Yay yay yay, guess who's in design mode ???
  61. //
  62. public bool DesignMode {
  63. get { return true; }
  64. }
  65. // The place where renaming of a component takes place.
  66. // We should validate the new name here, if INameCreation service is present
  67. //
  68. public string Name {
  69. get {
  70. return _componentName;
  71. }
  72. set {
  73. if (value != null && value.Trim().Length > 0) {
  74. INameCreationService nameService = this.GetService (typeof (INameCreationService)) as INameCreationService;
  75. // Get the component with that name, if any
  76. //
  77. IComponent component = _container.Components[value];
  78. // check for duplicated name
  79. //
  80. if (component != null && _component != component) { // duplicate name
  81. if (nameService != null)
  82. value = nameService.CreateName (_container, _component.GetType ());
  83. else
  84. value = _componentName;
  85. }
  86. else { // if not a duplicate -> check the validity of the name
  87. if (nameService != null && !nameService.IsValidName (value))
  88. value = _componentName;
  89. }
  90. string oldName = _componentName;
  91. _componentName = value;
  92. ((DesignerHost)this.GetService (typeof (IDesignerHost))).OnComponentRename (_component, oldName, _componentName);
  93. }
  94. }
  95. }
  96. #region IServiceContainer
  97. private ServiceContainer _siteSpecificServices;
  98. private ServiceContainer SiteSpecificServices {
  99. get {
  100. if (_siteSpecificServices == null)
  101. _siteSpecificServices = new ServiceContainer (null);
  102. return _siteSpecificServices;
  103. }
  104. }
  105. void IServiceContainer.AddService (Type serviceType, object serviceInstance)
  106. {
  107. SiteSpecificServices.AddService (serviceType, serviceInstance);
  108. }
  109. void IServiceContainer.AddService (Type serviceType, object serviceInstance, bool promote)
  110. {
  111. SiteSpecificServices.AddService (serviceType, serviceInstance, promote);
  112. }
  113. void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback)
  114. {
  115. SiteSpecificServices.AddService (serviceType, callback);
  116. }
  117. void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
  118. {
  119. SiteSpecificServices.AddService (serviceType, callback, promote);
  120. }
  121. void IServiceContainer.RemoveService (Type serviceType)
  122. {
  123. SiteSpecificServices.RemoveService (serviceType);
  124. }
  125. void IServiceContainer.RemoveService (Type serviceType, bool promote)
  126. {
  127. SiteSpecificServices.RemoveService (serviceType, promote);
  128. }
  129. #endregion
  130. #region IDictionaryService
  131. private Hashtable _dictionary;
  132. object IDictionaryService.GetKey (object value)
  133. {
  134. if (_dictionary != null) {
  135. foreach (DictionaryEntry entry in _dictionary) {
  136. if (value != null && value.Equals (entry.Value))
  137. return entry.Key;
  138. }
  139. }
  140. return null;
  141. }
  142. object IDictionaryService.GetValue (object key)
  143. {
  144. if (_dictionary != null)
  145. return _dictionary[key];
  146. return null;
  147. }
  148. // No Remove method: seting the value to null
  149. // will remove the pair.
  150. //
  151. void IDictionaryService.SetValue (object key, object value)
  152. {
  153. if (_dictionary == null)
  154. _dictionary = new Hashtable ();
  155. if (value == null)
  156. _dictionary.Remove (key);
  157. _dictionary[key] = value;
  158. }
  159. #endregion
  160. #region IServiceProvider
  161. public virtual object GetService (Type service)
  162. {
  163. object serviceInstance = null;
  164. if (typeof (IDictionaryService) == service)
  165. serviceInstance = (IDictionaryService) this;
  166. if (typeof (INestedContainer) == service) {
  167. if (_nestedContainer == null)
  168. _nestedContainer = new DesignModeNestedContainer (_component, null);
  169. serviceInstance = _nestedContainer;
  170. }
  171. if (serviceInstance == null && _siteSpecificServices != null)
  172. serviceInstance = _siteSpecificServices.GetService (service);
  173. if (serviceInstance == null)
  174. serviceInstance = _serviceProvider.GetService (service);
  175. return serviceInstance;
  176. }
  177. #endregion
  178. }
  179. }
  180. #endif