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);
}
}
});
}
Aggregations