2022-09-19 09:16:33 +08:00
|
|
|
|
using System.Collections.Generic;
|
2022-12-22 16:32:18 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
using System.Xml;
|
|
|
|
|
using Caliburn.Micro.Core;
|
|
|
|
|
|
|
|
|
|
namespace MECF.Framework.UI.Client.CenterViews.Editors.Sequence
|
|
|
|
|
{
|
|
|
|
|
public class FileNode : PropertyChangedBase
|
|
|
|
|
{
|
|
|
|
|
public FileNode()
|
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
this.Files = new ObservableCollection<FileNode>();
|
2022-09-19 09:16:33 +08:00
|
|
|
|
this.IsFile = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:48:08 +08:00
|
|
|
|
private string _name = string.Empty;
|
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
2022-12-19 11:48:08 +08:00
|
|
|
|
get => _name;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_name = value;
|
|
|
|
|
NotifyOfPropertyChange();
|
|
|
|
|
}
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public string FullPath { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public FileNode Parent { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-12-22 16:32:18 +08:00
|
|
|
|
public ObservableCollection<FileNode> Files { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public bool IsFile { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public string PrefixPath { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public bool IsSelected { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public bool IsExpanded { get; set; }
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
if (Parent != null)
|
|
|
|
|
return $"{Parent}{Name}{(IsFile ? "" : "\\")}";
|
|
|
|
|
else
|
|
|
|
|
return $"{Name}{(IsFile ? "" : "\\")}"; ;
|
|
|
|
|
}
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
2022-12-19 11:48:08 +08:00
|
|
|
|
|
2022-09-19 09:16:33 +08:00
|
|
|
|
public class RecipeSequenceTreeBuilder
|
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
public static ObservableCollection<FileNode> GetFiles(string prefixPath, List<string> filenames)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var folders = new ObservableCollection<FileNode>();
|
|
|
|
|
var root = new FileNode() { Name = "Files", IsFile = false, FullPath="", PrefixPath = prefixPath};
|
2022-09-19 09:16:33 +08:00
|
|
|
|
folders.Add(root);
|
|
|
|
|
|
2022-12-22 16:32:18 +08:00
|
|
|
|
foreach(var filename in filenames)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var filesplits = filename.Split('\\');
|
2022-09-19 09:16:33 +08:00
|
|
|
|
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var parent = root;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
if (filesplits.Length>1)
|
|
|
|
|
{
|
|
|
|
|
for(var index =0;index< filesplits.Length-1; index++)
|
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var found = false;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
for(var j = 0; j < parent.Files.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
if(parent.Files[j].Name == filesplits[index] && !parent.Files[j].IsFile)
|
|
|
|
|
{
|
|
|
|
|
found = true;
|
|
|
|
|
parent = parent.Files[j];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!found)
|
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var folder = new FileNode() { Name = filesplits[index], IsFile = false, PrefixPath = prefixPath };
|
2022-09-19 09:16:33 +08:00
|
|
|
|
folder.FullPath = (parent.FullPath == string.Empty ? filesplits[index] : parent.FullPath + "\\" + filesplits[index]);
|
|
|
|
|
folder.Parent = parent;
|
|
|
|
|
parent.Files.Add(folder);
|
|
|
|
|
parent = folder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var file = new FileNode() { Name = filesplits[filesplits.Length - 1], IsFile = true, PrefixPath = prefixPath };
|
2022-09-19 09:16:33 +08:00
|
|
|
|
file.FullPath = (parent.FullPath == string.Empty ? filesplits[filesplits.Length - 1] : parent.FullPath + "\\" + filesplits[filesplits.Length - 1]);
|
|
|
|
|
file.Parent = parent;
|
|
|
|
|
parent.Files.Add(file);
|
|
|
|
|
}
|
|
|
|
|
return folders;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:48:08 +08:00
|
|
|
|
public static void CreateTreeViewItems(XmlElement curElementNode, FileNode parent, string prefixPath, string selectionName, bool selectionIsFolder, bool isFolderNodeOnly)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (XmlElement ele in curElementNode.ChildNodes)
|
|
|
|
|
{
|
2022-12-19 11:48:08 +08:00
|
|
|
|
if (ele.Name == "File" && !isFolderNodeOnly)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var fileName = ele.Attributes["Name"].Value;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var fullPath = string.IsNullOrEmpty(parent.FullPath)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
? fileName
|
|
|
|
|
: parent.FullPath + "\\" + fileName;
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var file = new FileNode()
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
|
|
|
|
Name = fileName,
|
|
|
|
|
IsFile = true,
|
|
|
|
|
PrefixPath = prefixPath,
|
|
|
|
|
Parent = parent,
|
|
|
|
|
FullPath = fullPath,
|
|
|
|
|
};
|
|
|
|
|
if (!selectionIsFolder && selectionName == file.FullPath)
|
|
|
|
|
{
|
|
|
|
|
file.IsSelected = true;
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var node = file.Parent;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
while (node.FullPath.Contains("\\"))
|
|
|
|
|
{
|
|
|
|
|
node.IsExpanded = true;
|
|
|
|
|
node = node.Parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
parent.Files.Add(file);
|
|
|
|
|
}
|
|
|
|
|
else if (ele.Name == "Folder")
|
|
|
|
|
{
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var folderName = ele.Attributes["Name"].Value;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
folderName = folderName.Substring(folderName.LastIndexOf('\\') + 1);
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var fullPath = string.IsNullOrEmpty(parent.FullPath)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
? folderName
|
|
|
|
|
: parent.FullPath + "\\" + folderName;
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var folder = new FileNode()
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
|
|
|
|
Name = folderName,
|
|
|
|
|
IsFile = false,
|
|
|
|
|
PrefixPath = prefixPath,
|
|
|
|
|
Parent = parent,
|
|
|
|
|
FullPath = fullPath,
|
|
|
|
|
IsExpanded = !fullPath.Contains("\\"),
|
|
|
|
|
};
|
|
|
|
|
parent.Files.Add(folder);
|
|
|
|
|
if (selectionIsFolder && selectionName == folder.FullPath)
|
|
|
|
|
{
|
|
|
|
|
folder.IsSelected = true;
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var node = folder;
|
2022-09-19 09:16:33 +08:00
|
|
|
|
while (node.FullPath.Contains("\\"))
|
|
|
|
|
{
|
|
|
|
|
node.IsExpanded = true;
|
|
|
|
|
node = node.Parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-19 11:48:08 +08:00
|
|
|
|
CreateTreeViewItems(ele, folder, prefixPath, selectionName, selectionIsFolder, isFolderNodeOnly);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 11:48:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建Recipe文件树。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="prefixPath"></param>
|
|
|
|
|
/// <param name="selectionName"></param>
|
|
|
|
|
/// <param name="selectionIsFolder"></param>
|
|
|
|
|
/// <param name="fileListInXml"></param>
|
|
|
|
|
/// <param name="isFolderNodeOnly">是否仅显示文件夹</param>
|
|
|
|
|
/// <param name="isShowRoot">是否显示根节点</param>
|
|
|
|
|
/// <returns></returns>
|
2022-12-22 16:32:18 +08:00
|
|
|
|
public static ObservableCollection<FileNode> BuildFileNode(string prefixPath, string selectionName, bool selectionIsFolder, string fileListInXml, bool isFolderNodeOnly = false, bool isShowRoot = false)
|
2022-09-19 09:16:33 +08:00
|
|
|
|
{
|
2022-12-22 16:32:18 +08:00
|
|
|
|
var folders = new ObservableCollection<FileNode>();
|
2022-12-19 11:48:08 +08:00
|
|
|
|
var root = new FileNode() { Name = "Files", IsFile = false, FullPath = "", PrefixPath = prefixPath };
|
2022-09-19 09:16:33 +08:00
|
|
|
|
folders.Add(root);
|
|
|
|
|
|
2022-12-19 11:48:08 +08:00
|
|
|
|
// 是否显示根目录节点
|
|
|
|
|
if (isShowRoot)
|
|
|
|
|
{
|
|
|
|
|
root.Files.Add(new FileNode()
|
|
|
|
|
{ Name = "\\", IsExpanded = true, PrefixPath = prefixPath, IsFile = false, FullPath = "", });
|
|
|
|
|
|
|
|
|
|
root = root.Files[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var doc = new XmlDocument();
|
2022-09-19 09:16:33 +08:00
|
|
|
|
doc.LoadXml(fileListInXml);
|
2022-12-19 11:48:08 +08:00
|
|
|
|
CreateTreeViewItems(doc.DocumentElement, root, prefixPath, selectionName, selectionIsFolder, isFolderNodeOnly);
|
2022-09-19 09:16:33 +08:00
|
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|