/**
   * Called when a modified <tt>WhiteboardObject</tt> has been received.
   *
   * @param evt the <tt>WhiteboardObjectReceivedEvent</tt> containing the modified whiteboardObject,
   *     its sender and other details.
   */
  public void whiteboardObjecModified(WhiteboardObjectModifiedEvent evt) {
    WhiteboardFrame wbf = getWhiteboardFrame(evt.getSourceWhiteboardSession());

    if (wbf == null) return;
    wbf.setVisible(true);
    WhiteboardObject wbo = evt.getSourceWhiteboardObject();
    wbf.receiveWhiteboardObject(wbo);
  }
  /**
   * Returns the WhiteboardFrame associated with the Contact.
   *
   * @param c contact
   * @return WhiteboardFrame with the Contact or null (if nothing found)
   */
  private WhiteboardFrame getWhiteboardFrame(WhiteboardSession session) {
    WhiteboardFrame whiteboardFrame = null;

    for (int i = 0; i < wbFrames.size(); i++) {
      whiteboardFrame = (WhiteboardFrame) wbFrames.get(i);

      if (whiteboardFrame.getWhiteboardSession().equals(session)) return whiteboardFrame;
    }
    return null;
  }
  /**
   * Called when a deleted <tt>WhiteboardObject</tt> has been received.
   *
   * @param evt the <tt>WhiteboardObjectDeletedEvent</tt> containing the identification of the
   *     deleted WhiteboardObject, its sender and other details.
   */
  public void whiteboardObjectDeleted(WhiteboardObjectDeletedEvent evt) {
    WhiteboardFrame wbf = getWhiteboardFrame(evt.getSourceWhiteboardSession());

    if (wbf == null) {
      return;
    }

    wbf.setVisible(true);
    String id = evt.getId();
    wbf.receiveDeleteWhiteboardObject(id);
  }
  /**
   * Returns the WhiteboardFrame associated with the Contact.
   *
   * @param c contact
   * @return WhiteboardFrame with the Contact or null (if nothing found)
   */
  private WhiteboardFrame getWhiteboardFrame(Contact contact) {
    WhiteboardFrame whiteboardFrame = null;

    for (int i = 0; i < wbFrames.size(); i++) {
      whiteboardFrame = (WhiteboardFrame) wbFrames.get(i);

      if (whiteboardFrame.getContact() != null && whiteboardFrame.getContact().equals(contact))
        return whiteboardFrame;
    }
    return null;
  }
  /**
   * Called when a new incoming <tt>WhiteboardObject</tt> has been received.
   *
   * @param evt the <tt>WhiteboardObjectReceivedEvent</tt> containing the newly received
   *     WhiteboardObject, its sender and other details.
   */
  public void whiteboardObjectReceived(WhiteboardObjectReceivedEvent evt) {
    /*
     * There are 2 cases when a message is received:
     * - an existing session
     * - or a new session
     */
    WhiteboardFrame wbFrame = getWhiteboardFrame(evt.getSourceWhiteboardSession());

    if (wbFrame == null) {
      logger.debug("New WBParticipant" + evt.getSourceContact().getDisplayName());

      wbFrame = new WhiteboardFrame(this, evt.getSourceWhiteboardSession());

      wbFrames.add(wbFrame);
    }

    wbFrame.setVisible(true);
    WhiteboardObject wbObject = evt.getSourceWhiteboardObject();
    wbFrame.receiveWhiteboardObject(wbObject);
  }
    /**
     * Implements the <tt>WhiteboardSessionPresenceListener .whiteboardSessionPresenceChanged</tt>
     * method.
     */
    public void whiteboardSessionPresenceChanged(WhiteboardSessionPresenceChangeEvent evt) {
      WhiteboardSession whiteboardSession = evt.getWhiteboardSession();

      if (evt.getEventType().equals(WhiteboardSessionPresenceChangeEvent.LOCAL_USER_JOINED)) {
        whiteboardSession.addWhiteboardObjectListener(WhiteboardSessionManager.this);

        WhiteboardFrame frame = getWhiteboardFrame(evt.getWhiteboardSession());

        if (frame == null) {
          frame = new WhiteboardFrame(WhiteboardSessionManager.this, whiteboardSession);

          frame.setVisible(true);
          wbFrames.add(frame);
        }
      } else if (evt.getEventType()
          .equals(WhiteboardSessionPresenceChangeEvent.LOCAL_USER_JOIN_FAILED)) {
        WhiteboardActivator.getUiService()
            .getPopupDialog()
            .showMessagePopupDialog(
                Resources.getString(
                        "failedToJoinWhiteboard",
                        new String[] {whiteboardSession.getWhiteboardID()})
                    + evt.getReason(),
                Resources.getString("error"),
                PopupDialog.ERROR_MESSAGE);
      } else if (evt.getEventType().equals(WhiteboardSessionPresenceChangeEvent.LOCAL_USER_LEFT)) {
        WhiteboardFrame frame = getWhiteboardFrame(whiteboardSession);

        if (frame == null) return;

        wbFrames.remove(frame);
        frame.dispose();
        whiteboardSession.removeWhiteboardObjectListener(WhiteboardSessionManager.this);
      } else if (evt.getEventType()
          .equals(WhiteboardSessionPresenceChangeEvent.LOCAL_USER_KICKED)) {

      } else if (evt.getEventType()
          .equals(WhiteboardSessionPresenceChangeEvent.LOCAL_USER_DROPPED)) {

      }
    }
  /**
   * Initialize (a new) Whiteboard with contact
   *
   * @param contact Contact used to init whiteboard
   */
  public void initWhiteboard(final Contact contact) {
    opSetWb =
        (OperationSetWhiteboarding)
            contact.getProtocolProvider().getOperationSet(OperationSetWhiteboarding.class);

    if (opSetWb == null) {
      logger.info("Contact does not support whiteboarding");
      return;
    }

    WhiteboardFrame wbf = getWhiteboardFrame(contact);
    if (wbf != null) {
      wbf.setVisible(true);
      return;
    }

    new Thread() {
      public void run() {
        try {
          WhiteboardSession wbSession =
              opSetWb.createWhiteboardSession(contact.getDisplayName(), null);

          WhiteboardFrame wbFrame = new WhiteboardFrame(WhiteboardSessionManager.this, wbSession);

          wbFrames.add(wbFrame);
          wbFrame.setContact(contact);
          wbFrame.setVisible(true);

          wbSession.join();

          wbSession.invite(contact.getAddress());

        } catch (OperationFailedException e) {
          logger.error("Creating a whiteboard session failed.", e);
        } catch (OperationNotSupportedException e) {
          logger.error("Do not support create of whiteboard session", e);
        }
      }
    }.start();
  }