using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
using IReaper.Category;
namespace IReaper.CourseData{
/// <summary>
/// TagCoursesCollection
/// </summary>
public class WebcastCourseTreeNode<T> : TreeNode, IWebcastNode<T>
{
private IVector<T> vector;
private TreeNode selectedNode = null;
static NodeSorter ascendingSortoer = new NodeSorter(SortOrder.Ascending);
static NodeSorter descendingSortoer = new NodeSorter(SortOrder.Descending);
public void SetVector(IVector<T> vector)
{
this.vector = vector;
vector.SetWebcastNode(this);
}
/// <summary>
///
/// </summary>
public TreeNode SelectedNode
{
get { return selectedNode; }
set { selectedNode = value; }
}
/// <summary>
/// Course
/// </summary>
/// <param name="Data"></param>
public void AddData(CourseAttributeTypeEnum NodeType, object obj, BindingList<T> Data)
{
//
TreeNode node = this.Nodes[NodeType.ToString()];
TreeView treeview = node.TreeView;
if (treeview != null && treeview.InvokeRequired)
{
treeview.BeginInvoke(new System.Threading.ThreadStart(delegate()
{
this.AddData(NodeType, obj, Data);
}), null);
return;
}
string message = null;
if (obj is DateTime)
{
DateTime time = (DateTime)obj;
message = time.ToString("y");
}
else
message = obj.ToString();
//
if (!node.Nodes.ContainsKey(message))
{
WebcastSeriesTreeNode newNode = new WebcastSeriesTreeNode(NodeType);
newNode.Text = message;
newNode.Name = message;
newNode.Tag = Data;
newNode.ImageKey = node.ImageKey;
//node
//if (node.Nodes.Count == 0)
//{
// node.Nodes.Add(newNode);
//}
//else
//{
for (int i = 0; i < node.Nodes.Count; i++)
{
TreeNode current = node.Nodes[i];
if (ascendingSortoer.Compare(current, newNode) > 0)
continue;
else
{
node.Nodes.Insert(i, newNode);
return;
}
}
//}
node.Nodes.Add(newNode);
}
}
/// <summary>
/// Course
/// </summary>
/// <param name="NodeType">"Teacher","Series"</param>
/// <param name="obj"></param>
/// <param name="Data"></param>
public void RemoveData(CourseAttributeTypeEnum NodeType, object obj)
{
TreeNode node = this.Nodes[NodeType.ToString()];
string message = null;
if (obj is DateTime)
{
DateTime time = (DateTime)obj;
message = time.ToString("yyyy.M");
}
else
message = obj.ToString();
if (node.Nodes.ContainsKey(message))
{
TreeNode oldNode = node.Nodes[message];
ICollection tag = oldNode.Tag as ICollection;
if (tag != null && tag.Count == 0)
{
oldNode.Tag = null;
node.Nodes.Remove(oldNode);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="Data"></param>
public void SetRoot(BindingList<T> Data)
{
this.Tag = Data;
}
}
public class NodeSorter : IComparer<TreeNode>
{
private SortOrder order;
// Stores the sort order.
public NodeSorter(SortOrder theOrder)
{
order = theOrder;
}
// Compare the length of the strings, or the strings
// themselves, if they are the same length.
public int Compare(TreeNode tx, TreeNode ty)
{
// If they are the same length, call Compare.
int result = string.Compare(ty.Text, tx.Text);
if (order == SortOrder.Ascending)
{ return -result; }
else
{ return +result; }
}
}
}
|