[tests] Separate MONO_PATH directories by PLATFORM_PATH_SEPARATOR
[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         [ParseChildren (true, "Text")]
41         public sealed class ListItem : IAttributeAccessor, IParserAccessor, IStateManager
42         {
43                 public ListItem (string text, string value, bool enabled) : this (text, value)
44                 {
45                         this.enabled = enabled;
46                 }
47                 public ListItem (string text, string value)
48                 {
49                         this.text = text;
50                         this.value = value;
51                 }
52         
53                 public ListItem (string text) : this (text, null)
54                 {
55                 }
56         
57                 public ListItem () : this (null, null) 
58                 {
59                 }
60         
61                 public static ListItem FromString (string text)
62                 {
63                         return new ListItem (text);
64                 }
65         
66                 public override bool Equals (object o)
67                 {
68                         ListItem li = o as ListItem;
69                         if (li == null)
70                                 return false;
71                         return li.Text == Text && li.Value == Value;
72                 }
73         
74                 public override int GetHashCode ()
75                 {
76                         return Text.GetHashCode () ^ Value.GetHashCode ();
77                 
78                 }
79         
80                 string IAttributeAccessor.GetAttribute (string key)
81                 {
82                         if (attrs == null)
83                                 return null;
84
85                         return (string) Attributes [key];
86                 }
87         
88                 void IAttributeAccessor.SetAttribute (string key, string value)
89                 {
90                         Attributes [key] = value;
91                 }
92         
93                 void IParserAccessor.AddParsedSubObject (object obj)
94                 {
95                         LiteralControl lc = obj as LiteralControl;
96                         if (lc == null) {
97                                 // obj.GetType() will throw a NullRef if obj is null. That's fine according to the test.
98                                 throw new HttpException ("'ListItem' cannot have children of type " + obj.GetType ());
99                         }
100                         Text = lc.Text;
101                 }
102         
103                 void IStateManager.LoadViewState (object state)
104                 {
105                         LoadViewState (state);
106                 }
107                 
108                 internal void LoadViewState (object state)
109                 {
110                         if (state == null)
111                                 return;
112
113                         object [] states = (object []) state;
114
115                         if (states [0] != null) {
116                                 sb = new StateBag (true);
117                                 sb.LoadViewState (states[0]);
118                                 sb.SetDirty (true);
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 (states [4] != null)
128                                 enabled = (bool) states [4];
129                 }
130
131                 object IStateManager.SaveViewState () 
132                 {
133                         return SaveViewState ();
134                 }
135
136                 internal object SaveViewState ()
137                 {
138                         if (!dirty)
139                                 return null;
140
141                         object [] state = new object [5];
142                         state [0] = sb != null ? sb.SaveViewState () : null;
143                         state [1] = (object) text;
144                         state [2] = (object) value;
145                         state [3] = (object) selected;
146                         state [4] = (object) enabled;
147                         return state;
148                 }
149                 
150                 void IStateManager.TrackViewState ()
151                 {
152                         TrackViewState ();
153                 }
154                 
155                 internal void TrackViewState ()
156                 {
157                         tracking = true;
158                         if (sb != null) {
159                                 sb.TrackViewState ();
160                                 sb.SetDirty (true);
161                         }
162                 }
163
164                 public override string ToString ()
165                 {
166                         return Text;
167                 }
168         
169                 [Browsable(false)]
170                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
171                 public AttributeCollection Attributes {
172                         get {
173                                 if (attrs != null)
174                                         return attrs;
175
176                                 if (sb == null) {       
177                                         sb = new StateBag (true);
178                                         if (tracking)
179                                                 sb.TrackViewState ();
180                                 }
181
182                                 return attrs = new AttributeCollection (sb);
183                         }
184                 }
185
186                 bool IStateManager.IsTrackingViewState {
187                         get { return tracking; }
188                 }
189
190                 [TypeConverter ("System.Web.UI.MinimizableAttributeTypeConverter")]
191                 [DefaultValue(false)]
192                 public bool Selected {
193                         get { return selected; }
194                         set { 
195                                 selected = value;
196                                 if (tracking)
197                                         SetDirty ();
198                         }
199                 }
200
201                 [Localizable (true)]
202                 [DefaultValue("")]
203                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
204                 public string Text {
205                         get {
206                                 string r = text;
207                                 if (r == null)
208                                         r = value;
209                                 if (r == null)
210                                         r = String.Empty;
211                                 return r;
212                         }
213                 
214                         set {
215                                 text = value;
216                                 if (tracking)
217                                         SetDirty ();
218                         }
219                 }
220
221                 [Localizable (true)]
222                 [DefaultValue("")]
223                 public string Value {
224                         get {
225                                 string r = value;
226                                 if (r == null)
227                                         r = text;
228                                 if (r == null)
229                                         r = String.Empty;
230                                 return r;
231                         }
232                 
233                         set {
234                                 this.value = value;
235                                 if (tracking)
236                                         SetDirty ();
237                         }
238                 }
239
240                 internal void SetDirty ()
241                 {
242                         dirty = true;
243                 }
244
245                 [DefaultValue (true)]
246                 public bool Enabled
247                 {
248                         get { return enabled; }
249                         set {
250                                 enabled = value;
251                                 if (tracking)
252                                         SetDirty ();
253                         }
254                 }
255
256                 internal bool HasAttributes {
257                         get { return attrs != null && attrs.Count > 0; }
258                 }
259
260                 string text;
261                 string value;
262                 bool selected;
263                 bool dirty;
264                 bool enabled = true;
265                 bool tracking;
266                 StateBag sb;
267                 AttributeCollection attrs;
268         }
269 }