DesignModeNestedContainer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // System.ComponentModel.Design.DesignModeSite
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  8. //
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. #if NET_2_0
  28. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design.Serialization;
  32. namespace System.ComponentModel.Design
  33. {
  34. internal class DesignModeNestedContainer : NestedContainer
  35. {
  36. // DesignModeNestedContainer is a NestedContainer where:
  37. // * Site's Name property is a qualified name that includes the owning component's name
  38. // followed by a period (.) and the child component's name.
  39. // * GetService is routed through the owner
  40. private class Site : DesignModeSite, INestedSite
  41. {
  42. public Site (IComponent component, string name, IContainer container, IServiceProvider serviceProvider) :
  43. base (component, name, container, serviceProvider)
  44. {
  45. }
  46. // [owner].[container].[site]
  47. //
  48. public string FullName {
  49. get {
  50. if (this.Name == null)
  51. return null;
  52. string ownerName = ((DesignModeNestedContainer)this.Container).OwnerName;
  53. if (ownerName == null)
  54. return this.Name;
  55. return ownerName + "." + this.Name;
  56. }
  57. }
  58. }
  59. private string _containerName;
  60. public DesignModeNestedContainer (IComponent owner, string containerName) : base (owner)
  61. {
  62. _containerName = containerName;
  63. }
  64. public override void Add (IComponent component, string name)
  65. {
  66. if (this.Owner.Site != null) {
  67. DesignerHost host = this.Owner.Site.GetService (typeof (IDesignerHost)) as DesignerHost;
  68. if (host != null) {
  69. host.AddPreProcess (component, name);
  70. base.Add (component, name);
  71. host.AddPostProcess (component, name);
  72. }
  73. }
  74. }
  75. public override void Remove (IComponent component)
  76. {
  77. if (this.Owner.Site != null) {
  78. DesignerHost host = this.Owner.Site.GetService (typeof (IDesignerHost)) as DesignerHost;
  79. if (host != null) {
  80. host.RemovePreProcess (component);
  81. base.Remove (component);
  82. host.RemovePostProcess (component);
  83. }
  84. }
  85. }
  86. // [owner].[container]
  87. //
  88. protected override string OwnerName {
  89. get {
  90. if (_containerName != null)
  91. return base.OwnerName + "." + _containerName;
  92. else
  93. return base.OwnerName;
  94. }
  95. }
  96. protected override ISite CreateSite (IComponent component, string name)
  97. {
  98. if (component == null)
  99. throw new ArgumentNullException("component");
  100. if (Owner.Site == null)
  101. throw new InvalidOperationException ("Owner not sited.");
  102. return new DesignModeNestedContainer.Site (component, name, this, (IServiceProvider)Owner.Site);
  103. }
  104. protected override object GetService (Type service)
  105. {
  106. if (service == typeof (INestedContainer))
  107. return this;
  108. object serviceInstance = null;
  109. if (this.Owner.Site != null)
  110. serviceInstance = this.Owner.Site.GetService (service);
  111. if (serviceInstance == null)
  112. return base.GetService (service);
  113. return null;
  114. }
  115. }
  116. }
  117. #endif