2010-03-06 Marek Habersack <mhabersack@novell.com>
[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 {
66                                 if (keyTable.Count == 0)
67                                         return null;
68                                 return keyTable [0]; 
69                         }
70                 }
71                 
72                 public virtual IOrderedDictionary Values {
73                         get {
74                                 if (readonlyKeyTable == null) {
75                                         if (keyTable is OrderedDictionary)
76                                                 readonlyKeyTable = ((OrderedDictionary)keyTable).AsReadOnly ();
77                                         else
78                                                 readonlyKeyTable = keyTable;
79                                 }
80                                 return readonlyKeyTable; 
81                         }
82                 }
83                 
84                 protected virtual void LoadViewState (object savedState)
85                 {
86                         if (savedState is Pair) {
87                                 Pair p = (Pair) savedState;
88                                 object[] akeys = (object[]) p.First;
89                                 object[] avals = (object[]) p.Second;
90                                 for (int n=0; n<akeys.Length; n++) {
91                                         keyTable [akeys[n]] = avals [n];
92                                 }
93                         } else if (savedState is object[]) {
94                                 object[] avals = (object[]) savedState;
95                                 for (int n=0; n<avals.Length; n++)
96                                         keyTable [keyNames[n]] = avals [n];
97                         }
98                 }
99                 
100                 protected virtual object SaveViewState ()
101                 {
102                         if (keyTable.Count == 0) return null;
103                         
104                         if (keyNames != null) {
105                                 object[] avals = new object [keyTable.Count];
106                                 int n=0;
107                                 foreach (object val in keyTable.Values)
108                                         avals [n++] = val;
109                                 return avals;
110                         } else {
111                                 object[] avals = new object [keyTable.Count];
112                                 object[] akeys = new object [keyTable.Count];
113                                 int n=0;
114                                 foreach (DictionaryEntry de in keyTable) {
115                                         akeys [n] = de.Key;
116                                         avals [n++] = de.Value;
117                                 }
118                                 return new Pair (akeys, avals);
119                         }
120                 }
121                 
122                 protected virtual void TrackViewState ()
123                 {
124                         trackViewState = true;
125                 }
126                 
127                 protected virtual bool IsTrackingViewState {
128                         get { return trackViewState; }
129                 }
130                 
131                 void IStateManager.LoadViewState (object savedState)
132                 {
133                         LoadViewState (savedState);
134                 }
135                 
136                 object IStateManager.SaveViewState ()
137                 {
138                         return SaveViewState ();
139                 }
140                 
141                 void IStateManager.TrackViewState ()
142                 {
143                         TrackViewState ();
144                 }
145                 
146                 bool IStateManager.IsTrackingViewState {
147                         get { return IsTrackingViewState; }
148                 }
149         }
150 }
151 #endif