1 //------------------------------------------------------------------------------
2 // <copyright file="TreeNodeBindingCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.UI.WebControls {
8     using System;
9     using System.Collections;
10     using System.ComponentModel;
11     using System.Web;
12 
13 
14     /// <devdoc>
15     ///     Provides a collection of TreeNodeBinding objects
16     /// </devdoc>
17     public sealed class TreeNodeBindingCollection : StateManagedCollection {
18         private static readonly Type[] knownTypes = new Type[] { typeof(TreeNodeBinding) };
19 
20         private TreeNodeBinding _defaultBinding;
21 
TreeNodeBindingCollection()22         internal TreeNodeBindingCollection() {
23         }
24 
25         /// <devdoc>
26         /// Gets the TreeNodeBinding at the specified index
27         /// </devdoc>
28         public TreeNodeBinding this[int i] {
29             get {
30                 return (TreeNodeBinding)((IList)this)[i];
31             }
32             set {
33                 ((IList)this)[i] = value;
34             }
35         }
36 
37         /// <devdoc>
38         /// Adds a TreeNodeBinding to the collection
39         /// </devdoc>
Add(TreeNodeBinding binding)40         public int Add(TreeNodeBinding binding) {
41             return ((IList)this).Add(binding);
42         }
43 
Contains(TreeNodeBinding binding)44         public bool Contains(TreeNodeBinding binding) {
45             return ((IList)this).Contains(binding);
46         }
47 
CopyTo(TreeNodeBinding[] bindingArray, int index)48         public void CopyTo(TreeNodeBinding[] bindingArray, int index) {
49             base.CopyTo(bindingArray, index);
50         }
51 
CreateKnownType(int index)52         protected override object CreateKnownType(int index) {
53             return new TreeNodeBinding();
54         }
55 
FindDefaultBinding()56         private void FindDefaultBinding() {
57             _defaultBinding = null;
58             // Look for another binding that would be a good default
59             foreach (TreeNodeBinding binding in this) {
60                 if (binding.Depth == -1 && binding.DataMember.Length == 0) {
61                     _defaultBinding = binding;
62                     break;
63                 }
64             }
65         }
66 
67         /// <devdoc>
68         ///     Gets a TreeNodeBinding data binding definition for the specified depth or datamember
69         /// </devdoc>
GetBinding(string dataMember, int depth)70         internal TreeNodeBinding GetBinding(string dataMember, int depth) {
71             TreeNodeBinding bestMatch = null;
72             int match = 0;
73             if ((dataMember != null) && (dataMember.Length == 0)) {
74                 dataMember = null;
75             }
76 
77             foreach (TreeNodeBinding binding in this) {
78                 if ((binding.Depth == depth)) {
79                     if (String.Equals(binding.DataMember, dataMember, StringComparison.CurrentCultureIgnoreCase)) {
80                         return binding;
81                     }
82                     else if ((match < 1) && (binding.DataMember.Length == 0)) {
83                         bestMatch = binding;
84                         match = 1;
85                     }
86                 }
87                 else if (String.Equals(binding.DataMember, dataMember, StringComparison.CurrentCultureIgnoreCase) &&
88                     (match < 2) &&
89                     (binding.Depth == -1)) {
90 
91                     bestMatch = binding;
92                     match = 2;
93                 }
94             }
95 
96             if (bestMatch == null) {
97                 // Check that the default binding is still suitable (VSWhidbey 358817)
98                 if (_defaultBinding != null) {
99                     if (_defaultBinding.Depth != -1 || _defaultBinding.DataMember.Length != 0) {
100                         // Look for another binding that would be a good default
101                         FindDefaultBinding();
102                     }
103                     bestMatch = _defaultBinding;
104                 }
105             }
106 
107             return bestMatch;
108         }
109 
GetKnownTypes()110         protected override Type[] GetKnownTypes() {
111             return knownTypes;
112         }
113 
IndexOf(TreeNodeBinding binding)114         public int IndexOf(TreeNodeBinding binding) {
115             return ((IList)this).IndexOf(binding);
116         }
117 
Insert(int index, TreeNodeBinding binding)118         public void Insert(int index, TreeNodeBinding binding) {
119             ((IList)this).Insert(index, binding);
120         }
121 
OnClear()122         protected override void OnClear() {
123             base.OnClear();
124             _defaultBinding = null;
125         }
126 
OnRemoveComplete(int index, object value)127         protected override void OnRemoveComplete(int index, object value) {
128             if (value == _defaultBinding) {
129                 FindDefaultBinding();
130             }
131         }
132 
OnValidate(object value)133         protected override void OnValidate(object value) {
134             base.OnValidate(value);
135             TreeNodeBinding binding = value as TreeNodeBinding;
136             if ((binding != null) && (binding.DataMember.Length == 0) && (binding.Depth == -1)) {
137                 _defaultBinding = binding;
138             }
139         }
140 
141         /// <devdoc>
142         /// Removes a TreeNodeBinding from the collection.
143         /// </devdoc>
Remove(TreeNodeBinding binding)144         public void Remove(TreeNodeBinding binding) {
145             ((IList)this).Remove(binding);
146         }
147 
148         /// <devdoc>
149         /// Removes a TreeNodeBinding from the collection at a given index.
150         /// </devdoc>
RemoveAt(int index)151         public void RemoveAt(int index) {
152             ((IList)this).RemoveAt(index);
153         }
154 
SetDirtyObject(object o)155         protected override void SetDirtyObject(object o) {
156             if (o is TreeNodeBinding) {
157                 ((TreeNodeBinding)o).SetDirty();
158             }
159         }
160     }
161 }
162 
163