TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); if (selPath == null) return;  tree.setSelectionPath(selPath); DefaultMutableTreeNode popupNode = (DefaultMutableTreeNode) selPath.getLastPathComponent(); Object o = popupNode.getUserObject(); if (o instanceof SocietyComponent) { } else if (o instanceof RecipeComponent) {
TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths == null) return; boolean haveRecipes = false; for (int i = 0; i < selectionPaths.length; i++) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPaths[i].getLastPathComponent(); if (node.getUserObject() instanceof RecipeComponent) haveRecipes = true; else { haveRecipes = false; break; } } if (!haveRecipes) { TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());  tree.setSelectionPath(selPath); DefaultMutableTreeNode popupNode = (DefaultMutableTreeNode) selPath.getLastPathComponent(); Object o = popupNode.getUserObject(); if (popupNode == societies) societiesMenu.show(tree, e.getX(), e.getY()); else if (popupNode == recipes) recipesMenu.show(tree, e.getX(), e.getY()); } else
  private void doPopup(MouseEvent e) {    TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());    if (selPath == null) return;    // set the selected node to be the node the mouse is pointing at    tree.setSelectionPath(selPath);    DefaultMutableTreeNode popupNode =      (DefaultMutableTreeNode) selPath.getLastPathComponent();    Object o = popupNode.getUserObject();    if (o instanceof SocietyComponent) {    } else if (o instanceof RecipeComponent) {      popupMenu.show(tree, e.getX(), e.getY());    } else if (o instanceof Experiment) {      popupMenu.show(tree, e.getX(), e.getY());    } else if (popupNode == societies) {      societiesMenu.show(tree, e.getX(), e.getY());    } else if (popupNode == recipes) {      recipesMenu.show(tree, e.getX(), e.getY());    }  }