2010-02-18 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-2009 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                                 sb.SetDirty (true);
120                         }
121                         
122                         if (states [1] != null)
123                                 text = (string) states [1];
124                         if (states [2] != null)
125                                 value = (string) states [2];
126                         if (states [3] != null)
127                                 selected = (bool) states [3];
128 #if NET_2_0
129                         if (states [4] != null)
130                                 enabled = (bool) states [4];
131 #endif
132                 }
133
134                 object IStateManager.SaveViewState () 
135                 {
136                         return SaveViewState ();
137                 }
138
139                 internal object SaveViewState ()
140                 {
141                         if (!dirty)
142                                 return null;
143
144 #if NET_2_0
145                         object [] state = new object [5];
146 #else
147                         object [] state = new object [4];
148 #endif
149                         state [0] = sb != null ? sb.SaveViewState () : null;
150                         state [1] = (object) text;
151                         state [2] = (object) value;
152                         state [3] = (object) selected;
153 #if NET_2_0
154                         state [4] = (object) enabled;
155 #endif                  
156                         return state;
157                 }
158                 
159                 void IStateManager.TrackViewState ()
160                 {
161                         TrackViewState ();
162                 }
163                 
164                 internal void TrackViewState ()
165                 {
166                         tracking = true;
167                         if (sb != null) {
168                                 sb.TrackViewState ();
169                                 sb.SetDirty (true);
170                         }
171                 }
172
173                 public override string ToString ()
174                 {
175                         return Text;
176                 }
177         
178                 [Browsable(false)]
179                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
180                 public AttributeCollection Attributes {
181                         get {
182                                 if (attrs != null)
183                                         return attrs;
184
185                                 if (sb == null) {       
186                                         sb = new StateBag (true);
187                                         if (tracking)
188                                                 sb.TrackViewState ();
189                                 }
190
191                                 return attrs = new AttributeCollection (sb);
192                         }
193                 }
194
195                 bool IStateManager.IsTrackingViewState {
196                         get { return tracking; }
197                 }
198
199                 [DefaultValue(false)]
200                 public bool Selected {
201                         get { return selected; }
202                         set { 
203                                 selected = value;
204                                 if (tracking)
205                                         SetDirty ();
206                         }
207                 }
208
209                 [DefaultValue("")]
210                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
211                 public string Text {
212                         get {
213                                 string r = text;
214                                 if (r == null)
215                                         r = value;
216                                 if (r == null)
217                                         r = String.Empty;
218                                 return r;
219                         }
220                 
221                         set {
222                                 text = value;
223                                 if (tracking)
224                                         SetDirty ();
225                         }
226                 }
227
228                 [DefaultValue("")]
229                 public string Value {
230                         get {
231                                 string r = value;
232                                 if (r == null)
233                                         r = text;
234                                 if (r == null)
235                                         r = String.Empty;
236                                 return r;
237                         }
238                 
239                         set {
240                                 this.value = value;
241                                 if (tracking)
242                                         SetDirty ();
243                         }
244                 }
245
246                 internal void SetDirty ()
247                 {
248                         dirty = true;
249                 }
250
251 #if NET_2_0
252                 public bool Enabled
253                 {
254                         get { return enabled; }
255                         set {
256                                 enabled = value;
257                                 if (tracking)
258                                         SetDirty ();
259                         }
260                 }
261 #endif
262
263                 internal bool HasAttributes {
264                         get { return attrs != null && attrs.Count > 0; }
265                 }
266
267                 string text;
268                 string value;
269                 bool selected;
270                 bool dirty;
271 #if NET_2_0
272                 bool enabled = true;
273 #endif
274                 bool tracking;
275                 StateBag sb;
276                 AttributeCollection attrs;
277         }
278 }