updating to the latest module.
[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 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
31 #if NET_2_0
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35
36 namespace System.Web.UI.WebControls
37 {
38         public class DataKey : IStateManager
39         {
40                 IOrderedDictionary keyTable;
41                 string[] keyNames;
42                 bool trackViewState;
43                 IOrderedDictionary readonlyKeyTable;
44
45                 public DataKey (IOrderedDictionary keyTable)
46                 {
47                         this.keyTable = keyTable;
48                 }
49
50                 public DataKey (IOrderedDictionary keyTable, string[] keyNames)
51                 {
52                         this.keyTable = keyTable;
53                         this.keyNames = keyNames;
54                 }
55                 
56                 public virtual object this [int index] {
57                         get { return keyTable [index]; }
58                 }
59                 
60                 public virtual object this [string name] {
61                         get { return keyTable [name]; }
62                 }
63                 
64                 public virtual object Value {
65                         get { return keyTable [0]; }
66                 }
67                 
68                 public virtual IOrderedDictionary Values {
69                         get {
70                                 if (readonlyKeyTable == null) {
71                                         if (keyTable is OrderedDictionary)
72                                                 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
73                                         else
74                                                 readonlyKeyTable = keyTable;
75                                 }
76                                 return readonlyKeyTable; 
77                         }
78                 }
79                 
80                 protected virtual void LoadViewState (object savedState)
81                 {
82                         if (savedState is Pair) {
83                                 Pair p = (Pair) savedState;
84                                 object[] akeys = (object[]) p.First;
85                                 object[] avals = (object[]) p.Second;
86                                 for (int n=0; n<akeys.Length; n++) {
87                                         keyTable [akeys[n]] = avals [n];
88                                 }
89                         } else if (savedState is object[]) {
90                                 object[] avals = (object[]) savedState;
91                                 for (int n=0; n<avals.Length; n++)
92                                         keyTable [keyNames[n]] = avals [n];
93                         }
94                 }
95                 
96                 protected virtual object SaveViewState ()
97                 {
98                         if (keyTable.Count == 0) return null;
99                         
100                         if (keyNames != null) {
101                                 object[] avals = new object [keyTable.Count];
102                                 int n=0;
103                                 foreach (object val in keyTable.Values)
104                                         avals [n++] = val;
105                                 return avals;
106                         } else {
107                                 object[] avals = new object [keyTable.Count];
108                                 object[] akeys = new object [keyTable.Count];
109                                 int n=0;
110                                 foreach (DictionaryEntry de in keyTable) {
111                                         akeys [n] = de.Key;
112                                         avals [n++] = de.Value;
113                                 }
114                                 return new Pair (akeys, avals);
115                         }
116                 }
117                 
118                 protected virtual void TrackViewState ()
119                 {
120                         trackViewState = true;
121                 }
122                 
123                 protected virtual bool IsTrackingViewState {
124                         get { return trackViewState; }
125                 }
126                 
127                 void IStateManager.LoadViewState (object savedState)
128                 {
129                         LoadViewState (savedState);
130                 }
131                 
132                 object IStateManager.SaveViewState ()
133                 {
134                         return SaveViewState ();
135                 }
136                 
137                 void IStateManager.TrackViewState ()
138                 {
139                         TrackViewState ();
140                 }
141                 
142                 bool IStateManager.IsTrackingViewState {
143                         get { return IsTrackingViewState; }
144                 }
145         }
146 }
147 #endif