98 lines
2.8 KiB
C#
98 lines
2.8 KiB
C#
using S7Explorer.Models;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
|
|
namespace S7Explorer.Helpers
|
|
{
|
|
public static class TreeViewHelper
|
|
{
|
|
public static ProjectItem? FindItemByPath(ObservableCollection<ProjectItem> items, string path)
|
|
{
|
|
if (string.IsNullOrEmpty(path))
|
|
return null;
|
|
|
|
string[] parts = path.Split('/');
|
|
return FindItemByParts(items, parts, 0);
|
|
}
|
|
|
|
private static ProjectItem? FindItemByParts(IEnumerable<ProjectItem> items, string[] parts, int level)
|
|
{
|
|
if (level >= parts.Length)
|
|
return null;
|
|
|
|
var matchingItem = items.FirstOrDefault(i => i.Name == parts[level]);
|
|
if (matchingItem == null)
|
|
return null;
|
|
|
|
if (level == parts.Length - 1)
|
|
return matchingItem;
|
|
|
|
return FindItemByParts(matchingItem.Children, parts, level + 1);
|
|
}
|
|
|
|
public static ProjectItem? FindItemByName(ObservableCollection<ProjectItem> items, string name)
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
if (item.Name == name)
|
|
return item;
|
|
|
|
var found = FindItemByName(item.Children, name);
|
|
if (found != null)
|
|
return found;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static List<ProjectItem> FindItemsByText(ObservableCollection<ProjectItem> items, string text)
|
|
{
|
|
List<ProjectItem> results = new List<ProjectItem>();
|
|
FindItemsByTextRecursive(items, text.ToLower(), results);
|
|
return results;
|
|
}
|
|
|
|
private static void FindItemsByTextRecursive(IEnumerable<ProjectItem> items, string text, List<ProjectItem> results)
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
if (item.Name.ToLower().Contains(text))
|
|
results.Add(item);
|
|
|
|
FindItemsByTextRecursive(item.Children, text, results);
|
|
}
|
|
}
|
|
|
|
public static void ExpandToItem(ProjectItem item)
|
|
{
|
|
// Expand all parent items to make the item visible
|
|
ProjectItem? current = item.Parent;
|
|
while (current != null)
|
|
{
|
|
current.IsExpanded = true;
|
|
current = current.Parent;
|
|
}
|
|
}
|
|
|
|
public static void ExpandAll(ProjectItem item)
|
|
{
|
|
item.IsExpanded = true;
|
|
|
|
foreach (var child in item.Children)
|
|
{
|
|
ExpandAll(child);
|
|
}
|
|
}
|
|
|
|
public static void CollapseAll(ProjectItem item)
|
|
{
|
|
item.IsExpanded = false;
|
|
|
|
foreach (var child in item.Children)
|
|
{
|
|
CollapseAll(child);
|
|
}
|
|
}
|
|
}
|
|
} |