/*private void btProcessLoadedFindings_Click(object sender, EventArgs e)
         * {
         *  proccessLoadedFiles();
         *  cbShowRawData_CheckedChanged(null, null);
         *  cbShowSinksView_CheckedChanged(null, null);
         *  cbShowSourcesView_CheckedChanged(null, null);
         * }*/

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvAllTraces.SelectedNode != null)
            {
                TreeNode tnSelectedNode = tvAllTraces.SelectedNode;
                if (tnSelectedNode.Tag != null)
                {
                    var otdO2TraceBlock = (O2TraceBlock_OunceV6)tnSelectedNode.Tag;
                    DI.log.info(" Signature:{0}", otdO2TraceBlock.sSignature);
                    DI.log.info(" TraceRoot Text: {0}", otdO2TraceBlock.sTraceRootText);
                    DI.log.info(" Unique Name: {0}", otdO2TraceBlock.sUniqueName);
                }
                else
                {
                    TreeNode tnRootNode = O2Forms.getRootNode(tnSelectedNode);
                    DI.log.debug("Root Node:{0}", tnRootNode);
                    DI.log.debug("Selected Node:{0}", tnSelectedNode);
                    foreach (TreeNode tnTreeNode in tvRawData.Nodes)
                    {
                        if (tnTreeNode.Text == tnSelectedNode.Text)
                        {
                            tnSelectedNode.Nodes.Add((TreeNode)tnTreeNode.Clone());
                        }
                    }
                }
            }
        }
        public static bool addCompatibleTracesToNode_recursive(TreeNode tnTargetNode, FindingViewItem fviFindingViewItem,
                                                               O2TraceBlock_OunceV6 otbO2TraceBlockOunceV6OfToProcess,
                                                               String sMode,
                                                               Dictionary <String, O2TraceBlock_OunceV6> dRawData)
        //TreeView tvRawData)
        {
            //TreeNode tnParentNode = O2Forms.getRootNode(tnTargetNode);
            //int iNumberOfNodes = tnParentNode.GetNodeCount(true);
            var iNumberOfNodes = O2Forms.getRootNode(tnTargetNode).GetNodeCount(true);

            if (O2Forms.getRootNode(tnTargetNode).GetNodeCount(true) > 1500)
            {
                DI.log.info(String.Format("Max number of subnodes reached (250), aborting this root node: {0}",
                                          O2Forms.getRootNode(tnTargetNode).Text));
                return(false);
            }
            if (new StackTrace().FrameCount > 50)
            {
                DI.log.info(String.Format("Max StackTrace reached (50), aborting this leaf:{0}", tnTargetNode.Text));
                return(false);
            }

            var tnTreeFor_Root = new TreeNode();

            AnalysisUtils.addCallsToNode_Recursive(fviFindingViewItem.fFinding.Trace, tnTreeFor_Root,
                                                   fviFindingViewItem.oadO2AssessmentDataOunceV6,
                                                   Analysis.SmartTraceFilter.MethodName);
            if (sMode == "Sinks")
            {
                // first add the normal sinks
                foreach (AssessmentAssessmentFileFinding fFinding in otbO2TraceBlockOunceV6OfToProcess.dSinks.Keys)
                {
                    var tnTreeFor_ChildTrace            = new TreeNode();
                    var fviFindingViewItemForChildTrace = new FindingViewItem(fFinding,
                                                                              fFinding.vuln_name ?? OzasmtUtils_OunceV6.getStringIndexValue(
                                                                                  UInt32.Parse(
                                                                                      fFinding.vuln_name_id),
                                                                                  otbO2TraceBlockOunceV6OfToProcess.
                                                                                  dSinks[fFinding]), null,
                                                                              otbO2TraceBlockOunceV6OfToProcess.dSinks[fFinding
                                                                              ]);
                    AnalysisUtils.addCallsToNode_Recursive(fviFindingViewItemForChildTrace.fFinding.Trace,
                                                           tnTreeFor_ChildTrace,
                                                           fviFindingViewItemForChildTrace.oadO2AssessmentDataOunceV6,
                                                           Analysis.SmartTraceFilter.MethodName);

                    TreeNode tnRootNode_Sink = getTreeNodeOfTraceType_recursive(tnTreeFor_Root,
                                                                                TraceType.Known_Sink);
                    TreeNode tnRootNode_Source = getTreeNodeOfTraceType_recursive(tnTreeFor_ChildTrace,
                                                                                  TraceType.Source);


                    if (AreNodesCompatible(tnRootNode_Sink, tnRootNode_Source))
                    {
                        String sNodeText = getUniqueSignature(fFinding, TraceType.Known_Sink,
                                                              otbO2TraceBlockOunceV6OfToProcess.dSinks[fFinding], true);

                        // ensures we don't add the same source more that once per line (needs to be more optimized
                        List <String> ltnAllNodesAddedSofar = O2Forms.getStringListWithAllParentNodesText(tnTargetNode);
                        if (false == ltnAllNodesAddedSofar.Contains(sNodeText))
                        {
                            if (sNodeText != tnTargetNode.Text) // don't add if the child call is the same as the parent
                            {
                                tnTargetNode.Nodes.Add(O2Forms.newTreeNode(sNodeText, sNodeText, 0,
                                                                           fviFindingViewItemForChildTrace));
                            }
                            if (sNodeText == null)
                            {
                                return(false);
                            }
                        }
                    }
                }
                // then add the Glued Sinks

                foreach (AssessmentAssessmentFileFinding fFinding in otbO2TraceBlockOunceV6OfToProcess.dGluedSinks.Keys)
                {
                    var fviFindingViewItemForChildTrace = new FindingViewItem(fFinding,
                                                                              fFinding.vuln_name ?? OzasmtUtils_OunceV6.getStringIndexValue(
                                                                                  UInt32.Parse(
                                                                                      fFinding.vuln_name_id),
                                                                                  otbO2TraceBlockOunceV6OfToProcess.
                                                                                  dGluedSinks[fFinding]), null,
                                                                              otbO2TraceBlockOunceV6OfToProcess.dGluedSinks[
                                                                                  fFinding]);
                    String sNodeText = getUniqueSignature(fFinding, TraceType.Known_Sink,
                                                          otbO2TraceBlockOunceV6OfToProcess.dGluedSinks[fFinding], true);
                    tnTargetNode.Nodes.Add(O2Forms.newTreeNode(sNodeText, sNodeText, 0, fviFindingViewItemForChildTrace));
                }
            }
            foreach (TreeNode tnChildNode in tnTargetNode.Nodes)
            {
                //   int iNodeCount = tnChildNode.GetNodeCount(true);
                //    DI.log.info(iNodeCount + "   " + tnChildNode.Text);
                //if (tvRawData.Nodes[tnChildNode.Text] != null)
                if (dRawData.ContainsKey(tnChildNode.Text))
                {
                    // (now back to false) was true (check side effects)
                    if (false ==
                        addCompatibleTracesToNode_recursive(tnChildNode, (FindingViewItem)tnChildNode.Tag,
                                                            dRawData[tnChildNode.Text],
                                                            //                (O2TraceBlock_OunceV6) tvRawData.Nodes[tnChildNode.Text].Tag,
                                                            "Sinks", dRawData))
                    {
                        //tvRawData))
                        return(false);
                    }
                }
            }
            return(true);
        }