/**
   * Set the currently selected Variable.
   *
   * @param v select this Variable, must be already in the tree.
   */
  public void setSelected(VariableIF v) {
    if (v == null) return;

    // construct chain of variables
    List<VariableIF> vchain = new ArrayList<>();
    vchain.add(v);

    VariableIF vp = v;
    while (vp.isMemberOfStructure()) {
      vp = vp.getParentStructure();
      vchain.add(0, vp); // reverse
    }

    // construct chain of groups
    List<Group> gchain = new ArrayList<>();
    Group gp = vp.getParentGroup();

    gchain.add(gp);
    while (gp.getParentGroup() != null) {
      gp = gp.getParentGroup();
      gchain.add(0, gp); // reverse
    }

    List<Object> pathList = new ArrayList<>();

    // start at root, work down through the nested groups, if any
    GroupNode gnode = (GroupNode) model.getRoot();
    pathList.add(gnode);
    Group parentGroup = gchain.get(0); // always the root group

    for (int i = 1; i < gchain.size(); i++) {
      parentGroup = gchain.get(i);
      gnode = gnode.findNestedGroup(parentGroup);
      assert gnode != null;
      pathList.add(gnode);
    }

    vp = vchain.get(0);
    VariableNode vnode = gnode.findNestedVariable(vp);
    if (vnode == null) return; // not found
    pathList.add(vnode);

    // now work down through the structure members, if any
    for (int i = 1; i < vchain.size(); i++) {
      vp = vchain.get(i);
      vnode = vnode.findNestedVariable(vp);
      if (vnode == null) return; // not found
      pathList.add(vnode);
    }

    // convert to TreePath, and select it
    Object[] paths = pathList.toArray();
    TreePath treePath = new TreePath(paths);
    tree.setSelectionPath(treePath);
    tree.scrollPathToVisible(treePath);
  }