UndoEngine.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. //
  2. // System.ComponentModel.Design.UndoEngine.cs
  3. //
  4. // Author:
  5. // Ivan N. Zlatev <[email protected]>
  6. //
  7. // Copyright (C) 2007 Ivan N. Zlatev <[email protected]>
  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. #if NET_2_0
  30. using System;
  31. using System.ComponentModel;
  32. using System.ComponentModel.Design;
  33. using System.Collections.Generic;
  34. using System.ComponentModel.Design.Serialization;
  35. namespace System.ComponentModel.Design
  36. {
  37. public abstract class UndoEngine : IDisposable
  38. {
  39. private bool _undoing;
  40. private UndoUnit _currentUnit;
  41. private IServiceProvider _provider;
  42. private bool _enabled;
  43. protected UndoEngine (IServiceProvider provider)
  44. {
  45. if (provider == null)
  46. throw new ArgumentNullException ("provider");
  47. _provider = provider;
  48. _currentUnit = null;
  49. Enable ();
  50. }
  51. private void Enable ()
  52. {
  53. if (!_enabled) {
  54. IComponentChangeService changeService = GetRequiredService (typeof (IComponentChangeService)) as IComponentChangeService;
  55. changeService.ComponentAdding += new ComponentEventHandler (OnComponentAdding);
  56. changeService.ComponentAdded += new ComponentEventHandler (OnComponentAdded);
  57. changeService.ComponentRemoving += new ComponentEventHandler (OnComponentRemoving);
  58. changeService.ComponentRemoved += new ComponentEventHandler (OnComponentRemoved);
  59. changeService.ComponentChanging += new ComponentChangingEventHandler (OnComponentChanging);
  60. changeService.ComponentChanged += new ComponentChangedEventHandler (OnComponentChanged);
  61. changeService.ComponentRename += new ComponentRenameEventHandler (OnComponentRename);
  62. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  63. host.TransactionClosed += new DesignerTransactionCloseEventHandler (OnTransactionClosed);
  64. host.TransactionOpened += new EventHandler (OnTransactionOpened);
  65. _enabled = true;
  66. }
  67. }
  68. private void Disable ()
  69. {
  70. if (_enabled) {
  71. IComponentChangeService changeService = GetRequiredService (typeof (IComponentChangeService)) as IComponentChangeService;
  72. changeService.ComponentAdding -= new ComponentEventHandler (OnComponentAdding);
  73. changeService.ComponentAdded -= new ComponentEventHandler (OnComponentAdded);
  74. changeService.ComponentRemoving -= new ComponentEventHandler (OnComponentRemoving);
  75. changeService.ComponentRemoved -= new ComponentEventHandler (OnComponentRemoved);
  76. changeService.ComponentChanging -= new ComponentChangingEventHandler (OnComponentChanging);
  77. changeService.ComponentChanged -= new ComponentChangedEventHandler (OnComponentChanged);
  78. changeService.ComponentRename -= new ComponentRenameEventHandler (OnComponentRename);
  79. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  80. host.TransactionClosed -= new DesignerTransactionCloseEventHandler (OnTransactionClosed);
  81. host.TransactionOpened -= new EventHandler (OnTransactionOpened);
  82. _enabled = false;
  83. }
  84. }
  85. // FIXME: there could be more transactions opened and closed (but not commited) after the first one!!!
  86. // This means that there should be multiple units. Only the top level transaction is commited though
  87. //
  88. private void OnTransactionOpened (object sender, EventArgs args)
  89. {
  90. if (_currentUnit == null) {
  91. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  92. _currentUnit = CreateUndoUnit (host.TransactionDescription, true);
  93. }
  94. }
  95. private void OnTransactionClosed (object sender, DesignerTransactionCloseEventArgs args)
  96. {
  97. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  98. if (!host.InTransaction) { // the "top-most" transaction was closed (currentUnit one)
  99. _currentUnit.Close ();
  100. if (args.TransactionCommitted) {
  101. AddUndoUnit (_currentUnit);
  102. } else {
  103. _currentUnit.Undo ();
  104. DiscardUndoUnit (_currentUnit);
  105. }
  106. _currentUnit = null;
  107. }
  108. }
  109. private void OnComponentAdding (object sender, ComponentEventArgs args)
  110. {
  111. if (_currentUnit == null)
  112. _currentUnit = CreateUndoUnit ("Add " + args.Component.GetType ().Name, true);
  113. _currentUnit.ComponentAdding (args);
  114. }
  115. private void OnComponentAdded (object sender, ComponentEventArgs args)
  116. {
  117. if (_currentUnit == null)
  118. _currentUnit = CreateUndoUnit ("Add " + args.Component.Site.Name, true);
  119. _currentUnit.ComponentAdded (args);
  120. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  121. if (!host.InTransaction) {
  122. _currentUnit.Close ();
  123. AddUndoUnit (_currentUnit);
  124. _currentUnit = null;
  125. }
  126. }
  127. private void OnComponentRemoving (object sender, ComponentEventArgs args)
  128. {
  129. if (_currentUnit == null)
  130. _currentUnit = CreateUndoUnit ("Remove " + args.Component.Site.Name, true);
  131. _currentUnit.ComponentRemoving (args);
  132. }
  133. private void OnComponentRemoved (object sender, ComponentEventArgs args)
  134. {
  135. if (_currentUnit == null)
  136. _currentUnit = CreateUndoUnit ("Remove " + args.Component.GetType ().Name, true);
  137. _currentUnit.ComponentRemoved (args);
  138. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  139. if (!host.InTransaction) {
  140. _currentUnit.Close ();
  141. AddUndoUnit (_currentUnit);
  142. _currentUnit = null;
  143. }
  144. }
  145. private void OnComponentChanging (object sender, ComponentChangingEventArgs args)
  146. {
  147. if (_currentUnit == null)
  148. _currentUnit = CreateUndoUnit ("Modify " + ((IComponent)args.Component).Site.Name + "." + args.Member.Name, true);
  149. _currentUnit.ComponentChanging (args);
  150. }
  151. private void OnComponentChanged (object sender, ComponentChangedEventArgs args)
  152. {
  153. if (_currentUnit == null)
  154. _currentUnit = CreateUndoUnit ("Modify " + ((IComponent)args.Component).Site.Name + "." + args.Member.Name, true);
  155. _currentUnit.ComponentChanged (args);
  156. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  157. if (!host.InTransaction) {
  158. _currentUnit.Close ();
  159. AddUndoUnit (_currentUnit);
  160. _currentUnit = null;
  161. }
  162. }
  163. private void OnComponentRename (object sender, ComponentRenameEventArgs args)
  164. {
  165. if (_currentUnit == null)
  166. _currentUnit = CreateUndoUnit ("Rename " + ((IComponent)args.Component).Site.Name, true);
  167. _currentUnit.ComponentRename (args);
  168. IDesignerHost host = GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  169. if (!host.InTransaction) {
  170. _currentUnit.Close ();
  171. AddUndoUnit (_currentUnit);
  172. _currentUnit = null;
  173. }
  174. }
  175. public event EventHandler Undoing;
  176. public event EventHandler Undone;
  177. public bool Enabled {
  178. get { return _enabled; }
  179. set {
  180. if (value)
  181. Enable ();
  182. else
  183. Disable ();
  184. }
  185. }
  186. public bool UndoInProgress {
  187. get { return _undoing; }
  188. }
  189. protected virtual UndoEngine.UndoUnit CreateUndoUnit (string name, bool primary)
  190. {
  191. return new UndoUnit (this, name);
  192. }
  193. public void Dispose ()
  194. {
  195. Dispose (true);
  196. }
  197. protected virtual void Dispose (bool disposing)
  198. {
  199. if (disposing) {
  200. if (_currentUnit != null) {
  201. _currentUnit.Close ();
  202. _currentUnit = null;
  203. }
  204. }
  205. }
  206. protected object GetRequiredService (Type serviceType)
  207. {
  208. object service = this.GetService (serviceType);
  209. if (service == null)
  210. throw new NotSupportedException ("Service '" + serviceType.Name + "' missing");
  211. return service;
  212. }
  213. protected object GetService (Type serviceType)
  214. {
  215. if (serviceType == null)
  216. throw new ArgumentNullException ("serviceType");
  217. if (_provider != null)
  218. return _provider.GetService (serviceType);
  219. return null;
  220. }
  221. protected virtual void OnUndoing (EventArgs e)
  222. {
  223. Disable ();
  224. _undoing = true;
  225. if (Undoing != null)
  226. Undoing (this, e);
  227. }
  228. protected virtual void OnUndone (EventArgs e)
  229. {
  230. Enable ();
  231. _undoing = false;
  232. if (Undone != null)
  233. Undone (this, e);
  234. }
  235. protected abstract void AddUndoUnit (UndoEngine.UndoUnit unit);
  236. protected virtual void DiscardUndoUnit (UndoEngine.UndoUnit unit)
  237. {
  238. }
  239. protected class UndoUnit
  240. {
  241. private class Action
  242. {
  243. public virtual void Undo (UndoEngine engine)
  244. {
  245. }
  246. }
  247. private class ComponentRenameAction : Action
  248. {
  249. private string _oldName;
  250. private string _currentName;
  251. public ComponentRenameAction (string currentName, string oldName)
  252. {
  253. _currentName = currentName;
  254. _oldName = oldName;
  255. }
  256. public override void Undo (UndoEngine engine)
  257. {
  258. IComponentChangeService changeService = engine.GetRequiredService (typeof (IComponentChangeService)) as IComponentChangeService;
  259. IDesignerHost host = engine.GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  260. IComponent component = host.Container.Components[_currentName];
  261. changeService.OnComponentChanging (component, null);
  262. component.Site.Name = _oldName;
  263. string tmp = _currentName;
  264. _currentName = _oldName;
  265. _oldName = tmp;
  266. changeService.OnComponentChanged (component, null, null, null);
  267. }
  268. } // ComponentRenameAction
  269. private class ComponentAddRemoveAction : Action
  270. {
  271. private string _componentName;
  272. private SerializationStore _serializedComponent;
  273. private bool _added;
  274. public ComponentAddRemoveAction (UndoEngine engine, IComponent component, bool added)
  275. {
  276. if (component == null)
  277. throw new ArgumentNullException ("component");
  278. ComponentSerializationService serializationService = engine.GetRequiredService (
  279. typeof (ComponentSerializationService)) as ComponentSerializationService;
  280. _serializedComponent = serializationService.CreateStore ();
  281. serializationService.Serialize (_serializedComponent, component);
  282. _serializedComponent.Close ();
  283. _added = added;
  284. _componentName = component.Site.Name;
  285. }
  286. public override void Undo (UndoEngine engine)
  287. {
  288. IDesignerHost host = engine.GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  289. if (_added) {
  290. host.DestroyComponent (host.Container.Components[_componentName]);
  291. _added = false;
  292. } else {
  293. ComponentSerializationService serializationService = engine.GetRequiredService (
  294. typeof (ComponentSerializationService)) as ComponentSerializationService;
  295. serializationService.DeserializeTo (_serializedComponent, host.Container);
  296. _added = true;
  297. }
  298. }
  299. } // ComponentAddRemoveAction
  300. private class ComponentChangeAction : Action
  301. {
  302. private SerializationStore _currentState;
  303. private SerializationStore _oldState;
  304. public ComponentChangeAction ()
  305. {
  306. }
  307. public void SetOriginalState (UndoEngine engine, IComponent component, MemberDescriptor member)
  308. {
  309. ComponentSerializationService serializationService = engine.GetRequiredService (
  310. typeof (ComponentSerializationService)) as ComponentSerializationService;
  311. _oldState = serializationService.CreateStore ();
  312. serializationService.SerializeMember (_oldState, component, member);
  313. _oldState.Close ();
  314. }
  315. public void SetModifiedState (UndoEngine engine, IComponent component, MemberDescriptor member)
  316. {
  317. ComponentSerializationService serializationService = engine.GetRequiredService (
  318. typeof (ComponentSerializationService)) as ComponentSerializationService;
  319. _currentState = serializationService.CreateStore ();
  320. serializationService.SerializeMember (_currentState, component, member);
  321. _currentState.Close ();
  322. }
  323. public override void Undo (UndoEngine engine)
  324. {
  325. ComponentSerializationService serializationService = engine.GetRequiredService (
  326. typeof (ComponentSerializationService)) as ComponentSerializationService;
  327. IDesignerHost host = engine.GetRequiredService (typeof (IDesignerHost)) as IDesignerHost;
  328. serializationService.Deserialize (_oldState, host.Container);
  329. SerializationStore tmp = _currentState;
  330. _currentState = _oldState;
  331. _oldState = tmp;
  332. }
  333. } // ComponentChangeAction
  334. private UndoEngine _engine;
  335. private string _name;
  336. private bool _closed;
  337. private Stack <Action> _actions;
  338. public UndoUnit (UndoEngine engine, string name)
  339. {
  340. if (engine == null)
  341. throw new ArgumentNullException ("engine");
  342. if (name == null)
  343. throw new ArgumentNullException ("name");
  344. //name = String.Empty;
  345. _engine = engine;
  346. _name = name;
  347. _actions = new Stack <Action> ();
  348. }
  349. public void Undo ()
  350. {
  351. _engine.OnUndoing (EventArgs.Empty);
  352. UndoCore ();
  353. _engine.OnUndone (EventArgs.Empty);
  354. }
  355. protected virtual void UndoCore ()
  356. {
  357. foreach (Action action in _actions)
  358. action.Undo (_engine);
  359. }
  360. protected UndoEngine UndoEngine {
  361. get { return _engine; }
  362. }
  363. public virtual bool IsEmpty {
  364. get { return _actions.Count == 0; }
  365. }
  366. public virtual string Name {
  367. get { return _name; }
  368. }
  369. public virtual void Close ()
  370. {
  371. _closed = true;
  372. }
  373. public virtual void ComponentAdded (ComponentEventArgs e)
  374. {
  375. if (!_closed)
  376. _actions.Push (new ComponentAddRemoveAction (_engine, (IComponent) e.Component, true));
  377. }
  378. public virtual void ComponentAdding (ComponentEventArgs e)
  379. {
  380. }
  381. public virtual void ComponentChanged (ComponentChangedEventArgs e)
  382. {
  383. if (!_closed) {
  384. ComponentChangeAction action = _actions.Peek () as ComponentChangeAction;
  385. if (action != null)
  386. action.SetModifiedState (_engine, (IComponent) e.Component, e.Member);
  387. }
  388. }
  389. public virtual void ComponentChanging (ComponentChangingEventArgs e)
  390. {
  391. if (!_closed) {
  392. ComponentChangeAction action = new ComponentChangeAction ();
  393. action.SetOriginalState (_engine, (IComponent) e.Component, e.Member);
  394. _actions.Push (action);
  395. }
  396. }
  397. public virtual void ComponentRemoved (ComponentEventArgs e)
  398. {
  399. if (!_closed)
  400. _actions.Push (new ComponentAddRemoveAction (_engine, e.Component, false));
  401. }
  402. public virtual void ComponentRemoving (ComponentEventArgs e)
  403. {
  404. }
  405. public virtual void ComponentRename (ComponentRenameEventArgs e)
  406. {
  407. if (!_closed)
  408. _actions.Push (new ComponentRenameAction (((IComponent)e.Component).Site.Name, e.OldName));
  409. }
  410. protected object GetService (Type serviceType)
  411. {
  412. return _engine.GetService (serviceType);
  413. }
  414. public override string ToString ()
  415. {
  416. return _name;
  417. }
  418. }
  419. }
  420. }
  421. #endif