{\r
public class ItemTreeView : TreeView\r
{\r
- private ItemStatus[] _prevItemList;\r
-\r
public void SetNodes(ItemStatus[] itemList)\r
{\r
- if (_prevItemList != null && _prevItemList.SequenceEqual(itemList, new ItemStatusComparer()))\r
- return;\r
- _prevItemList = itemList.Select(CloneItemStatus).ToArray();\r
SetNodes(CreateItemNodes(itemList));\r
}\r
\r
return root;\r
}\r
\r
- private ItemStatus CloneItemStatus(ItemStatus org)\r
- {\r
- return new ItemStatus\r
- {\r
- Level = org.Level,\r
- Spec = org.Spec,\r
- Holder = new ShipStatus {Id = org.Holder.Id, Fleet = org.Holder.Fleet}\r
- };\r
- }\r
-\r
- private class ItemStatusComparer : IEqualityComparer<ItemStatus>\r
- {\r
- public bool Equals(ItemStatus x, ItemStatus y)\r
- // ReSharper disable PossibleNullReferenceException\r
- => x.Level == y.Level && x.Spec == y.Spec && x.Holder.Id == y.Holder.Id &&\r
- x.Holder.Fleet == y.Holder.Fleet;\r
- // ReSharper restore PossibleNullReferenceException\r
-\r
- public int GetHashCode(ItemStatus obj) => obj.Level + obj.Spec.GetHashCode() + obj.Holder.GetHashCode();\r
- }\r
-\r
- [DllImport("user32.dll")]\r
- private static extern int GetScrollPos(IntPtr hWnd, int nBar);\r
-\r
- [DllImport("user32.dll")]\r
- private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);\r
-\r
private void SetNodes(TreeNode root)\r
{\r
- var y = GetScrollPos(Handle, 1);\r
- BeginUpdate();\r
var save = SaveTreeViewState(Nodes);\r
- Nodes.Clear();\r
- foreach (TreeNode child in root.Nodes)\r
- Nodes.Add(child);\r
+ UpdateNodes(Nodes, root.Nodes);\r
RestoreTreeViewState(Nodes, save.Nodes);\r
- EndUpdate();\r
- SetScrollPos(Handle, 1, y, true);\r
}\r
\r
private TreeNode SaveTreeViewState(IEnumerable nodes)\r
}\r
}\r
\r
+ private void UpdateNodes(TreeNodeCollection prev, TreeNodeCollection now)\r
+ {\r
+ for (var i = 0; i < now.Count; i++)\r
+ {\r
+ if (prev.Count <= i)\r
+ {\r
+ prev.Add(now[i]);\r
+ continue;\r
+ }\r
+ if (prev.Count > i && prev[i].Name == now[i].Name)\r
+ {\r
+ UpdateNodes(prev[i].Nodes, now[i].Nodes);\r
+ continue;\r
+ }\r
+ prev.RemoveAt(i);\r
+ prev.Insert(i, now[i]);\r
+ }\r
+ }\r
+\r
// ReSharper disable InconsistentNaming\r
// ReSharper disable IdentifierTypo\r
\r