/**
   * Shows the appropriate user interface that would allow the user to add the given
   * <tt>SourceUIContact</tt> to their contact list.
   *
   * @param contact the contact to add
   */
  private void addContact(SourceUIContact contact) {
    SourceContact sourceContact = (SourceContact) contact.getDescriptor();

    List<ContactDetail> details =
        sourceContact.getContactDetails(OperationSetPersistentPresence.class);
    int detailsCount = details.size();

    if (detailsCount > 1) {
      JMenuItem addContactMenu =
          TreeContactList.createAddContactMenu((SourceContact) contact.getDescriptor());

      JPopupMenu popupMenu = ((JMenu) addContactMenu).getPopupMenu();

      // Add a title label.
      JLabel infoLabel = new JLabel();
      infoLabel.setText(
          "<html><b>"
              + GuiActivator.getResources().getI18NString("service.gui.ADD_CONTACT")
              + "</b></html>");

      popupMenu.insert(infoLabel, 0);
      popupMenu.insert(new Separator(), 1);

      popupMenu.setFocusable(true);
      popupMenu.setInvoker(treeContactList);

      Point location =
          new Point(
              addContactButton.getX(), addContactButton.getY() + addContactButton.getHeight());

      SwingUtilities.convertPointToScreen(location, treeContactList);

      location.y = location.y + treeContactList.getPathBounds(treeContactList.getSelectionPath()).y;

      popupMenu.setLocation(location.x + 8, location.y - 8);
      popupMenu.setVisible(true);
    } else if (details.size() == 1) {
      TreeContactList.showAddContactDialog(details.get(0), sourceContact.getDisplayName());
    }
  }
  public PopupMenuButton(
      Application app,
      Object[] data,
      Integer rows,
      Integer columns,
      Dimension iconSize,
      Integer mode,
      final boolean hasTable,
      boolean hasSlider) {
    super();
    this.app = app;
    this.hasTable = hasTable;
    this.mode = mode;
    this.iconSize = iconSize;
    this.thisButton = this;

    this.setFocusable(false);

    // create the popup
    myPopup = new JPopupMenu();
    myPopup.setFocusable(false);
    myPopup.setBackground(Color.WHITE);
    myPopup.setBorder(
        BorderFactory.createCompoundBorder(
            BorderFactory.createLineBorder(Color.GRAY),
            BorderFactory.createEmptyBorder(3, 3, 3, 3)));

    // add a mouse listener to our button that triggers the popup
    addMouseListener(
        new MouseAdapter() {

          @Override
          public void mouseEntered(MouseEvent e) {
            popupIsVisible = myPopup.isShowing();
          }

          @Override
          public void mousePressed(MouseEvent e) {

            if (!thisButton.isEnabled()) return;
            if (popupIsVisible == true && !myPopup.isVisible()) {
              popupIsVisible = false;
              return;
            }

            if (prepareToShowPopup() == false) return;

            Point locButton = getLocation();

            // trigger popup
            // default: trigger only when the mouse is over the right side of the button
            // if isStandardButton: pressing anywhere triggers the popup
            if (isStandardButton || e.getX() >= getWidth() - 16 && e.getX() <= getWidth()) {
              if (hasTable) myTable.updateFonts();
              if (isDownwardPopup)
                // popup appears below the button
                myPopup.show(getParent(), locButton.x, locButton.y + getHeight());
              else
                // popup appears above the button
                myPopup.show(
                    getParent(),
                    locButton.x - myPopup.getPreferredSize().width + thisButton.getWidth(),
                    locButton.y - myPopup.getPreferredSize().height - 2);
            }

            popupIsVisible = myPopup.isShowing();
          }
        });

    // place text to the left of drop down icon
    this.setHorizontalTextPosition(SwingConstants.LEFT);
    this.setHorizontalAlignment(SwingConstants.LEFT);

    // create selection table
    if (hasTable) {
      this.data = data;

      myTable = new SelectionTable(app, data, rows, columns, iconSize, mode);
      setSelectedIndex(0);

      // add a mouse listener to handle table selection
      myTable.addMouseListener(
          new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
              handlePopupActionEvent();
            }
          });

      /*
      // if displaying text only, then adjust the width
      if(mode == SelectionTable.MODE_TEXT){
      	 Dimension d = this.getPreferredSize();
      	 d.width = myTable.getColumnWidth();
      	 setMinimumSize(d);
      	 setMaximumSize(d);
       }
       */

      myTable.setBackground(myPopup.getBackground());
      myPopup.add(myTable);
    }

    // create slider
    if (hasSlider) getMySlider();

    isIniting = false;

    if (mode == SelectionTable.MODE_TEXT && iconSize.width == -1) {
      iconSize.width = myTable.getColumnWidth() - 4;
      iconSize.height = myTable.getRowHeight() - 4;
    }
  }