* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / BindingContext.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 //      Peter Bartok    pbartok@novell.com
24 //      Jackson Harper  jackson@ximian.com
25
26
27 using System.Data;
28 using System.Collections;
29 using System.Globalization;
30 using System.ComponentModel;
31
32
33 namespace System.Windows.Forms {
34
35         [DefaultEvent("CollectionChanged")]
36         public class BindingContext : ICollection, IEnumerable {
37
38                 private Hashtable managers;
39                 private EventHandler onCollectionChangedHandler;
40
41                 private class HashKey {
42                         public object source;
43                         public string member;
44
45                         public HashKey (object source, string member)
46                         {
47                                 this.source = source;
48                                 this.member = member;
49                         }
50
51                         public override int GetHashCode ()
52                         {
53                                 return source.GetHashCode() + member.GetHashCode ();
54                         }
55
56                         public override bool Equals (object o)
57                         {
58                                 HashKey hk = o as HashKey;
59                                 if (hk == null)
60                                         return false;
61                                 return hk.source == source && hk.member == member;
62                         }
63                 }
64
65                 public BindingContext () 
66                 {
67                         managers = new Hashtable ();
68                 }
69
70                 public bool IsReadOnly {
71                         get { return false; }
72                 }
73
74                 public BindingManagerBase this [object dataSource] {
75                         get { return this [dataSource, String.Empty]; }
76                 }
77
78                 public BindingManagerBase this [object data_source, string data_member] {
79                         get {
80                                 if (data_source == null)
81                                         throw new ArgumentNullException ("data_source");
82                                 if (data_member == null)
83                                         data_member = String.Empty;
84
85                                 HashKey key = new HashKey (data_source, data_member);
86                                 BindingManagerBase res = managers [key] as BindingManagerBase;
87
88                                 if (res != null)
89                                         return res;
90
91                                 res = CreateBindingManager (data_source, data_member);
92                                 if (res == null)
93                                         return null;
94                                 managers [key] = res;
95                                 return res;
96                         }
97                 }
98
99                 private BindingManagerBase CreateBindingManager (object data_source, string data_member)
100                 {
101                         if (data_member == "") {
102                                 if (IsListType (data_source.GetType ()))
103                                         return new CurrencyManager (data_source);
104                                 else
105                                         return new PropertyManager (data_source);
106                         }
107                         else {
108                                 BindingMemberInfo info = new BindingMemberInfo (data_member);
109
110                                 BindingManagerBase parent_manager = this[data_source, info.BindingPath];
111
112                                 PropertyDescriptor pd = parent_manager == null ? null : parent_manager.GetItemProperties ().Find (info.BindingField, true);
113
114                                 if (pd == null)
115                                         throw new ArgumentException (String.Format ("Cannot create a child list for field {0}.", info.BindingField));
116
117                                 if (IsListType (pd.PropertyType))
118                                         return new RelatedCurrencyManager (parent_manager, pd);
119                                 else
120                                         return new RelatedPropertyManager (parent_manager, info.BindingField);
121                         }
122                 }
123
124                 bool IsListType (Type t)
125                 {
126                         return (typeof (IList).IsAssignableFrom (t)
127                                 || typeof (IListSource).IsAssignableFrom (t));
128                 }
129
130                 #region Public Instance Methods
131                 public bool Contains(object dataSource)
132                 {
133                         return Contains (dataSource, String.Empty);
134                 }
135
136                 public bool Contains (object dataSource, string dataMember)
137                 {
138                         if (dataSource == null)
139                                 throw new ArgumentNullException ("dataSource");
140                         if (dataMember == null)
141                                 dataMember = String.Empty;
142
143                         HashKey key = new HashKey (dataSource, dataMember);
144                         return managers [key] != null;
145                 }
146                 #endregion      // Public Instance Methods
147
148                 #region Protected Instance Methods
149
150                 protected internal void Add (object dataSource, BindingManagerBase listManager)
151                 {
152                         AddCore (dataSource, listManager);
153                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, dataSource));
154                 }
155
156                 protected virtual void AddCore (object dataSource, BindingManagerBase listManager)
157                 {
158                         if (dataSource == null)
159                                 throw new ArgumentNullException ("dataSource");
160                         if (listManager == null)
161                                 throw new ArgumentNullException ("listManager");
162
163                         HashKey key = new HashKey (dataSource, String.Empty);
164                         managers [key] = listManager;
165                 }
166
167                 protected internal void Clear ()
168                 {
169                         ClearCore();
170                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
171                 }
172
173                 protected virtual void ClearCore ()
174                 {
175                         managers.Clear ();
176                 }
177
178                 protected virtual void OnCollectionChanged (CollectionChangeEventArgs ccevent)
179                 {
180                         if (onCollectionChangedHandler != null) {
181                                 onCollectionChangedHandler (this, ccevent);
182                         }
183                 }
184
185                 protected internal void Remove (object dataSource)
186                 {
187                         if (dataSource == null)
188                                 throw new ArgumentNullException ("dataSource");
189
190                         RemoveCore (dataSource);
191                         OnCollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, dataSource));
192                 }
193
194                 protected virtual void RemoveCore (object dataSource)
195                 {
196                         HashKey[] keys = new HashKey [managers.Keys.Count];
197                         managers.Keys.CopyTo (keys, 0);
198
199                         for (int i = 0; i < keys.Length; i ++) {
200                                 if (keys[i].source == dataSource)
201                                         managers.Remove (keys[i]);
202                         }
203                 }
204                 #endregion      // Protected Instance Methods
205
206                 #region Events
207                 public event CollectionChangeEventHandler CollectionChanged {
208                         add { throw new NotImplementedException (); }
209                         remove { /* nothing to do here.. */ }
210                 }
211                 #endregion      // Events
212
213                 #region ICollection Interfaces
214                 void ICollection.CopyTo (Array array, int index)
215                 {
216                         managers.CopyTo (array, index);
217                 }
218
219                 int ICollection.Count {
220                         get { return managers.Count; }
221                 }
222
223                 bool ICollection.IsSynchronized {
224                         get { return false; }
225                 }
226
227                 object ICollection.SyncRoot {
228                         get { return null; }
229                 }
230
231                 #endregion      // ICollection Interfaces
232
233                 #region IEnumerable Interfaces
234                 [MonoTODO ("our enumerator is slightly different.  in MS's implementation the Values are WeakReferences to the managers.")]
235                 IEnumerator IEnumerable.GetEnumerator() {
236                         return managers.GetEnumerator ();
237                 }
238                 #endregion      // IEnumerable Interfaces
239         }
240 }