2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web.Mobile / System.Web.UI.MobileControls / DeviceSpecificChoice.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /**
23  * Project   : Mono
24  * Namespace : System.Web.UI.MobileControls
25  * Class     : DeviceSpecificChoice
26  * Author    : Gaurav Vaish
27  *
28  * Copyright : 2003 with Gaurav Vaish, and with
29  *             Ximian Inc
30  */
31
32 using System.ComponentModel;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Reflection;
36 using System.Web.UI;
37 using System.Web.Mobile;
38
39 namespace System.Web.UI.MobileControls
40 {
41         public class DeviceSpecificChoice : IParserAccessor,
42                                             IAttributeAccessor
43         {
44                 private string argument;
45                 private IDictionary contents;
46                 private string filter;
47                 private DeviceSpecific owner;
48                 private IDictionary templates;
49                 private string xmlns;
50
51                 private static IComparer caseInsensitiveComparer
52                                       = new CaseInsensitiveComparer();
53
54                 public DeviceSpecificChoice()
55                 {
56                 }
57
58                 string IAttributeAccessor.GetAttribute(string key)
59                 {
60                         object val = Contents[key];
61                         if(val != null && val is string)
62                                 return (string)val;
63                         //FIXME
64                         throw new ArgumentException("DeviceSpecificChoice" +
65                                                     "_PropetyNotAnAttribute");
66                 }
67
68                 void IAttributeAccessor.SetAttribute(string key, string value)
69                 {
70                         Contents[key] = value;
71                 }
72
73                 void IParserAccessor.AddParsedSubObject(object obj)
74                 {
75                         if(obj is DeviceSpecificChoiceTemplateContainer)
76                         {
77                                 DeviceSpecificChoiceTemplateContainer ctr =
78                                     (DeviceSpecificChoiceTemplateContainer)obj;
79                                 Templates[ctr.Name] = ctr.Template;
80                         }
81                 }
82
83                 public string Argument
84                 {
85                         get
86                         {
87                                 return this.argument;
88                         }
89                         set
90                         {
91                                 this.argument = value;
92                         }
93                 }
94
95                 public IDictionary Contents
96                 {
97                         get
98                         {
99                                 if(this.contents == null)
100                                 {
101                                         this.contents = new ListDictionary(caseInsensitiveComparer);
102                                 }
103                                 return this.contents;
104                         }
105                 }
106
107                 public string Filter
108                 {
109                         get
110                         {
111                                 return this.filter;
112                         }
113                         set
114                         {
115                                 this.filter = value;
116                         }
117                 }
118
119                 public DeviceSpecific Owner
120                 {
121                         get
122                         {
123                                 return this.owner;
124                         }
125                         set
126                         {
127                                 this.owner = value;
128                         }
129                 }
130
131                 public IDictionary Templates
132                 {
133                         get
134                         {
135                                 if(this.templates == null)
136                                 {
137                                         this.templates = new ListDictionary(caseInsensitiveComparer);
138                                 }
139                                 return this.templates;
140                         }
141                 }
142
143                 internal void ApplyProperties()
144                 {
145                         IDictionaryEnumerator ide = Contents.GetEnumerator();
146                         while(ide.MoveNext())
147                         {
148                                 object owner = Owner.Owner;
149                                 string key = (string)ide.Key;
150                                 string value = (string)ide.Value;
151                                 if(key.ToLower() == "id")
152                                 {
153                                         //FIXME
154                                         throw new ArgumentException("DeviceSpecificChoice" +
155                                                                     "_InvalidPropertyOverride");
156                                 }
157                                 if(value != null)
158                                 {
159                                         int dash = 0;
160                                         while((dash = key.IndexOf('-')) != -1)
161                                         {
162                                                 string first = key.Substring(0, dash);
163                                                 PropertyDescriptor pd =
164                                                              TypeDescriptor.GetProperties(owner).Find(key, true);
165                                                 if(pd == null)
166                                                 {
167                                                         //FIXME
168                                                         throw new ArgumentException("DeviceSpecificChoice" +
169                                                                                     "_OverridingPropertyNotFound");
170                                                 }
171                                                 owner = pd.GetValue(owner);
172                                                 key = key.Substring(dash + 1);
173                                         }
174                                         if(!FindAndApplyProperty(owner, key, value) &&
175                                            !FindAndApplyEvent(owner, key, value))
176                                         {
177                                                 if(owner is IAttributeAccessor)
178                                                 {
179                                                         ((IAttributeAccessor)owner).SetAttribute(key, value);
180                                                 } else
181                                                 {
182                                                         //FIXME
183                                                         throw new ArgumentException("DeviceSpecificChoice" +
184                                                                                     "_OverridingPropertyNotFound");
185                                                 }
186                                         }
187                                 }
188                         }
189                 }
190
191                 /// <summary>
192                 /// Returns false if not found or not applied
193                 /// </summary>
194                 private bool FindAndApplyProperty(object parentObj, string key,
195                                                   string value)
196                 {
197                         bool retVal = false;
198                         PropertyDescriptor pd =
199                             TypeDescriptor.GetProperties(parentObj).Find(key, true);
200                         if(pd != null)
201                         {
202                                 if(pd.Attributes.Contains(
203                                    DesignerSerializationVisibilityAttribute.Hidden))
204                                 {
205                                         throw new ArgumentException("DeviceSpecificChoice" +
206                                                          "_OverridingPropertyNotDeclarable");
207                                 }
208                                 throw new NotImplementedException();
209                         }
210                         return retVal;
211                 }
212
213                 private bool FindAndApplyEvent(object parentObj, string key,
214                                                string value)
215                 {
216                         bool retVal = false;
217                         if(key.Length > 0)
218                         {
219                                 if(key.ToLower().StartsWith("on"))
220                                 {
221                                         string eventName = key.Substring(2);
222                                         EventDescriptor ed =
223                                               TypeDescriptor.GetEvents(parentObj).Find(key, true);
224                                         if(ed != null)
225                                         {
226                                                 ed.AddEventHandler(parentObj,
227                                                   Delegate.CreateDelegate(ed.EventType,
228                                                           Owner.MobilePage, eventName));
229                                         }
230                                 }
231                         }
232                         return retVal;
233                 }
234
235                 private bool CheckOnPageEvaluator(MobileCapabilities capabilities,
236                                                   out bool evaluatorResult)
237                 {
238                         bool retVal = false;
239                         evaluatorResult = false;
240                         TemplateControl tc = Owner.ClosestTemplateControl;
241                         // I have to get the method (MethodInfo?) and then invoke
242                         // the method and send back the results of the method!
243                         throw new NotImplementedException();
244                 }
245
246                 public bool HasTemplates
247                 {
248                         get
249                         {
250                                 return (templates != null && templates.Count > 0);
251                         }
252                 }
253         }
254 }