2 // System.ComponentModel.Container.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Collections
;
35 namespace System
.ComponentModel
{
38 // Container class: encapsulates components.
44 public class Container
: IContainer
, IDisposable
{
46 private ArrayList c
= new ArrayList ();
47 //ComponentCollection cc;
50 // Auxiliary class to support the default behaviour of CreateSite
54 // This is an internal class that is used to provide a
55 // default implementation of an ISite class. Container
56 // is just a default implementation of IContainer, and
57 // provides this as a way of getting started
60 class DefaultSite
: ISite
{
61 private IComponent component
;
62 private Container container
;
65 public DefaultSite (string name
, IComponent component
, Container container
)
67 this.component
= component
;
68 this.container
= container
;
72 public IComponent Component
{
78 public IContainer Container
{
85 public bool DesignMode
{
87 // FIXME: should we provide a way to set
103 public virtual object GetService (Type t
)
105 if (typeof(ISite
) != t
) {
108 return container
.GetService (t
);
113 // Container constructor
119 public virtual ComponentCollection Components
{
121 Array a
= c
.ToArray(typeof (IComponent
));
122 return new ComponentCollection((IComponent
[]) a
);
126 public virtual void Add (IComponent component
)
128 Add (component
, null);
131 public virtual void Add (IComponent component
, string name
)
133 component
.Site
= CreateSite (component
, name
);
137 protected virtual ISite
CreateSite (IComponent component
, string name
)
140 foreach (IComponent Comp
in c
) {
141 if (Comp
.Site
!= null && Comp
.Site
.Name
== name
)
142 throw new ArgumentException ("duplicate component name", "name");
146 return new DefaultSite (name
, component
, this);
149 public void Dispose ()
152 GC
.SuppressFinalize (this);
155 bool disposed
= false;
157 protected virtual void Dispose (bool release_all
)
164 foreach (IComponent component
in c
)
165 component
.Dispose ();
176 protected virtual object GetService (Type service
)
178 if (typeof(IContainer
) != service
) {
184 public virtual void Remove (IComponent component
)
186 c
.Remove (component
);