/**
   * Creates an instance of the <tt>LoginWindow</tt>.
   *
   * @param chatRoom the chat room for which we're authenticating
   */
  public ChatRoomAuthenticationWindow(ChatRoomWrapper chatRoom) {
    this.chatRoom = chatRoom;

    ImageIcon logoImage = new ImageIcon(chatRoom.getParentProvider().getImage());

    backgroundPanel = new LoginWindowBackground(logoImage);

    this.backgroundPanel.setPreferredSize(new Dimension(420, 230));

    this.backgroundPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

    this.backgroundPanel.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));

    this.getContentPane().setLayout(new BorderLayout());

    this.init();

    this.getContentPane().add(backgroundPanel, BorderLayout.CENTER);

    this.setResizable(false);

    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    this.setTitle(
        GuiActivator.getResources()
            .getI18NString(
                "service.gui.AUTHENTICATION_WINDOW_TITLE",
                new String[] {chatRoom.getParentProvider().getName()}));

    this.enableKeyActions();
  }
  /**
   * Joins the given chat room with the given password and manages all the exceptions that could
   * occur during the join process.
   *
   * @param chatRoomWrapper the chat room to join.
   * @param nickName the nickname we choose for the given chat room.
   * @param password the password.
   * @param rememberPassword if true the password should be saved.
   * @param isFirstAttempt is this the first attempt to join room, used to check whether to show
   *     some error messages
   * @param subject the subject which will be set to the room after the user join successful.
   */
  private void joinChatRoom(
      ChatRoomWrapper chatRoomWrapper,
      String nickName,
      byte[] password,
      boolean rememberPassword,
      boolean isFirstAttempt,
      String subject) {
    ChatRoom chatRoom = chatRoomWrapper.getChatRoom();

    if (chatRoom == null) {
      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.WARNING"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CHAT_ROOM_NOT_CONNECTED",
                      new String[] {chatRoomWrapper.getChatRoomName()}));
      return;
    }

    new JoinChatRoomTask(
            (ChatRoomWrapperImpl) chatRoomWrapper,
            nickName,
            password,
            rememberPassword,
            isFirstAttempt,
            subject)
        .start();
  }
  /**
   * Leaves the given chat room.
   *
   * @param chatRoomWrapper the chat room to leave.
   * @return <tt>ChatRoomWrapper</tt> instance associated with the chat room.
   */
  public ChatRoomWrapper leaveChatRoom(ChatRoomWrapper chatRoomWrapper) {
    ChatRoom chatRoom = chatRoomWrapper.getChatRoom();

    if (chatRoom == null) {
      ResourceManagementService resources = MUCActivator.getResources();

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              resources.getI18NString("service.gui.WARNING"),
              resources.getI18NString("service.gui.CHAT_ROOM_LEAVE_NOT_CONNECTED"));

      return null;
    }

    if (chatRoom.isJoined()) chatRoom.leave();

    ChatRoomWrapper existChatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

    if (existChatRoomWrapper == null) return null;

    // We save the choice of the user, before the chat room is really
    // joined, because even the join fails we want the next time when
    // we login to join this chat room automatically.
    ConfigurationUtils.updateChatRoomStatus(
        chatRoomWrapper.getParentProvider().getProtocolProvider(),
        chatRoomWrapper.getChatRoomID(),
        GlobalStatusEnum.OFFLINE_STATUS);

    return existChatRoomWrapper;
  }
  /** Constructs the <tt>AuthenticationWindow</tt>. */
  private void init() {
    this.idValue =
        new JTextField(
            chatRoom.getParentProvider().getProtocolProvider().getAccountID().getUserID());

    this.infoTextArea.setOpaque(false);
    this.infoTextArea.setLineWrap(true);
    this.infoTextArea.setWrapStyleWord(true);
    this.infoTextArea.setFont(infoTextArea.getFont().deriveFont(Font.BOLD));
    this.infoTextArea.setEditable(false);
    this.infoTextArea.setText(
        GuiActivator.getResources()
            .getI18NString(
                "service.gui.CHAT_ROOM_REQUIRES_PASSWORD",
                new String[] {chatRoom.getChatRoomName()}));

    this.idLabel.setFont(idLabel.getFont().deriveFont(Font.BOLD));
    this.passwdLabel.setFont(passwdLabel.getFont().deriveFont(Font.BOLD));

    this.labelsPanel.add(idLabel);
    this.labelsPanel.add(passwdLabel);

    this.textFieldsPanel.add(idValue);
    this.textFieldsPanel.add(passwdField);

    this.buttonsPanel.add(loginButton);
    this.buttonsPanel.add(cancelButton);

    this.mainPanel.add(infoTextArea, BorderLayout.NORTH);
    this.mainPanel.add(labelsPanel, BorderLayout.WEST);
    this.mainPanel.add(textFieldsPanel, BorderLayout.CENTER);
    this.mainPanel.add(buttonsPanel, BorderLayout.SOUTH);

    this.backgroundPanel.add(mainPanel, BorderLayout.CENTER);

    this.loginButton.setName("ok");
    this.cancelButton.setName("cancel");

    this.loginButton.setMnemonic(GuiActivator.getResources().getI18nMnemonic("service.gui.OK"));
    this.cancelButton.setMnemonic(
        GuiActivator.getResources().getI18nMnemonic("service.gui.CANCEL"));

    this.loginButton.addActionListener(this);
    this.cancelButton.addActionListener(this);

    this.getRootPane().setDefaultButton(loginButton);

    this.setTransparent(true);
  }
 /**
  * Destroys the given <tt>ChatRoom</tt> from the list of all chat rooms.
  *
  * @param chatRoomWrapper the <tt>ChatRoomWrapper</tt> to be destroyed.
  * @param reason the reason for destroying.
  * @param alternateAddress the alternate address.
  */
 public void destroyChatRoom(
     ChatRoomWrapper chatRoomWrapper, String reason, String alternateAddress) {
   if (chatRoomWrapper.getChatRoom().destroy(reason, alternateAddress)) {
     MUCActivator.getUIService().closeChatRoomWindow(chatRoomWrapper);
     chatRoomList.removeChatRoom(chatRoomWrapper);
   } else {
     // if we leave a chat room which is not persistent
     // the room can be destroyed on the server, and error is returned
     // when we try to destroy it not-authorized(401)
     if (!chatRoomWrapper.getChatRoom().isPersistent()
         && !chatRoomWrapper.getChatRoom().isJoined()) {
       chatRoomList.removeChatRoom(chatRoomWrapper);
     }
   }
 }
  /**
   * Joins the given chat room with the given password and manages all the exceptions that could
   * occur during the join process.
   *
   * @param chatRoomWrapper the chat room to join.
   * @param nickName the nickname we choose for the given chat room.
   * @param password the password.
   */
  public void joinChatRoom(ChatRoomWrapper chatRoomWrapper, String nickName, byte[] password) {
    ChatRoom chatRoom = chatRoomWrapper.getChatRoom();

    if (chatRoom == null) {
      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.WARNING"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CHAT_ROOM_NOT_CONNECTED",
                      new String[] {chatRoomWrapper.getChatRoomName()}));
      return;
    }

    new JoinChatRoomTask((ChatRoomWrapperImpl) chatRoomWrapper, nickName, password).start();
  }
  /** Reloads logo image. */
  public void loadSkin() {
    ImageIcon logoImage = new ImageIcon(chatRoom.getParentProvider().getImage());

    backgroundPanel.setBgImage(logoImage);
  }
  /**
   * Opens a chat window for the chat room.
   *
   * @param room the chat room.
   */
  public void openChatRoom(ChatRoomWrapper room) {
    if (room.getChatRoom() == null) {
      room =
          createChatRoom(
              room.getChatRoomName(),
              room.getParentProvider().getProtocolProvider(),
              new ArrayList<String>(),
              "",
              false,
              false,
              true);

      // leave the chatroom because getChatRoom().isJoined() returns true
      // otherwise
      if (room.getChatRoom().isJoined()) room.getChatRoom().leave();
    }

    if (!room.getChatRoom().isJoined()) {
      String savedNick =
          ConfigurationUtils.getChatRoomProperty(
              room.getParentProvider().getProtocolProvider(), room.getChatRoomID(), "userNickName");
      String subject = null;

      if (savedNick == null) {
        String[] joinOptions =
            ChatRoomJoinOptionsDialog.getJoinOptions(
                room.getParentProvider().getProtocolProvider(),
                room.getChatRoomID(),
                getDefaultNickname(room.getParentProvider().getProtocolProvider()));
        savedNick = joinOptions[0];
        subject = joinOptions[1];
      }

      if (savedNick != null) {
        joinChatRoom(room, savedNick, null, subject);
      } else return;
    }

    MUCActivator.getUIService().openChatRoomWindow(room);
  }
  /**
   * Creates a chat room, by specifying the chat room name, the parent protocol provider and
   * eventually, the contacts invited to participate in this chat room.
   *
   * @param roomName the name of the room
   * @param protocolProvider the parent protocol provider.
   * @param contacts the contacts invited when creating the chat room.
   * @param reason
   * @param join whether we should join the room after creating it.
   * @param persistent whether the newly created room will be persistent.
   * @param isPrivate whether the room will be private or public.
   * @return the <tt>ChatRoomWrapper</tt> corresponding to the created room
   */
  public ChatRoomWrapper createChatRoom(
      String roomName,
      ProtocolProviderService protocolProvider,
      Collection<String> contacts,
      String reason,
      boolean join,
      boolean persistent,
      boolean isPrivate) {
    ChatRoomWrapper chatRoomWrapper = null;
    OperationSetMultiUserChat groupChatOpSet =
        protocolProvider.getOperationSet(OperationSetMultiUserChat.class);

    // If there's no group chat operation set we have nothing to do here.
    if (groupChatOpSet == null) return null;

    ChatRoom chatRoom = null;
    try {

      HashMap<String, Object> roomProperties = new HashMap<String, Object>();
      roomProperties.put("isPrivate", isPrivate);
      chatRoom = groupChatOpSet.createChatRoom(roomName, roomProperties);

      if (join) {
        chatRoom.join();

        for (String contact : contacts) chatRoom.invite(contact, reason);
      }
    } catch (OperationFailedException ex) {
      logger.error("Failed to create chat room.", ex);

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.ERROR"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CREATE_CHAT_ROOM_ERROR",
                      new String[] {protocolProvider.getProtocolDisplayName()}),
              ex);
    } catch (OperationNotSupportedException ex) {
      logger.error("Failed to create chat room.", ex);

      MUCActivator.getAlertUIService()
          .showAlertDialog(
              MUCActivator.getResources().getI18NString("service.gui.ERROR"),
              MUCActivator.getResources()
                  .getI18NString(
                      "service.gui.CREATE_CHAT_ROOM_ERROR",
                      new String[] {protocolProvider.getProtocolDisplayName()}),
              ex);
    }

    if (chatRoom != null) {
      ChatRoomProviderWrapper parentProvider =
          chatRoomList.findServerWrapperFromProvider(protocolProvider);

      // if there is the same room ids don't add new wrapper as old one
      // maybe already created
      chatRoomWrapper = chatRoomList.findChatRoomWrapperFromChatRoom(chatRoom);

      if (chatRoomWrapper == null) {
        chatRoomWrapper = new ChatRoomWrapperImpl(parentProvider, chatRoom);
        chatRoomWrapper.setPersistent(persistent);
        chatRoomList.addChatRoom(chatRoomWrapper);
      }
    }

    return chatRoomWrapper;
  }