Dictionary.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using FastReport.Table;
  2. using FastReport.Utils;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace FastReport.Preview
  7. {
  8. internal class Dictionary
  9. {
  10. private SortedList<string, DictionaryItem> names;
  11. //private SortedDictionary<string, DictionaryItem> FNames;
  12. private Hashtable baseNames;
  13. private PreparedPages preparedPages;
  14. private void AddBaseName(string name)
  15. {
  16. for (int i = 0; i < name.Length; i++)
  17. {
  18. if (name[i] >= '0' && name[i] <= '9')
  19. {
  20. string baseName = name.Substring(0, i);
  21. int num = int.Parse(name.Substring(i));
  22. if (baseNames.ContainsKey(baseName))
  23. {
  24. int maxNum = (int)baseNames[baseName];
  25. if (num < maxNum)
  26. num = maxNum;
  27. }
  28. baseNames[baseName] = num;
  29. return;
  30. }
  31. }
  32. }
  33. public string CreateUniqueName(string baseName)
  34. {
  35. int num = 1;
  36. if (baseNames.ContainsKey(baseName))
  37. num = (int)baseNames[baseName] + 1;
  38. baseNames[baseName] = num;
  39. return baseName + num.ToString();
  40. }
  41. private void Add(string name, string sourceName, Base obj)
  42. {
  43. names.Add(name, new DictionaryItem(sourceName, obj));
  44. }
  45. public string AddUnique(string baseName, string sourceName, Base obj)
  46. {
  47. string result = CreateUniqueName(baseName);
  48. Add(result, sourceName, obj);
  49. return result;
  50. }
  51. public Base GetObject(string name)
  52. {
  53. DictionaryItem item;
  54. if (names.TryGetValue(name, out item))
  55. {
  56. // return item.CloneObject(name);
  57. if (item.OriginalComponent != null)
  58. item.OriginalComponent.SetReport(this.preparedPages.Report);
  59. Base result = item.CloneObject(name);
  60. //result.SetReport(this);
  61. return result;
  62. }
  63. else
  64. return null;
  65. //int i = FNames.IndexOfKey(name);
  66. //if (i == -1)
  67. // return null;
  68. //return FNames.Values[i].CloneObject(name);
  69. }
  70. public Base GetOriginalObject(string name)
  71. {
  72. DictionaryItem item;
  73. if (names.TryGetValue(name, out item))
  74. return item.OriginalComponent;
  75. else
  76. return null;
  77. // int i = FNames.IndexOfKey(name);
  78. //if (i == -1)
  79. // return null;
  80. //return FNames.Values[i].OriginalComponent;
  81. }
  82. public void Clear()
  83. {
  84. names.Clear();
  85. baseNames.Clear();
  86. }
  87. public void Save(XmlItem rootItem)
  88. {
  89. rootItem.Clear();
  90. foreach (KeyValuePair<string, DictionaryItem> pair in names)
  91. {
  92. XmlItem xi = rootItem.Add();
  93. xi.Name = pair.Key;
  94. xi.ClearProps();
  95. xi.SetProp("name", pair.Value.SourceName);
  96. //xi.Text = String.Concat("name=\"", pair.Value.SourceName, "\"");
  97. }
  98. //for (int i = 0; i < FNames.Count; i++)
  99. //{
  100. // XmlItem xi = rootItem.Add();
  101. // xi.Name = FNames.Keys[i];
  102. // xi.Text = "name=\"" + FNames.Values[i].SourceName + "\"";
  103. //}
  104. }
  105. public void Load(XmlItem rootItem)
  106. {
  107. Clear();
  108. for (int i = 0; i < rootItem.Count; i++)
  109. {
  110. // rootItem[i].Name is 's1', rootItem[i].Text is 'name="Page0.Shape1"'
  111. string sourceName = rootItem[i].GetProp("name");
  112. // split to Page0, Shape1
  113. string[] objName = sourceName.Split('.');
  114. // get page number
  115. int pageN = int.Parse(objName[0].Substring(4));
  116. // get the object
  117. Base obj = null;
  118. if (objName.Length == 2)
  119. obj = preparedPages.SourcePages[pageN].FindObject(objName[1]);
  120. else
  121. obj = preparedPages.SourcePages[pageN];
  122. // add s1, Page0.Shape1, object
  123. string name = rootItem[i].Name;
  124. Add(name, sourceName, obj);
  125. AddBaseName(name);
  126. }
  127. }
  128. public Dictionary(PreparedPages preparedPages)
  129. {
  130. this.preparedPages = preparedPages;
  131. names = new SortedList<string, DictionaryItem>();
  132. //FNames = new SortedDictionary<string, DictionaryItem>();
  133. baseNames = new Hashtable();
  134. }
  135. private sealed class DictionaryItem
  136. {
  137. private readonly string sourceName;
  138. private readonly Base originalComponent;
  139. public string SourceName
  140. {
  141. get { return sourceName; }
  142. }
  143. public Base OriginalComponent
  144. {
  145. get { return originalComponent; }
  146. }
  147. public Base CloneObject(string alias)
  148. {
  149. Base result = null;
  150. Type type = originalComponent.GetType();
  151. // try frequently used objects first. The CreateInstance method is very slow.
  152. if (type == typeof(TextObject))
  153. result = new TextObject();
  154. else if (type == typeof(TableCell))
  155. result = new TableCell();
  156. else if (type == typeof(DataBand))
  157. result = new DataBand();
  158. else
  159. result = Activator.CreateInstance(type) as Base;
  160. result.Assign(originalComponent);
  161. result.OriginalComponent = originalComponent;
  162. result.Alias = alias;
  163. result.SetName(originalComponent.Name);
  164. if (result is ReportComponentBase)
  165. (result as ReportComponentBase).AssignPreviewEvents(originalComponent);
  166. return result;
  167. }
  168. public DictionaryItem(string name, Base obj)
  169. {
  170. sourceName = name;
  171. originalComponent = obj;
  172. }
  173. }
  174. }
  175. }