RecipeSequenceTreeBuilder.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Caliburn.Micro.Core;
  8. namespace VirgoUI.Client.Models.Recipe
  9. {
  10. public class FileNode : PropertyChangedBase
  11. {
  12. public FileNode()
  13. {
  14. this.Files = new ObservableCollection<FileNode>();
  15. this.IsFile = false;
  16. }
  17. private string name = string.Empty;
  18. public string Name
  19. {
  20. get { return name; }
  21. set { name = value; NotifyOfPropertyChange("Name"); }
  22. }
  23. public string FullPath { get; set; }
  24. public FileNode Parent { get; set; }
  25. public ObservableCollection<FileNode> Files { get; set; }
  26. public bool IsFile { get; set; }
  27. }
  28. public class RecipeSequenceTreeBuilder
  29. {
  30. public static List<FileNode> GetFiles(List<string> filenames)
  31. {
  32. List<FileNode> folders = new List<FileNode>();
  33. FileNode root = new FileNode() { Name = "Files", IsFile = false, FullPath="" };
  34. folders.Add(root);
  35. foreach(string filename in filenames)
  36. {
  37. string[] filesplits = filename.Split('\\');
  38. FileNode parent = root;
  39. if (filesplits.Length>1)
  40. {
  41. for(var index =0;index< filesplits.Length-1; index++)
  42. {
  43. bool found = false;
  44. for(var j = 0; j < parent.Files.Count; j++)
  45. {
  46. if(parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile)
  47. {
  48. found = true;
  49. parent = parent.Files[j];
  50. break;
  51. }
  52. }
  53. if (!found)
  54. {
  55. FileNode folder = new FileNode() { Name = filesplits[index], IsFile = false };
  56. folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]);
  57. folder.Parent = parent;
  58. parent.Files.Add(folder);
  59. parent = folder;
  60. }
  61. }
  62. }
  63. FileNode file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true };
  64. file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]);
  65. file.Parent = parent;
  66. parent.Files.Add(file);
  67. }
  68. return folders;
  69. }
  70. }
  71. }