/// <summary> /// /// </summary> /// <param name="node"></param> private void MoveUnorphanedNodeToParent(TreeModelNode node) { _evtRaiser.RaiseEvent("Node", "Moving unorphaned node ID:[" + node.NodeID + "] to parent node ID:[" + node.ParentNodeID + "]", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + node.NodeID + "; ParentNodeID: " + node.ParentNodeID), threadNodeID, threadParentNodeID); //System.Diagnostics.Debug.WriteLine("TreeModel: Moving unorphaned node ID:[" + node.NodeID + "] to parent node ID:[" + node.ParentNodeID + "]"); RemoveNodesRecursive(node); AddNodesRecursive(node); //_view.RemoveNode(node.NodeID); //_view.AddNode(node.NodeID, node.ParentNodeID, node.NodeText, node.IsRoot, false); }
private void RemoveNodesRecursive(TreeModelNode node) { //get node children List<TreeModelNode> childrenNodes = GetNodeChildren(node); //remove nodes at children foreach (TreeModelNode mNode in childrenNodes) { RemoveNodesRecursive(mNode); } //remove node _view.RemoveNode(node.NodeID); }
/// <summary> /// Get a list of children nodes for this node /// </summary> /// <param name="pNode"></param> /// <returns></returns> private List<TreeModelNode> GetNodeChildren(TreeModelNode pNode) { try { List<TreeModelNode> childrenNodes = new List<TreeModelNode>(); //System.Diagnostics.Debug.WriteLine("TreeModel: Scanning nodes for updates"); //AcquireInternalLock("ScanNodesForUpdates"); foreach (KeyValuePair<string, TreeModelNode> kvp in _nodes) { TreeModelNode node = kvp.Value; if (node.ParentNodeID == pNode.NodeID) { //this node is a child childrenNodes.Add(node); } } return childrenNodes; } catch (Exception ex) { logger.Error("Failed to get node childen for node [" + pNode.NodeID + "]: " + ex.Message); _evtRaiser.RaiseEvent("NodeError", "Failed to get node children for node [" + pNode.NodeID + "]: " + ex.Message, Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", ""), threadNodeID, threadParentNodeID); throw ex; } finally { //ReleaseInternalLock(); } }
private void AddNodesRecursive(TreeModelNode node) { //add node _view.AddNode(node.NodeID, node.ParentNodeID, node.NodeText, node.IsRoot, false); //get node children List<TreeModelNode> childrenNodes = GetNodeChildren(node); //add children node foreach (TreeModelNode mNode in childrenNodes) { AddNodesRecursive(mNode); } }
/// <summary> /// Selects the node by setting the current node, selecting the node in the tree view, and activating the node view /// </summary> /// <param name="nodeID"></param> public void SelectNode(string nodeID) { try { _evtRaiser.RaiseEvent("Node", "Selecting node ID:[" + nodeID + "]", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + nodeID), threadNodeID, threadParentNodeID); //System.Diagnostics.Debug.WriteLine("TreeModel: Selecting node ID:[" + nodeID + "]"); //AcquireInternalLock("SelectNode"); if (DoesNodeExist(nodeID)) { //get the selected node TreeModelNode node = GetNode(nodeID); //deactivate the current node view if (_currentNode != null) _currentNode.NodeView.DeactivateView(); //set the current node _currentNode = node; //select the node in the tree view _view.SelectNode(nodeID); //activate the node in the node view _currentNode.NodeView.ActivateView(); } else { //could not find node to select //System.Diagnostics.Debug.WriteLine("TreeModel: Could not find node [" + nodeID + "]"); _evtRaiser.RaiseEvent("NodeError", "Could not find node", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + nodeID), nodeID, threadParentNodeID); throw new InvalidOperationException("Could not find node [" + nodeID + "] in TreeModel"); } } catch (Exception ex) { logger.Error("Failed to select node [" + nodeID + "]: " + ex.Message); _evtRaiser.RaiseEvent("NodeError", "Failed to select node: " + ex.Message, Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + nodeID), nodeID, threadParentNodeID); throw ex; } finally { //ReleaseInternalLock(); } }
/// <summary> /// Add a node to the TreeModel and TreeModelView /// </summary> /// <param name="node"></param> public void AddNode(TreeModelNode node) { try { //System.Diagnostics.Debug.WriteLine("TreeModel: Adding node ID:[" + node.NodeID + "] Text:[" + node.NodeText + "]"); _evtRaiser.RaiseEvent("Node", "Adding node ID:[" + node.NodeID + "] Text:[" + node.NodeText + "]", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details","NodeID: " + node.NodeID + "; NodeText: " + node.NodeText), threadNodeID, threadParentNodeID); AcquireInternalLock("AddNode"); if (_nodes.ContainsKey(node.NodeID)) { //node already exists //System.Diagnostics.Debug.WriteLine("TreeModel: Node already exists"); _evtRaiser.RaiseEvent("NodeError", "Node already exists", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + node.NodeID), threadNodeID, threadParentNodeID); throw new InvalidOperationException("Node [" + node.NodeID + "] already exists in TreeModel"); } else { //add node _nodes.Add(node.NodeID, node); //add node the the view bool success = _view.AddNode(node.NodeID, node.ParentNodeID, node.NodeText, node.IsRoot, node.IsOrphaned); if (!success) { _evtRaiser.RaiseEvent("NodeError", "Add node operation failed in the TreeModelView implementation", Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + node.NodeID), threadNodeID, threadParentNodeID); throw new InvalidOperationException("Add node [" + node.NodeID + "] operation failed in the TreeModelView implementation"); } //scan nodes for changes to orphaned or disabled status ScanNodesForUpdates(); } } catch (Exception ex) { logger.Error("Failed to add node [" + node.NodeText + "]: " + ex.Message, ex); _evtRaiser.RaiseEvent("NodeError", "Failed to add node: " + ex.Message, Niawa.Utilities.InlineSortedListCreator.CreateStrStr("str:Details", "NodeID: " + node.NodeID), threadNodeID, threadParentNodeID); throw ex; } finally { ReleaseInternalLock(); } }
/// <summary> /// Refresh tree model with any changes present in the IpcEvent /// </summary> /// <param name="evt"></param> private void RefreshTreeModel(ITreeModelEvent evt) { try { _treeModel.AcquireLock("RefreshTreeModel"); //check if node exists if (_treeModel.DoesNodeExist(evt.NodeID)) { // check if node text has changed if (_treeModel.HasNodeTextChanged(evt.NodeID, evt.NodeText)) { TreeModelNode node = _treeModel.GetNode(evt.NodeID); logger.Info("Node [" + evt.NodeID + "] [" + node.NodeText + "] text changed to: " + evt.NodeText); //update text _treeModel.UpdateNodeText(evt.NodeID, evt.NodeText); } } else { //adding new node logger.Info("Adding node [" + evt.NodeID + "]: " + evt.NodeText); //create a new node view ITreeModelNodeView nodeView = _nodeViewFactory.CreateNodeView(_callerSessionID); bool isRoot = false; if (evt.ParentNodeID.Trim().Length == 0) isRoot = true; else isRoot = false; if (evt.NodeID.Trim().Length == 0) throw new InvalidOperationException("Failed to add node ID [" + evt.NodeID + "] text [" + evt.NodeText + "]: NodeID is a required field to create a tree model node."); //create a new node TreeModelNode node = new TreeModelNode(evt.NodeID, evt.NodeText, evt.ParentNodeID, isRoot, nodeView, _treeModel); //add _treeModel.AddNode(node); } } catch (Exception ex) { throw new InvalidOperationException("Failed to Refresh Tree Model: " + ex.Message); } finally { _treeModel.ReleaseLock(); } }