use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.
the class JoinRoomSwingWorker method construct.
@Override
public Object construct() {
try {
groupChat = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getMultiUserChat(roomJID);
// Create a UI component, if one was not yet created. It is important that this happens before the MUC is
// joined server-side, as the UI component needs to be able to display data that is sent by the server upon
// joining the room.
ChatRoom room;
try {
final String roomName = XmppStringUtils.parseBareJid(groupChat.getRoom());
room = SparkManager.getChatManager().getChatContainer().getChatRoom(roomName);
} catch (ChatRoomNotFoundException e) {
room = UIComponentRegistry.createGroupChatRoom(groupChat);
((GroupChatRoom) room).setPassword(password);
((GroupChatRoom) room).setTabTitle(tabTitle);
}
// Use the default nickname, if none has been provided.
if (!ModelUtil.hasLength(nickname)) {
nickname = SettingsManager.getRelodLocalPreferences().getNickname().trim();
}
// Join the MUC server-sided, if we're not already in.
if (!groupChat.isJoined()) {
if (password == null && ConferenceUtils.isPasswordRequired(roomJID)) {
JLabel label = new JLabel(Res.getString("message.enter.room.password"));
JPasswordField passwordField = new JPasswordField();
passwordField.addAncestorListener(new RequestFocusListener());
JOptionPane.showConfirmDialog(null, new Object[] { label, passwordField }, Res.getString("title.password.required"), JOptionPane.OK_CANCEL_OPTION);
password = new String(passwordField.getPassword());
if (!ModelUtil.hasLength(password)) {
return null;
}
}
if (!ConferenceUtils.confirmToRevealVisibility()) {
return null;
}
if (ModelUtil.hasLength(password)) {
groupChat.join(nickname, password);
} else {
groupChat.join(nickname);
}
}
return room;
} catch (XMPPException | SmackException ex) {
Log.error("An exception occurred while trying to join room '" + roomJID + "'.", ex);
XMPPError error = null;
if (ex instanceof XMPPException.XMPPErrorException) {
error = ((XMPPException.XMPPErrorException) ex).getXMPPError();
if (XMPPError.Condition.conflict.equals(error.getCondition())) {
final Object userInput = JOptionPane.showInputDialog(SparkManager.getMainWindow(), Res.getString("message.nickname.in.use"), Res.getString("title.change.nickname"), JOptionPane.WARNING_MESSAGE, null, // null selection values implies text field.
null, nickname);
if (userInput != null) {
Log.debug("Retry joining room '" + roomJID + "', using nickname: " + userInput);
this.nickname = (String) userInput;
return construct();
}
}
}
final String errorText = ConferenceUtils.getReason(error);
errors.add(errorText);
return null;
}
}
use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.
the class DefaultTabHandler method isTabHandled.
public boolean isTabHandled(SparkTab tab, Component component, boolean isSelectedTab, boolean chatFrameFocused) {
if (component instanceof ChatRoom) {
ChatRoom room = (ChatRoom) component;
boolean isStaleRoom = SparkManager.getChatManager().isStaleRoom(room);
boolean isTyping = SparkManager.getChatManager().containsTypingNotification((ChatRoom) component);
// Check if is typing.
if (isTyping) {
tab.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_EDIT_IMAGE));
} else if (room instanceof ChatRoomImpl && !isStaleRoom) {
// User is not typing, therefore show default presence icon.
String participantJID = ((ChatRoomImpl) room).getParticipantJID();
Presence presence = PresenceManager.getPresence(participantJID);
Icon icon = PresenceManager.getIconFromPresence(presence);
tab.setIcon(icon);
}
if (!chatFrameFocused || !isSelectedTab) {
if (room.getUnreadMessageCount() > 0) {
// Make tab red.
tab.setTitleColor((Color) UIManager.get("Chat.unreadMessageColor"));
tab.setTabBold(true);
}
// Handle unread message count.
int unreadMessageCount = room.getUnreadMessageCount();
String appendedMessage = "";
if (unreadMessageCount > 1) {
appendedMessage = " (" + unreadMessageCount + ")";
}
tab.setTabTitle(room.getTabTitle() + appendedMessage);
}
// Check if the room is stale.
if (isStaleRoom && component instanceof ChatRoomImpl) {
decorateStaleTab(tab, (ChatRoom) component);
} else // and the tab is the selected component.
if (isSelectedTab && chatFrameFocused) {
tab.setTitleColor(Color.black);
// tab.setTabFont(tab.getDefaultFont());
tab.setTabTitle(room.getTabTitle());
// Clear unread message count.
room.clearUnreadMessageCount();
}
} else {
if (!chatFrameFocused || !isSelectedTab) {
// Make tab red.
tab.setTitleColor((Color) UIManager.get("Chat.unreadMessageColor"));
tab.setTabBold(true);
}
if (isSelectedTab && chatFrameFocused) {
tab.setTitleColor(Color.black);
tab.setTabFont(tab.getDefaultFont());
}
}
return true;
}
use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.
the class SparkTransferManager method addPresenceListener.
private void addPresenceListener() {
SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
Presence presence = (Presence) stanza;
if (presence.isAvailable()) {
String bareJID = XmppStringUtils.parseBareJid(presence.getFrom());
// Iterate through map.
ArrayList<File> list = waitMap.get(bareJID);
if (list != null) {
// Iterate through list and send.
Iterator<File> iter = list.iterator();
ChatRoom room = null;
while (iter.hasNext()) {
File file = iter.next();
room = sendFile(file, bareJID);
}
if (room != null) {
Message message = new Message();
message.setBody(Res.getString("message.sent.offline.files"));
room.sendMessage(message);
}
}
waitMap.remove(bareJID);
}
}, new StanzaTypeFilter(Presence.class));
}
use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.
the class SparkTransferManager method sendFile.
/**
* Send a file to a user.
*
* @param file the file to send.
* @param jid the jid of the user to send the file to.
* @return the ChatRoom of the user.
*/
public ChatRoom sendFile(File file, String jid) {
long maxsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_MAXIMUM_SIZE));
long warningsize = Long.parseLong(Default.getString(Default.FILE_TRANSFER_WARNING_SIZE));
if (file.length() >= maxsize && maxsize != -1) {
String maxsizeString = TransferUtils.getAppropriateByteWithSuffix(maxsize);
String yoursizeString = TransferUtils.getAppropriateByteWithSuffix(file.length());
String output = Res.getString("message.file.transfer.file.too.big.error", maxsizeString, yoursizeString);
UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
JOptionPane.showMessageDialog(null, output, Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
return null;
}
if (file.length() >= warningsize && warningsize != -1) {
int result = JOptionPane.showConfirmDialog(null, Res.getString("message.file.transfer.file.too.big.warning"), Res.getString("title.error"), JOptionPane.YES_NO_OPTION);
if (result != 0) {
return null;
}
}
final ContactList contactList = SparkManager.getWorkspace().getContactList();
String bareJID = XmppStringUtils.parseBareJid(jid);
String fullJID = PresenceManager.getFullyQualifiedJID(jid);
if (!PresenceManager.isOnline(jid)) {
ArrayList<File> list = waitMap.get(jid);
if (list == null) {
list = new ArrayList<>();
}
list.add(file);
waitMap.put(jid, list);
ChatRoom chatRoom;
ContactItem contactItem = contactList.getContactItemByJID(jid);
if (contactItem != null) {
chatRoom = SparkManager.getChatManager().createChatRoom(jid, contactItem.getDisplayName(), contactItem.getDisplayName());
} else {
chatRoom = SparkManager.getChatManager().createChatRoom(jid, jid, jid);
}
chatRoom.getTranscriptWindow().insertNotificationMessage("The user is offline. Will auto-send \"" + file.getName() + "\" when user comes back online.", ChatManager.ERROR_COLOR);
return null;
}
// Create the outgoing file transfer
final OutgoingFileTransfer transfer = transferManager.createOutgoingFileTransfer(fullJID);
ContactItem contactItem = contactList.getContactItemByJID(bareJID);
ChatRoom chatRoom;
if (contactItem != null) {
chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, contactItem.getDisplayName(), contactItem.getDisplayName());
} else {
chatRoom = SparkManager.getChatManager().createChatRoom(bareJID, bareJID, bareJID);
}
TranscriptWindow transcriptWindow = chatRoom.getTranscriptWindow();
SendFileTransfer sendingUI = new SendFileTransfer();
try {
transfer.sendFile(file, "Sending file");
} catch (SmackException e) {
Log.error(e);
}
// Add listener to cancel transfer is sending file to user who just went offline.
AndFilter presenceFilter = new AndFilter(new StanzaTypeFilter(Presence.class), FromMatchesFilter.createBare(bareJID));
final StanzaListener packetListener = stanza -> {
Presence presence = (Presence) stanza;
if (!presence.isAvailable()) {
if (transfer != null) {
transfer.cancel();
}
}
};
// Add presence listener to check if user is offline and cancel sending.
SparkManager.getConnection().addAsyncStanzaListener(packetListener, presenceFilter);
chatRoom.addClosingListener(() -> {
SparkManager.getConnection().removeAsyncStanzaListener(packetListener);
if (!transfer.isDone()) {
transfer.cancel();
}
});
try {
sendingUI.sendFile(transfer, transferManager, fullJID, contactItem.getDisplayName());
} catch (NullPointerException e) {
Log.error(e);
}
transcriptWindow.addComponent(sendingUI);
chatRoom.scrollToBottom();
return chatRoom;
}
use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.
the class BattleshipPlugin method showInvitationInChat.
private void showInvitationInChat(final GameOfferPacket invitation) {
invitation.setType(IQ.Type.result);
invitation.setTo(invitation.getFrom());
final ChatRoom room = SparkManager.getChatManager().getChatRoom(XmppStringUtils.parseBareJid(invitation.getFrom()));
String name = XmppStringUtils.parseLocalpart(invitation.getFrom());
final JPanel panel = new JPanel();
JLabel text = new JLabel("Game request from" + name);
JLabel game = new JLabel("Battleships");
game.setFont(new Font("Dialog", Font.BOLD, 24));
game.setForeground(Color.RED);
JButton accept = new JButton(Res.getString("button.accept").replace("&", ""));
JButton decline = new JButton(Res.getString("button.decline").replace("&", ""));
panel.add(text);
panel.add(game);
panel.add(accept);
panel.add(decline);
room.getTranscriptWindow().addComponent(panel);
accept.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
SparkManager.getConnection().sendStanza(invitation);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to send invitation accept to " + invitation.getTo(), e1);
}
invitation.setStartingPlayer(!invitation.isStartingPlayer());
ChatRoomOpeningListener.createWindow(invitation, invitation.getFrom());
panel.remove(3);
panel.remove(2);
panel.repaint();
panel.revalidate();
}
});
decline.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
invitation.setType(IQ.Type.error);
try {
SparkManager.getConnection().sendStanza(invitation);
} catch (SmackException.NotConnectedException e1) {
Log.warning("Unable to send invitation decline to " + invitation.getTo(), e1);
}
panel.remove(3);
panel.remove(2);
panel.repaint();
panel.revalidate();
}
});
}
Aggregations