copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TreeNodeCollection.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Jackson Harper (jackson@ximian.com)
24
25 // TODO: Sorting
26
27 using System;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Globalization;
31
32 namespace System.Windows.Forms {
33         [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
34         public class TreeNodeCollection : IList, ICollection, IEnumerable {
35
36                 private static readonly int OrigSize = 50;
37
38                 private TreeNode owner;
39                 private int count;
40                 private TreeNode [] nodes;
41
42                 private TreeNodeCollection ()
43                 {
44                 }
45
46                 internal TreeNodeCollection (TreeNode owner)
47                 {
48                         this.owner = owner;
49                         nodes = new TreeNode [OrigSize];
50                 }
51
52                 [Browsable(false)]
53                 [EditorBrowsable(EditorBrowsableState.Advanced)]
54                 public virtual int Count {
55                         get { return count; }
56                 }
57
58                 public virtual bool IsReadOnly {
59                         get { return false; }
60                 }
61
62                 bool ICollection.IsSynchronized {
63                         get { return false; }
64                 }
65
66                 object ICollection.SyncRoot {
67                         get { return this; }
68                 }
69
70                 bool IList.IsFixedSize {
71                         get { return false; }
72                 }
73
74                 object IList.this [int index] {
75                         get {
76                                 if (index < 0 || index > Count)
77                                         throw new ArgumentOutOfRangeException ("index");
78                                 return nodes [index];
79                         }
80                         set {
81                                 if (index < 0 || index > Count)
82                                         throw new ArgumentOutOfRangeException ("index");
83                                 TreeNode node = (TreeNode) value;
84                                 SetData (node);
85                                 nodes [index] = node;
86                         }
87                 }
88
89                 public virtual TreeNode this [int index] {
90                         get {
91                                 if (index < 0 || index > Count)
92                                         throw new ArgumentOutOfRangeException ("index");
93                                 return nodes [index];
94                         }
95                         set {
96                                 if (index < 0 || index > Count)
97                                         throw new ArgumentOutOfRangeException ("index");
98                                 SetData (value);
99                                 nodes [index] = value;
100                         }
101                 }
102
103                 public virtual TreeNode Add (string text)
104                 {
105                         TreeNode res = new TreeNode (text);
106                         Add (res);
107                         return res;
108                 }
109
110                 public virtual int Add (TreeNode node)
111                 {
112                         if (node == null)
113                                 throw new ArgumentNullException("node");
114
115                         if (owner != null && owner.TreeView != null && owner.TreeView.Sorted)
116                                 return AddSorted (node);
117                         SetData (node);
118                         if (count >= nodes.Length)
119                                 Grow ();
120                         nodes [count++] = node;
121
122                         if (owner.TreeView != null)
123                                 owner.TreeView.Refresh ();
124                         return count;
125                 }
126
127                 public virtual void AddRange (TreeNode [] nodes)
128                 {
129                         if (nodes == null)
130                                 throw new ArgumentNullException("node");
131
132                         // We can't just use Array.Copy because the nodes also
133                         // need to have some properties set when they are added.
134                         for (int i = 0; i < nodes.Length; i++)
135                                 Add (nodes [i]);
136                 }
137
138                 public virtual void Clear ()
139                 {
140                         Array.Clear (nodes, 0, count);
141                         count = 0;
142                 }
143
144                 public bool Contains (TreeNode node)
145                 {
146                         return (Array.BinarySearch (nodes, node) > 0);
147                 }
148
149                 public virtual void CopyTo (Array dest, int index)
150                 {
151                         nodes.CopyTo (dest, index);
152                 }
153
154                 public virtual IEnumerator GetEnumerator ()
155                 {
156                         return new TreeNodeEnumerator (this);
157                 }
158
159                 public int IndexOf (TreeNode node)
160                 {
161                         return Array.IndexOf (nodes, node);
162                 }
163
164                 public virtual void Insert (int index, TreeNode node)
165                 {
166                         SetData (node);
167                         IList list = (IList) nodes;
168                         list.Insert (index, node);
169                 }
170
171                 public virtual void Remove (TreeNode node)
172                 {
173                         int index = IndexOf (node);
174                         if (index > 0)
175                                 RemoveAt (index);
176                 }
177
178                 public virtual void RemoveAt (int index)
179                 {
180                         Array.Copy (nodes, index, nodes, index - 1, count - index);
181                         count--;
182                         if (nodes.Length > OrigSize && nodes.Length > (count * 2))
183                                 Shrink ();
184                         if (owner.TreeView != null)
185                                 owner.TreeView.TotalNodeCount--;
186                 }
187
188                 int IList.Add (object node)
189                 {
190                         return Add ((TreeNode) node);
191                 }
192
193                 bool IList.Contains (object node)
194                 {
195                         return Contains ((TreeNode) node);
196                 }
197                 
198                 int IList.IndexOf (object node)
199                 {
200                         return IndexOf ((TreeNode) node);
201                 }
202
203                 void IList.Insert (int index, object node)
204                 {
205                         Insert (index, (TreeNode) node);
206                 }
207
208                 void IList.Remove (object node)
209                 {
210                         Remove ((TreeNode) node);
211                 }
212
213                 private int AddSorted (TreeNode node)
214                 {
215                         
216                         if (count >= nodes.Length)
217                                 Grow ();
218
219                         CompareInfo compare = Application.CurrentCulture.CompareInfo;
220                         int pos = 0;
221                         bool found = false;
222                         for (int i = 0; i < count; i++) {
223                                 pos = i;
224                                 int comp = compare.Compare (node.Text, nodes [i].Text);
225                                 if (comp < 0) {
226                                         found = true;
227                                         break;
228                                 }
229                         }
230
231                         // Stick it at the end
232                         if (!found)
233                                 pos = count;
234
235                         // Move the nodes up and adjust their indices
236                         for (int i = count - 1; i >= pos; i--) {
237                                 nodes [i + 1] = nodes [i];
238                                 nodes [i + 1].SetIndex (i + 1);
239                         }
240                         count++;
241                         nodes [pos] = node;
242
243                         SetData (node, pos);
244                         return count;
245                 }
246
247                 // Would be nice to do this without running through the collection twice
248                 internal void Sort () {
249
250                         Array.Sort (nodes, 0, count, new TreeNodeComparer (Application.CurrentCulture.CompareInfo));
251
252                         for (int i = 0; i < count; i++) {
253                                 nodes [i].SetIndex (i);
254                                 nodes [i].Nodes.Sort ();
255                         }
256                 }
257
258                 private void SetData (TreeNode node)
259                 {
260                         SetData (node, count);
261                 }
262
263                 private void SetData (TreeNode node, int pos)
264                 {
265                         node.SetAddedData ((owner != null ? owner.TreeView : null), owner, pos);
266                 }
267
268                 private void Grow ()
269                 {
270                         TreeNode [] nn = new TreeNode [nodes.Length + 50];
271                         Array.Copy (nodes, nn, nodes.Length);
272                         nodes = nn;
273                 }
274
275                 private void Shrink ()
276                 {
277                         int len = (count > OrigSize ? count : OrigSize);
278                         TreeNode [] nn = new TreeNode [len];
279                         Array.Copy (nodes, nn, count);
280                         nodes = nn;
281                 }
282
283                 
284                 internal class TreeNodeEnumerator : IEnumerator {
285
286                         private TreeNodeCollection collection;
287                         private int index = -1;
288
289                         public TreeNodeEnumerator (TreeNodeCollection collection)
290                         {
291                                 this.collection = collection;
292                         }
293
294                         public object Current {
295                                 get { return collection [index]; }
296                         }
297
298                         public bool MoveNext ()
299                         {
300                                 if (index + 1 >= collection.Count)
301                                         return false;
302                                 index++;
303                                 return true;
304                         }
305
306                         public void Reset ()
307                         {
308                                 index = 0;
309                         }
310                 }
311
312                 private class TreeNodeComparer : IComparer {
313
314                         private CompareInfo compare;
315                 
316                         public TreeNodeComparer (CompareInfo compare)
317                         {
318                                 this.compare = compare;
319                         }
320                 
321                         public int Compare (object x, object y)
322                         {
323                                 TreeNode l = (TreeNode) x;
324                                 TreeNode r = (TreeNode) y;
325                                 int res = compare.Compare (l.Text, r.Text);
326
327                                 return (res == 0 ? l.Index - r.Index : res);
328                         }
329                 }
330         }
331 }
332