2007-08-29 Ivan N. Zlatev <contact@i-nz.net>
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / DesignSurfaceCollection.cs
1 //
2 // System.ComponentModel.Design.DesignSurfaceCollection
3 //
4 // Authors:      
5 //        Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2006 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.ComponentModel;
34 using System.Collections;
35
36 namespace System.ComponentModel.Design
37 {
38         // A read-only collection of design surfaces.
39         // A wrapper around a DesignerCollection, which get's the DesignSurface for the IDesignerHost
40         // on the fly.
41         //
42         public sealed class DesignSurfaceCollection : ICollection, IEnumerable
43         {
44                 
45                 private class DesignSurfaceEnumerator : IEnumerator
46                 {
47                         IEnumerator _designerCollectionEnumerator;
48                         
49                         public DesignSurfaceEnumerator (IEnumerator designerCollectionEnumerator)
50                         {
51                                 _designerCollectionEnumerator = designerCollectionEnumerator;
52                         }
53
54                         public bool MoveNext ()
55                         {
56                                 return _designerCollectionEnumerator.MoveNext ();
57                         }
58
59                         public void Reset ()
60                         {
61                                 _designerCollectionEnumerator.Reset ();
62                         }
63
64                         public object Current {
65                                 get {
66                                         IDesignerHost designer = (IDesignerHost) _designerCollectionEnumerator.Current;
67                                         DesignSurface surface = designer.GetService (typeof (DesignSurface)) as DesignSurface;
68                                         if (surface == null)
69                                                 throw new NotSupportedException ();
70                                         
71                                         return surface;
72                                 }
73                         }
74                         
75                 } // DesignSurfaceEnumerator
76
77                 
78                 private DesignerCollection _designers;
79                 
80                 internal DesignSurfaceCollection (DesignerCollection designers)
81                 {
82                         if (designers == null)
83                                 designers = new DesignerCollection (null);
84
85                         _designers = designers;
86                 }
87
88                 public int Count {
89                         get { return _designers.Count; }
90                 }
91
92                 public object this[int index] {
93                         get {
94                                 IDesignerHost designer = _designers[index];
95                                 DesignSurface surface = designer.GetService (typeof (DesignSurface)) as DesignSurface;
96                                 if (surface == null)
97                                         throw new NotSupportedException ();
98
99                                 return surface;
100                         }
101                 }
102
103                 public void CopyTo (DesignSurface[] array, int index)
104                 {
105                         ((ICollection) this).CopyTo (array, index);
106                 }
107
108                 void ICollection.CopyTo (Array array, int index)
109                 {
110                         foreach (DesignSurface surface in this) {
111                                 array.SetValue (surface, index);
112                                 index++;
113                         }
114                 }
115                 
116                 public IEnumerator GetEnumerator ()
117                 {
118                         return new DesignSurfaceEnumerator (_designers.GetEnumerator ());
119                 }
120
121                 IEnumerator IEnumerable.GetEnumerator ()
122                 {
123                         return GetEnumerator ();
124                 }
125
126                 int ICollection.Count {
127                         get { return this.Count; }
128                 }
129
130                 bool ICollection.IsSynchronized {
131                         get { return false; }
132                 }
133
134                 object ICollection.SyncRoot {
135                         get { return null; }
136                 }
137                 
138         }
139         
140 }
141 #endif