ReplaceDocumentHistoryMemento.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. namespace PaintDotNet.Measurement.HistoryMementos
  11. {
  12. /// <summary>
  13. /// This HistoryMemento can be used to save an entire Document for undo purposes
  14. /// Create this HistoryMemento, then use SetDocument(), then push this on to the
  15. /// History using PushNewAction.
  16. /// </summary>
  17. public class ReplaceDocumentHistoryMemento
  18. : HistoryMemento
  19. {
  20. private IDocumentWorkspace historyWorkspace;
  21. [Serializable]
  22. private sealed class ReplaceDocumentHistoryMementoData
  23. : HistoryMementoData
  24. {
  25. private Document oldDocument;
  26. public Document OldDocument
  27. {
  28. get
  29. {
  30. return oldDocument;
  31. }
  32. }
  33. public ReplaceDocumentHistoryMementoData(Document oldDocument)
  34. {
  35. this.oldDocument = oldDocument;
  36. }
  37. protected override void Dispose(bool disposing)
  38. {
  39. if (disposing)
  40. {
  41. if (oldDocument != null)
  42. {
  43. oldDocument.Dispose();
  44. oldDocument = null;
  45. }
  46. }
  47. }
  48. }
  49. public ReplaceDocumentHistoryMemento(string name, ImageResource image, IDocumentWorkspace historyWorkspace)
  50. : base(name, image)
  51. {
  52. this.historyWorkspace = historyWorkspace;
  53. ReplaceDocumentHistoryMementoData data = new ReplaceDocumentHistoryMementoData(this.historyWorkspace.GetDocument());
  54. this.Data = data;
  55. }
  56. protected override HistoryMemento OnUndo()
  57. {
  58. ReplaceDocumentHistoryMemento ha = new ReplaceDocumentHistoryMemento(Name, Image, this.historyWorkspace);
  59. this.historyWorkspace.SetDocument(((ReplaceDocumentHistoryMementoData)Data).OldDocument);
  60. return ha;
  61. }
  62. }
  63. }