Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataKey.cs
1 //
2 // System.Web.UI.WebControls.DataKey.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.Collections.Specialized;
33
34 namespace System.Web.UI.WebControls
35 {
36         public class DataKey : IStateManager
37         , IEquatable <DataKey>
38         {
39                 IOrderedDictionary keyTable;
40                 string[] keyNames;
41                 bool trackViewState;
42                 IOrderedDictionary readonlyKeyTable;
43
44                 public DataKey (IOrderedDictionary keyTable)
45                         : this (keyTable, null)
46                 {
47                 }
48
49                 public DataKey (IOrderedDictionary keyTable, string[] keyNames)
50                 {
51                         this.keyTable = keyTable;
52                         this.keyNames = keyNames;
53                 }
54                 
55                 public virtual object this [int index] {
56                         get { return keyTable [index]; }
57                 }
58                 
59                 public virtual object this [string name] {
60                         get { return keyTable [name]; }
61                 }
62                 
63                 public virtual object Value {
64                         get {
65                                 if (keyTable.Count == 0)
66                                         return null;
67                                 return keyTable [0]; 
68                         }
69                 }
70                 
71                 public virtual IOrderedDictionary Values {
72                         get {
73                                 if (readonlyKeyTable == null) {
74                                         if (keyTable is OrderedDictionary)
75                                                 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
76                                         else
77                                                 readonlyKeyTable = keyTable;
78                                 }
79                                 return readonlyKeyTable; 
80                         }
81                 }
82                 public bool Equals (DataKey other)
83                 {
84                         if (other == null)
85                                 return false;
86
87                         IOrderedDictionary otherKeyTable = other.keyTable;
88                         if (keyTable != null && otherKeyTable != null) {
89                                 if (keyTable.Count != otherKeyTable.Count)
90                                         return false;
91                                 
92                                 object thisValue, otherValue;
93                                 
94                                 foreach (object key in keyTable.Keys) {
95                                         if (!otherKeyTable.Contains (key))
96                                                 return false;
97
98                                         thisValue = keyTable [key];
99                                         otherValue = otherKeyTable [key];
100
101                                         if (thisValue == null ^ otherValue == null)
102                                                 return false;
103
104                                         if (!thisValue.Equals (otherValue))
105                                                 return false;
106                                 }
107                         }
108                         
109                         string[] otherKeyNames = other.keyNames;
110                         if (keyNames != null && otherKeyNames != null) {
111                                 int len = keyNames.Length;
112                                 if (len != otherKeyNames.Length)
113                                         return false;
114
115                                 for (int i = 0; i < len; i++)
116                                         if (String.Compare (keyNames [i], otherKeyNames [i], StringComparison.Ordinal) != 0)
117                                                 return false;
118                         } else if (keyNames == null ^ otherKeyNames == null)
119                                 return false;
120                         
121                         return true;
122                 }
123                 protected virtual void LoadViewState (object savedState)
124                 {
125                         if (savedState is Pair) {
126                                 Pair p = (Pair) savedState;
127                                 object[] akeys = (object[]) p.First;
128                                 object[] avals = (object[]) p.Second;
129                                 for (int n=0; n<akeys.Length; n++) {
130                                         keyTable [akeys[n]] = avals [n];
131                                 }
132                         } else if (savedState is object[]) {
133                                 object[] avals = (object[]) savedState;
134                                 for (int n=0; n<avals.Length; n++)
135                                         keyTable [keyNames[n]] = avals [n];
136                         }
137                 }
138                 
139                 protected virtual object SaveViewState ()
140                 {
141                         if (keyTable.Count == 0)
142                                 return null;
143                         
144                         if (keyNames != null) {
145                                 object[] avals = new object [keyTable.Count];
146                                 int n=0;
147                                 foreach (object val in keyTable.Values)
148                                         avals [n++] = val;
149                                 return avals;
150                         } else {
151                                 object[] avals = new object [keyTable.Count];
152                                 object[] akeys = new object [keyTable.Count];
153                                 int n=0;
154                                 foreach (DictionaryEntry de in keyTable) {
155                                         akeys [n] = de.Key;
156                                         avals [n++] = de.Value;
157                                 }
158                                 return new Pair (akeys, avals);
159                         }
160                 }
161                 
162                 protected virtual void TrackViewState ()
163                 {
164                         trackViewState = true;
165                 }
166                 
167                 protected virtual bool IsTrackingViewState {
168                         get { return trackViewState; }
169                 }
170                 
171                 void IStateManager.LoadViewState (object savedState)
172                 {
173                         LoadViewState (savedState);
174                 }
175                 
176                 object IStateManager.SaveViewState ()
177                 {
178                         return SaveViewState ();
179                 }
180                 
181                 void IStateManager.TrackViewState ()
182                 {
183                         TrackViewState ();
184                 }
185                 
186                 bool IStateManager.IsTrackingViewState {
187                         get { return IsTrackingViewState; }
188                 }
189         }
190 }
191