RecipeSequenceTreeBuilder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. namespace CyberX8_MainPages.Sequence
  8. {
  9. public class RecipeSequenceTreeBuilder
  10. {
  11. public static List<FileNode> GetFiles(string prefixPath, List<string> filenames)
  12. {
  13. List<FileNode> folders = new List<FileNode>();
  14. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  15. folders.Add(root);
  16. foreach (string filename in filenames)
  17. {
  18. string[] filesplits = filename.Split('\\');
  19. FileNode parent = root;
  20. if (filesplits.Length > 1)
  21. {
  22. for (var index = 0; index < filesplits.Length - 1; index++)
  23. {
  24. bool found = false;
  25. for (var j = 0; j < parent.Files.Count; j++)
  26. {
  27. if (parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile)
  28. {
  29. found = true;
  30. parent = parent.Files[j];
  31. break;
  32. }
  33. }
  34. if (!found)
  35. {
  36. FileNode folder = new FileNode() { Name = filesplits[index], IsFile = false, PrefixPath = prefixPath };
  37. folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]);
  38. folder.Parent = parent;
  39. parent.Files.Add(folder);
  40. parent = folder;
  41. }
  42. }
  43. }
  44. FileNode file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true, PrefixPath = prefixPath };
  45. file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]);
  46. file.Parent = parent;
  47. parent.Files.Add(file);
  48. }
  49. return folders;
  50. }
  51. public static void CreateTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder)
  52. {
  53. foreach (XmlElement ele in curElementNode.ChildNodes)
  54. {
  55. if (ele.Name == "File")
  56. {
  57. string fileName = ele.Attributes["Name"].Value;
  58. fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  59. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  60. ? fileName
  61. : parent.FullPath + "\\" + fileName;
  62. FileNode file = new FileNode()
  63. {
  64. Name = fileName,
  65. IsFile = true,
  66. PrefixPath = prefixPath,
  67. Parent = parent,
  68. FullPath = fullPath,
  69. };
  70. if (!selectionIsFolder && selectionName == file.FullPath)
  71. {
  72. file.IsSelected = true;
  73. FileNode node = file.Parent;
  74. while (node.FullPath.Contains("\\"))
  75. {
  76. node.IsExpanded = true;
  77. node = node.Parent;
  78. }
  79. }
  80. parent.Files.Add(file);
  81. }
  82. else if (ele.Name == "Folder")
  83. {
  84. string folderName = ele.Attributes["Name"].Value;
  85. folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
  86. string fullPath = string.IsNullOrEmpty(parent.FullPath)
  87. ? folderName
  88. : parent.FullPath + "\\" + folderName;
  89. FileNode folder = new FileNode()
  90. {
  91. Name = folderName,
  92. IsFile = false,
  93. PrefixPath = prefixPath,
  94. Parent = parent,
  95. FullPath = fullPath,
  96. IsExpanded = !fullPath.Contains("\\"),
  97. };
  98. parent.Files.Add(folder);
  99. if (selectionIsFolder && selectionName == folder.FullPath)
  100. {
  101. folder.IsSelected = true;
  102. FileNode node = folder;
  103. while (node.FullPath.Contains("\\"))
  104. {
  105. node.IsExpanded = true;
  106. node = node.Parent;
  107. }
  108. }
  109. CreateTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder);
  110. }
  111. }
  112. }
  113. public static List<FileNode> BuildFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml)
  114. {
  115. List<FileNode> folders = new List<FileNode>();
  116. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
  117. folders.Add(root);
  118. XmlDocument doc = new XmlDocument();
  119. doc.LoadXml(fileListInXml);
  120. CreateTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder);
  121. return folders;
  122. }
  123. }
  124. }