118 lines
3.4 KiB
C#
118 lines
3.4 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Reflection;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using PutincevLibrary.Exceptions;
|
|||
|
using PutincevLibrary.Attributes;
|
|||
|
|
|||
|
namespace PutincevLibrary
|
|||
|
{
|
|||
|
public partial class TreeViewControl : UserControl
|
|||
|
{
|
|||
|
public TreeViewControl()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
public 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;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public T? getSelecetedNodeValue<T>()
|
|||
|
where T : class, new()
|
|||
|
{
|
|||
|
var node = treeView.SelectedNode;
|
|||
|
|
|||
|
if (node == null)
|
|||
|
{
|
|||
|
throw new NotSelectedNodeException();
|
|||
|
}
|
|||
|
|
|||
|
if ((hierarchy?.Count ?? 0) == 0)
|
|||
|
{
|
|||
|
throw new TreeHierarchyNotSetException();
|
|||
|
}
|
|||
|
|
|||
|
if (node.Nodes.Count > 0)
|
|||
|
{
|
|||
|
throw new NodeIsNotLeafException();
|
|||
|
}
|
|||
|
|
|||
|
T obj = new T();
|
|||
|
int propIndex = GetNodeDepth(node);
|
|||
|
while (node != null)
|
|||
|
{
|
|||
|
string propValue = node.Text;
|
|||
|
string propName = hierarchy[propIndex].Item1;
|
|||
|
|
|||
|
var prop = obj.GetType().GetProperty(propName);
|
|||
|
if (prop == null)
|
|||
|
{
|
|||
|
throw new PropertyNotDeclaratedException(propName);
|
|||
|
}
|
|||
|
|
|||
|
prop.SetValue(obj, propValue);
|
|||
|
|
|||
|
node = node.Parent;
|
|||
|
propIndex--;
|
|||
|
}
|
|||
|
|
|||
|
return obj;
|
|||
|
}
|
|||
|
private int GetNodeDepth(TreeNode node)
|
|||
|
{
|
|||
|
int depth = 0;
|
|||
|
while (node.Parent != null)
|
|||
|
{
|
|||
|
depth++;
|
|||
|
node = node.Parent;
|
|||
|
}
|
|||
|
return depth;
|
|||
|
}
|
|||
|
|
|||
|
public void addItems<T>(List<T> items)
|
|||
|
{
|
|||
|
var type = typeof(T);
|
|||
|
var fields = type.GetFields();
|
|||
|
foreach (T item in items)
|
|||
|
{
|
|||
|
TreeNodeCollection nodes = treeView.Nodes;
|
|||
|
for (int i = 0; i < hierarchy.Count; i++)
|
|||
|
{
|
|||
|
var field = fields.FirstOrDefault(x => x.Name.Equals(hierarchy[i].Item1));
|
|||
|
if (field is not null)
|
|||
|
{
|
|||
|
var node = nodes.Find(field.Name, false).FirstOrDefault(x => x.Text == field.GetValue(item).ToString());
|
|||
|
if (node is not null && !hierarchy[i].Item2)
|
|||
|
{
|
|||
|
nodes = node.Nodes;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
TreeNode newNode = nodes.Add(field.Name, field.GetValue(item).ToString());
|
|||
|
nodes = newNode.Nodes;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|