BasicDesignerLoader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // System.ComponentModel.Design.Serialization.BasicDesignerLoader
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design;
  33. using System.Windows.Forms;
  34. namespace System.ComponentModel.Design.Serialization
  35. {
  36. public abstract class BasicDesignerLoader : DesignerLoader, IDesignerLoaderService
  37. {
  38. [Flags]
  39. protected enum ReloadOptions
  40. {
  41. Default,
  42. Force,
  43. ModifyOnError,
  44. NoFlush
  45. }
  46. private bool _loaded;
  47. private bool _loading;
  48. private IDesignerLoaderHost _host;
  49. private int _dependenciesCount;
  50. private bool _notificationsEnabled;
  51. private bool _modified;
  52. private string _baseComponentClassName;
  53. private DesignerSerializationManager _serializationMananger;
  54. private bool _flushing;
  55. private bool _reloadScheduled;
  56. private ReloadOptions _reloadOptions;
  57. protected BasicDesignerLoader ()
  58. {
  59. _loading = _loaded = _flushing = _reloadScheduled = false;
  60. _host = null;
  61. _notificationsEnabled = false;
  62. _modified = false;
  63. _dependenciesCount = 0;
  64. }
  65. protected virtual void Initialize ()
  66. {
  67. _serializationMananger = new DesignerSerializationManager (_host);
  68. DesignSurfaceServiceContainer serviceContainer = _host.GetService (typeof (IServiceContainer)) as DesignSurfaceServiceContainer;
  69. if (serviceContainer != null) {
  70. serviceContainer.AddService (typeof (IDesignerLoaderService), (IDesignerLoaderService) this);
  71. serviceContainer.AddNonReplaceableService (typeof (IDesignerSerializationManager), _serializationMananger);
  72. }
  73. }
  74. public override void BeginLoad (IDesignerLoaderHost host)
  75. {
  76. if (host == null)
  77. throw new ArgumentNullException ("host");
  78. if (_loaded)
  79. throw new InvalidOperationException ("Already loaded.");
  80. if (_host != null && _host != host)
  81. throw new InvalidOperationException ("Trying to load with a different host");
  82. if (_host == null) { // beingload is called on reload - no need to initialize twice.
  83. _host = host;
  84. Initialize ();
  85. }
  86. IDisposable session = _serializationMananger.CreateSession ();
  87. IDesignerLoaderService loader = _host.GetService (typeof (IDesignerLoaderService)) as IDesignerLoaderService;
  88. if (loader != null) {
  89. _dependenciesCount = -1;
  90. loader.AddLoadDependency ();
  91. } else {
  92. OnBeginLoad ();
  93. }
  94. bool successful = true;
  95. try {
  96. PerformLoad (_serializationMananger);
  97. } catch (Exception e) {
  98. successful = false;
  99. _serializationMananger.Errors.Add (e);
  100. }
  101. if (loader != null)
  102. loader.DependentLoadComplete (successful, _serializationMananger.Errors);
  103. else
  104. OnEndLoad (successful, _serializationMananger.Errors);
  105. session.Dispose ();
  106. }
  107. protected abstract void PerformLoad (IDesignerSerializationManager serializationManager);
  108. protected virtual void OnBeginLoad ()
  109. {
  110. _loading = true;
  111. }
  112. protected virtual void OnEndLoad (bool successful, ICollection errors)
  113. {
  114. _host.EndLoad (_baseComponentClassName, successful, errors);
  115. if (successful) {
  116. _loaded = true;
  117. EnableComponentNotification (true);
  118. } else {
  119. if (_reloadScheduled) { // we are reloading
  120. bool modify = ((_reloadOptions & ReloadOptions.ModifyOnError) == ReloadOptions.ModifyOnError);
  121. if (modify) {
  122. OnModifying ();
  123. this.Modified = true;
  124. }
  125. }
  126. }
  127. _loading = false;
  128. }
  129. public override bool Loading {
  130. get { return _loading; }
  131. }
  132. protected IDesignerLoaderHost LoaderHost {
  133. get { return _host; }
  134. }
  135. protected virtual bool Modified {
  136. get { return _modified; }
  137. set { _modified = value; }
  138. }
  139. protected object PropertyProvider {
  140. get {
  141. if (!_loaded)
  142. throw new InvalidOperationException ("host not initialized");
  143. return _serializationMananger.PropertyProvider;
  144. }
  145. set {
  146. if (!_loaded)
  147. throw new InvalidOperationException ("host not initialized");
  148. _serializationMananger.PropertyProvider = value;
  149. }
  150. }
  151. protected bool ReloadPending {
  152. get { return _reloadScheduled; }
  153. }
  154. protected virtual bool EnableComponentNotification (bool enable)
  155. {
  156. if (!_loaded)
  157. throw new InvalidOperationException ("host not initialized");
  158. IComponentChangeService service = _host.GetService (typeof (IComponentChangeService)) as IComponentChangeService;
  159. if (service != null && _notificationsEnabled != enable) {
  160. if (enable) {
  161. service.ComponentAdding += new ComponentEventHandler (OnComponentAdding);
  162. service.ComponentAdded += new ComponentEventHandler (OnComponentAdded);
  163. service.ComponentRemoving += new ComponentEventHandler (OnComponentRemoving);
  164. service.ComponentRemoved += new ComponentEventHandler (OnComponentRemoved);
  165. service.ComponentChanging += new ComponentChangingEventHandler (OnComponentChanging);
  166. service.ComponentChanged += new ComponentChangedEventHandler (OnComponentChanged);
  167. service.ComponentRename += new ComponentRenameEventHandler (OnComponentRename);
  168. } else {
  169. service.ComponentAdding -= new ComponentEventHandler (OnComponentAdding);
  170. service.ComponentAdded -= new ComponentEventHandler (OnComponentAdded);
  171. service.ComponentRemoving -= new ComponentEventHandler (OnComponentRemoving);
  172. service.ComponentRemoved -= new ComponentEventHandler (OnComponentRemoved);
  173. service.ComponentChanging -= new ComponentChangingEventHandler (OnComponentChanging);
  174. service.ComponentChanged -= new ComponentChangedEventHandler (OnComponentChanged);
  175. service.ComponentRename -= new ComponentRenameEventHandler (OnComponentRename);
  176. }
  177. }
  178. return _notificationsEnabled == true ? true : false;
  179. }
  180. private void OnComponentAdded (object sender, ComponentEventArgs args)
  181. {
  182. if (!_loading && _loaded)
  183. this.Modified = true;
  184. }
  185. private void OnComponentRemoved (object sender, ComponentEventArgs args)
  186. {
  187. if (!_loading && _loaded)
  188. this.Modified = true;
  189. }
  190. private void OnComponentAdding (object sender, ComponentEventArgs args)
  191. {
  192. if (!_loading && _loaded)
  193. OnModifying ();
  194. }
  195. private void OnComponentRemoving (object sender, ComponentEventArgs args)
  196. {
  197. if (!_loading && _loaded)
  198. OnModifying ();
  199. }
  200. private void OnComponentChanged (object sender, ComponentChangedEventArgs args)
  201. {
  202. if (!_loading && _loaded)
  203. this.Modified = true;
  204. }
  205. private void OnComponentChanging (object sender, ComponentChangingEventArgs args)
  206. {
  207. if (!_loading && _loaded)
  208. OnModifying ();
  209. }
  210. private void OnComponentRename (object sender, ComponentRenameEventArgs args)
  211. {
  212. if (!_loading && _loaded) {
  213. OnModifying ();
  214. this.Modified = true;
  215. }
  216. }
  217. public override void Flush ()
  218. {
  219. if (!_loaded)
  220. throw new InvalidOperationException ("host not initialized");
  221. if (!_flushing && this.Modified) {
  222. _flushing = true;
  223. using ((IDisposable)_serializationMananger.CreateSession ()) {
  224. try {
  225. PerformFlush (_serializationMananger);
  226. } catch (Exception e) {
  227. _serializationMananger.Errors.Add (e);
  228. ReportFlushErrors (_serializationMananger.Errors);
  229. }
  230. }
  231. _flushing = false;
  232. }
  233. }
  234. protected abstract void PerformFlush (IDesignerSerializationManager serializationManager);
  235. // MSDN: The default implementation always returns true.
  236. protected virtual bool IsReloadNeeded ()
  237. {
  238. return true;
  239. }
  240. protected virtual void OnBeginUnload ()
  241. {
  242. }
  243. protected virtual void OnModifying ()
  244. {
  245. }
  246. // MSDN: reloads are performed at idle time
  247. protected void Reload (ReloadOptions flags)
  248. {
  249. if (!_reloadScheduled) {
  250. _reloadScheduled = true;
  251. _reloadOptions = flags;
  252. bool force = ((flags & ReloadOptions.Force) == ReloadOptions.Force);
  253. if (force)
  254. ReloadCore ();
  255. else
  256. Application.Idle += new EventHandler (OnIdle);
  257. }
  258. }
  259. private void OnIdle (object sender, EventArgs args)
  260. {
  261. Application.Idle -= new EventHandler (OnIdle);
  262. ReloadCore ();
  263. }
  264. private void ReloadCore ()
  265. {
  266. bool flush = !((_reloadOptions & ReloadOptions.NoFlush) == ReloadOptions.NoFlush);
  267. if (flush)
  268. Flush ();
  269. Unload ();
  270. _host.Reload ();
  271. BeginLoad (_host); // calls EndLoad, which will check for ReloadOptions.ModifyOnError
  272. _reloadScheduled = false;
  273. }
  274. private void Unload ()
  275. {
  276. if (_loaded) {
  277. OnBeginUnload ();
  278. EnableComponentNotification (false);
  279. _loaded = false;
  280. _baseComponentClassName = null;
  281. }
  282. }
  283. // The default implementation of ReportFlushErrors raises the last exception in the collection.
  284. //
  285. protected virtual void ReportFlushErrors (ICollection errors)
  286. {
  287. object last = null;
  288. foreach (object o in errors)
  289. last = o;
  290. throw (Exception)last;
  291. }
  292. // Must be called during PerformLoad by subclasses.
  293. protected void SetBaseComponentClassName (string name)
  294. {
  295. if (name == null)
  296. throw new ArgumentNullException ("name");
  297. _baseComponentClassName = name;
  298. }
  299. #region IDesignerLoaderService implementation
  300. void IDesignerLoaderService.AddLoadDependency ()
  301. {
  302. _dependenciesCount++;
  303. if (_dependenciesCount == 0) {
  304. _dependenciesCount = 1;
  305. OnBeginLoad ();
  306. }
  307. }
  308. void IDesignerLoaderService.DependentLoadComplete (bool successful, ICollection errorCollection)
  309. {
  310. if (_dependenciesCount == 0)
  311. throw new InvalidOperationException ("dependencies == 0");
  312. _dependenciesCount--;
  313. if (_dependenciesCount == 0) {
  314. OnEndLoad (successful, errorCollection);
  315. }
  316. }
  317. bool IDesignerLoaderService.Reload ()
  318. {
  319. if (_dependenciesCount == 0) {
  320. this.Reload (ReloadOptions.Force);
  321. return true;
  322. }
  323. return false;
  324. }
  325. #endregion
  326. protected object GetService (Type serviceType)
  327. {
  328. if (_host != null)
  329. return _host.GetService (serviceType);
  330. return null;
  331. }
  332. public override void Dispose ()
  333. {
  334. this.LoaderHost.RemoveService (typeof (IDesignerLoaderService));
  335. Unload ();
  336. }
  337. }
  338. }
  339. #endif