2023-09-21 21:05:38 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace AbazovViewComponents.Components
|
|
|
|
|
{
|
|
|
|
|
public partial class AbazovTreeView : UserControl
|
|
|
|
|
{
|
|
|
|
|
public AbazovTreeView()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
private List<(string, bool)> hierarchy = new List<(string, bool)>();
|
|
|
|
|
|
|
|
|
|
public int SelectedNodeIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return treeView.SelectedNode?.Index ?? -1;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (treeView.SelectedNode == null) treeView.SelectedNode = value >= 0 && value < treeView.Nodes.Count ? treeView.Nodes[value] : treeView.SelectedNode;
|
|
|
|
|
else treeView.SelectedNode = value >= 0 && value < treeView.SelectedNode.Nodes.Count ? treeView.SelectedNode.Nodes[value] : treeView.SelectedNode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 22:56:56 +04:00
|
|
|
|
public T? getSelecetedNodeValue<T>()
|
2023-09-21 21:05:38 +04:00
|
|
|
|
{
|
2023-09-21 22:56:56 +04:00
|
|
|
|
if (treeView.SelectedNode == null || treeView.SelectedNode.Nodes.Count > 0) return default(T);
|
|
|
|
|
|
|
|
|
|
TreeNode? node = treeView.SelectedNode;
|
|
|
|
|
|
|
|
|
|
var type = typeof(T);
|
2023-11-30 00:55:32 +04:00
|
|
|
|
var properties = type.GetProperties();
|
2023-09-25 20:52:43 +04:00
|
|
|
|
|
|
|
|
|
var item = Activator.CreateInstance(type);
|
2023-09-21 22:56:56 +04:00
|
|
|
|
|
|
|
|
|
while (node != null)
|
|
|
|
|
{
|
2023-11-30 00:55:32 +04:00
|
|
|
|
var property = properties.FirstOrDefault(x => x.Name == node.Name);
|
|
|
|
|
if (property != null)
|
2023-09-21 22:56:56 +04:00
|
|
|
|
{
|
2023-11-30 00:55:32 +04:00
|
|
|
|
property.SetValue(item, node.Text);
|
2023-09-21 22:56:56 +04:00
|
|
|
|
}
|
|
|
|
|
node = node.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 20:52:43 +04:00
|
|
|
|
return item != null ? (T) item : default(T);
|
2023-09-21 21:05:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setHierarchy(List<(string, bool)> fields)
|
|
|
|
|
{
|
|
|
|
|
hierarchy = fields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addItems<T>(List<T> items)
|
|
|
|
|
{
|
|
|
|
|
var type = typeof(T);
|
2023-11-30 00:55:32 +04:00
|
|
|
|
var properties = type.GetProperties();
|
2023-09-21 21:05:38 +04:00
|
|
|
|
foreach (T item in items)
|
|
|
|
|
{
|
|
|
|
|
TreeNodeCollection nodes = treeView.Nodes;
|
|
|
|
|
for (int i = 0; i < hierarchy.Count; i++)
|
|
|
|
|
{
|
2023-11-30 00:55:32 +04:00
|
|
|
|
var property = properties.FirstOrDefault(x => x.Name.Equals(hierarchy[i].Item1));
|
|
|
|
|
if (property is not null)
|
2023-09-21 21:05:38 +04:00
|
|
|
|
{
|
2023-11-30 00:55:32 +04:00
|
|
|
|
var node = nodes.Find(property.Name, false).FirstOrDefault(x => x.Text == property.GetValue(item).ToString());
|
2023-09-21 21:05:38 +04:00
|
|
|
|
if (node is not null && !hierarchy[i].Item2)
|
|
|
|
|
{
|
|
|
|
|
nodes = node.Nodes;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-30 00:55:32 +04:00
|
|
|
|
TreeNode newNode = nodes.Add(property.Name, property.GetValue(item).ToString());
|
2023-09-21 21:05:38 +04:00
|
|
|
|
nodes = newNode.Nodes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|