SequenceData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Xml;
  6. using System.Text;
  7. using RecipeEditorLib.DGExtension.CustomColumn;
  8. using RecipeEditorLib.RecipeModel.Params;
  9. using Caliburn.Micro.Core;
  10. namespace VirgoUI.Client.Models.Recipe.Sequence
  11. {
  12. public class SequenceData : PropertyChangedBase
  13. {
  14. public SequenceData()
  15. {
  16. this.Steps = new ObservableCollection<ObservableCollection<Param>>();
  17. }
  18. public ObservableCollection<ObservableCollection<Param>> Steps { get; set; }
  19. public ObservableCollection<Param> CreateStep(ObservableCollection<ColumnBase> _columns, XmlNode step = null)
  20. {
  21. PositionParam posParam = null;
  22. string posValue = string.Empty;
  23. ObservableCollection<Param> Params = new ObservableCollection<Param>();
  24. foreach (ColumnBase col in _columns)
  25. {
  26. Param parm = null;
  27. string value = string.Empty;
  28. if (!(col is ExpanderColumn) && step != null && !(col is StepColumn))
  29. {
  30. if (step.Attributes[col.ControlName] != null)
  31. value = step.Attributes[col.ControlName].Value;
  32. }
  33. if (col is StepColumn)
  34. parm = new StepParam() { Name = col.ControlName };
  35. else if (col is TextBoxColumn)
  36. parm = new StringParam() { Name = col.ControlName, Value = value, IsEnabled = !col.IsReadOnly };
  37. else if (col is NumColumn)
  38. {
  39. if (!string.IsNullOrEmpty(value))
  40. parm = new IntParam() { Name = col.ControlName, Value = int.Parse(value), IsEnabled = !col.IsReadOnly };
  41. else
  42. parm = new IntParam() { Name = col.ControlName, Value = 0, IsEnabled = !col.IsReadOnly };
  43. }
  44. else if (col is ComboxColumn)
  45. {
  46. parm = new ComboxParam() { Name = col.ControlName, Value = value, Options = ((ComboxColumn)col).Options, IsEnabled = !col.IsReadOnly };
  47. }
  48. else if (col is PositionColumn)
  49. {
  50. posParam = new PositionParam() { Name = col.ControlName, Options = ((PositionColumn)col).Options, IsEnabled = !col.IsReadOnly };
  51. parm = posParam;
  52. posValue = value;
  53. }
  54. else if (col is ExpanderColumn)
  55. parm = new ExpanderParam() { };
  56. else if (col is RecipeSelectColumn)
  57. {
  58. string path = ((RecipeSelectColumn)col).RecipeFilePath;
  59. if (!string.IsNullOrEmpty(path) && value.StartsWith(path))
  60. {
  61. value = value.Substring(path.Length + 1);
  62. }
  63. parm = new PathFileParam() { Name = col.ControlName, Value = value, IsEnabled = !col.IsReadOnly, Path = ((RecipeSelectColumn)col).RecipeFilePath };
  64. }
  65. else if (col is MultipleSelectColumn)
  66. {
  67. parm = new MultipleSelectParam((MultipleSelectColumn)col) { Name = col.ControlName, IsEnabled = !col.IsReadOnly };
  68. string[] opts = value.Split(',');
  69. ((MultipleSelectParam)parm).Options.Apply(opt => {
  70. for(var index = 0; index < opts.Length; index++)
  71. {
  72. if (opt.ControlName == opts[index])
  73. {
  74. opt.IsChecked = true;
  75. break;
  76. }
  77. }
  78. });
  79. parm.IsSaved = true;
  80. }
  81. parm.Parent = Params;
  82. Params.Add(parm);
  83. }
  84. posParam.Value = posValue;
  85. return Params;
  86. }
  87. public ObservableCollection<Param> CloneStep(ObservableCollection<ColumnBase> _columns, ObservableCollection<Param> _sourceParams)
  88. {
  89. ObservableCollection<Param> targetParams = this.CreateStep(_columns);
  90. PositionParam posParam = null;
  91. string posValue = string.Empty;
  92. for (var index = 0; index < _sourceParams.Count; index++)
  93. {
  94. if (_sourceParams[index] is PositionParam)
  95. {
  96. posParam = (PositionParam)targetParams[index];
  97. posValue = ((PositionParam)_sourceParams[index]).Value;
  98. }
  99. else if (_sourceParams[index] is StringParam)
  100. {
  101. ((StringParam)targetParams[index]).Value = ((StringParam)_sourceParams[index]).Value;
  102. }
  103. else if (_sourceParams[index] is PathFileParam)
  104. {
  105. ((PathFileParam)targetParams[index]).Value = ((PathFileParam)_sourceParams[index]).Value;
  106. ((PathFileParam)targetParams[index]).Path = ((PathFileParam)_sourceParams[index]).Path;
  107. }
  108. else if (_sourceParams[index] is IntParam)
  109. {
  110. ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value;
  111. }
  112. else if (_sourceParams[index] is ComboxParam)
  113. {
  114. ((ComboxParam)targetParams[index]).Value = ((ComboxParam)_sourceParams[index]).Value;
  115. }
  116. else if (_sourceParams[index] is BoolParam)
  117. {
  118. ((BoolParam)targetParams[index]).Value = ((BoolParam)_sourceParams[index]).Value;
  119. }
  120. else if (_sourceParams[index] is IntParam)
  121. {
  122. ((IntParam)targetParams[index]).Value = ((IntParam)_sourceParams[index]).Value;
  123. }
  124. else if (_sourceParams[index] is DoubleParam)
  125. {
  126. ((DoubleParam)targetParams[index]).Value = ((DoubleParam)_sourceParams[index]).Value;
  127. }
  128. targetParams[index].Parent = targetParams;
  129. }
  130. posParam.Value = posValue;
  131. return targetParams;
  132. }
  133. public void LoadSteps(ObservableCollection<ColumnBase> _columns, XmlNodeList _steps)
  134. {
  135. foreach (XmlNode _step in _steps)
  136. {
  137. this.Steps.Add(this.CreateStep(_columns, _step));
  138. }
  139. }
  140. public void Load(ObservableCollection<ColumnBase> _columns, XmlDocument doc)
  141. {
  142. XmlNode node = doc.SelectSingleNode("Aitex/TableSequenceData");
  143. if (node.Attributes["CreatedBy"] != null)
  144. this.Creator = node.Attributes["CreatedBy"].Value;
  145. if (node.Attributes["CreationTime"] != null)
  146. this.CreateTime = DateTime.Parse(node.Attributes["CreationTime"].Value);
  147. if (node.Attributes["LastRevisedBy"] != null)
  148. this.Revisor = node.Attributes["LastRevisedBy"].Value;
  149. if (node.Attributes["LastRevisionTime"] != null)
  150. this.ReviseTime = DateTime.Parse(node.Attributes["LastRevisionTime"].Value);
  151. if (node.Attributes["Description"] != null)
  152. this.Description = node.Attributes["Description"].Value;
  153. foreach (XmlNode _step in doc.SelectNodes("Aitex/TableSequenceData/Step"))
  154. {
  155. this.Steps.Add(this.CreateStep(_columns, _step));
  156. }
  157. }
  158. public string ToXml()
  159. {
  160. StringBuilder builder = new StringBuilder();
  161. builder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  162. builder.Append(string.Format("<Aitex><TableSequenceData CreatedBy=\"{0}\" CreationTime=\"{1}\" LastRevisedBy=\"{2}\" LastRevisionTime=\"{3}\" Description=\"{4}\" >", this.Creator, this.CreateTime, this.Revisor, this.ReviseTime, this.Description));
  163. foreach (ObservableCollection<Param> parameters in Steps)
  164. {
  165. builder.Append("<Step ");
  166. foreach (Param parameter in parameters)
  167. {
  168. if (parameter.Visible == System.Windows.Visibility.Visible)
  169. {
  170. if (parameter is IntParam)
  171. builder.Append(parameter.Name + "=\"" + ((IntParam)parameter).Value + "\" ");
  172. else if (parameter is DoubleParam)
  173. builder.Append(parameter.Name + "=\"" + ((DoubleParam)parameter).Value + "\" ");
  174. else if (parameter is StringParam)
  175. builder.Append(parameter.Name + "=\"" + ((StringParam)parameter).Value + "\" ");
  176. else if (parameter is PathFileParam)
  177. {
  178. if (string.IsNullOrEmpty(((PathFileParam)parameter).Path))
  179. {
  180. builder.Append(parameter.Name + "=\"" + ((PathFileParam)parameter).Value + "\" ");
  181. }
  182. else
  183. {
  184. builder.Append(parameter.Name + "=\"" + ((PathFileParam)parameter).Path + "\\" + ((PathFileParam)parameter).Value + "\" ");
  185. }
  186. }
  187. else if (parameter is ComboxParam)
  188. builder.Append(parameter.Name + "=\"" + ((ComboxParam)parameter).Value + "\" ");
  189. else if (parameter is PositionParam)
  190. builder.Append(parameter.Name + "=\"" + ((PositionParam)parameter).Value + "\" ");
  191. else if (parameter is BoolParam)
  192. builder.Append(parameter.Name + "=\"" + ((BoolParam)parameter).Value + "\" ");
  193. else if (parameter is MultipleSelectParam)
  194. {
  195. List<string> selected = new List<string>();
  196. ((MultipleSelectParam)parameter).Options.Apply(
  197. opt =>
  198. {
  199. if (opt.IsChecked)
  200. selected.Add(opt.ControlName);
  201. }
  202. );
  203. builder.Append(parameter.Name + "=\"" + string.Join(",", selected) + "\" ");
  204. }
  205. }
  206. }
  207. builder.Append("/>");
  208. }
  209. builder.Append("</TableSequenceData></Aitex>");
  210. return builder.ToString();
  211. }
  212. private string name;
  213. public string Name
  214. {
  215. get { return this.name; }
  216. set
  217. {
  218. this.name = value;
  219. this.NotifyOfPropertyChange("Name");
  220. }
  221. }
  222. private string creator;
  223. public string Creator
  224. {
  225. get { return this.creator; }
  226. set
  227. {
  228. this.creator = value;
  229. this.NotifyOfPropertyChange("Creator");
  230. }
  231. }
  232. private DateTime createTime;
  233. public DateTime CreateTime
  234. {
  235. get { return this.createTime; }
  236. set
  237. {
  238. this.createTime = value;
  239. this.NotifyOfPropertyChange("CreateTime");
  240. }
  241. }
  242. private string description;
  243. public string Description
  244. {
  245. get { return this.description; }
  246. set
  247. {
  248. this.description = value;
  249. this.NotifyOfPropertyChange("Description");
  250. }
  251. }
  252. private string devisor;
  253. public string Revisor
  254. {
  255. get { return this.devisor; }
  256. set
  257. {
  258. this.devisor = value;
  259. this.NotifyOfPropertyChange("Revisor");
  260. }
  261. }
  262. private DateTime deviseTime;
  263. public DateTime ReviseTime
  264. {
  265. get { return this.deviseTime; }
  266. set
  267. {
  268. this.deviseTime = value;
  269. this.NotifyOfPropertyChange("ReviseTime");
  270. }
  271. }
  272. }
  273. }