/**
   * Add all the menu currently available to the menu. This may be called any time BlueJ feels that
   * the menu needs to be updated.
   *
   * @param onThisProject a specific project to look for, or null for all projects.
   */
  public void addExtensionMenu(Project onThisProject) {
    // Get all menus that can be possibly be generated now.
    List<JMenuItem> menuItems = extMgr.getMenuItems(menuGenerator, onThisProject);

    // Retrieve all the items from the current menu
    MenuElement[] elements = popupMenu.getSubElements();

    for (int index = 0; index < elements.length; index++) {
      JComponent aComponent = (JComponent) elements[index].getComponent();

      if (aComponent == null) {
        continue;
      }

      if (!(aComponent instanceof JMenuItem)) {
        continue;
      }

      ExtensionWrapper aWrapper =
          (ExtensionWrapper) aComponent.getClientProperty("bluej.extmgr.ExtensionWrapper");

      if (aWrapper == null) {
        continue;
      }

      popupMenu.remove(aComponent);
    }

    popupMenu.remove(menuSeparator);

    // If the provided menu is empty we are done here.
    if (menuItems.isEmpty()) {
      return;
    }

    popupMenu.add(menuSeparator);

    for (JMenuItem menuItem : menuItems) {
      popupMenu.add(menuItem);
    }
  }
  /**
   * Notify to all valid extensions that this menu Item is about to be displayed.
   *
   * @param event The associated event
   */
  @Override
  public void popupMenuWillBecomeVisible(PopupMenuEvent event) {
    int itemsCount = 0;

    JPopupMenu aPopup = (JPopupMenu) event.getSource();

    MenuElement[] elements = aPopup.getSubElements();

    for (int index = 0; index < elements.length; index++) {
      JComponent aComponent = (JComponent) elements[index].getComponent();

      if (aComponent == null) {
        continue;
      }

      if (!(aComponent instanceof JMenuItem)) {
        continue;
      }

      ExtensionWrapper aWrapper =
          (ExtensionWrapper) aComponent.getClientProperty("bluej.extmgr.ExtensionWrapper");

      if (aWrapper == null) {
        continue;
      }

      if (!aWrapper.isValid()) {
        popupMenu.remove(aComponent);

        continue;
      }

      aWrapper.safePostMenuItem(menuGenerator, (JMenuItem) aComponent);

      itemsCount++;
    }

    if (itemsCount <= 0) {
      popupMenu.remove(menuSeparator);
    }
  }