/**
   * 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;
  }
  /**
   * 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);
  }
 /**
  * Removes a white board frame.
  *
  * @param frame the frame to remove
  */
 public void removeWhiteboardWindow(WhiteboardFrame frame) {
   synchronized (wbFrames) {
     wbFrames.remove(frame);
   }
 }
  /**
   * Construct (with WhiteboardSession) and send a WhiteboardObject to a contact.
   *
   * @param wbSession the white-board session, to which the object would be send
   * @param ws WhiteboardShape to convert and send
   * @param c contact
   * @return WhiteboardObject sent
   */
  public WhiteboardObject sendWhiteboardObject(WhiteboardSession wbSession, WhiteboardShape ws)
      throws OperationFailedException {
    Vector supportedWBO = new Vector(Arrays.asList(wbSession.getSupportedWhiteboardObjects()));

    if (ws instanceof WhiteboardObjectPath) {
      if (!supportedWBO.contains(WhiteboardObjectPath.NAME)) return null;
      WhiteboardObjectPath obj =
          (WhiteboardObjectPath) wbSession.createWhiteboardObject(WhiteboardObjectPath.NAME);
      ws.setID(obj.getID());
      obj.setPoints(((WhiteboardObjectPath) ws).getPoints());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectPolyLine) {
      if (!supportedWBO.contains(WhiteboardObjectPolyLine.NAME)) return null;
      WhiteboardObjectPolyLine obj =
          (WhiteboardObjectPolyLine)
              wbSession.createWhiteboardObject(WhiteboardObjectPolyLine.NAME);
      ws.setID(obj.getID());
      obj.setPoints(((WhiteboardObjectPolyLine) ws).getPoints());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectPolygon) {
      if (!supportedWBO.contains(WhiteboardObjectPolygon.NAME)) return null;
      WhiteboardObjectPolygon obj =
          (WhiteboardObjectPolygon) wbSession.createWhiteboardObject(WhiteboardObjectPolygon.NAME);
      ws.setID(obj.getID());
      obj.setPoints(((WhiteboardObjectPolygon) ws).getPoints());
      obj.setBackgroundColor(((WhiteboardObjectPolygon) ws).getBackgroundColor());
      obj.setFill(((WhiteboardObjectPolygon) ws).isFill());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectLine) {
      if (!supportedWBO.contains(WhiteboardObjectLine.NAME)) return null;
      WhiteboardObjectLine obj =
          (WhiteboardObjectLine) wbSession.createWhiteboardObject(WhiteboardObjectLine.NAME);
      ws.setID(obj.getID());
      obj.setWhiteboardPointStart(((WhiteboardObjectLine) ws).getWhiteboardPointStart());
      obj.setWhiteboardPointEnd(((WhiteboardObjectLine) ws).getWhiteboardPointEnd());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectRect) {
      if (!supportedWBO.contains(WhiteboardObjectRect.NAME)) return null;
      WhiteboardObjectRect obj =
          (WhiteboardObjectRect) wbSession.createWhiteboardObject(WhiteboardObjectRect.NAME);
      ws.setID(obj.getID());
      obj.setFill(((WhiteboardObjectRect) ws).isFill());
      obj.setHeight(((WhiteboardObjectRect) ws).getHeight());
      obj.setWhiteboardPoint(((WhiteboardObjectRect) ws).getWhiteboardPoint());
      obj.setWidth((((WhiteboardObjectRect) ws)).getWidth());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectCircle) {
      if (!supportedWBO.contains(WhiteboardObjectCircle.NAME)) return null;
      WhiteboardObjectCircle obj =
          (WhiteboardObjectCircle) wbSession.createWhiteboardObject(WhiteboardObjectCircle.NAME);
      ws.setID(obj.getID());
      obj.setFill(((WhiteboardObjectCircle) ws).isFill());
      obj.setRadius(((WhiteboardObjectCircle) ws).getRadius());
      obj.setWhiteboardPoint(((WhiteboardObjectCircle) ws).getWhiteboardPoint());
      obj.setBackgroundColor((((WhiteboardObjectCircle) ws)).getBackgroundColor());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectText) {
      if (!supportedWBO.contains(WhiteboardObjectText.NAME)) return null;
      WhiteboardObjectText obj =
          (WhiteboardObjectText) wbSession.createWhiteboardObject(WhiteboardObjectText.NAME);
      ws.setID(obj.getID());
      obj.setFontName(((WhiteboardObjectText) ws).getFontName());
      obj.setFontSize(((WhiteboardObjectText) ws).getFontSize());
      obj.setText(((WhiteboardObjectText) ws).getText());
      obj.setWhiteboardPoint(((WhiteboardObjectText) ws).getWhiteboardPoint());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);
      return obj;
    } else if (ws instanceof WhiteboardObjectImage) {
      if (!supportedWBO.contains(WhiteboardObjectImage.NAME)) return null;
      WhiteboardObjectImage obj =
          (WhiteboardObjectImage) wbSession.createWhiteboardObject(WhiteboardObjectImage.NAME);
      ws.setID(obj.getID());
      obj.setBackgroundImage(((WhiteboardObjectImage) ws).getBackgroundImage());
      obj.setHeight(((WhiteboardObjectImage) ws).getHeight());
      obj.setWhiteboardPoint(((WhiteboardObjectImage) ws).getWhiteboardPoint());
      obj.setWidth(((WhiteboardObjectImage) ws).getWidth());
      obj.setColor(ws.getColor());
      obj.setThickness(ws.getThickness());
      wbSession.sendWhiteboardObject(obj);

      return obj;
    }

    return null;
  }