MobileListItem.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Project : Mono
  23. * Namespace : System.Web.UI.MobileControls
  24. * Class : MobileListItem
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System;
  31. using System.Collections;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. namespace System.Web.UI.MobileControls
  35. {
  36. public class MobileListItem : TemplateContainer, IStateManager
  37. {
  38. private int index;
  39. private string text;
  40. private string value;
  41. private object dataItem;
  42. private MobileListItemType itemType;
  43. private const int SELECTED = 0x00;
  44. private const int MARKED = 0x01; // Tracking?
  45. private const int SELECTD = 0x02; // Selection dirty flag
  46. private const int TEXTD = 0x03; // Text dirty flag
  47. private const int VALUED = 0x04; // Value dirty flag
  48. private BitArray flags = new BitArray(5);
  49. public MobileListItem()
  50. : this(null, null, null)
  51. {
  52. }
  53. public MobileListItem(MobileListItemType type)
  54. : this(null, null, null)
  55. {
  56. this.itemType = type;
  57. }
  58. public MobileListItem(string text)
  59. : this(null, text, null)
  60. {
  61. }
  62. public MobileListItem(string text, string value)
  63. : this(null, text, value)
  64. {
  65. }
  66. public MobileListItem(object dataItem, string text, string value)
  67. : base()
  68. {
  69. this.dataItem = dataItem;
  70. this.text = text;
  71. this.value = value;
  72. this.itemType = MobileListItemType.ListItem;
  73. }
  74. internal void SetIndex(int index)
  75. {
  76. this.index = index;
  77. }
  78. public object DataItem
  79. {
  80. get
  81. {
  82. return this.dataItem;
  83. }
  84. set
  85. {
  86. this.dataItem = value;
  87. }
  88. }
  89. public int Index
  90. {
  91. get
  92. {
  93. return this.index;
  94. }
  95. }
  96. internal MobileListItemType ItemType
  97. {
  98. get
  99. {
  100. return this.itemType;
  101. }
  102. }
  103. public bool Selected
  104. {
  105. get
  106. {
  107. return flags[SELECTED];
  108. }
  109. set
  110. {
  111. flags[SELECTED] = value;
  112. if(IsTrackingViewState)
  113. {
  114. flags[SELECTD] = true;
  115. }
  116. }
  117. }
  118. internal bool IsSelectionDirty
  119. {
  120. get
  121. {
  122. return flags[SELECTD];
  123. }
  124. set
  125. {
  126. flags[SELECTD] = value;
  127. }
  128. }
  129. internal bool IsDirty
  130. {
  131. get
  132. {
  133. return (flags[TEXTD] || flags[VALUED]);
  134. }
  135. set
  136. {
  137. flags[TEXTD] = value;
  138. flags[VALUED] = value;
  139. }
  140. }
  141. public string Text
  142. {
  143. get
  144. {
  145. if(this.text != null)
  146. return this.text;
  147. if(this.value != null)
  148. return this.value;
  149. return String.Empty;
  150. }
  151. set
  152. {
  153. this.text = value;
  154. if(IsTrackingViewState)
  155. {
  156. flags[TEXTD] = true;
  157. }
  158. }
  159. }
  160. public string Value
  161. {
  162. get
  163. {
  164. if(this.value != null)
  165. return this.value;
  166. if(this.text != null)
  167. return this.text;
  168. return String.Empty;
  169. }
  170. set
  171. {
  172. this.value = value;
  173. if(IsTrackingViewState)
  174. {
  175. flags[VALUED] = true;
  176. }
  177. }
  178. }
  179. public static implicit operator MobileListItem(string text)
  180. {
  181. return new MobileListItem(text);
  182. }
  183. bool IStateManager.IsTrackingViewState
  184. {
  185. get
  186. {
  187. return flags[MARKED];
  188. }
  189. }
  190. public override bool Equals(object obj)
  191. {
  192. if(obj is MobileListItem)
  193. {
  194. MobileListItem other = (MobileListItem) obj;
  195. return (this.Text == other.Text &&
  196. this.Value == other.Value);
  197. }
  198. return false;
  199. }
  200. public override int GetHashCode()
  201. {
  202. return (Text.GetHashCode() + Value.GetHashCode());
  203. }
  204. public static MobileListItem FromString(string text)
  205. {
  206. return new MobileListItem(text);
  207. }
  208. public override string ToString()
  209. {
  210. return this.Text;
  211. }
  212. protected override bool OnBubbleEvent(object sender, EventArgs e)
  213. {
  214. if(e is CommandEventArgs)
  215. {
  216. CommandEventArgs cmdArgs = (CommandEventArgs)e;
  217. RaiseBubbleEvent(this,
  218. new ListCommandEventArgs(this, sender,
  219. cmdArgs));
  220. return true;
  221. }
  222. return false;
  223. }
  224. void IStateManager.TrackViewState()
  225. {
  226. flags[MARKED] = true;
  227. }
  228. object IStateManager.SaveViewState()
  229. {
  230. object retVal = null;
  231. string text = (flags[TEXTD] ? this.text : null);
  232. string value = (flags[VALUED] ? this.value : null);
  233. if(text != null || value != null)
  234. {
  235. retVal = new string[] { text, value };
  236. }
  237. return retVal;
  238. }
  239. void IStateManager.LoadViewState(object state)
  240. {
  241. if(state != null)
  242. {
  243. string[] data = (string[]) state;
  244. this.text = data[0];
  245. this.value = data[1];
  246. }
  247. }
  248. }
  249. }