Container.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Container container;
  36. private string name;
  37. public DefaultSite (string name, IComponent component, Container 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. public virtual object GetService (Type t)
  70. {
  71. if (typeof(ISite) != t) {
  72. return null;
  73. }
  74. return container.GetService (t);
  75. }
  76. }
  77. // <summary>
  78. // Container constructor
  79. // </summary>
  80. public Container ()
  81. {
  82. }
  83. public virtual ComponentCollection Components {
  84. get {
  85. Array a = c.ToArray(typeof (IComponent));
  86. return new ComponentCollection((IComponent[]) a);
  87. }
  88. }
  89. public virtual void Add (IComponent component)
  90. {
  91. Add (component, null);
  92. }
  93. public virtual void Add (IComponent component, string name)
  94. {
  95. component.Site = CreateSite (component, name);
  96. c.Add (component);
  97. }
  98. protected virtual ISite CreateSite (IComponent component, string name)
  99. {
  100. if (name != null) {
  101. foreach (IComponent Comp in c) {
  102. if (Comp.Site != null && Comp.Site.Name == name)
  103. throw new ArgumentException ("duplicate component name", "name");
  104. }
  105. }
  106. return new DefaultSite (name, component, this);
  107. }
  108. public void Dispose ()
  109. {
  110. Dispose (true);
  111. GC.SuppressFinalize (this);
  112. }
  113. bool disposed = false;
  114. protected virtual void Dispose (bool release_all)
  115. {
  116. if (disposed)
  117. return;
  118. disposed = true;
  119. if (release_all){
  120. //??
  121. }
  122. c = null;
  123. }
  124. ~Container ()
  125. {
  126. Dispose (false);
  127. }
  128. protected virtual object GetService (Type service)
  129. {
  130. if (typeof(IContainer) != service) {
  131. return null;
  132. }
  133. return this;
  134. }
  135. public virtual void Remove (IComponent component)
  136. {
  137. c.Remove (component);
  138. }
  139. }
  140. }