Search in sources :

Example 21 with ChatRoomImpl

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

the class PhoneManager method chatRoomOpened.

public void chatRoomOpened(final ChatRoom room) {
    if (!phones.isEmpty() && room instanceof ChatRoomImpl) {
        final ChatRoomImpl chatRoomImpl = (ChatRoomImpl) room;
        final ChatRoomButton dialButton = new ChatRoomButton(SparkRes.getImageIcon(SparkRes.DIAL_PHONE_IMAGE_24x24));
        dialButton.setToolTipText(Res.getString("tooltip.place.voice.call"));
        final List<Action> actions = new ArrayList<>();
        SwingWorker actionWorker = new SwingWorker() {

            public Object construct() {
                for (Phone phone : phones) {
                    final Collection<Action> phoneActions = phone.getPhoneActions(chatRoomImpl.getParticipantJID());
                    if (phoneActions != null) {
                        for (Action action : phoneActions) {
                            actions.add(action);
                        }
                    }
                }
                return actions;
            }

            public void finished() {
                if (!actions.isEmpty()) {
                    room.addChatRoomButton(dialButton, true);
                }
            }
        };
        actionWorker.start();
        dialButton.addMouseListener(new MouseAdapter() {

            public void mousePressed(final MouseEvent e) {
                SwingWorker worker = new SwingWorker() {

                    public Object construct() {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        return true;
                    }

                    public void finished() {
                        // Handle actions.
                        if (actions.size() > 0) {
                            // Display PopupMenu
                            final JPopupMenu menu = new JPopupMenu();
                            for (Action action : actions) {
                                menu.add(action);
                            }
                            menu.show(dialButton, e.getX(), e.getY());
                        }
                    }
                };
                worker.start();
            }
        });
    }
}
Also used : Action(javax.swing.Action) MouseEvent(java.awt.event.MouseEvent) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MouseAdapter(java.awt.event.MouseAdapter) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) JPopupMenu(javax.swing.JPopupMenu) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ChatRoomButton(org.jivesoftware.spark.ui.ChatRoomButton)

Example 22 with ChatRoomImpl

use of org.jivesoftware.spark.ui.rooms.ChatRoomImpl 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 23 with ChatRoomImpl

use of org.jivesoftware.spark.ui.rooms.ChatRoomImpl 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 24 with ChatRoomImpl

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

the class ChatManager method cancelledNotification.

public void cancelledNotification(final String from, final ChatState state) {
    SwingUtilities.invokeLater(() -> {
        ContactList contactList = SparkManager.getWorkspace().getContactList();
        ChatRoom chatRoom;
        try {
            chatRoom = getChatContainer().getChatRoom(XmppStringUtils.parseBareJid(from));
            if (chatRoom != null && chatRoom instanceof ChatRoomImpl) {
                typingNotificationList.remove(chatRoom);
                // Notify Decorators
                notifySparkTabHandlers(chatRoom);
                ((ChatRoomImpl) chatRoom).notifyChatStateChange(state);
            }
        } catch (ChatRoomNotFoundException e) {
        // Do nothing
        }
        contactList.useDefaults(from);
    });
}
Also used : GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Example 25 with ChatRoomImpl

use of org.jivesoftware.spark.ui.rooms.ChatRoomImpl 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;
}
Also used : ChatRoom(org.jivesoftware.spark.ui.ChatRoom) Presence(org.jivesoftware.smack.packet.Presence) Icon(javax.swing.Icon) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl)

Aggregations

ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)25 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)9 ChatManager (org.jivesoftware.spark.ChatManager)8 GroupChatRoom (org.jivesoftware.spark.ui.rooms.GroupChatRoom)6 SwingWorker (org.jivesoftware.spark.util.SwingWorker)5 File (java.io.File)4 StanzaListener (org.jivesoftware.smack.StanzaListener)4 Message (org.jivesoftware.smack.packet.Message)4 Presence (org.jivesoftware.smack.packet.Presence)4 ChatRoomButton (org.jivesoftware.spark.ui.ChatRoomButton)4 ChatRoomNotFoundException (org.jivesoftware.spark.ui.ChatRoomNotFoundException)4 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 SmackException (org.jivesoftware.smack.SmackException)3 Stanza (org.jivesoftware.smack.packet.Stanza)3 SparkManager (org.jivesoftware.spark.SparkManager)3 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)3 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)3 java.awt.event (java.awt.event)2 TimerTask (java.util.TimerTask)2