Search in sources :

Example 36 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class SoftPhoneTabHandler method handlePhoneCall.

/**
 * Called when the underlying component has a phone call.
 *
 * @param state            the CallRoomState.
 * @param tab              the SparkTab.
 * @param component        the component within the tab.
 * @param isSelectedTab    true if the tab is selected.
 * @param chatFrameFocused true if the chat frame is in focus.
 */
private void handlePhoneCall(CallRoomState state, SparkTab tab, Component component, boolean isSelectedTab, boolean chatFrameFocused) {
    boolean isTyping = false;
    if (component instanceof ChatRoom) {
        isTyping = SparkManager.getChatManager().containsTypingNotification(((ChatRoom) component));
    }
    // Check if is typing.
    if (isTyping) {
        tab.setIcon(SparkRes.getImageIcon(SparkRes.SMALL_MESSAGE_EDIT_IMAGE));
    } else if (CallRoomState.inCall == state) {
        tab.setIcon(PhoneRes.getImageIcon("RECEIVER2_IMAGE"));
    } else if (CallRoomState.muted == state) {
        tab.setIcon(PhoneRes.getImageIcon("MUTE_IMAGE"));
    } else if (CallRoomState.onHold == state) {
        tab.setIcon(PhoneRes.getImageIcon("ON_HOLD_IMAGE"));
    } else if (CallRoomState.callWasEnded == state) {
        tab.setIcon(PhoneRes.getImageIcon("HANG_UP_PHONE_16x16_IMAGE"));
    }
    if (component instanceof ChatRoom) {
        handleChatRoom(component, tab, chatFrameFocused, isSelectedTab);
        return;
    } else {
        if (isSelectedTab && chatFrameFocused) {
            tab.setTitleColor(Color.black);
            tab.setTabFont(tab.getDefaultFont());
        }
    }
    // Handle title frame
    if (isSelectedTab && component instanceof PhonePanel) {
        final ChatFrame chatFrame = SparkManager.getChatManager().getChatContainer().getChatFrame();
        chatFrame.setTitle(((PhonePanel) component).getFrameTitle());
    }
}
Also used : ChatFrame(org.jivesoftware.spark.ui.ChatFrame) ChatRoom(org.jivesoftware.spark.ui.ChatRoom)

Example 37 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class TicTacToePlugin method addButtonToToolBar.

/**
 * Add the TTT-Button to every opening Chatroom
 * and create Listeners for it
 */
private void addButtonToToolBar() {
    _chatRoomListener = new ChatRoomListenerAdapter() {

        @Override
        public void chatRoomOpened(final ChatRoom room) {
            if (!(room instanceof ChatRoomImpl)) {
                // Don't do anything if this is not a 1on1-Chat
                return;
            }
            final ChatRoomButton sendGameButton = new ChatRoomButton(buttonimg);
            room.getToolBar().addChatRoomButton(sendGameButton);
            final String opponentJID = ((ChatRoomImpl) room).getJID();
            sendGameButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (_currentInvitations.contains(XmppStringUtils.parseBareJid(opponentJID))) {
                        return;
                    }
                    final GameOfferPacket offer = new GameOfferPacket();
                    offer.setTo(opponentJID);
                    offer.setType(IQ.Type.get);
                    _currentInvitations.add(XmppStringUtils.parseBareJid(opponentJID));
                    room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.sent"), false, false, Color.BLUE);
                    try {
                        SparkManager.getConnection().sendStanza(offer);
                    } catch (SmackException.NotConnectedException e1) {
                        Log.warning("Unable to send offer to " + opponentJID, e1);
                    }
                    SparkManager.getConnection().addAsyncStanzaListener(new StanzaListener() {

                        @Override
                        public void processPacket(Stanza stanza) {
                            GameOfferPacket answer = (GameOfferPacket) stanza;
                            answer.setStartingPlayer(offer.isStartingPlayer());
                            answer.setGameID(offer.getGameID());
                            if (answer.getType() == IQ.Type.result) {
                                // ACCEPT
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.accept"), false, false, Color.BLUE);
                                createTTTWindow(answer, opponentJID);
                            } else {
                                // DECLINE
                                room.getTranscriptWindow().insertCustomText(TTTRes.getString("ttt.request.decline"), false, false, Color.RED);
                                _currentInvitations.remove(XmppStringUtils.parseBareJid(opponentJID));
                            }
                        }
                    }, new PacketIDFilter(offer.getPacketID()));
                }
            });
        }

        @Override
        public void chatRoomClosed(ChatRoom room) {
            if (room instanceof ChatRoomImpl) {
                ChatRoomImpl cri = (ChatRoomImpl) room;
                _currentInvitations.remove(cri.getParticipantJID());
            }
        }
    };
    SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
Also used : ActionEvent(java.awt.event.ActionEvent) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ActionListener(java.awt.event.ActionListener) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GameOfferPacket(tic.tac.toe.packet.GameOfferPacket) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 38 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class TranslatorPlugin method initialize.

/**
 * Called after Spark is loaded to initialize the new plugin.
 */
public void initialize() {
    // Retrieve ChatManager from the SparkManager
    final ChatManager chatManager = SparkManager.getChatManager();
    // Add to a new ChatRoom when the ChatRoom opens.
    chatManager.addChatRoomListener(new ChatRoomListenerAdapter() {

        public void chatRoomOpened(ChatRoom room) {
            // only do the translation for single chat
            if (room instanceof ChatRoomImpl) {
                final ChatRoomImpl roomImpl = (ChatRoomImpl) room;
                // Create a new ChatRoomButton.
                final JComboBox<TranslatorUtil.TranslationType> translatorBox = new JComboBox<>(TranslatorUtil.TranslationType.getTypes());
                translatorBox.addActionListener(e -> {
                    // Set the focus back to the message box.
                    roomImpl.getChatInputEditor().requestFocusInWindow();
                });
                roomImpl.addChatRoomComponent(translatorBox);
                // do the translation for outgoing messages.
                final MessageEventListener messageListener = new MessageEventListener() {

                    public void sendingMessage(Message message) {
                        String currentBody = message.getBody();
                        String oldBody = message.getBody();
                        TranslatorUtil.TranslationType type = (TranslatorUtil.TranslationType) translatorBox.getSelectedItem();
                        if (type != null && type != TranslatorUtil.TranslationType.None) {
                            message.setBody(null);
                            currentBody = TranslatorUtil.translate(currentBody, type);
                            TranscriptWindow transcriptWindow = chatManager.getChatRoom(XmppStringUtils.parseBareJid(message.getTo())).getTranscriptWindow();
                            if (oldBody.equals(currentBody.substring(0, currentBody.length() - 1))) {
                                transcriptWindow.insertNotificationMessage("Could not translate: " + currentBody, ChatManager.ERROR_COLOR);
                            } else {
                                transcriptWindow.insertNotificationMessage("-> " + currentBody, Color.gray);
                                message.setBody(currentBody);
                            }
                        }
                    }

                    public void receivingMessage(Message message) {
                    // do nothing
                    }
                };
                roomImpl.addMessageEventListener(messageListener);
            }
        }
    });
}
Also used : Color(java.awt.Color) ChatManager(org.jivesoftware.spark.ChatManager) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) SparkManager(org.jivesoftware.spark.SparkManager) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) MessageEventListener(org.jivesoftware.spark.ui.MessageEventListener) Message(org.jivesoftware.smack.packet.Message) JComboBox(javax.swing.JComboBox) Plugin(org.jivesoftware.spark.plugin.Plugin) XmppStringUtils(org.jxmpp.util.XmppStringUtils) JComboBox(javax.swing.JComboBox) Message(org.jivesoftware.smack.packet.Message) MessageEventListener(org.jivesoftware.spark.ui.MessageEventListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatRoomListenerAdapter(org.jivesoftware.spark.ui.ChatRoomListenerAdapter) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) TranscriptWindow(org.jivesoftware.spark.ui.TranscriptWindow) ChatManager(org.jivesoftware.spark.ChatManager)

Example 39 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class Workspace method handleIncomingPacket.

private void handleIncomingPacket(Stanza stanza) throws SmackException.NotConnectedException {
    // We only handle message packets here.
    if (stanza instanceof Message) {
        final Message message = (Message) stanza;
        boolean isGroupChat = message.getType() == Message.Type.groupchat;
        // Check if Conference invite. If so, do not handle here.
        if (message.getExtension("x", "jabber:x:conference") != null) {
            return;
        }
        final String body = message.getBody();
        final JivePropertiesExtension extension = ((JivePropertiesExtension) message.getExtension(JivePropertiesExtension.NAMESPACE));
        final boolean broadcast = extension != null && extension.getProperty("broadcast") != null;
        // Handle offline message.
        DelayInformation offlineInformation = message.getExtension("delay", "urn:xmpp:delay");
        if (offlineInformation != null && (Message.Type.chat == message.getType() || Message.Type.normal == message.getType())) {
            handleOfflineMessage(message);
        }
        if (body == null || isGroupChat || broadcast || message.getType() == Message.Type.normal || message.getType() == Message.Type.headline || message.getType() == Message.Type.error) {
            return;
        }
        // Create new chat room for Agent Invite.
        final String from = stanza.getFrom();
        final String host = SparkManager.getSessionManager().getServerAddress();
        // Don't allow workgroup notifications to come through here.
        final String bareJID = XmppStringUtils.parseBareJid(from);
        if (host.equalsIgnoreCase(from) || from == null) {
            return;
        }
        ChatRoom room = null;
        try {
            room = SparkManager.getChatManager().getChatContainer().getChatRoom(bareJID);
        } catch (ChatRoomNotFoundException e) {
        // Ignore
        }
        // Check for non-existent rooms.
        if (room == null) {
            createOneToOneRoom(bareJID, message);
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension)

Example 40 with ChatRoom

use of org.jivesoftware.spark.ui.ChatRoom in project Spark by igniterealtime.

the class InvitationDialog method inviteUsersToRoom.

public void inviteUsersToRoom(final String serviceName, Collection<BookmarkedConference> rooms, String adHocRoomName, Collection<String> jids) {
    fillRoomsUI(rooms, adHocRoomName);
    JFrame parent = SparkManager.getChatManager().getChatContainer().getChatFrame();
    if (parent == null || !parent.isVisible()) {
        parent = SparkManager.getMainWindow();
    }
    // Add jids to user list
    if (jids != null) {
        for (Object jid : jids) {
            invitedUsers.addElement(jid);
        }
    }
    final JOptionPane pane;
    TitlePanel titlePanel;
    // Create the title panel for this dialog
    titlePanel = new TitlePanel(Res.getString("title.invite.to.conference"), Res.getString("message.invite.users.to.conference"), SparkRes.getImageIcon(SparkRes.BLANK_IMAGE), true);
    // Construct main panel w/ layout.
    final JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(titlePanel, BorderLayout.NORTH);
    // The user should only be able to close this dialog.
    Object[] options = { Res.getString("invite"), Res.getString("cancel") };
    pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    mainPanel.add(pane, BorderLayout.CENTER);
    final JOptionPane p = new JOptionPane();
    dlg = p.createDialog(parent, Res.getString("title.conference.rooms"));
    dlg.setModal(false);
    dlg.pack();
    dlg.setSize(500, 450);
    dlg.setResizable(true);
    dlg.setContentPane(mainPanel);
    dlg.setLocationRelativeTo(parent);
    PropertyChangeListener changeListener = e -> {
        String value = (String) pane.getValue();
        if (Res.getString("cancel").equals(value)) {
            pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            dlg.dispose();
        } else if (Res.getString("invite").equals(value)) {
            final String roomTitle = getSelectedRoomName();
            final BookmarkedConference selectedBookmarkedConf = getSelectedBookmarkedConference();
            int size = invitedUserList.getModel().getSize();
            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
            if (size == 0) {
                JOptionPane.showMessageDialog(dlg, Res.getString("message.specify.users.to.join.conference"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            if (!ModelUtil.hasLength(roomTitle)) {
                JOptionPane.showMessageDialog(dlg, Res.getString("message.no.room.to.join.error"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            String roomName = "";
            // Add all rooms the user is in to list.
            ChatManager chatManager = SparkManager.getChatManager();
            for (ChatRoom chatRoom : chatManager.getChatContainer().getChatRooms()) {
                if (chatRoom instanceof GroupChatRoom) {
                    GroupChatRoom groupRoom = (GroupChatRoom) chatRoom;
                    if (groupRoom.getRoomname().equals(roomTitle)) {
                        roomName = groupRoom.getMultiUserChat().getRoom();
                        break;
                    }
                }
            }
            String message = messageField.getText();
            final String messageText = message != null ? message : Res.getString("message.please.join.in.conference");
            if (invitedUsers.getSize() > 0) {
                invitedUserList.setSelectionInterval(0, invitedUsers.getSize() - 1);
            }
            GroupChatRoom chatRoom;
            try {
                chatRoom = SparkManager.getChatManager().getGroupChat(roomName);
            } catch (ChatNotFoundException e1) {
                dlg.setVisible(false);
                final List<String> jidList = new ArrayList<>();
                Object[] jids1 = invitedUserList.getSelectedValues();
                final int no = jids1 != null ? jids1.length : 0;
                for (int i = 0; i < no; i++) {
                    try {
                        jidList.add((String) jids1[i]);
                    } catch (NullPointerException ee) {
                        Log.error(ee);
                    }
                }
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            Thread.sleep(15);
                        } catch (InterruptedException e2) {
                            Log.error(e2);
                        }
                        return "ok";
                    }

                    public void finished() {
                        try {
                            if (selectedBookmarkedConf == null) {
                                ConferenceUtils.createPrivateConference(serviceName, messageText, roomTitle, jidList);
                            } else {
                                ConferenceUtils.joinConferenceOnSeperateThread(selectedBookmarkedConf.getName(), selectedBookmarkedConf.getJid(), selectedBookmarkedConf.getPassword(), messageText, jidList);
                            }
                        } catch (SmackException ex) {
                            UIManager.put("OptionPane.okButtonText", Res.getString("ok"));
                            JOptionPane.showMessageDialog(pane, "An error occurred.", Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                        }
                    }
                };
                worker.start();
                pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                return;
            }
            pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
            dlg.dispose();
            Object[] values = invitedUserList.getSelectedValues();
            final int no = values != null ? values.length : 0;
            for (int i = 0; i < no; i++) {
                String jid = (String) values[i];
                try {
                    chatRoom.getMultiUserChat().invite(jid, message != null ? message : Res.getString("message.please.join.in.conference"));
                } catch (SmackException.NotConnectedException e1) {
                    Log.warning("Unable to send stanza to " + jid, e1);
                }
                String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
                chatRoom.getTranscriptWindow().insertNotificationMessage("Invited " + nickname, ChatManager.NOTIFICATION_COLOR);
            }
        }
    };
    pane.addPropertyChangeListener(changeListener);
    dlg.setVisible(true);
    dlg.toFront();
    dlg.requestFocus();
}
Also used : SmackException(org.jivesoftware.smack.SmackException) UIManager(javax.swing.UIManager) Insets(java.awt.Insets) JDialog(javax.swing.JDialog) JTextField(javax.swing.JTextField) SettingsManager(org.jivesoftware.sparkimpl.settings.local.SettingsManager) Res(org.jivesoftware.resource.Res) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Log(org.jivesoftware.spark.util.log.Log) Action(javax.swing.Action) ResourceUtils(org.jivesoftware.spark.util.ResourceUtils) ArrayList(java.util.ArrayList) MouseAdapter(java.awt.event.MouseAdapter) JComboBox(javax.swing.JComboBox) JFrame(javax.swing.JFrame) BorderLayout(java.awt.BorderLayout) JComponent(javax.swing.JComponent) ChatManager(org.jivesoftware.spark.ChatManager) ModelUtil(org.jivesoftware.spark.util.ModelUtil) JButton(javax.swing.JButton) SparkRes(org.jivesoftware.resource.SparkRes) JPopupMenu(javax.swing.JPopupMenu) Collection(java.util.Collection) JList(javax.swing.JList) SwingWorker(org.jivesoftware.spark.util.SwingWorker) JOptionPane(javax.swing.JOptionPane) GridBagConstraints(java.awt.GridBagConstraints) TitlePanel(org.jivesoftware.spark.component.TitlePanel) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ActionEvent(java.awt.event.ActionEvent) XmppStringUtils(org.jxmpp.util.XmppStringUtils) MouseEvent(java.awt.event.MouseEvent) JScrollPane(javax.swing.JScrollPane) ChatNotFoundException(org.jivesoftware.spark.ChatNotFoundException) List(java.util.List) DefaultListModel(javax.swing.DefaultListModel) AbstractAction(javax.swing.AbstractAction) PropertyChangeListener(java.beans.PropertyChangeListener) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) SparkManager(org.jivesoftware.spark.SparkManager) JLabel(javax.swing.JLabel) RosterPickList(org.jivesoftware.spark.ui.RosterPickList) GridBagLayout(java.awt.GridBagLayout) JPanel(javax.swing.JPanel) JPanel(javax.swing.JPanel) PropertyChangeListener(java.beans.PropertyChangeListener) SmackException(org.jivesoftware.smack.SmackException) BookmarkedConference(org.jivesoftware.smackx.bookmarks.BookmarkedConference) JOptionPane(javax.swing.JOptionPane) TitlePanel(org.jivesoftware.spark.component.TitlePanel) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChatNotFoundException(org.jivesoftware.spark.ChatNotFoundException) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) RosterPickList(org.jivesoftware.spark.ui.RosterPickList) ChatManager(org.jivesoftware.spark.ChatManager)

Aggregations

ChatRoom (org.jivesoftware.spark.ui.ChatRoom)45 ChatManager (org.jivesoftware.spark.ChatManager)10 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)10 ActionEvent (java.awt.event.ActionEvent)8 Message (org.jivesoftware.smack.packet.Message)8 ChatRoomNotFoundException (org.jivesoftware.spark.ui.ChatRoomNotFoundException)8 ContactItem (org.jivesoftware.spark.ui.ContactItem)7 Presence (org.jivesoftware.smack.packet.Presence)6 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)6 ActionListener (java.awt.event.ActionListener)5 MouseEvent (java.awt.event.MouseEvent)5 Date (java.util.Date)5 JButton (javax.swing.JButton)5 JLabel (javax.swing.JLabel)5 JPanel (javax.swing.JPanel)5 SmackException (org.jivesoftware.smack.SmackException)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JPopupMenu (javax.swing.JPopupMenu)4 SparkManager (org.jivesoftware.spark.SparkManager)4