Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System / System.Diagnostics / TraceListenerCollection.cs
1 //
2 // System.Diagnostics.TraceListenerCollection.cs
3 //
4 // Authors:
5 //   Jonathan Pryor (jonpryor@vt.edu)
6 //
7 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
8 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
9 //
10 // (C) 2002 Jonathan Pryor
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34
35 using System;
36 using System.Collections;
37 using System.Diagnostics;
38 using System.Globalization;
39
40 namespace System.Diagnostics {
41
42         public class TraceListenerCollection : IList, ICollection, IEnumerable {
43
44                 private ArrayList listeners = ArrayList.Synchronized (new ArrayList (1));
45
46                 internal TraceListenerCollection ()
47                         : this (true)
48                 {
49                 }
50
51                 internal TraceListenerCollection (bool addDefault)
52                 {
53                         if (addDefault)
54                                 Add (new DefaultTraceListener ());
55                 }
56
57                 public int Count{
58                         get {return listeners.Count;}
59                 }
60
61                 public TraceListener this [string name] {
62                         get {
63                                 lock (listeners.SyncRoot) {
64                                         foreach (TraceListener listener in listeners) {
65                                                 if (listener.Name == name)
66                                                         return listener;
67                                         }
68                                 }
69                                 return null;
70                         }
71                 }
72
73                 public TraceListener this [int index] {
74                         get {return (TraceListener) listeners[index];}
75                         set {
76                                 InitializeListener (value);
77                                 listeners[index] = value;
78                         }
79                 }
80
81                 object IList.this [int index] {
82                         get {return listeners[index];}
83                         set {
84                                 TraceListener l = (TraceListener) value;
85                                 InitializeListener (l);
86                                 this[index] = l;
87                         }
88                 }
89
90                 bool ICollection.IsSynchronized {
91                         get {return listeners.IsSynchronized;}
92                 }
93
94                 object ICollection.SyncRoot {
95                         get {return listeners.SyncRoot;}
96                 }
97
98                 bool IList.IsFixedSize {
99                         get {return listeners.IsFixedSize;}
100                 }
101
102                 bool IList.IsReadOnly {
103                         get {return listeners.IsReadOnly;}
104                 }
105
106                 public int Add (TraceListener listener)
107                 {
108                         InitializeListener (listener);
109                         return listeners.Add (listener);
110                 }
111
112 #if !MOBILE
113                 internal void Add (TraceListener listener, TraceImplSettings settings)
114                 {
115                         listener.IndentLevel = settings.IndentLevel;
116                         listener.IndentSize  = settings.IndentSize;
117                         listeners.Add (listener);
118                 }
119 #endif
120
121                 private void InitializeListener (TraceListener listener)
122                 {
123                         listener.IndentLevel = TraceImpl.IndentLevel;
124                         listener.IndentSize = TraceImpl.IndentSize;
125                 }
126
127                 private void InitializeRange (IList listeners)
128                 {
129                         int e = listeners.Count;
130                         for (int i = 0; i != e; ++i)
131                                 InitializeListener (
132                                         (TraceListener) listeners[i]);
133                 }
134
135                 public void AddRange (TraceListener[] value)
136                 {
137                         InitializeRange (value);
138                         listeners.AddRange (value);
139                 }
140
141                 public void AddRange (TraceListenerCollection value)
142                 {
143                         InitializeRange (value);
144                         listeners.AddRange (value.listeners);
145                 }
146
147                 public void Clear ()
148                 {
149                         listeners.Clear ();
150                 }
151
152                 public bool Contains (TraceListener listener)
153                 {
154                         return listeners.Contains (listener);
155                 }
156
157                 public void CopyTo (TraceListener[] listeners, int index)
158                 {
159                         listeners.CopyTo (listeners, index);
160                 }
161
162                 public IEnumerator GetEnumerator ()
163                 {
164                         return listeners.GetEnumerator ();
165                 }
166
167                 void ICollection.CopyTo (Array array, int index)
168                 {
169                         listeners.CopyTo (array, index);
170                 }
171
172                 int IList.Add (object value)
173                 {
174                         if (value is TraceListener)
175                                 return Add ((TraceListener) value);
176                         throw new NotSupportedException (Locale.GetText (
177                                 "You can only add TraceListener objects to the collection"));
178                 }
179
180                 bool IList.Contains (object value)
181                 {
182                         if (value is TraceListener)
183                                 return listeners.Contains (value);
184                         return false;
185                 }
186
187                 int IList.IndexOf (object value)
188                 {
189                         if (value is TraceListener)
190                                 return listeners.IndexOf (value);
191                         return -1;
192                 }
193
194                 void IList.Insert (int index, object value)
195                 {
196                         if (value is TraceListener) {
197                                 Insert (index, (TraceListener) value);
198                                 return;
199                         }
200                         throw new NotSupportedException (Locale.GetText (
201                                 "You can only insert TraceListener objects into the collection"));
202                 }
203
204                 void IList.Remove (object value)
205                 {
206                         if (value is TraceListener)
207                                 listeners.Remove (value);
208                 }
209
210                 public int IndexOf (TraceListener listener)
211                 {
212                         return listeners.IndexOf (listener);
213                 }
214
215                 public void Insert (int index, TraceListener listener)
216                 {
217                         InitializeListener (listener);
218                         listeners.Insert (index, listener);
219                 }
220
221                 public void Remove (string name)
222                 {
223                         TraceListener found = null;
224
225                         lock (listeners.SyncRoot) {
226                                 foreach (TraceListener listener in listeners) {
227                                         if (listener.Name == name) {
228                                                 found = listener;
229                                                 break;
230                                         }
231                                 }
232
233                                 if (found != null)
234                                         listeners.Remove (found);
235                                 else
236                                         throw new ArgumentException (Locale.GetText (
237                                                 "TraceListener " + name + " was not in the collection"));
238                         }
239                 }
240
241                 public void Remove (TraceListener listener)
242                 {
243                         listeners.Remove (listener);
244                 }
245
246                 public void RemoveAt (int index)
247                 {
248                         listeners.RemoveAt (index);
249                 }
250         }
251 }
252