2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ListItem.cs
1 //
2 // System.Web.UI.WebControls.ListItem.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS - no inheritance demand required because the class is sealed
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [ControlBuilder(typeof(ListItemControlBuilder))]
39         [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
40         public sealed class ListItem : IAttributeAccessor, IParserAccessor, IStateManager
41         {
42 #if NET_2_0
43                 public ListItem (string text, string value, bool enabled) : this (text, value)
44                 {
45                         this.enabled = enabled;
46                 }
47 #endif
48                 public ListItem (string text, string value)
49                 {
50                         this.text = text;
51                         this.value = value;
52                 }
53         
54                 public ListItem (string text) : this (text, null)
55                 {
56                 }
57         
58                 public ListItem () : this (null, null) 
59                 {
60                 }
61         
62                 public static ListItem FromString (string text)
63                 {
64                         return new ListItem (text);
65                 }
66         
67                 public override bool Equals (object o)
68                 {
69                         ListItem li = o as ListItem;
70                         if (li == null)
71                                 return false;
72                         return li.Text == Text && li.Value == Value;
73                 }
74         
75                 public override int GetHashCode ()
76                 {
77                         return Text.GetHashCode () ^ Value.GetHashCode ();
78                 
79                 }
80         
81                 string IAttributeAccessor.GetAttribute (string key)
82                 {
83                         if (attrs == null)
84                                 return null;
85
86                         return (string) Attributes [key];
87                 }
88         
89                 void IAttributeAccessor.SetAttribute (string key, string value)
90                 {
91                         Attributes [key] = value;
92                 }
93         
94                 void IParserAccessor.AddParsedSubObject (object obj)
95                 {
96                         LiteralControl lc = obj as LiteralControl;
97                         if (lc == null) {
98                                 // obj.GetType() will throw a NullRef if obj is null. That's fine according to the test.
99                                 throw new HttpException ("'ListItem' cannot have children of type " + obj.GetType ());
100                         }
101                         Text = lc.Text;
102                 }
103         
104                 void IStateManager.LoadViewState (object state)
105                 {
106                         LoadViewState (state);
107                 }
108                 
109                 internal void LoadViewState (object state)
110                 {
111                         if (state == null)
112                                 return;
113
114                         object [] states = (object []) state;
115
116                         if (states [0] != null) {
117                                 sb = new StateBag (true);
118                                 sb.LoadViewState (states[0]);
119                         }
120                         
121                         if (states [1] != null)
122                                 text = (string) states [1];
123                         if (states [2] != null)
124                                 value = (string) states [2];
125                         if (states [3] != null)
126                                 selected = (bool) states [3];
127 #if NET_2_0
128                         if (states [4] != null)
129                                 enabled = (bool) states [4];
130 #endif
131                 }
132
133                 object IStateManager.SaveViewState () 
134                 {
135                         return SaveViewState ();
136                 }
137
138                 internal object SaveViewState ()
139                 {
140                         if (!dirty)
141                                 return null;
142
143 #if NET_2_0
144                         object [] state = new object [5];
145 #else
146                         object [] state = new object [4];
147 #endif
148                         state [0] = sb != null ? sb.SaveViewState () : null;
149                         state [1] = (object) text;
150                         state [2] = (object) value;
151                         state [3] = (object) selected;
152 #if NET_2_0
153                         state [4] = (object) enabled;
154 #endif                  
155                         return state;
156                 }
157                 
158                 void IStateManager.TrackViewState ()
159                 {
160                         TrackViewState ();
161                 }
162                 
163                 internal void TrackViewState ()
164                 {
165                         tracking = true;
166                 }
167
168                 public override string ToString ()
169                 {
170                         return Text;
171                 }
172         
173                 [Browsable(false)]
174                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
175                 public AttributeCollection Attributes {
176                         get {
177                                 if (attrs != null)
178                                         return attrs;
179
180                                 if (sb == null) {       
181                                         sb = new StateBag (true);
182                                         if (tracking)
183                                                 sb.TrackViewState ();
184                                 }
185
186                                 return attrs = new AttributeCollection (sb);
187                         }
188                 }
189
190                 bool IStateManager.IsTrackingViewState {
191                         get { return tracking; }
192                 }
193
194                 [DefaultValue(false)]
195                 public bool Selected {
196                         get { return selected; }
197                         set { 
198                                 selected = value;
199                                 if (tracking)
200                                         SetDirty ();
201                         }
202                 }
203
204                 [DefaultValue("")]
205                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
206                 public string Text {
207                         get {
208                                 string r = text;
209                                 if (r == null)
210                                         r = value;
211                                 if (r == null)
212                                         r = "";
213                                 return r;
214                         }
215                 
216                         set {
217                                 text = value;
218                                 if (tracking)
219                                         SetDirty ();
220                         }
221                 }
222
223                 [DefaultValue("")]
224                 public string Value {
225                         get {
226                                 string r = value;
227                                 if (r == null)
228                                         r = text;
229                                 if (r == null)
230                                         r = "";
231                                 return r;
232                         }
233                 
234                         set {
235                                 this.value = value;
236                                 if (tracking)
237                                         SetDirty ();
238                         }
239                 }
240
241                 internal void SetDirty ()
242                 {
243                         dirty = true;
244                 }
245
246 #if NET_2_0
247                 public bool Enabled
248                 {
249                         get { return enabled; }
250                         set {
251                                 enabled = value;
252                                 if (tracking)
253                                         SetDirty ();
254                         }
255                 }
256 #endif
257
258                 internal bool HasAttributes {
259                         get { return attrs != null && attrs.Count > 0; }
260                 }
261
262                 string text;
263                 string value;
264                 bool selected;
265                 bool dirty;
266 #if NET_2_0
267                 bool enabled = true;
268 #endif
269                 bool tracking;
270                 StateBag sb;
271                 AttributeCollection attrs;
272         }
273 }