-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_chat.java
113 lines (101 loc) · 3.69 KB
/
new_chat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.packet.Message;
public class new_chat extends Thread implements MessageListener
{
JFrame frame_chat;
JPanel panel_chat;
JPanel send_file;
JButton send_but;
JTextArea text;
JTextField textarea;
String receipient;
ChatManager Manager;
Chat chat;
JScrollPane scrollar;
String to = null, tos;
new_chat link = null;
public new_chat(String str)
{
receipient = str;
System.out.println("New_chat " + str);
}
public void run()
{
System.out.println("Run in New_chat ");
frame_chat = new JFrame(receipient);
panel_chat = new JPanel();
send_file = new JPanel();
send_but = new JButton("send a file");
/*These two lines are for send files Button
send_but.addActionListener(new sendfileto());
send_but.setActionCommand(connection.getRoster().getPresence(receipient).getFrom());
*/
text = new JTextArea(15, 30);
text.setLineWrap(true);
text.setEditable(false);
textarea = new JTextField("", 10);
scrollar = new JScrollPane(text);
scrollar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollar.setAutoscrolls(true);
scrollar.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel_chat.add(scrollar);
frame_chat.getContentPane().add(BorderLayout.NORTH, send_file);
frame_chat.getContentPane().add(BorderLayout.CENTER, panel_chat);
frame_chat.getContentPane().add(BorderLayout.SOUTH, textarea);
send_file.add(send_but);
textarea.requestFocus(); //better textarea.requestFocusInWindow() -> platform independent
System.out.println("New_chat in the middle of run " + receipient);
textarea.setActionCommand(receipient);
textarea.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
System.out.println("Action performed in new_chat: ");
String msg = textarea.getText();
textarea.setText("");
text.setForeground(Color.red);
text.append("YOU: " + msg + "\n");
String receipient = event.getActionCommand();
try{
sendMessage(msg, receipient);
}
catch(XMPPException ex){}
}
});
frame_chat.setSize(350, 300);
frame_chat.setVisible(true);
chat = (ley.connection).getXmppAddressOfChatPartner().chatWith(receipient,this); //getChatManager() -> getXmppAddressOfChatPartner
}
public void sendMessage(String message, String tos)throws XMPPException, InterruptedException, NotConnectedException
{
System.out.print("in send message");
chat.send(message);
scrollar.getCorner(ScrollPaneConstants.LOWER_LEFT_CORNER);
}
public void processMessage(Message msg, Chat chat)
{
System.out.println(Thread.currentThread());
System.out.print("in the process message of newchat");
if(msg.getType() == Message.Type.chat){
System.out.println(chat.getParticipant() + " says:" + msg.getBody());
}
scrollar.getCorner(ScrollPaneConstants.LOWER_LEFT_CORNER);
text.setForeground(Color.BLUE);
text.append(chat.getParticipant() + ": " + msg.getBody() + "\n");
}
}