using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace JGTreeView
{
public class JTreeViewBase : System.Windows.Forms.TreeView
{
public delegate void MessageSenderEventHandler(object sender, string Message, bool Error);
public event MessageSenderEventHandler OnMessageSender;
public void RaiseMessageSenderEventHandler(object sender, string Message, bool Error)
{
if (OnMessageSender != null)
{
OnMessageSender(sender, Message, Error);
}
}
public JTreeViewBase()
: base()
{
this.HideSelection = false;
}
private TreeNode DoGetNodeByName(string nodeName, bool searchAllChildren)
{
TreeNode[] tnFound = this.Nodes.Find(nodeName, searchAllChildren);
if (tnFound.Length > 0)
return tnFound[0];
return null;
}
private TreeNode DoGetNodeByName(TreeNode tnStart, string nodeName, bool searchAllChildren)
{
if (tnStart != null)
{
tnStart.Collapse();
tnStart.Expand();
TreeNode[] tnFound = tnStart.Nodes.Find(nodeName, searchAllChildren);
if (tnFound.Length > 0)
return tnFound[0];
}
return null;
}
private TreeNode DoSetNodeToName(string nodeName, bool isFilePath, bool searchAllChildren)
{
if (nodeName == "")
return null;
this.BeginUpdate();
TreeNode tnFind = null;
string searchPath = "";
// Let's see if it already exists
TreeNode[] tnArray = this.Nodes.Find(nodeName, true);
if (tnArray.Length > 0)
tnFind = tnArray[0];
if (tnFind != null)
{
this.SelectedNode = tnFind;
tnFind.Collapse();
tnFind.Expand();
}
else
{
string[] nameArray = nodeName.Split(Path.DirectorySeparatorChar);
foreach (string current in nameArray)
{
if (current != "")
{
searchPath = Path.Combine(searchPath, current);
if (isFilePath && searchPath.Length == 2)
searchPath += Path.DirectorySeparatorChar.ToString();
tnFind = (TreeNode)GetNodeByName(searchPath, true);
if (tnFind != null)
{
this.SelectedNode = tnFind;
tnFind.Collapse();
tnFind.Expand();
}
}
}
}
this.EndUpdate();
return tnFind;
}
private TreeNode DoSetNodeToName(TreeNode tnStart, string nodeName, bool isFilePath, bool searchAllChildren)
{
if (nodeName == "")
return null;
this.BeginUpdate();
TreeNode tnFind = null;
string searchPath = "";
// Let's see if it already exists
TreeNode[] tnArray = this.Nodes.Find(nodeName, true);
if (tnArray.Length > 0)
tnFind = tnArray[0];
if (tnFind != null)
{
this.SelectedNode = tnFind;
tnFind.Collapse();
tnFind.Expand();
}
else
{
string[] nameArray = nodeName.Split(Path.DirectorySeparatorChar);
foreach (string current in nameArray)
{
if (current != "")
{
searchPath = Path.Combine(searchPath, current);
if (isFilePath && searchPath.Length == 2)
searchPath += Path.DirectorySeparatorChar.ToString();
tnFind = (TreeNode)GetNodeByName(tnStart, searchPath, searchAllChildren);
if (tnFind != null)
{
this.SelectedNode = tnFind;
tnFind.Collapse();
tnFind.Expand();
}
}
}
}
this.EndUpdate();
return tnFind;
}
/// <summary>
/// Searches the complete TreeView for a TreeNode with the Name 'name'
/// </summary>
/// <param name="name">The Name of the TreeNode to search for</param>
/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
/// <returns>The requested node or null if not found</returns>
public TreeNode GetNodeByName(string name, bool searchAllChildren)
{
return DoGetNodeByName(name, searchAllChildren);
}
/// <summary>
/// Searches the TreeNode for a child TreeNode with the Name 'name'
/// </summary>
/// <param name="tnStart">Starting TreeNode for the search</param>
/// <param name="name">The Name of the TreeNode to search for</param>
/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
/// <returns>The requested node or null if not found</returns>
public TreeNode GetNodeByName(TreeNode tnStart, string name, bool searchAllChildren)
{
return DoGetNodeByName(tnStart, name, searchAllChildren);
}
/// <summary>
/// Searches for the TreeNode with the Name 'name' and selects it if found
/// </summary>
/// <param name="name">The Name of the TreeNode to search for</param>
/// <param name="isFilePath">Flag to indicate if it is a file path so that drive paths can be adjusted</param>
/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
/// <returns>The requested node or null if not found</returns>
public TreeNode SetNodeToName(string name, bool isFilePath, bool searchAllChildren)
{
return DoSetNodeToName(name, isFilePath, searchAllChildren);
}
/// <summary>
/// Searches for the TreeNode with the Name 'name' and selects it if found
/// </summary>
/// <param name="tnStart">Starting TreeNode for the search</param>
/// <param name="name">The Name of the TreeNode to search for</param>
/// <param name="isFilePath">Flag to indicate if it is a file path so that drive paths can be adjusted</param>
/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
/// <returns>The requested node or null if not found</returns>
public TreeNode SetNodeToName(TreeNode tnStart, string name, bool isFilePath, bool searchAllChildren)
{
return DoSetNodeToName(tnStart, name, isFilePath, searchAllChildren);
}
}
}