Move Mono.CompilerServices.SymbolWriter to build after 2.1 raw mscorlib
[mono.git] / mcs / class / System.Design / System.ComponentModel.Design / DesignModeSite.cs
1 //
2 // System.ComponentModel.Design.DesignModeSite
3 //
4 // Authors:      
5 //        Ivan N. Zlatev (contact i-nZ.net)
6 //
7 // (C) 2006-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 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.ComponentModel.Design.Serialization;
36
37 namespace System.ComponentModel.Design
38 {
39
40         internal class DesignModeSite : ISite, IDictionaryService, IServiceProvider, IServiceContainer
41         {
42
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
48                 
49                 private IServiceProvider _serviceProvider;
50                 private IComponent _component;
51                 private IContainer _container;
52                 private string _componentName;
53                 private NestedContainer _nestedContainer;
54
55
56                 public DesignModeSite (IComponent component, string name, IContainer container, IServiceProvider serviceProvider)
57                 {
58                         _component = component;
59                         _container = container;
60                         _componentName = name;
61                         _serviceProvider = serviceProvider;
62                 }
63
64                 public IComponent Component {
65                         get { return _component; }
66                 }
67
68                 public IContainer Container {
69                         get { return _container; }
70                 }
71
72                 // Yay yay yay, guess who's in design mode ???
73                 //
74                 public bool DesignMode {
75                         get { return true; }
76                 }
77
78                 // The place where renaming of a component takes place.
79                 // We should validate the new name here, if INameCreation service is present
80                 //
81                 public string Name {
82                         get {
83                                 return _componentName;
84                         }
85                         set {
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
89
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);
95                                         }
96                                 }
97                         }
98                 }
99
100 #region IServiceContainer
101
102                 private ServiceContainer _siteSpecificServices;
103
104                 private ServiceContainer SiteSpecificServices {
105                         get {
106                                 if (_siteSpecificServices == null)
107                                         _siteSpecificServices = new ServiceContainer (null);
108
109                                 return _siteSpecificServices;
110                         }
111                 }
112
113                 void IServiceContainer.AddService (Type serviceType, object serviceInstance)
114                 {
115                         SiteSpecificServices.AddService (serviceType, serviceInstance);
116                 }
117
118                 void IServiceContainer.AddService (Type serviceType, object serviceInstance, bool promote)
119                 {
120                         SiteSpecificServices.AddService (serviceType, serviceInstance, promote);
121                 }
122                 void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback)
123                 {
124                         SiteSpecificServices.AddService (serviceType, callback);
125                 }
126
127                 void IServiceContainer.AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
128                 {
129                         SiteSpecificServices.AddService (serviceType, callback, promote);
130                 }
131
132                 void IServiceContainer.RemoveService (Type serviceType)
133                 {
134                         SiteSpecificServices.RemoveService (serviceType);
135                 }
136
137                 void IServiceContainer.RemoveService (Type serviceType, bool promote)
138                 {
139                         SiteSpecificServices.RemoveService (serviceType, promote);
140                 }
141
142 #endregion
143
144
145 #region IDictionaryService
146
147                 private Hashtable _dictionary;
148
149                 object IDictionaryService.GetKey (object value)
150                 {
151                         if (_dictionary != null) {
152                                 foreach (DictionaryEntry entry in _dictionary) {
153                                         if (value != null && value.Equals (entry.Value))
154                                                 return entry.Key;
155                                 }
156                         }
157                         return null;
158                 }
159
160                 object IDictionaryService.GetValue (object key)
161                 {
162                         if (_dictionary != null)
163                                 return _dictionary[key];
164
165                         return null;
166                 }
167
168                 // No Remove method: seting the value to null
169                 // will remove the pair.
170                 //
171                 void IDictionaryService.SetValue (object key, object value)
172                 {
173                         if (_dictionary == null)
174                                 _dictionary = new Hashtable ();
175
176                         if (value == null)
177                                 _dictionary.Remove (key);
178
179                         _dictionary[key] = value;
180                 }
181
182 #endregion
183
184                 
185 #region IServiceProvider
186
187                 public virtual object GetService (Type service)
188                 {
189                         object serviceInstance = null;
190
191                         if (typeof (IDictionaryService) == service)
192                                 serviceInstance = (IDictionaryService) this;
193
194                         if (typeof (INestedContainer) == service) {
195                                 if (_nestedContainer == null)
196                                         _nestedContainer = new DesignModeNestedContainer (_component, null);
197                                 serviceInstance = _nestedContainer;
198                         }
199
200                         // Avoid returning the site specific IServiceContainer
201                         if (serviceInstance == null && service != typeof (IServiceContainer) &&
202                             _siteSpecificServices != null)
203                                 serviceInstance = _siteSpecificServices.GetService (service);
204
205                         if (serviceInstance == null)
206                                 serviceInstance = _serviceProvider.GetService (service);
207
208                         return serviceInstance;
209                 }
210 #endregion
211
212         }
213 }
214 #endif