This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/Windows/Forms/BookmarkTreeViewSNode.cs

203 lines
5.0 KiB
C#

using bzit.bomg.Models;
using System;
using System.Windows.Forms;
using static RyzStudio.Windows.Forms.BookmarkTreeView;
namespace RyzStudio.Windows.Forms
{
public class BookmarkTreeViewSNode
{
protected BookmarkTreeView Treeview = null;
public BookmarkTreeViewSNode(BookmarkTreeView treeview)
{
Treeview = treeview;
}
protected TreeNode SelectedNode
{
get => this.Treeview.SelectedNode;
set => this.Treeview.SelectedNode = value;
}
public TreeNode AddFolder() => this.Treeview.AddFolder(this.SelectedNode);
public TreeNode AddFolder(string name) => this.Treeview.AddFolder(this.SelectedNode, name);
public TreeNode AddItem(string name, object tag)
{
if (this.SelectedNode == null)
{
return null;
}
if (this.SelectedNode.Tag != null)
{
return null;
}
TreeNode tn = new TreeNode(name, (int)BookmarkTreeView.IconSet.Default, (int)BookmarkTreeView.IconSet.Default);
tn.Tag = tag;
this.SelectedNode.Nodes.Add(tn);
return tn;
}
public void AddOrUpdateItem(BookmarkItemViewModel viewModel)
{
switch (this.GetNodeType())
{
case NodeType.Root:
case NodeType.Folder:
Treeview.AddItem(this.SelectedNode, viewModel);
break;
case NodeType.Page:
Treeview.UpdateItem(this.SelectedNode, viewModel);
break;
default:
break;
}
}
public void Delete()
{
if (this.SelectedNode == null)
{
return;
}
if (this.Treeview.Nodes.Count <= 0)
{
return;
}
if (this.SelectedNode.Equals(this.Treeview.Nodes[0]))
{
return;
}
this.SelectedNode.Remove();
}
public void Edit()
{
if (this.SelectedNode == null)
{
return;
}
if (this.SelectedNode.IsEditing)
{
return;
}
switch (this.GetNodeType())
{
case BookmarkTreeView.NodeType.Root:
case BookmarkTreeView.NodeType.Folder:
this.Treeview.AllowBeginEdit = true;
this.SelectedNode.BeginEdit();
break;
case BookmarkTreeView.NodeType.Page:
this.Treeview.AllowBeginEdit = true;
this.SelectedNode.BeginEdit();
break;
default:
break;
}
}
public bool FindTextNode(string term) => this.Treeview.FindTextNode(this.SelectedNode, term);
public BookmarkTreeView.NodeType GetNodeType() => this.Treeview.GetNodeType(this.SelectedNode);
public BookmarkItemViewModel GetViewModel() => this.Treeview.GetViewModel(this.SelectedNode);
public void MoveDown()
{
if (this.SelectedNode == null)
{
return;
}
TreeNode tn = this.SelectedNode;
if (tn.Parent == null)
{
return;
}
TreeNode tn1 = tn.Parent;
if (tn.Index >= (tn1.Nodes.Count - 1))
{
return;
}
int n = tn.Index + 1;
tn1.Nodes.Remove(tn);
tn1.Nodes.Insert(n, tn);
this.SelectedNode = tn;
}
public void MoveUp()
{
if (this.SelectedNode == null)
{
return;
}
TreeNode tn = this.SelectedNode;
if (tn.Parent == null)
{
return;
}
if (tn.Index <= 0)
{
return;
}
int n = tn.Index - 1;
TreeNode tn1 = tn.Parent;
tn1.Nodes.Remove(tn);
tn1.Nodes.Insert(n, tn);
this.SelectedNode = tn;
}
public void Sort()
{
if (this.SelectedNode == null)
{
return;
}
string[] tnv = new string[0];
TreeNode[] tna = new TreeNode[0];
foreach (TreeNode tn2 in this.SelectedNode.Nodes)
{
Array.Resize(ref tna, (tna.Length + 1));
tna[(tna.Length - 1)] = tn2;
Array.Resize(ref tnv, (tnv.Length + 1));
tnv[(tnv.Length - 1)] = tn2.Text;
}
Array.Sort(tnv, tna);
this.SelectedNode.Nodes.Clear();
foreach (TreeNode tn2 in tna)
{
this.SelectedNode.Nodes.Add(tn2);
}
}
}
}