MobileListItem.cs 4.4 KB

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