Mark tests as not working under TARGET_JVM
[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                         Text = text;
51                         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                         object [] states = (object []) state;
112
113                         if (states [0] != null) sb = (StateBag) states[0];
114                         if (states [1] != null) text = (string) states [1];
115                         if (states [2] != null) value = (string) states [2];
116 #if NET_2_0
117                         if (states [3] != null) enabled = (bool) states [3];
118 #endif
119                 }
120
121                 object IStateManager.SaveViewState () 
122                 {
123                         return SaveViewState ();
124                 }
125
126                 internal object SaveViewState ()
127                 {
128 #if NET_2_0
129                         object [] state = new object [4];
130 #else
131                         object [] state = new object [3];
132 #endif
133                         state [0] = sb;
134                         state [1] = (object) text;
135                         state [2] = (object) value;
136 #if NET_2_0
137                         state [3] = (object) enabled;
138 #endif                  
139                         return state;
140                 }
141                 
142                 void IStateManager.TrackViewState ()
143                 {
144                         TrackViewState ();
145                 }
146                 
147                 internal void TrackViewState ()
148                 {
149                         tracking = true;
150                 }
151         
152
153                 public override string ToString ()
154                 {
155                         return Text;
156                 }
157         
158         
159                 [Browsable(false)]
160                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
161                 public AttributeCollection Attributes {
162                         get {
163                                 if (attrs != null)
164                                         return attrs;
165
166                                 if (sb == null) {       
167                                         sb = new StateBag (true);
168                                         if (tracking)
169                                                 sb.TrackViewState ();
170                                 }
171
172                                 return attrs = new AttributeCollection (sb);
173                         }
174                 }
175
176                 bool IStateManager.IsTrackingViewState {
177                         get {
178                                 return tracking;
179                         }
180                 
181                 }
182
183                 [DefaultValue(false)]
184                 public bool Selected {
185                         get {
186                                 return selected;
187                         }
188                 
189                         set {
190                                 selected = value;
191                         }
192                 }
193
194                 [DefaultValue("")]
195                 [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
196                 public string Text {
197                         get {
198                                 string r = text;
199                                 if (r == null)
200                                         r = value;
201                                 if (r == null)
202                                         r = "";
203                                 return r;
204                         }
205                 
206                         set {
207                                 text = value;
208                         }
209                 }
210
211                 [DefaultValue("")]
212                 public string Value {
213                         get {
214                                 string r = value;
215                                 if (r == null)
216                                         r = text;
217                                 if (r == null)
218                                         r = "";
219                                 return r;
220                         }
221                 
222                         set {
223                                 this.value = value;
224                         }
225                 }
226
227 #if NET_2_0
228                 public bool Enabled
229                 {
230                         get { return enabled; }
231                         set { enabled = value; }
232                 }
233
234                 internal bool HasAttributes {
235                         get { return attrs != null && attrs.Count > 0; }
236                 }
237 #endif
238
239                 string text;
240                 string value;
241                 bool selected;
242 #if NET_2_0
243                 bool enabled = true;
244 #endif
245                 bool tracking;
246                 StateBag sb;
247                 AttributeCollection attrs;
248         }
249 }