File: ComponentDesigner.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (451 lines) | stat: -rw-r--r-- 12,923 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//
// System.ComponentModel.Design.ComponentDesigner
//
// Authors:
//	  Ivan N. Zlatev (contact i-nZ.net)
//
// (C) 2006-2007 Ivan N. Zlatev

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Collections;
using System.ComponentModel;

namespace System.ComponentModel.Design
{

#if NET_2_0
	public class ComponentDesigner : ITreeDesigner, IDesigner, IDisposable, IDesignerFilter, IComponentInitializer
#else
	public class ComponentDesigner : IDesigner, IDisposable, IDesignerFilter
#endif
	{

#region ShadowPropertyCollection

		protected sealed class ShadowPropertyCollection
		{

			private Hashtable _properties = null;
			private IComponent _component;

			internal ShadowPropertyCollection (IComponent component)
			{
				_component = component;
			}

			// Returns Control's property value (if available) if there is no shadowed one.
			//
			public object this[string propertyName]
			{
				get {
					if (propertyName == null)
						throw new System.ArgumentNullException("propertyName");

					if (_properties != null && _properties.ContainsKey (propertyName))
						return _properties[propertyName];

					PropertyDescriptor property = TypeDescriptor.GetProperties (_component.GetType ())[propertyName];
					if (property != null)
						return property.GetValue (_component);
					else
						throw new System.Exception ("Propery not found!");
				}
				set {
					if (_properties == null)
						_properties = new Hashtable ();
					_properties[propertyName] = value;
				}
			}

			public bool Contains (string propertyName)
			{
				if (_properties != null)
					return _properties.ContainsKey (propertyName);
				else
					return false;
			}

		} // ShadowPropertyCollection
#endregion

		public ComponentDesigner ()
		{
		}


		private IComponent _component;
		private DesignerVerbCollection _verbs;
		private ShadowPropertyCollection _shadowPropertyCollection;
#if NET_2_0
		private DesignerActionListCollection _designerActionList;
#endif

		// This property indicates any components to copy or move along with the component managed
		// by the designer during a copy, drag, or move operation.
		// If this collection contains references to other components in the current design mode document,
		// those components will be copied along with the component managed by the designer during a copy operation.
		// When the component managed by the designer is selected, this collection is filled with any nested controls.
		// This collection can also include other components, such as the buttons of a toolbar.
		//
		// supposedly contains all the children of the component, thus used for ITreeDesigner.Children
		//
		public virtual ICollection AssociatedComponents {
			get { return new IComponent[0]; }
		}

		public IComponent Component {
			get { return _component; }
		}

		public virtual DesignerVerbCollection Verbs {
			get {
				if (_verbs == null)
					_verbs = new DesignerVerbCollection ();

				return _verbs;
			}
		}

		protected virtual InheritanceAttribute InheritanceAttribute {
			get {
				IInheritanceService service = (IInheritanceService) this.GetService (typeof (IInheritanceService));
				if (service != null)
					return service.GetInheritanceAttribute (_component);
				else
					return InheritanceAttribute.Default;
			}
		}

		protected bool Inherited {
			get { return !this.InheritanceAttribute.Equals (InheritanceAttribute.NotInherited); }
		}

		//Gets a collection of property values that override user settings.
		//
		protected ShadowPropertyCollection ShadowProperties {
			get {
				if (_shadowPropertyCollection == null) {
					_shadowPropertyCollection = new ShadowPropertyCollection(_component);
				}
				return _shadowPropertyCollection;
			}
		}

#if NET_2_0
		public virtual DesignerActionListCollection ActionLists {
			get {
				if (_designerActionList == null)
					_designerActionList = new DesignerActionListCollection ();

				return _designerActionList;
			}
		}

		protected virtual IComponent ParentComponent {
			get {
				IDesignerHost host = GetService (typeof (IDesignerHost)) as IDesignerHost;
				if (host != null) {
					IComponent rootComponent = host.RootComponent;
					if (rootComponent != _component)
						return rootComponent;
				}
				return null;
			}
		}

		public virtual void InitializeNewComponent (IDictionary defaultValues)
		{
			// Reset
			//
			OnSetComponentDefaults ();
		}

		// MSDN: The default implementation of this method does nothing.
		//
		public virtual void InitializeExistingComponent (IDictionary defaultValues)
		{
			InitializeNonDefault ();
		}
#endif


		public virtual void Initialize (IComponent component)
		{
			if (component == null)
				throw new ArgumentNullException ("component");
								
			_component = component;
		}

#if NET_2_0
		[Obsolete ("This method has been deprecated. Use InitializeExistingComponent instead.")]
#endif
		public virtual void InitializeNonDefault ()
		{
		}


		// This method is called when a user double-clicks (the representation of) a component.
		// Tries to bind the default event to a method or creates a new one.
		// 
		public virtual void DoDefaultAction()
		{
			IDesignerHost host = (IDesignerHost) this.GetService(typeof(IDesignerHost));
			DesignerTransaction transaction = null;
			if (host != null)
				transaction = host.CreateTransaction ("ComponentDesigner_AddEvent");

			IEventBindingService eventBindingService = GetService (typeof(IEventBindingService)) as IEventBindingService;
			EventDescriptor defaultEventDescriptor = null;

			if (eventBindingService != null) {
				ISelectionService selectionService = this.GetService (typeof (ISelectionService)) as ISelectionService;
				try {
					if (selectionService != null) {
						ICollection selectedComponents = selectionService.GetSelectedComponents ();

						foreach (IComponent component in selectedComponents) {
							EventDescriptor eventDescriptor = TypeDescriptor.GetDefaultEvent (component);
							if (eventDescriptor != null) {
								PropertyDescriptor eventProperty = eventBindingService.GetEventProperty (eventDescriptor);
								if (eventProperty != null && !eventProperty.IsReadOnly) {
									string methodName = eventProperty.GetValue (component) as string;
									bool newMethod = true;

									if (methodName != null || methodName != String.Empty) {
										ICollection compatibleMethods = eventBindingService.GetCompatibleMethods (eventDescriptor);
										foreach (string signature in compatibleMethods) {
											if (signature == methodName) {
												newMethod = false;
												break;
											}
										}
									}
									if (newMethod) {
										if (methodName == null)
											methodName = eventBindingService.CreateUniqueMethodName (component, eventDescriptor);
															
										eventProperty.SetValue (component, methodName);
									}

									if (component == _component)
										defaultEventDescriptor = eventDescriptor;
								}
							}
						}

					}
				}
				catch {
					if (transaction != null) {
						transaction.Cancel ();
						transaction = null;
					}
				}
				finally {
					if (transaction != null)
						transaction.Commit ();
				}

				if (defaultEventDescriptor != null)
					eventBindingService.ShowCode (_component, defaultEventDescriptor);
			}
		}



#if NET_2_0
		[Obsolete ("This method has been deprecated. Use InitializeNewComponent instead.")]
#endif
		// The default implementation of this method sets the default property of the component to
		// the name of the component if the default property is a string and the property is not already set.
		// This method can be implemented in a derived class to customize the initialization of the component
		// that this designer is designing.
		//
		public virtual void OnSetComponentDefaults ()
		{
			if (_component != null && _component.Site != null) {
				PropertyDescriptor property = TypeDescriptor.GetDefaultProperty (_component);
				if (property != null && property.PropertyType.Equals (typeof (string))) {
					string propertyValue = (string)property.GetValue (_component);
					if (propertyValue != null && propertyValue.Length != 0)
						property.SetValue (_component, _component.Site.Name);
				}
			}
		}




		protected InheritanceAttribute InvokeGetInheritanceAttribute (ComponentDesigner toInvoke)
		{
			return toInvoke.InheritanceAttribute;
		}

#region IDesignerFilter

		// TypeDescriptor queries the component's site for ITypeDescriptorFilterService 
		// then invokes ITypeDescriptorFilterService.XXXX before retrieveing props/event/attributes, 
		// which then invokes the IDesignerFilter implementation of the component
		// 
		protected virtual void PostFilterAttributes (IDictionary attributes)
		{
		}

		protected virtual void PostFilterEvents (IDictionary events)
		{
		}

		protected virtual void PostFilterProperties (IDictionary properties)
		{
		}

		protected virtual void PreFilterAttributes (IDictionary attributes)
		{
		}

		protected virtual void PreFilterEvents (IDictionary events)
		{
		}

		protected virtual void PreFilterProperties (IDictionary properties)
		{
		}
#endregion

		protected void RaiseComponentChanged (MemberDescriptor member, object oldValue, object newValue)
		{
			IComponentChangeService service = GetService (typeof (IComponentChangeService)) as IComponentChangeService;
			if (service != null)
				service.OnComponentChanged (_component, member, oldValue, newValue);
		}

		protected void RaiseComponentChanging (MemberDescriptor member)
		{
			IComponentChangeService service = GetService (typeof (IComponentChangeService)) as IComponentChangeService;
			if (service != null)
				service.OnComponentChanging (_component, member);
		}

#region Implementation of IDesignerFilter

		void IDesignerFilter.PostFilterAttributes (IDictionary attributes)
		{
			PostFilterAttributes (attributes);
		}

		void IDesignerFilter.PostFilterEvents (IDictionary events)
		{
			PostFilterEvents (events);
		}

		void IDesignerFilter.PostFilterProperties (IDictionary properties)
		{
			PostFilterProperties (properties);
		}

		void IDesignerFilter.PreFilterAttributes (IDictionary attributes)
		{
			PreFilterAttributes (attributes);
		}

		void IDesignerFilter.PreFilterEvents (IDictionary events)
		{
			PreFilterEvents (events);
		}

		void IDesignerFilter.PreFilterProperties (IDictionary properties)
		{
			PreFilterProperties (properties);
		}

#endregion

#if NET_2_0

#region ITreeDesigner
		// Returns a collection of the designers of the associated components
		//
		ICollection ITreeDesigner.Children {
			get {
				ICollection components = this.AssociatedComponents;
				IDesignerHost host = GetService (typeof (IDesignerHost)) as IDesignerHost;
				
				if (host != null) {
					ArrayList designers = new ArrayList ();
					foreach (IComponent component in components) {
						IDesigner designer = host.GetDesigner (component);
						if (designer != null)
							designers.Add (designer);
					}
					IDesigner[] result = new IDesigner[designers.Count];
					designers.CopyTo (result);
					return result;
				}
				return new IDesigner[0];
			}
		}

		IDesigner ITreeDesigner.Parent {
			get {
				IDesignerHost host = GetService (typeof (IDesignerHost)) as IDesignerHost;
				if (host != null && this.ParentComponent != null)
					return host.GetDesigner (this.ParentComponent);
	
				return null;
			}
		}
#endregion

#endif
		// Helper method - not an ISerivceProvider
		//
		protected virtual object GetService (Type service)
		{
			if (_component != null && _component.Site != null)
				return _component.Site.GetService (service);

			return null;
		}

		public void Dispose ()
		{
			this.Dispose (true);
			GC.SuppressFinalize (this);
		}


		protected virtual void Dispose (bool disposing)
		{
			if (disposing)
				_component = null;
		}

		~ComponentDesigner ()
		{
			this.Dispose (false);
		}
	}
}