Search in sources :

Example 1 with MessageEventListener

use of org.jivesoftware.spark.ui.MessageEventListener 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)

Aggregations

Color (java.awt.Color)1 JComboBox (javax.swing.JComboBox)1 Message (org.jivesoftware.smack.packet.Message)1 ChatManager (org.jivesoftware.spark.ChatManager)1 SparkManager (org.jivesoftware.spark.SparkManager)1 Plugin (org.jivesoftware.spark.plugin.Plugin)1 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)1 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)1 MessageEventListener (org.jivesoftware.spark.ui.MessageEventListener)1 TranscriptWindow (org.jivesoftware.spark.ui.TranscriptWindow)1 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)1 XmppStringUtils (org.jxmpp.util.XmppStringUtils)1