2008-03-02 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / ReferenceService.cs
1 //
2 // System.ComponentModel.Design.ReferenceService
3 //
4 // Authors: 
5 //  Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2007 Ivan N. Zlatev
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections.Generic;
34 using System.Globalization;
35 using System.ComponentModel;
36
37 namespace System.ComponentModel.Design
38 {
39
40         internal class ReferenceService : IReferenceService, IDisposable
41         {
42
43                 private List<IComponent> _references;
44
45                 internal ReferenceService (IServiceProvider provider)
46                 {
47                         if (provider == null)
48                                 throw new ArgumentNullException ("provider");
49
50                         _references = new List<IComponent>();
51                         IComponentChangeService serv = provider.GetService (typeof (IComponentChangeService)) as IComponentChangeService;
52                         if (serv != null) {
53                                 serv.ComponentAdded += OnComponentAdded;
54                                 serv.ComponentRemoved += OnComponentRemoved;
55                         }
56                 }
57
58                 private void OnComponentAdded (object sender, ComponentEventArgs args)
59                 {
60                         _references.Add (args.Component);
61                 }
62
63                 private void OnComponentRemoved (object sender, ComponentEventArgs args)
64                 {
65                         _references.Remove (args.Component);
66                 }
67
68                 public IComponent GetComponent (object reference)
69                 {
70                         return reference as IComponent;
71                 }
72
73                 public string GetName (object reference)
74                 {
75                         IComponent comp = reference as IComponent;
76                         if (comp != null && comp.Site != null)
77                                 return comp.Site.Name;
78                         return null;
79                 }
80
81                 public object GetReference (string name)
82                 {
83                         foreach (IComponent component in _references)
84                                 if (component.Site != null && component.Site.Name == name)
85                                         return component;
86                         return null;
87                 }
88
89                 public object[] GetReferences ()
90                 {
91                         IComponent[] references = new IComponent[_references.Count];
92                         _references.CopyTo (references);
93                         return references;
94                 }
95
96                 public object[] GetReferences (Type baseType)
97                 {
98                         List<IComponent> references = new List<IComponent>();
99
100                         foreach (IComponent component in _references)
101                                 if (baseType.IsAssignableFrom ((component.GetType ())))
102                                         references.Add (component);
103
104                         IComponent[] refArray = new IComponent[references.Count];
105                         references.CopyTo (refArray);
106                         return refArray;
107                 }
108
109                 public void Dispose ()
110                 {
111                         _references.Clear ();
112                 }
113         }
114 }
115 #endif