Merge pull request #4540 from kumpera/android-changes-part1
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / EventBindingService.cs
1 //
2 // System.ComponentModel.Design.EventBindingService
3 // 
4 // Authors:      
5 //        Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2007 Ivan N. Zlatev
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using System;
32 using System.Collections.Generic;
33 using System.Collections;
34 using System.ComponentModel;
35
36 namespace System.ComponentModel.Design
37 {
38         public abstract class EventBindingService : IEventBindingService
39         {
40
41                 private IServiceProvider _provider;
42
43                 protected EventBindingService (IServiceProvider provider)
44                 {
45                         if (provider == null)
46                                 throw new ArgumentNullException ("provider");
47                         _provider = provider;
48                 }
49                                         
50                 protected abstract bool ShowCode (IComponent component, EventDescriptor e, string methodName);
51                 protected abstract bool ShowCode (int lineNumber);
52                 protected abstract bool ShowCode ();
53                 protected abstract string CreateUniqueMethodName (IComponent component, EventDescriptor e);
54                 protected abstract ICollection GetCompatibleMethods (EventDescriptor e);
55
56                 protected virtual void FreeMethod (IComponent component, EventDescriptor e, string methodName)
57                 {
58                 }
59
60                 protected virtual void UseMethod (IComponent component, EventDescriptor e, string methodName)
61                 {
62                 }
63
64
65                 protected virtual void ValidateMethodName (string methodName)
66                 {
67                 }
68  
69
70                 protected object GetService (Type serviceType)
71                 {
72                         if (_provider != null)
73                                 return _provider.GetService (serviceType);
74                         return null;
75                 }
76
77 #region IEventBindingService implementation
78
79                 string IEventBindingService.CreateUniqueMethodName (IComponent component, EventDescriptor eventDescriptor)
80                 {
81                         if (eventDescriptor == null)
82                                 throw new ArgumentNullException ("eventDescriptor");
83                         if (component == null)
84                                 throw new ArgumentNullException ("component");
85
86                         return this.CreateUniqueMethodName (component, eventDescriptor);
87                 }
88
89                 ICollection IEventBindingService.GetCompatibleMethods (EventDescriptor eventDescriptor)
90                 {
91                         if (eventDescriptor == null)
92                                 throw new ArgumentNullException ("eventDescriptor");
93
94                         return this.GetCompatibleMethods (eventDescriptor);
95                 }
96
97                 EventDescriptor IEventBindingService.GetEvent (PropertyDescriptor property)
98                 {
99                         if (property == null)
100                                 throw new ArgumentNullException ("property");
101
102                         EventPropertyDescriptor eventPropDescriptor = property as EventPropertyDescriptor;
103                         if (eventPropDescriptor == null)
104                                 return null;
105                         
106                         return eventPropDescriptor.InternalEventDescriptor;
107                 }
108
109                 PropertyDescriptorCollection IEventBindingService.GetEventProperties (EventDescriptorCollection events)
110                 {
111                         if (events == null)
112                                 throw new ArgumentNullException ("events");
113
114                         List<PropertyDescriptor> properties = new List <PropertyDescriptor>();
115                         foreach (EventDescriptor eventDescriptor in events)
116                                 properties.Add (((IEventBindingService)this).GetEventProperty (eventDescriptor));
117                                 
118                         return new PropertyDescriptorCollection (properties.ToArray ());
119                 }
120
121                 PropertyDescriptor IEventBindingService.GetEventProperty (EventDescriptor eventDescriptor)
122                 {
123                         if (eventDescriptor == null) 
124                                 throw new ArgumentNullException ("eventDescriptor");
125
126                         return new EventPropertyDescriptor (eventDescriptor);
127                 }
128
129                 bool IEventBindingService.ShowCode (IComponent component, EventDescriptor eventDescriptor)
130                 {
131                         if (component == null)
132                                 throw new ArgumentNullException ("component");
133                         if (eventDescriptor == null)
134                                 throw new ArgumentNullException ("eventDescriptor");
135
136                         return this.ShowCode (component, eventDescriptor, (string) ((IEventBindingService)this).GetEventProperty (eventDescriptor).GetValue (component));
137                 }
138
139                 bool IEventBindingService.ShowCode (int lineNumber)
140                 {
141                         return this.ShowCode (lineNumber);
142                 }
143
144                 bool IEventBindingService.ShowCode ()
145                 {
146                         return this.ShowCode ();
147                 }
148 #endregion
149
150         }
151
152         internal class EventPropertyDescriptor : PropertyDescriptor
153         {
154                 private EventDescriptor _eventDescriptor;
155         
156                 public EventPropertyDescriptor (EventDescriptor eventDescriptor)
157                         : base (eventDescriptor)
158                 {
159                         if (eventDescriptor == null)
160                                 throw new ArgumentNullException ("eventDescriptor");
161                         _eventDescriptor = eventDescriptor;
162                 }
163                 
164                 public override bool CanResetValue (object component)
165                 {
166                         return true;
167                 }
168
169                 public override Type ComponentType {
170                         get { return _eventDescriptor.ComponentType; }
171                 }
172
173                 public override bool IsReadOnly {
174                         get { return false; }
175                 }
176
177                 public override Type PropertyType {
178                         get { return _eventDescriptor.EventType; }
179                 }
180
181                 public override void ResetValue (object component)
182                 {
183                         this.SetValue (component, null);
184                 }
185
186                 public override object GetValue (object component)
187                 {
188                         if (component is IComponent && ((IComponent)component).Site != null) {
189                                 IDictionaryService dictionary = ((IComponent)component).Site.GetService (typeof (IDictionaryService)) as IDictionaryService;
190                                 if (dictionary != null)
191                                         return dictionary.GetValue (base.Name);
192                         }
193                         return null;
194                 }
195
196                 public override void SetValue (object component, object value)
197                 {
198                         if (component is IComponent && ((IComponent)component).Site != null) {
199                                 IDictionaryService dictionary = ((IComponent)component).Site.GetService (typeof (IDictionaryService)) as IDictionaryService;
200                                 if (dictionary != null)
201                                         dictionary.SetValue (base.Name, value);
202                         }
203                 }
204
205                 public override bool ShouldSerializeValue (object component)
206                 {
207                         if (this.GetValue (component) != null)
208                                 return true;
209                         return false;
210                 }
211                 
212                 public override TypeConverter Converter {
213                         get { return TypeDescriptor.GetConverter (String.Empty); }
214                 }
215                 
216                 internal EventDescriptor InternalEventDescriptor {
217                         get { return _eventDescriptor; }
218                 }
219         }
220 }