@Override
  public void setOwnPresence(UserPresence userPresence) {
    if (isOnline() && !userPresence.isOnline()) {
      myFacade.disconnect();
    } else if (!isOnline() && userPresence.isOnline()) {
      myUI.connectAndLogin(null);
    }

    if (isOnline() && presenceModeChanged(userPresence.getPresenceMode())) {
      myFacade.setOnlinePresence(userPresence);
      myPresenceMode = userPresence.getPresenceMode();
    }
  }
 @Nullable
 private IDEtalkEvent createPresenceChangeEvent(User user, UserPresence newPresence) {
   UserPresence oldPresence = getUserPresence(user);
   if (!newPresence.equals(oldPresence)) {
     if (newPresence.isOnline() ^ oldPresence.isOnline()) {
       return newPresence.isOnline() ? new UserEvent.Online(user) : new UserEvent.Offline(user);
     } else {
       return new UserEvent.Updated(
           user, PRESENCE, oldPresence.getPresenceMode(), newPresence.getPresenceMode());
     }
   }
   return null;
 }
  public void testSetOwnPresence() throws Exception {

    registerResponseProviders(myUserModel, myIdeFacade);

    // Add self to contact list
    User self = myUserModel.createUser(StringUtil.getMyUsername(), myTransport.getName());
    myUserModel.addUser(self);

    // Wait for next cycle of user finding
    new WaitFor(1000) {
      @Override
      protected boolean condition() {
        return !myTransport.getUserMonitorThread().isFinding();
      }
    };

    // make self away
    UserPresence presence = new UserPresence(PresenceMode.AWAY);
    assert presence.isOnline();
    myTransport.setOwnPresence(presence);

    // make sure that away status will be updated
    myTransport.findUsers(new NullProgressIndicator());
    assert PresenceMode.AWAY == self.getPresence().getPresenceMode();

    // Now, test itself. We go online and want this status to be updated almost immediately.
    myEvents.clear();
    addEventListener();
    myTransport.setOwnPresence(new UserPresence(PresenceMode.AVAILABLE));

    final User self1 = self;
    new WaitFor(200) {
      @Override
      protected boolean condition() {
        return self1.getPresence().getPresenceMode() == PresenceMode.AVAILABLE;
      }
    };
    assertSame(
        "Should provide correct presence mode",
        PresenceMode.AVAILABLE,
        myTransport.getUserPresence(self).getPresenceMode());
    UserEvent.Updated event = (UserEvent.Updated) checkEvent(true);
    assertNotNull("Expect go online event", event);
    assertEquals("presence", event.getPropertyName());
  }