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
|
//
// System.ComponentModel.Design.DesignSurface
//
// 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.
//
#if NET_2_0
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
namespace System.ComponentModel.Design
{
public class DesignSurface : IServiceProvider, IDisposable
{
#region DefaultDesignerLoader : DesignerLoader
internal class DefaultDesignerLoader : DesignerLoader
{
// When DesignSurface.BeginLoad is invoked, the designer loader loads the design document, displays the designer
// surface using the IDesignerHost interface, and calls IDesignerLoaderHost.EndLoad
// when done. The IDesignerLoaderHost implementation is usually the same class that implements IDesignerHost.
// The designer loader informs the designer host that it needs to invoke a load or reload so that the designer
// host can perform additional tasks at these times.
private Type _componentType;
private bool _loading;
public override bool Loading
{
get { return _loading; }
}
public DefaultDesignerLoader (Type componentType)
{
if (componentType == null)
throw new ArgumentNullException ("componentType");
_componentType = componentType;
}
// Note that IDesignerLoader : IDesignerHost
//
public override void BeginLoad (IDesignerLoaderHost loaderHost)
{
_loading = true;
// initializa root component and designer
//
loaderHost.CreateComponent (_componentType);
// finish off loading - no error collection here.
//
loaderHost.EndLoad (_componentType.FullName, true, null);
_loading = false;
}
public override void Dispose ()
{
_componentType = null;
}
} // DesignerLoader
#endregion
private DesignerHost _designerHost;
private DesignSurfaceServiceContainer _serviceContainer;
private ICollection _loadErrors;
private bool _isLoaded;
private DesignerLoader _designerLoader;
public DesignSurface () : this ((IServiceProvider) null)
{
}
public DesignSurface (Type rootComponentType) : this (null, rootComponentType)
{
}
public DesignSurface (IServiceProvider parentProvider, Type rootComponentType) : this (parentProvider)
{
if (rootComponentType == null)
throw new System.ArgumentNullException ("rootComponentType");
BeginLoad (rootComponentType);
}
// this ctor doesn't load the surface
//
public DesignSurface (IServiceProvider parentProvider)
{
_serviceContainer = new DesignSurfaceServiceContainer (parentProvider);
_serviceContainer.AddNonReplaceableService (typeof (IServiceContainer), _serviceContainer);
_designerHost = new DesignerHost ((IServiceProvider) _serviceContainer);
_designerHost.DesignerLoaderHostLoaded += new LoadedEventHandler (OnDesignerHost_Loaded);
_designerHost.DesignerLoaderHostLoading += new EventHandler (OnDesignerHost_Loading);
_designerHost.DesignerLoaderHostUnloading += new EventHandler (OnDesignerHost_Unloading);
_designerHost.DesignerLoaderHostUnloaded += new EventHandler (OnDesignerHost_Unloaded);
_designerHost.Activated += new EventHandler (OnDesignerHost_Activated);
_serviceContainer.AddNonReplaceableService (typeof (IComponentChangeService), _designerHost);
_serviceContainer.AddNonReplaceableService (typeof (IDesignerHost), _designerHost);
_serviceContainer.AddNonReplaceableService (typeof (IContainer), _designerHost);
_serviceContainer.AddService (typeof (ITypeDescriptorFilterService),
(ITypeDescriptorFilterService) new TypeDescriptorFilterService (_serviceContainer));
ExtenderService extenderService = new ExtenderService ();
_serviceContainer.AddService (typeof (IExtenderProviderService), (IExtenderProviderService) extenderService);
_serviceContainer.AddService (typeof (IExtenderListService), (IExtenderListService) extenderService);
_serviceContainer.AddService (typeof (DesignSurface), this);
SelectionService selectionService = new SelectionService (_serviceContainer);
_serviceContainer.AddService (typeof (ISelectionService), (ISelectionService) selectionService);
}
protected ServiceContainer ServiceContainer {
get {
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
return _serviceContainer;
}
}
public IContainer ComponentContainer {
get {
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
return _designerHost.Container;
}
}
public bool IsLoaded {
get { return _isLoaded; }
}
// Returns a collection of loading errors or a void collection.
//
public ICollection LoadErrors {
get {
if (_loadErrors == null)
_loadErrors = new object[0];
return _loadErrors;
}
}
public object View {
get {
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
if (_designerHost.RootComponent == null || this.LoadErrors.Count > 0)
throw new InvalidOperationException ("The DesignSurface isn't loaded.");
IRootDesigner designer = _designerHost.GetDesigner (_designerHost.RootComponent) as IRootDesigner;
if (designer == null)
throw new InvalidOperationException ("The DesignSurface isn't loaded.");
ViewTechnology[] viewTech = designer.SupportedTechnologies;
for (int i = 0; i < viewTech.Length; i++) {
try {
return designer.GetView (viewTech[i]);
} catch {}
}
throw new NotSupportedException ("No supported View Technology found.");
}
}
public event EventHandler Disposed;
public event EventHandler Flushed;
public event LoadedEventHandler Loaded;
public event EventHandler Loading;
public event EventHandler Unloaded;
public event EventHandler Unloading;
public event EventHandler ViewActivated;
public void BeginLoad (Type rootComponentType)
{
if (rootComponentType == null)
throw new System.ArgumentNullException ("rootComponentType");
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
this.BeginLoad (new DefaultDesignerLoader (rootComponentType));
}
public void BeginLoad (DesignerLoader loader)
{
if (loader == null)
throw new System.ArgumentNullException ("loader");
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
if (!_isLoaded) {
_loadErrors = null;
_designerLoader = loader;
this.OnLoading (EventArgs.Empty);
_designerLoader.BeginLoad (_designerHost);
}
}
#region IDisposable
public void Dispose ()
{
this.Dispose (true);
}
protected virtual void Dispose (bool disposing)
{
if (_designerLoader != null) {
_designerLoader.Dispose ();
_designerLoader = null;
}
if (_designerHost != null) {
_designerHost.Dispose ();
_designerHost.DesignerLoaderHostLoaded -= new LoadedEventHandler (OnDesignerHost_Loaded);
_designerHost.DesignerLoaderHostLoading -= new EventHandler (OnDesignerHost_Loading);
_designerHost.DesignerLoaderHostUnloading -= new EventHandler (OnDesignerHost_Unloading);
_designerHost.DesignerLoaderHostUnloaded -= new EventHandler (OnDesignerHost_Unloaded);
_designerHost.Activated -= new EventHandler (OnDesignerHost_Activated);
_designerHost = null;
}
if (_serviceContainer != null) {
_serviceContainer.Dispose ();
_serviceContainer = null;
}
if (Disposed != null)
Disposed (this, EventArgs.Empty);
}
#endregion
public void Flush ()
{
if (_designerLoader != null)
_designerLoader.Flush ();
if (Flushed != null)
Flushed (this, EventArgs.Empty);
}
private void OnDesignerHost_Loaded (object sender, LoadedEventArgs e)
{
this.OnLoaded (e);
}
private void OnDesignerHost_Loading (object sender, EventArgs e)
{
this.OnLoading (EventArgs.Empty);
}
private void OnDesignerHost_Unloading (object sender, EventArgs e)
{
this.OnUnloading (EventArgs.Empty);
}
private void OnDesignerHost_Unloaded (object sender, EventArgs e)
{
this.OnUnloaded (EventArgs.Empty);
}
protected virtual void OnLoaded (LoadedEventArgs e)
{
_loadErrors = e.Errors;
_isLoaded = e.HasSucceeded;
if (Loaded != null)
Loaded (this, e);
}
protected virtual void OnLoading (EventArgs e)
{
if (Loading != null)
Loading (this, e);
}
protected virtual void OnUnloaded (EventArgs e)
{
if (Unloaded != null)
Unloaded (this, e);
}
protected virtual void OnUnloading (EventArgs e)
{
if (Unloading != null)
Unloading (this, e);
}
internal void OnDesignerHost_Activated (object sender, EventArgs args)
{
this.OnViewActivate (EventArgs.Empty);
}
protected virtual void OnViewActivate (EventArgs e)
{
if (ViewActivated != null)
ViewActivated (this, e);
}
[ObsoleteAttribute("CreateComponent has been replaced by CreateInstance")]
protected internal virtual IComponent CreateComponent (Type componentType)
{
return (this.CreateInstance (componentType)) as IComponent;
}
// XXX: I am not quite sure if this should add the created instance of the component
// to the surface, but it does. (If one finds out that this is wrong just use
// _designerHost.CreateInstance (..)
//
protected internal virtual object CreateInstance (Type type)
{
if (type == null)
throw new System.ArgumentNullException ("type");
return _designerHost.CreateComponent (type);
}
protected internal virtual IDesigner CreateDesigner (IComponent component, bool rootDesigner)
{
if (component == null)
throw new System.ArgumentNullException ("component");
if (_designerHost == null)
throw new System.ObjectDisposedException ("DesignerSurface");
return _designerHost.CreateDesigner (component, rootDesigner);
}
public INestedContainer CreateNestedContainer (IComponent owningComponent)
{
return this.CreateNestedContainer (owningComponent, null);
}
public INestedContainer CreateNestedContainer (IComponent owningComponent, string containerName)
{
if (_designerHost == null)
throw new ObjectDisposedException ("DesignSurface");
return new DesignModeNestedContainer (owningComponent, containerName);
}
#region IServiceProvider
public object GetService (Type serviceType)
{
if (typeof (IServiceContainer) == serviceType)
return _serviceContainer;
return _serviceContainer.GetService (serviceType);
}
#endregion
}
}
#endif
|