|
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.UI;
namespace DaveControls.HierarchicalControls.Data
{
public class HierarchyDataHelper
{
private const char NODE_CHAR = '.';
internal static HierarchyDataItemBase CreateDummyDataSource(string dataHierarchy)
{
string workingString = dataHierarchy;
HierarchyDataItemBase dataSource = new HierarchyDataItemBase();
HierarchyDataItemBase currentParent = dataSource;
for (int i = 0; i < dataHierarchy.Length; i++)
{
if (dataHierarchy[i] == NODE_CHAR)
{
HierarchyDataItemBase newNode = new HierarchyDataItemBase();
currentParent.Add(newNode);
}
else if (dataHierarchy[i] == '(')
{
// we're going down a level. Set the current parent to be the last node that was added
currentParent = currentParent[currentParent.Count - 1];
}
else if (dataHierarchy[i] == ')')
{
// we're going up a level. Set the current parent to be the parent of the last item that was added
if (currentParent.Parent == null)
{
// we are back to the root level again
currentParent = dataSource;
}
else
{
currentParent = currentParent.Parent;
}
}
}
return dataSource;
}
internal static string CreateDataHierarchyString(HierarchyDataItemBase dataSource)
{
IEnumerator<HierarchyDataItemBase> dataEnum = (IEnumerator<HierarchyDataItemBase>)dataSource.GetEnumerator();
StringBuilder sb = new StringBuilder();
while (dataEnum.MoveNext())
{
HierarchyDataItemBase data = dataEnum.Current;
sb.Append(NODE_CHAR);
if (data.HasChildren)
{
sb.Append("(");
sb.Append(CreateDataHierarchyString(data));
sb.Append(")");
}
}
return sb.ToString();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.