/**
   * Changes the selection in the menu hierarchy. The elements in the array are sorted in order from
   * the root menu element to the currently selected menu element.
   *
   * <p>Note that this method is public but is used by the look and feel engine and should not be
   * called by client applications.
   *
   * @param path an array of <code>MenuElement</code> objects specifying the selected path
   */
  public void setSelectedPath(MenuElement[] path) {
    int i, c;
    int currentSelectionCount = selection.size();
    int firstDifference = 0;

    if (path == null) {
      path = new MenuElement[0];
    }

    if (DEBUG) {
      System.out.print("Previous:  ");
      printMenuElementArray(getSelectedPath());
      System.out.print("New:  ");
      printMenuElementArray(path);
    }

    for (i = 0, c = path.length; i < c; i++) {
      if (i < currentSelectionCount && selection.elementAt(i) == path[i]) firstDifference++;
      else break;
    }

    for (i = currentSelectionCount - 1; i >= firstDifference; i--) {
      MenuElement me = selection.elementAt(i);
      selection.removeElementAt(i);
      me.menuSelectionChanged(false);
    }

    for (i = firstDifference, c = path.length; i < c; i++) {
      if (path[i] != null) {
        selection.addElement(path[i]);
        path[i].menuSelectionChanged(true);
      }
    }

    fireStateChanged();
  }