2003-09-04 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls / MobileListItem.cs
1 /**
2  * Project   : Mono
3  * Namespace : System.Web.UI.MobileControls
4  * Class     : MobileListItem
5  * Author    : Gaurav Vaish
6  *
7  * Copyright : 2003 with Gaurav Vaish, and with
8  *             Ximian Inc
9  */
10
11 using System;
12 using System.Collections;
13 using System.Web.UI;
14 using System.Web.UI.WebControls;
15
16 namespace System.Web.UI.MobileControls
17 {
18         public class MobileListItem : TemplateContainer, IStateManager
19         {
20                 private int    index;
21                 private string text;
22                 private string value;
23
24                 private object             dataItem;
25                 private MobileListItemType itemType;
26
27                 private const int SELECTED = 0x00;
28                 private const int MARKED   = 0x01; // Tracking?
29                 private const int SELECTD  = 0x02; // Selection dirty flag
30                 private const int TEXTD    = 0x03; // Text      dirty flag
31                 private const int VALUED   = 0x04; // Value     dirty flag
32
33                 private BitArray flags = new BitArray(5);
34
35                 public MobileListItem()
36                                      : this(null, null, null)
37                 {
38                 }
39
40                 public MobileListItem(MobileListItemType type)
41                                      : this(null, null, null)
42                 {
43                         this.itemType = type;
44                 }
45
46                 public MobileListItem(string text)
47                                      : this(null, text, null)
48                 {
49                 }
50
51                 public MobileListItem(string text, string value)
52                                      : this(null, text, value)
53                 {
54                 }
55
56                 public MobileListItem(object dataItem, string text, string value)
57                                      : base()
58                 {
59                         this.dataItem = dataItem;
60                         this.text     = text;
61                         this.value    = value;
62                         this.itemType = MobileListItemType.ListItem;
63                 }
64
65                 internal void SetIndex(int index)
66                 {
67                         this.index = index;
68                 }
69
70                 public object DataItem
71                 {
72                         get
73                         {
74                                 return this.dataItem;
75                         }
76                         set
77                         {
78                                 this.dataItem = value;
79                         }
80                 }
81
82                 public int Index
83                 {
84                         get
85                         {
86                                 return this.index;
87                         }
88                 }
89
90                 internal MobileListItemType ItemType
91                 {
92                         get
93                         {
94                                 return this.itemType;
95                         }
96                 }
97
98                 public bool Selected
99                 {
100                         get
101                         {
102                                 return flags[SELECTED];
103                         }
104                         set
105                         {
106                                 flags[SELECTED] = value;
107                                 if(IsTrackingViewState)
108                                 {
109                                         flags[SELECTD] = true;
110                                 }
111                         }
112                 }
113
114                 internal bool IsSelectionDirty
115                 {
116                         get
117                         {
118                                 return flags[SELECTD];
119                         }
120                         set
121                         {
122                                 flags[SELECTD] = value;
123                         }
124                 }
125
126                 internal bool IsDirty
127                 {
128                         get
129                         {
130                                 return (flags[TEXTD] || flags[VALUED]);
131                         }
132                         set
133                         {
134                                 flags[TEXTD] = value;
135                                 flags[VALUED] = value;
136                         }
137                 }
138
139                 public string Text
140                 {
141                         get
142                         {
143                                 if(this.text != null)
144                                         return this.text;
145                                 if(this.value != null)
146                                         return this.value;
147                                 return String.Empty;
148                         }
149                         set
150                         {
151                                 this.text = value;
152                                 if(IsTrackingViewState)
153                                 {
154                                         flags[TEXTD] = true;
155                                 }
156                         }
157                 }
158
159                 public string Value
160                 {
161                         get
162                         {
163                                 if(this.value != null)
164                                         return this.value;
165                                 if(this.text != null)
166                                         return this.text;
167                                 return String.Empty;
168                         }
169                         set
170                         {
171                                 this.value = value;
172                                 if(IsTrackingViewState)
173                                 {
174                                         flags[VALUED] = true;
175                                 }
176                         }
177                 }
178
179                 public static implicit operator MobileListItem(string text)
180                 {
181                         return new MobileListItem(text);
182                 }
183
184                 bool IStateManager.IsTrackingViewState
185                 {
186                         get
187                         {
188                                 return flags[MARKED];
189                         }
190                 }
191
192                 public override bool Equals(object obj)
193                 {
194                         if(obj is MobileListItem)
195                         {
196                                 MobileListItem other = (MobileListItem) obj;
197                                 return (this.Text == other.Text &&
198                                         this.Value == other.Value);
199                         }
200                         return false;
201                 }
202
203                 public override int GetHashCode()
204                 {
205                         return (Text.GetHashCode() + Value.GetHashCode());
206                 }
207
208                 public static MobileListItem FromString(string text)
209                 {
210                         return new MobileListItem(text);
211                 }
212
213                 public override string ToString()
214                 {
215                         return this.Text;
216                 }
217
218                 protected override bool OnBubbleEvent(object sender, EventArgs e)
219                 {
220                         if(e is CommandEventArgs)
221                         {
222                                 CommandEventArgs cmdArgs = (CommandEventArgs)e;
223                                 RaiseBubbleEvent(this,
224                                                  new ListCommandEventArgs(this, sender,
225                                                                           cmdArgs));
226                                 return true;
227                         }
228                         return false;
229                 }
230
231                 void IStateManager.TrackViewState()
232                 {
233                         flags[MARKED] = true;
234                 }
235
236                 object IStateManager.SaveViewState()
237                 {
238                         object retVal = null;
239                         string text = (flags[TEXTD] ? this.text : null);
240                         string value = (flags[VALUED] ? this.value : null);
241                         if(text != null || value != null)
242                         {
243                                 retVal = new string[] { text, value };
244                         }
245                         return retVal;
246                 }
247
248                 void IStateManager.LoadViewState(object state)
249                 {
250                         if(state != null)
251                         {
252                                 string[] data = (string[]) state;
253                                 this.text = data[0];
254                                 this.value = data[1];
255                         }
256                 }
257         }
258 }