UndoRedo.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using FastReport.Utils;
  6. using FastReport.Data;
  7. using System.Threading;
  8. namespace FastReport.Design
  9. {
  10. internal class UndoRedo : IDisposable
  11. {
  12. private ReportTab owner;
  13. private List<UndoRedoInfo> undo;
  14. private List<UndoRedoInfo> redo;
  15. private CancellationTokenSource cancelToken;
  16. public Designer Designer
  17. {
  18. get { return owner.Designer; }
  19. }
  20. public Report Report
  21. {
  22. get { return owner.Report; }
  23. }
  24. public BlobStore BlobStore
  25. {
  26. get { return owner.BlobStore; }
  27. }
  28. public int UndoCount
  29. {
  30. get { return undo.Count; }
  31. }
  32. public int RedoCount
  33. {
  34. get { return redo.Count; }
  35. }
  36. public string[] UndoNames
  37. {
  38. get
  39. {
  40. string[] result = new string[UndoCount];
  41. for (int i = 0; i < undo.Count; i++)
  42. {
  43. result[i] = undo[i].name;
  44. }
  45. return result;
  46. }
  47. }
  48. public string[] RedoNames
  49. {
  50. get
  51. {
  52. string[] result = new string[RedoCount];
  53. for (int i = 0; i < redo.Count; i++)
  54. {
  55. result[i] = redo[i].name;
  56. }
  57. return result;
  58. }
  59. }
  60. private void SaveReport(Stream stream)
  61. {
  62. using (FRWriter writer = new FRWriter())
  63. {
  64. writer.SerializeTo = SerializeTo.Undo;
  65. writer.BlobStore = BlobStore;
  66. writer.Write(Report);
  67. writer.Save(stream);
  68. }
  69. }
  70. private void LoadReport(Stream stream)
  71. {
  72. ParameterCollection saveParams = new ParameterCollection(null);
  73. saveParams.Assign(Report.Parameters);
  74. Report.Clear();
  75. using (FRReader reader = new FRReader(Report))
  76. {
  77. reader.DeserializeFrom = SerializeTo.Undo;
  78. stream.Position = 0;
  79. reader.BlobStore = BlobStore;
  80. reader.Load(stream);
  81. reader.Read(Report);
  82. }
  83. Report.Parameters.AssignValues(saveParams);
  84. }
  85. private string GetUndoActionName(string action, string objName)
  86. {
  87. string s = "";
  88. if (!String.IsNullOrEmpty(objName))
  89. s = objName;
  90. else if (Designer.SelectedObjects != null)
  91. {
  92. if (Designer.SelectedObjects.Count == 1)
  93. s = Designer.SelectedObjects[0].Name;
  94. if (Designer.SelectedObjects.Count > 1)
  95. s = String.Format(Res.Get("Designer,UndoRedo,NObjects"), Designer.SelectedObjects.Count);
  96. }
  97. return String.Format(Res.Get("Designer,UndoRedo," + action), s);
  98. }
  99. public void ClearUndo()
  100. {
  101. foreach (UndoRedoInfo info in undo)
  102. {
  103. info.Dispose();
  104. }
  105. undo.Clear();
  106. if (BlobStore != null)
  107. BlobStore.Clear();
  108. }
  109. public void ClearRedo()
  110. {
  111. foreach (UndoRedoInfo info in redo)
  112. {
  113. info.Dispose();
  114. }
  115. redo.Clear();
  116. }
  117. public void AddUndo(string name, string objName)
  118. {
  119. UndoRedoInfo info = new UndoRedoInfo(GetUndoActionName(name, objName));
  120. SaveReport(info.stream);
  121. undo.Insert(0, info);
  122. }
  123. public void GetUndo(int actionsCount)
  124. {
  125. if (actionsCount >= undo.Count - 1)
  126. actionsCount = undo.Count - 1;
  127. UndoRedoInfo info = undo[actionsCount];
  128. LoadReport(info.stream);
  129. for (int i = 0; i < actionsCount; i++)
  130. {
  131. info = undo[0];
  132. redo.Insert(0, info);
  133. undo.Remove(info);
  134. }
  135. if(cancelToken != null)
  136. cancelToken.Cancel();
  137. cancelToken = new CancellationTokenSource();
  138. foreach(var obj in Report.AllObjects)
  139. {
  140. if (obj is BandBase)
  141. Validator.ValidateIntersectionAllObjectsAsync(obj as BandBase, cancelToken.Token);
  142. }
  143. }
  144. public void GetRedo(int actionsCount)
  145. {
  146. UndoRedoInfo info = redo[actionsCount - 1];
  147. LoadReport(info.stream);
  148. for (int i = 0; i < actionsCount; i++)
  149. {
  150. info = redo[0];
  151. undo.Insert(0, info);
  152. redo.Remove(info);
  153. }
  154. if(cancelToken != null)
  155. cancelToken.Cancel();
  156. cancelToken = new CancellationTokenSource();
  157. foreach(var obj in Report.AllObjects)
  158. {
  159. if (obj is BandBase)
  160. Validator.ValidateIntersectionAllObjectsAsync(obj as BandBase, cancelToken.Token);
  161. }
  162. }
  163. public void Dispose()
  164. {
  165. ClearUndo();
  166. ClearRedo();
  167. }
  168. public UndoRedo(ReportTab tab)
  169. {
  170. owner = tab;
  171. undo = new List<UndoRedoInfo>();
  172. redo = new List<UndoRedoInfo>();
  173. }
  174. private class UndoRedoInfo : IDisposable
  175. {
  176. public string name;
  177. public MemoryStream stream;
  178. public void Dispose()
  179. {
  180. stream.Dispose();
  181. }
  182. public UndoRedoInfo(string name)
  183. {
  184. this.name = name;
  185. stream = new MemoryStream();
  186. }
  187. }
  188. }
  189. }