private Map<ThreadObjectGCRoot, Map<Integer, List<JavaFrameGCRoot>>> computeJavaFrameMap(
      Collection<GCRoot> roots) {
    Map<ThreadObjectGCRoot, Map<Integer, List<JavaFrameGCRoot>>> javaFrameMap = new HashMap();

    for (GCRoot root : roots) {
      if (GCRoot.JAVA_FRAME.equals(root.getKind())) {
        JavaFrameGCRoot frameGCroot = (JavaFrameGCRoot) root;
        ThreadObjectGCRoot threadObj = frameGCroot.getThreadGCRoot();
        Integer frameNo = Integer.valueOf(frameGCroot.getFrameNumber());
        Map<Integer, List<JavaFrameGCRoot>> stackMap = javaFrameMap.get(threadObj);
        List<JavaFrameGCRoot> locals;

        if (stackMap == null) {
          stackMap = new HashMap();
          javaFrameMap.put(threadObj, stackMap);
        }
        locals = stackMap.get(frameNo);
        if (locals == null) {
          locals = new ArrayList(2);
          stackMap.put(frameNo, locals);
        }
        locals.add(frameGCroot);
      }
    }
    return javaFrameMap;
  }
  private ThreadObjectGCRoot getOOMEThread(Heap heap) {
    Collection<GCRoot> roots = heap.getGCRoots();

    for (GCRoot root : roots) {
      if (root.getKind().equals(GCRoot.THREAD_OBJECT)) {
        ThreadObjectGCRoot threadRoot = (ThreadObjectGCRoot) root;
        StackTraceElement[] stackTrace = threadRoot.getStackTrace();

        if (stackTrace != null && stackTrace.length >= 1) {
          StackTraceElement ste = stackTrace[0];

          if (OutOfMemoryError.class.getName().equals(ste.getClassName())
              && "<init>".equals(ste.getMethodName())) {
            return threadRoot;
          }
        }
      }
    }
    return null;
  }
  private synchronized String getStackTrace() {
    if (stackTrace == null) {
      boolean gotoSourceAvailable =
          heapFragmentWalker.getHeapDumpProject() != null && GoToSource.isAvailable();
      StringBuilder sb = new StringBuilder();
      Heap h = heapFragmentWalker.getHeapFragment();
      Collection<GCRoot> roots = h.getGCRoots();
      Map<ThreadObjectGCRoot, Map<Integer, List<JavaFrameGCRoot>>> javaFrameMap =
          computeJavaFrameMap(roots);
      // Use this to enable VisualVM color scheme for threads dumps:
      // sw.append("<pre style='color: #cc3300;'>"); // NOI18N
      sb.append("<pre>"); // NOI18N
      for (GCRoot root : roots) {
        if (root.getKind().equals(GCRoot.THREAD_OBJECT)) {
          ThreadObjectGCRoot threadRoot = (ThreadObjectGCRoot) root;
          Instance threadInstance = threadRoot.getInstance();
          if (threadInstance != null) {
            String threadName = getThreadName(threadInstance);
            Boolean daemon = (Boolean) threadInstance.getValueOfField("daemon"); // NOI18N
            Integer priority = (Integer) threadInstance.getValueOfField("priority"); // NOI18N
            Long threadId = (Long) threadInstance.getValueOfField("tid"); // NOI18N
            Integer threadStatus =
                (Integer) threadInstance.getValueOfField("threadStatus"); // NOI18N
            StackTraceElement stack[] = threadRoot.getStackTrace();
            Map<Integer, List<JavaFrameGCRoot>> localsMap = javaFrameMap.get(threadRoot);
            String style = "";

            if (threadRoot.equals(oome)) {
              style = "style=\"color: #FF0000\"";
            }
            // --- Use this to enable VisualVM color scheme for threads dumps: ---
            // sw.append("&nbsp;&nbsp;<span style=\"color: #0033CC\">"); // NOI18N
            sb.append("&nbsp;&nbsp;<a name=")
                .append(threadInstance.getInstanceId())
                .append("></a><b ")
                .append(style)
                .append(">"); // NOI18N
            // -------------------------------------------------------------------
            sb.append("\"")
                .append(htmlize(threadName))
                .append("\"")
                .append(daemon.booleanValue() ? " daemon" : "")
                .append(" prio=")
                .append(priority); // NOI18N
            if (threadId != null) {
              sb.append(" tid=").append(threadId); // NOI18N
            }
            if (threadStatus != null) {
              State tState = sun.misc.VM.toThreadState(threadStatus.intValue());
              sb.append(" ").append(tState); // NOI18N
            }
            // --- Use this to enable VisualVM color scheme for threads dumps: ---
            // sw.append("</span><br>"); // NOI18N
            sb.append("</b><br>"); // NOI18N
            // -------------------------------------------------------------------
            if (stack != null) {
              for (int i = 0; i < stack.length; i++) {
                String stackElHref;
                StackTraceElement stackElement = stack[i];
                String stackElementText = htmlize(stackElement.toString());

                if (gotoSourceAvailable) {
                  String className = stackElement.getClassName();
                  String method = stackElement.getMethodName();
                  int lineNo = stackElement.getLineNumber();
                  String stackUrl =
                      OPEN_THREADS_URL + className + "|" + method + "|" + lineNo; // NOI18N

                  // --- Use this to enable VisualVM color scheme for threads dumps: ---
                  // stackElHref = "&nbsp;&nbsp;<a style=\"color: #CC3300;\"
                  // href=\""+stackUrl+"\">"+stackElement+"</a>"; // NOI18N
                  stackElHref =
                      "<a href=\"" + stackUrl + "\">" + stackElementText + "</a>"; // NOI18N
                  // -------------------------------------------------------------------
                } else {
                  stackElHref = stackElementText;
                }
                sb.append("\tat ").append(stackElHref).append("<br>"); // NOI18N
                if (localsMap != null) {
                  List<JavaFrameGCRoot> locals = localsMap.get(Integer.valueOf(i));

                  if (locals != null) {
                    for (JavaFrameGCRoot localVar : locals) {
                      Instance localInstance = localVar.getInstance();

                      if (localInstance != null) {
                        sb.append("\t   Local Variable: ")
                            .append(printInstance(localInstance))
                            .append("<br>"); // NOI18N
                      } else {
                        sb.append(
                            "\t   Unknown Local Variable<br>"); // NOI18N
                      }
                    }
                  }
                }
              }
            }
          } else {
            sb.append("&nbsp;&nbsp;Unknown thread"); // NOI18N
          }
          sb.append("<br>"); // NOI18N
        }
      }
      sb.append("</pre>"); // NOI18N
      stackTrace = sb.toString();
    }
    return stackTrace;
  }
  private void showPopupMenu(int row, int x, int y) {
    HeapWalkerNode node =
        (HeapWalkerNode) fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();

    if (node instanceof HeapWalkerInstanceNode && ((HeapWalkerInstanceNode) node).isLoop()) {
      showLoopOriginItem.setVisible(true);
    } else {
      showLoopOriginItem.setVisible(false);
    }

    if (node.isRoot()) {
      showInstanceItem.setEnabled(false);
      copyPathFromRootItem.setEnabled(false);
    } else {
      showInstanceItem.setEnabled(
          node instanceof HeapWalkerInstanceNode
              && !(node instanceof HeapWalkerFieldNode && ((HeapWalkerFieldNode) node).isStatic()));
      copyPathFromRootItem.setEnabled(true);
    }

    //        showClassItem.setEnabled(node instanceof HeapWalkerInstanceNode || node instanceof
    // ClassNode);
    showGcRootItem.setEnabled(
        node instanceof HeapWalkerInstanceNode
            && (!node.currentlyHasChildren()
                || (node.getNChildren() != 1
                    || !HeapWalkerNodeFactory.isMessageNode(node.getChild(0))))); // #124306
    if (showSourceItem != null) showSourceItem.setEnabled(node instanceof HeapWalkerInstanceNode);
    showInThreadsItem.setEnabled(false);
    if (node instanceof HeapWalkerInstanceNode) {
      Instance rootInstance = ((HeapWalkerInstanceNode) node).getInstance();
      Heap heap =
          referencesBrowserController
              .getReferencesControllerHandler()
              .getHeapFragmentWalker()
              .getHeapFragment();
      GCRoot gcRoot = heap.getGCRoot(rootInstance);

      if (gcRoot != null && GCRoot.JAVA_FRAME.equals(gcRoot.getKind())) {
        // make sure that thread information is available
        JavaFrameGCRoot frameVar = (JavaFrameGCRoot) gcRoot;

        if (frameVar.getFrameNumber() != -1) {
          showInThreadsItem.setEnabled(true);
        }
      }
    }

    if ((x == -1) || (y == -1)) {
      Rectangle rowBounds = fieldsListTable.getCellRect(row, 0, true);

      if (x == -1) {
        x = rowBounds.x + (rowBounds.width / 2);
      }

      if (y == -1) {
        y = rowBounds.y + (rowBounds.height / 2);
      }
    }

    tablePopup.show(fieldsListTable, x, y);
  }