Container.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. using System.Collections;
  12. namespace System.ComponentModel {
  13. // <summary>
  14. // Container class: encapsulates components.
  15. // </summary>
  16. //
  17. // <remarks>
  18. //
  19. // </remarks>
  20. public class Container : IContainer, IDisposable {
  21. private ArrayList c = new ArrayList ();
  22. //ComponentCollection cc;
  23. // <summary>
  24. // Auxiliary class to support the default behaviour of CreateSite
  25. // </summary>
  26. //
  27. // <remarks>
  28. // This is an internal class that is used to provide a
  29. // default implementation of an ISite class. Container
  30. // is just a default implementation of IContainer, and
  31. // provides this as a way of getting started
  32. // </remarks>
  33. class DefaultSite : ISite {
  34. private IComponent component;
  35. private IContainer container;
  36. private string name;
  37. public DefaultSite (string name, IComponent component, IContainer container)
  38. {
  39. this.component = component;
  40. this.container = container;
  41. this.name = name;
  42. }
  43. public IComponent Component {
  44. get {
  45. return component;
  46. }
  47. }
  48. public IContainer Container {
  49. get {
  50. return container;
  51. }
  52. }
  53. [MonoTODO]
  54. public bool DesignMode {
  55. get {
  56. // FIXME: should we provide a way to set
  57. // this value?
  58. return false;
  59. }
  60. }
  61. public string Name {
  62. get {
  63. return name;
  64. }
  65. set {
  66. name = value;
  67. }
  68. }
  69. [MonoTODO]
  70. public virtual object GetService (Type t)
  71. {
  72. // FIXME: do not know what this is supposed to do.
  73. return null;
  74. }
  75. }
  76. // <summary>
  77. // Container constructor
  78. // </summary>
  79. public Container ()
  80. {
  81. }
  82. public virtual ComponentCollection Components {
  83. get
  84. {
  85. Array a = c.ToArray(typeof (IComponent));
  86. return new ComponentCollection((IComponent[]) a);
  87. }
  88. }
  89. // <summary>
  90. // Adds an IComponent to the Container
  91. // </summary>
  92. public virtual void Add (IComponent component)
  93. {
  94. Add (component, null);
  95. }
  96. // <summary>
  97. // Adds an IComponent to the Container. With a name binding.
  98. // </summary>
  99. public virtual void Add (IComponent component, string name)
  100. {
  101. component.Site = CreateSite (component, name);
  102. c.Add (component);
  103. }
  104. // <summary>
  105. // Returns an ISite for a component.
  106. // <summary>
  107. protected virtual ISite CreateSite (IComponent component, string name)
  108. {
  109. foreach (IComponent Comp in c) {
  110. if (Comp.Site != null)
  111. if (Comp.Site.Name == name)
  112. throw new ArgumentException ("duplicate component name", "name");
  113. }
  114. return new DefaultSite (name, component, this);
  115. }
  116. public void Dispose ()
  117. {
  118. Dispose (true);
  119. GC.SuppressFinalize (this);
  120. }
  121. bool disposed = false;
  122. protected virtual void Dispose (bool release_all)
  123. {
  124. if (disposed)
  125. return;
  126. disposed = true;
  127. if (release_all){
  128. //??
  129. }
  130. c = null;
  131. }
  132. ~Container ()
  133. {
  134. Dispose (false);
  135. }
  136. [MonoTODO]
  137. protected virtual object GetService (Type service)
  138. {
  139. // FIXME: Not clear what GetService does.
  140. return null;
  141. }
  142. public virtual void Remove (IComponent component)
  143. {
  144. c.Remove (component);
  145. }
  146. }
  147. }