Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / WindowsBase / System.Windows.Markup / ValueSerializer.cs
1 #if !NET_4_0
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 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
22 //
23 // Authors:
24 //      Chris Toshok (toshok@ximian.com)
25 //
26
27 using System;
28 using System.Collections.Generic;
29 using System.ComponentModel;
30 using System.Windows.Converters;
31
32 namespace System.Windows.Markup {
33
34         // I don't like the idea of hardcoding the types in here, but
35         // I'm not sure what else to do about it..
36         class DefaultValueSerializerContext : IValueSerializerContext
37         {
38                 public ValueSerializer GetValueSerializerFor (PropertyDescriptor descriptor)
39                 {
40                         Attribute attribute = (Attribute)descriptor.Attributes[typeof (ValueSerializerAttribute)];
41                         if (attribute != null)
42                                 return (ValueSerializer)Activator.CreateInstance (((ValueSerializerAttribute)attribute).ValueSerializerType);
43
44                         return GetValueSerializerFor (descriptor.PropertyType);
45                 }
46
47                 public ValueSerializer GetValueSerializerFor (Type type)
48                 {
49                         Attribute attribute = (Attribute)TypeDescriptor.GetAttributes (type)[typeof (ValueSerializerAttribute)];
50                         if (attribute != null)
51                                 return (ValueSerializer)Activator.CreateInstance (((ValueSerializerAttribute)attribute).ValueSerializerType);
52
53                         if (typeof (DateTime).IsAssignableFrom (type))
54                                 return new DateTimeValueSerializer();
55                         else if (typeof (Int32Rect).IsAssignableFrom (type))
56                                 return new Int32RectValueSerializer ();
57                         else if (typeof (Point).IsAssignableFrom (type))
58                                 return new PointValueSerializer ();
59                         else if (typeof (Rect).IsAssignableFrom (type))
60                                 return new RectValueSerializer ();
61                         else if (typeof (Size).IsAssignableFrom (type))
62                                 return new SizeValueSerializer ();
63                         else if (typeof (Vector).IsAssignableFrom (type))
64                                 return new VectorValueSerializer ();
65                         else
66                                 return null;
67                 }
68
69                 public void OnComponentChanged ()
70                 {
71                 }
72
73                 public bool OnComponentChanging ()
74                 {
75                         return false;
76                 }
77
78                 public IContainer Container {
79                         get { return null; }
80                 }
81
82                 public object Instance {
83                         get { return null; }
84                 }
85
86                 public PropertyDescriptor PropertyDescriptor {
87                         get { return null; }
88                 }
89
90                 public object GetService (Type serviceType)
91                 {
92                         return null;
93                 }
94         }
95
96         public abstract class ValueSerializer
97         {
98                 protected ValueSerializer ()
99                 {
100                 }
101
102                 public virtual bool CanConvertFromString (string value, IValueSerializerContext context)
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107                 public virtual bool CanConvertToString (object value, IValueSerializerContext context)
108                 {
109                         throw new NotImplementedException ();
110                 }
111
112                 public virtual object ConvertFromString (string value, IValueSerializerContext context)
113                 {
114                         throw new NotImplementedException ();
115                 }
116
117                 public virtual string ConvertToString (object value, IValueSerializerContext context)
118                 {
119                         throw new NotImplementedException ();
120                 }
121
122                 protected Exception GetConvertFromException (object value)
123                 {
124                         throw new NotImplementedException ();
125                 }
126
127                 protected Exception GetConvertToException (object value, Type destinationType)
128                 {
129                         throw new NotImplementedException ();
130                 }
131
132                 public virtual IEnumerable<Type> TypeReferences (object value, IValueSerializerContext context)
133                 {
134                         throw new NotImplementedException ();
135                 }
136
137                 public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor)
138                 {
139                         DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
140                         return defaultContext.GetValueSerializerFor (descriptor);
141                 }
142
143                 public static ValueSerializer GetSerializerFor (Type type)
144                 {
145                         DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
146                         return defaultContext.GetValueSerializerFor (type);
147                 }
148
149                 public static ValueSerializer GetSerializerFor (PropertyDescriptor descriptor, IValueSerializerContext context)
150                 {
151                         ValueSerializer s = context.GetValueSerializerFor (descriptor);
152                         if (s == null) {
153                                 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
154                                 s = defaultContext.GetValueSerializerFor (descriptor);
155                         }
156                         return s;
157                 }
158
159                 public static ValueSerializer GetSerializerFor (Type type, IValueSerializerContext context)
160                 {
161                         ValueSerializer s = context.GetValueSerializerFor (type);
162                         if (s == null) {
163                                 DefaultValueSerializerContext defaultContext = new DefaultValueSerializerContext();
164                                 s = defaultContext.GetValueSerializerFor (type);
165                         }
166                         return s;
167                 }
168         }
169
170 }
171 #endif