2 // System.ComponentModel.Design.DesignModeSite
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2006-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.
33 using System.Collections;
34 using System.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
37 namespace System.ComponentModel.Design
40 internal class DesignModeSite : ISite, IDictionaryService, IServiceProvider, IServiceContainer
43 // The DesignModeSite:
44 // * offers the IDictionaryService and INestedContaineroffers site-specific services
45 // * implements the IServiceContainer interface, but according to the tests it:
46 // - does *NOT* offer IServiceContainer as a site-specific service
47 // - offers the added site-specific services via IServiceProvider
49 private IServiceProvider _serviceProvider;
50 private IComponent _component;
51 private IContainer _container;
52 private string _componentName;
53 private NestedContainer _nestedContainer;
56 public DesignModeSite (IComponent component, string name, IContainer container, IServiceProvider serviceProvider)
58 _component = component;
59 _container = container;
60 _componentName = name;
61 _serviceProvider = serviceProvider;
64 public IComponent Component {
65 get { return _component; }
68 public IContainer Container {
69 get { return _container; }
72 // Yay yay yay, guess who's in design mode ???
74 public bool DesignMode {
78 // The place where renaming of a component takes place.
79 // We should validate the new name here, if INameCreation service is present
83 return _componentName;
86 if (value != _componentName && value != null && value.Trim().Length > 0) {
87 INameCreationService nameService = this.GetService (typeof (INameCreationService)) as INameCreationService;
88 IComponent component = _container.Components[value]; // get the component with that name
90 if (component == null &&
91 (nameService == null || (nameService != null && nameService.IsValidName (value)))) {
92 string oldName = _componentName;
93 _componentName = value;
94 ((DesignerHost)this.GetService (typeof (IDesignerHost))).OnComponentRename (_component, oldName, _componentName);
100 #region IServiceContainer
102 private ServiceContainer _siteSpecificServices;
104 private ServiceContainer SiteSpecificServices {
106 if (_siteSpecificServices == null)
107 _siteSpecificServices = new ServiceContainer (null);
109 return _siteSpecificServices;
113 void IServiceContainer.AddService (Type serviceType, object serviceInstance)
115 SiteSpecificServices.AddService (serviceType, serviceInstance);
118 void IServiceContainer.AddService (Type serviceType, object serviceInstance, bool promote)
120 SiteSpecificServices.AddService (serviceType, serviceInstance, promote);
122 void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback)
124 SiteSpecificServices.AddService (serviceType, callback);
127 void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
129 SiteSpecificServices.AddService (serviceType, callback, promote);
132 void IServiceContainer.RemoveService (Type serviceType)
134 SiteSpecificServices.RemoveService (serviceType);
137 void IServiceContainer.RemoveService (Type serviceType, bool promote)
139 SiteSpecificServices.RemoveService (serviceType, promote);
145 #region IDictionaryService
147 private Hashtable _dictionary;
149 object IDictionaryService.GetKey (object value)
151 if (_dictionary != null) {
152 foreach (DictionaryEntry entry in _dictionary) {
153 if (value != null && value.Equals (entry.Value))
160 object IDictionaryService.GetValue (object key)
162 if (_dictionary != null)
163 return _dictionary[key];
168 // No Remove method: seting the value to null
169 // will remove the pair.
171 void IDictionaryService.SetValue (object key, object value)
173 if (_dictionary == null)
174 _dictionary = new Hashtable ();
177 _dictionary.Remove (key);
179 _dictionary[key] = value;
185 #region IServiceProvider
187 public virtual object GetService (Type service)
189 object serviceInstance = null;
191 if (typeof (IDictionaryService) == service)
192 serviceInstance = (IDictionaryService) this;
194 if (typeof (INestedContainer) == service) {
195 if (_nestedContainer == null)
196 _nestedContainer = new DesignModeNestedContainer (_component, null);
197 serviceInstance = _nestedContainer;
200 // Avoid returning the site specific IServiceContainer
201 if (serviceInstance == null && service != typeof (IServiceContainer) &&
202 _siteSpecificServices != null)
203 serviceInstance = _siteSpecificServices.GetService (service);
205 if (serviceInstance == null)
206 serviceInstance = _serviceProvider.GetService (service);
208 return serviceInstance;