2003-09-04 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls / ObjectListItem.cs
1 /**
2  * Project   : Mono
3  * Namespace : System.Web.UI.MobileControls
4  * Class     : ObjectListCommandEventHandler
5  * Author    : Gaurav Vaish
6  *
7  * Copyright : 2003 with Gaurav Vaish, and with
8  *             Ximian Inc
9  */
10
11 using System;
12 using System.Web.Mobile;
13
14 namespace System.Web.UI.MobileControls
15 {
16         public class ObjectListItem : MobileListItem
17         {
18                 private ObjectList owner;
19                 private string[]   fields;
20                 private bool       dirty = false;
21
22                 internal ObjectListItem(ObjectList owner, object dataItem)
23                                       : base(dataItem, null, null)
24                 {
25                         this.owner = owner;
26                         this.fields = new string[owner.AllFields.Count];
27                 }
28
29                 internal ObjectListItem(ObjectList owner)
30                                     : this(owner, null)
31                 {
32                 }
33
34                 public string this[int key]
35                 {
36                         get
37                         {
38                                 if(fields != null && fields.Length >= key - 1
39                                    && fields[key] != null)
40                                         return fields[key];
41                                 return String.Empty;
42                         }
43                         set
44                         {
45                                 if(fields != null && fields.Length >= key - 1)
46                                         fields[key] = value;
47                                 if(IsTrackingViewState)
48                                         dirty = true;
49                         }
50                 }
51
52                 internal bool Dirty
53                 {
54                         get
55                         {
56                                 return dirty;
57                         }
58                         set
59                         {
60                                 dirty = value;
61                         }
62                 }
63
64                 public string this[string fieldName]
65                 {
66                         get
67                         {
68                                 return this[IndexOf(fieldName)];
69                         }
70                         set
71                         {
72                                 this[IndexOf(fieldName)] = value;
73                         }
74                 }
75
76                 [MonoTODO("Exception_Details_Not_Exact")]
77                 private int IndexOf(string fieldName)
78                 {
79                         int index = owner.AllFields.IndexOf(fieldName);
80                         if(index < 0)
81                         {
82                                 throw new ArgumentException("ObjectList_FieldNotFound");
83                         }
84                         return index;
85                 }
86
87                 public override bool Equals(object obj)
88                 {
89                         bool retVal = false;
90                         if(obj is ObjectListItem)
91                         {
92                                 ObjectListItem oli = (ObjectListItem) obj;
93                                 if(oli.fields != null && this.fields != null)
94                                 {
95                                         if(this.fields.Length == oli.fields.Length)
96                                         {
97                                                 int i;
98                                                 for(i = 0; i < fields.Length; i++)
99                                                 {
100                                                         if(fields[i] != oli.fields[i])
101                                                                 break;
102                                                 }
103                                                 if(i == fields.Length)
104                                                         retVal = true;
105                                         }
106                                 }
107                                 retVal &= (Value == oli.Value);
108                                 retVal &= (Text == oli.Text);
109                         }
110                         return retVal;
111                 }
112
113                 public override int GetHashCode()
114                 {
115                         return (fields == null ? Value.GetHashCode() :
116                                                      fields[0].GetHashCode());
117                 }
118         }
119 }