using System; using System.Collections; using System.ComponentModel; namespace DevComponents.Tree { /// /// Represents collection for Node objects. /// public class NodeCollection:CollectionBase { #region Private Variables private Node m_ParentNode=null; private TreeGX m_TreeControl=null; private eTreeAction m_SourceAction=eTreeAction.Code; #endregion #region Internal Implementation public NodeCollection() { } /// /// Gets or sets the node this collection is associated with. /// [Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public Node ParentNode { get {return m_ParentNode;} } /// /// Sets the node collection belongs to. /// /// Node that is parent of this collection. internal void SetParentNode(Node parent) { m_ParentNode=parent; } internal TreeGX TreeControl { get {return m_TreeControl;} set {m_TreeControl=value;} } /// /// Adds new object to the collection. /// /// Object to add. /// Index of newly added object. public int Add(Node node) { return List.Add(node); } /// /// Adds new object to the collection and provides information about the source of the command /// /// Node to add /// Source action /// public int Add(Node node, eTreeAction action) { m_SourceAction = action; return List.Add(node); } /// /// Adds an array of objects to the collection. /// /// Array of Node objects. public void AddRange(Node[] nodes) { foreach(Node node in nodes) List.Add(node); } /// /// Returns reference to the object in collection based on it's index. /// public Node this[int index] { get {return (Node)(List[index]);} set {List[index] = value;} } /// /// Inserts new object into the collection. /// /// Position of the object. /// Object to insert. public void Insert(int index, Node value) { List.Insert(index, value); } /// /// Inserts new object into the collection. /// /// Position of the object. /// Object to insert. /// Action that is causing the event public void Insert(int index, Node value, eTreeAction action) { m_SourceAction = action; List.Insert(index, value); } /// /// Returns index of the object inside of the collection. /// /// Reference to the object. /// Index of the object. public int IndexOf(Node value) { return List.IndexOf(value); } /// /// Returns whether collection contains specified object. /// /// Object to look for. /// true if object is part of the collection, otherwise false. public bool Contains(Node value) { return List.Contains(value); } /// /// Removes specified object from the collection. /// /// public void Remove(Node value) { List.Remove(value); } /// /// Removes specified object from the collection and provides information about source of the command /// /// Node to remove /// Source action public void Remove(Node node, eTreeAction action) { m_SourceAction = action; List.Remove(node); } protected override void OnRemove(int index, object value) { TreeGX tree = GetTreeControl(); if(tree!=null) tree.InvokeBeforeNodeRemove(m_SourceAction, value as Node, m_ParentNode); base.OnRemove (index, value); } protected override void OnRemoveComplete(int index,object value) { base.OnRemoveComplete(index,value); Node node=value as Node; node.SetParent(null); node.internalTreeControl=null; if(m_ParentNode!=null) m_ParentNode.OnChildNodeRemoved(node); TreeGX tree = GetTreeControl(); if(tree!=null) { tree.InvokeAfterNodeRemove(m_SourceAction, value as Node, m_ParentNode); tree.NodeRemoved(m_SourceAction, value as Node, m_ParentNode, index); } } protected override void OnInsert(int index, object value) { TreeGX tree = GetTreeControl(); if(tree!=null) tree.InvokeBeforeNodeInsert(m_SourceAction, value as Node, m_ParentNode); base.OnInsert (index, value); } protected override void OnInsertComplete(int index,object value) { base.OnInsertComplete(index,value); Node node=value as Node; if(m_ParentNode!=null) { if(node.Parent!=null && node.Parent!=m_ParentNode) node.Remove(); node.SetParent(m_ParentNode); } node.internalTreeControl=m_TreeControl; if(m_ParentNode!=null) m_ParentNode.OnChildNodeInserted(node); TreeGX tree = GetTreeControl(); if(tree!=null) tree.InvokeAfterNodeInsert(m_SourceAction, value as Node, m_ParentNode); m_SourceAction = eTreeAction.Code; } /// /// Copies collection into the specified array. /// /// Array to copy collection to. /// Starting index. public void CopyTo(Node[] array, int index) { List.CopyTo(array, index); } /// /// Copies contained items to the Node array. /// /// Array to copy to. internal void CopyTo(Node[] array) { List.CopyTo(array,0); } protected override void OnClear() { foreach(Node node in this.List) { node.SetParent(null); node.internalTreeControl=null; } base.OnClear(); } protected override void OnClearComplete() { if (m_TreeControl != null && !PassiveCollection) { m_TreeControl.OnNodesCleared(); } else if (m_ParentNode != null && !PassiveCollection) m_ParentNode.OnNodesCleared(); TreeGX tree = GetTreeControl(); if (tree != null) { tree.ValidateSelectedNode(); } base.OnClearComplete(); } private TreeGX GetTreeControl() { if(m_TreeControl!=null) return m_TreeControl; if(m_ParentNode!=null) return m_ParentNode.TreeControl; return null; } private bool _PassiveCollection = false; internal bool PassiveCollection { get { return _PassiveCollection; } set { _PassiveCollection = value; } } #endregion } #region NodeCollectionEditor /// /// Support for Node tabs design-time editor. /// public class NodeCollectionEditor:System.ComponentModel.Design.CollectionEditor { public NodeCollectionEditor(Type type):base(type) { } protected override Type CreateCollectionItemType() { return typeof(Node); } protected override Type[] CreateNewItemTypes() { return new Type[] {typeof(Node)}; } protected override object CreateInstance(Type itemType) { object item=base.CreateInstance(itemType); if(item is Node) { Node node=item as Node; node.Text=node.Name; } return item; } } #endregion }