public static void installOn(final JTree tree){
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
      @Override
      public void treeWillCollapse(TreeExpansionEvent event) {
        TreePath path = event.getPath();
        TreeModel model = tree.getModel();
        Object lastPathComponent = path.getLastPathComponent();
        int childCount = model.getChildCount(lastPathComponent);
        for (int i = 0; i < childCount; i++) {
          Object child = model.getChild(lastPathComponent, i);
          TreePath childPath = path.pathByAddingChild(child);
          if (tree.isExpanded(childPath)) {
            tree.collapsePath(childPath);
          }
        }
      }

      @Override
      public void treeWillExpand(TreeExpansionEvent event) {
      }
    });

    tree.addTreeExpansionListener(new TreeExpansionListener() {
      @Override
      public void treeCollapsed(TreeExpansionEvent event) {
      }

      @Override
      public void treeExpanded(TreeExpansionEvent event) {
        TreePath path = event.getPath();
        TreeModel model = tree.getModel();
        Object lastPathComponent = path.getLastPathComponent();
        if (model.getChildCount(lastPathComponent) == 1) {
          TreePath firstChildPath = path.pathByAddingChild(model.getChild(lastPathComponent, 0));
          tree.expandPath(firstChildPath);
        }
      }
    });
  }
public static void installOn(final JTree tree)