DeviceSpecificChoice.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * Project : Mono
  3. * Namespace : System.Web.UI.MobileControls
  4. * Class : DeviceSpecificChoice
  5. * Author : Gaurav Vaish
  6. *
  7. * Copyright : 2003 with Gaurav Vaish, and with
  8. * Ximian Inc
  9. */
  10. using System.ComponentModel;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.Reflection;
  14. using System.Web.UI;
  15. using System.Web.Mobile;
  16. namespace System.Web.UI.MobileControls
  17. {
  18. public class DeviceSpecificChoice : IParserAccessor,
  19. IAttributeAccessor
  20. {
  21. private string argument;
  22. private IDictionary contents;
  23. private string filter;
  24. private DeviceSpecific owner;
  25. private IDictionary templates;
  26. private string xmlns;
  27. private static IComparer caseInsensitiveComparer
  28. = new CaseInsensitiveComparer();
  29. public DeviceSpecificChoice()
  30. {
  31. }
  32. string IAttributeAccessor.GetAttribute(string key)
  33. {
  34. object val = Contents[key];
  35. if(val != null && val is string)
  36. return (string)val;
  37. //FIXME
  38. throw new ArgumentException("DeviceSpecificChoice" +
  39. "_PropetyNotAnAttribute");
  40. }
  41. void IAttributeAccessor.SetAttribute(string key, string value)
  42. {
  43. Contents[key] = value;
  44. }
  45. void IParserAccessor.AddParsedSubObject(object obj)
  46. {
  47. if(obj is DeviceSpecificChoiceTemplateContainer)
  48. {
  49. DeviceSpecificChoiceTemplateContainer ctr =
  50. (DeviceSpecificChoiceTemplateContainer)obj;
  51. Templates[ctr.Name] = ctr.Template;
  52. }
  53. }
  54. public string Argument
  55. {
  56. get
  57. {
  58. return this.argument;
  59. }
  60. set
  61. {
  62. this.argument = value;
  63. }
  64. }
  65. public IDictionary Contents
  66. {
  67. get
  68. {
  69. if(this.contents == null)
  70. {
  71. this.contents = new ListDictionary(caseInsensitiveComparer);
  72. }
  73. return this.contents;
  74. }
  75. }
  76. public string Filter
  77. {
  78. get
  79. {
  80. return this.filter;
  81. }
  82. set
  83. {
  84. this.filter = value;
  85. }
  86. }
  87. public DeviceSpecific Owner
  88. {
  89. get
  90. {
  91. return this.owner;
  92. }
  93. set
  94. {
  95. this.owner = value;
  96. }
  97. }
  98. public IDictionary Templates
  99. {
  100. get
  101. {
  102. if(this.templates == null)
  103. {
  104. this.templates = new ListDictionary(caseInsensitiveComparer);
  105. }
  106. return this.templates;
  107. }
  108. }
  109. internal void ApplyProperties()
  110. {
  111. IDictionaryEnumerator ide = Contents.GetEnumerator();
  112. while(ide.MoveNext())
  113. {
  114. object owner = Owner.Owner;
  115. string key = (string)ide.Key;
  116. string value = (string)ide.Value;
  117. if(key.ToLower() == "id")
  118. {
  119. //FIXME
  120. throw new ArgumentException("DeviceSpecificChoice" +
  121. "_InvalidPropertyOverride");
  122. }
  123. if(value != null)
  124. {
  125. int dash = 0;
  126. while((dash = key.IndexOf('-')) != -1)
  127. {
  128. string first = key.Substring(0, dash);
  129. PropertyDescriptor pd =
  130. TypeDescriptor.GetProperties(owner).Find(key, true);
  131. if(pd == null)
  132. {
  133. //FIXME
  134. throw new ArgumentException("DeviceSpecificChoice" +
  135. "_OverridingPropertyNotFound");
  136. }
  137. owner = pd.GetValue(owner);
  138. key = key.Substring(dash + 1);
  139. }
  140. if(!FindAndApplyProperty(owner, key, value) &&
  141. !FindAndApplyEvent(owner, key, value))
  142. {
  143. if(owner is IAttributeAccessor)
  144. {
  145. ((IAttributeAccessor)owner).SetAttribute(key, value);
  146. } else
  147. {
  148. //FIXME
  149. throw new ArgumentException("DeviceSpecificChoice" +
  150. "_OverridingPropertyNotFound");
  151. }
  152. }
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// Returns false if not found or not applied
  158. /// </summary>
  159. private bool FindAndApplyProperty(object parentObj, string key,
  160. string value)
  161. {
  162. bool retVal = false;
  163. PropertyDescriptor pd =
  164. TypeDescriptor.GetProperties(parentObj).Find(key, true);
  165. if(pd != null)
  166. {
  167. if(pd.Attributes.Contains(
  168. DesignerSerializationVisibilityAttribute.Hidden))
  169. {
  170. throw new ArgumentException("DeviceSpecificChoice" +
  171. "_OverridingPropertyNotDeclarable");
  172. }
  173. throw new NotImplementedException();
  174. }
  175. return retVal;
  176. }
  177. private bool FindAndApplyEvent(object parentObj, string key,
  178. string value)
  179. {
  180. bool retVal = false;
  181. if(key.Length > 0)
  182. {
  183. if(key.ToLower().StartsWith("on"))
  184. {
  185. string eventName = key.Substring(2);
  186. EventDescriptor ed =
  187. TypeDescriptor.GetEvents(parentObj).Find(key, true);
  188. if(ed != null)
  189. {
  190. ed.AddEventHandler(parentObj,
  191. Delegate.CreateDelegate(ed.EventType,
  192. Owner.MobilePage, eventName));
  193. }
  194. }
  195. }
  196. return retVal;
  197. }
  198. private bool CheckOnPageEvaluator(MobileCapabilities capabilities,
  199. out bool evaluatorResult)
  200. {
  201. bool retVal = false;
  202. evaluatorResult = false;
  203. TemplateControl tc = Owner.ClosestTemplateControl;
  204. // I have to get the method (MethodInfo?) and then invoke
  205. // the method and send back the results of the method!
  206. throw new NotImplementedException();
  207. }
  208. public bool HasTemplates
  209. {
  210. get
  211. {
  212. return (templates != null && templates.Count > 0);
  213. }
  214. }
  215. }
  216. }