DeviceSpecificChoice.cs 6.2 KB

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