Container.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // System.ComponentModel.Container.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Collections;
  32. namespace System.ComponentModel {
  33. // <summary>
  34. // Container class: encapsulates components.
  35. // </summary>
  36. //
  37. // <remarks>
  38. //
  39. // </remarks>
  40. public class Container : IContainer, IDisposable {
  41. private ArrayList c = new ArrayList ();
  42. //ComponentCollection cc;
  43. // <summary>
  44. // Auxiliary class to support the default behaviour of CreateSite
  45. // </summary>
  46. //
  47. // <remarks>
  48. // This is an internal class that is used to provide a
  49. // default implementation of an ISite class. Container
  50. // is just a default implementation of IContainer, and
  51. // provides this as a way of getting started
  52. // </remarks>
  53. class DefaultSite : ISite {
  54. private IComponent component;
  55. private Container container;
  56. private string name;
  57. public DefaultSite (string name, IComponent component, Container container)
  58. {
  59. this.component = component;
  60. this.container = container;
  61. this.name = name;
  62. }
  63. public IComponent Component {
  64. get {
  65. return component;
  66. }
  67. }
  68. public IContainer Container {
  69. get {
  70. return container;
  71. }
  72. }
  73. [MonoTODO]
  74. public bool DesignMode {
  75. get {
  76. // FIXME: should we provide a way to set
  77. // this value?
  78. return false;
  79. }
  80. }
  81. public string Name {
  82. get {
  83. return name;
  84. }
  85. set {
  86. name = value;
  87. }
  88. }
  89. public virtual object GetService (Type t)
  90. {
  91. if (typeof(ISite) != t) {
  92. return null;
  93. }
  94. return container.GetService (t);
  95. }
  96. }
  97. // <summary>
  98. // Container constructor
  99. // </summary>
  100. public Container ()
  101. {
  102. }
  103. public virtual ComponentCollection Components {
  104. get {
  105. Array a = c.ToArray(typeof (IComponent));
  106. return new ComponentCollection((IComponent[]) a);
  107. }
  108. }
  109. public virtual void Add (IComponent component)
  110. {
  111. Add (component, null);
  112. }
  113. public virtual void Add (IComponent component, string name)
  114. {
  115. component.Site = CreateSite (component, name);
  116. c.Add (component);
  117. }
  118. protected virtual ISite CreateSite (IComponent component, string name)
  119. {
  120. if (name != null) {
  121. foreach (IComponent Comp in c) {
  122. if (Comp.Site != null && Comp.Site.Name == name)
  123. throw new ArgumentException ("duplicate component name", "name");
  124. }
  125. }
  126. return new DefaultSite (name, component, this);
  127. }
  128. public void Dispose ()
  129. {
  130. Dispose (true);
  131. GC.SuppressFinalize (this);
  132. }
  133. bool disposed = false;
  134. protected virtual void Dispose (bool release_all)
  135. {
  136. if (disposed)
  137. return;
  138. disposed = true;
  139. if (release_all){
  140. foreach (IComponent component in c)
  141. component.Dispose ();
  142. }
  143. c = null;
  144. }
  145. ~Container ()
  146. {
  147. Dispose (false);
  148. }
  149. protected virtual object GetService (Type service)
  150. {
  151. if (typeof(IContainer) != service) {
  152. return null;
  153. }
  154. return this;
  155. }
  156. public virtual void Remove (IComponent component)
  157. {
  158. c.Remove (component);
  159. }
  160. }
  161. }