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