2 // System.ComponentModel.Design.EventBindingService
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2007 Ivan N. Zlatev
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using System.Collections;
34 using System.ComponentModel;
36 namespace System.ComponentModel.Design
38 public abstract class EventBindingService : IEventBindingService
41 private IServiceProvider _provider;
43 protected EventBindingService (IServiceProvider provider)
46 throw new ArgumentNullException ("provider");
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);
56 protected virtual void FreeMethod (IComponent component, EventDescriptor e, string methodName)
60 protected virtual void UseMethod (IComponent component, EventDescriptor e, string methodName)
65 protected virtual void ValidateMethodName (string methodName)
70 protected object GetService (Type serviceType)
72 if (_provider != null)
73 return _provider.GetService (serviceType);
77 #region IEventBindingService implementation
79 string IEventBindingService.CreateUniqueMethodName (IComponent component, EventDescriptor eventDescriptor)
81 if (eventDescriptor == null)
82 throw new ArgumentNullException ("eventDescriptor");
83 if (component == null)
84 throw new ArgumentNullException ("component");
86 return this.CreateUniqueMethodName (component, eventDescriptor);
89 ICollection IEventBindingService.GetCompatibleMethods (EventDescriptor eventDescriptor)
91 if (eventDescriptor == null)
92 throw new ArgumentNullException ("eventDescriptor");
94 return this.GetCompatibleMethods (eventDescriptor);
97 EventDescriptor IEventBindingService.GetEvent (PropertyDescriptor property)
100 throw new ArgumentNullException ("property");
102 EventPropertyDescriptor eventPropDescriptor = property as EventPropertyDescriptor;
103 if (eventPropDescriptor == null)
106 return eventPropDescriptor.InternalEventDescriptor;
109 PropertyDescriptorCollection IEventBindingService.GetEventProperties (EventDescriptorCollection events)
112 throw new ArgumentNullException ("events");
114 List<PropertyDescriptor> properties = new List <PropertyDescriptor>();
115 foreach (EventDescriptor eventDescriptor in events)
116 properties.Add (((IEventBindingService)this).GetEventProperty (eventDescriptor));
118 return new PropertyDescriptorCollection (properties.ToArray ());
121 PropertyDescriptor IEventBindingService.GetEventProperty (EventDescriptor eventDescriptor)
123 if (eventDescriptor == null)
124 throw new ArgumentNullException ("eventDescriptor");
126 return new EventPropertyDescriptor (eventDescriptor);
129 bool IEventBindingService.ShowCode (IComponent component, EventDescriptor eventDescriptor)
131 if (component == null)
132 throw new ArgumentNullException ("component");
133 if (eventDescriptor == null)
134 throw new ArgumentNullException ("eventDescriptor");
136 return this.ShowCode (component, eventDescriptor, (string) ((IEventBindingService)this).GetEventProperty (eventDescriptor).GetValue (component));
139 bool IEventBindingService.ShowCode (int lineNumber)
141 return this.ShowCode (lineNumber);
144 bool IEventBindingService.ShowCode ()
146 return this.ShowCode ();
152 internal class EventPropertyDescriptor : PropertyDescriptor
154 private EventDescriptor _eventDescriptor;
156 public EventPropertyDescriptor (EventDescriptor eventDescriptor)
157 : base (eventDescriptor)
159 if (eventDescriptor == null)
160 throw new ArgumentNullException ("eventDescriptor");
161 _eventDescriptor = eventDescriptor;
164 public override bool CanResetValue (object component)
169 public override Type ComponentType {
170 get { return _eventDescriptor.ComponentType; }
173 public override bool IsReadOnly {
174 get { return false; }
177 public override Type PropertyType {
178 get { return _eventDescriptor.EventType; }
181 public override void ResetValue (object component)
183 this.SetValue (component, null);
186 public override object GetValue (object component)
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);
196 public override void SetValue (object component, object value)
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);
205 public override bool ShouldSerializeValue (object component)
207 if (this.GetValue (component) != null)
212 public override TypeConverter Converter {
213 get { return TypeDescriptor.GetConverter (String.Empty); }
216 internal EventDescriptor InternalEventDescriptor {
217 get { return _eventDescriptor; }