Sending funny faces and icons using smack

Does anyone can help me by sending me an example of sending icons between two smack clients? I mean faces like in MSN messenger…

Hi ziv,

Are you referring to something like this, ? If so, that isn’'t really anything for smack to handle, rather your client will have to be written to look for colon immediately followed by a left parenthesis and if it finds one substitute with an icon (a url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JEditorPane.htmlJEditorPane[/url] might work well for such a thing).

Hope that helps,

Ryan

can you send me please a simple example?

Hi ziv,

Below is an example that will get you started (I went with a JTextPane instead of a JOptionPane). Modifying it to recognize other emoticons would be fairly straight forward.

import javax.swing.ImageIcon;

import javax.swing.JDialog;

import javax.swing.JTextPane;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

import javax.swing.text.BadLocationException;

import javax.swing.text.Element;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

import org.jivesoftware.smack.PacketListener;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.filter.PacketTypeFilter;

import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Packet;

public class SmileyDemo extends JDialog {

private JTextPane pane = new JTextPane();

private StyledDocument doc = pane.getStyledDocument();

private XMPPConnection connection = null;

private PacketFilter filter = null;

private static String server = “localhost”;

private static String user = “test”;

public static void main(String[] args) {

new SmileyDemo();

}

private SmileyDemo() {

initUI();

initListeners();

try {

initConnection();

} catch (XMPPException e) {

e.printStackTrace();

}

}

private void initUI() {

setTitle(“SmileyDemo”);

setSize(300, 200);

setVisible(true);

setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);

pane.setEditable(false);

getContentPane().add(pane);

}

private void initListeners() {

doc.addDocumentListener(new ChatListener());

}

private void initConnection() throws XMPPException {

// XMPPConnection.DEBUG_ENABLED = true;

connection = new XMPPConnection(server);

connection.login(user, “test”);

filter = new PacketTypeFilter(Message.class);

connection.addPacketListener(new PPacketListener(), filter);

}

private class PPacketListener implements PacketListener {

public void processPacket(Packet packet) {

Message message = (Message) packet;

String from = packet.getFrom();

String body = message.getBody();

try {

doc.insertString(doc.getLength(), “\n” + from + ": " , null);

doc.insertString(doc.getLength(), body, null);

} catch (BadLocationException e) {

e.printStackTrace();

}

}

}

private class ChatListener implements DocumentListener {

public void changedUpdate(DocumentEvent e) {

}

public void insertUpdate(DocumentEvent e) {

Thread th = new Thread(new SmileyThread());

th.start();

}

public void removeUpdate(DocumentEvent e) {

}

}

private class SmileyThread implements Runnable {

public void run() {

try {

StyledDocument docCopy = pane.getStyledDocument();

String text = docCopy.getText(0, docCopy.getLength());

SimpleAttributeSet smi;

int index;

int start = 0;

char c;

index = text.indexOf(’’:’’);

while (index > -1) {

Element e1 = docCopy.getCharacterElement(index);

if (StyleConstants.getIcon(e1.getAttributes()) == null) {

if (index < text.length()) {

c = text.charAt(index + 1);

} else {

break;

}

switch © {

case ‘’)’’:

docCopy.remove(index, 2);

smi = new SimpleAttributeSet();

StyleConstants.setIcon(smi, new ImageIcon(“happy.gif”));

docCopy.insertString(index, “:)”, smi);

start = (index + 2);

break;

case ‘’(’’:

docCopy.remove(index, 2);

smi = new SimpleAttributeSet();

StyleConstants.setIcon(smi, new ImageIcon(“sad.gif”));

docCopy.insertString(index, “:(”, smi);

start = (index + 2);

break;

default:

start = (index + 1);

}

} else {

start = (index + 1);

}

index = (text.indexOf(’’:’’, start));

}

pane.setStyledDocument(docCopy);

} catch (BadLocationException e) {

e.printStackTrace();

}

}

}

}

/code

Good luck,

Ryan

I really want to thnak you!

You made my work with smack easier!

Thanks,

Ziv

But if I want to send an icon from client to client not searching for : and than replace it with an icon. I mean to choose an icon from a list and than send it to other client. What is the easiest way to do that?

Hi Ziv,

I am not sure if I understand what you are asking. Do you want to send the file that contains the icon?

Thanks,

Ryan

It’'s okay I finally understand what to do

thanks

ziv

Hi!

I have tried to run your code it works fine.

but it throws badLocation when reaching to the end of the JTextPane.

I think this is because when the thread change it to an icon it activate another thread and we have kind of infient loop. is this the best solution or maybe we have a better solution to it?