📄 controller.java
字号:
package client;
import java.awt.Color;
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.BindException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import rule.AudioPlay;
import rule.Embody;
import rule.Order;
import rule.Message;
import rule.MyBean;
public class Controller {
private AudioPlay aplay = new AudioPlay();
private Login login = null;
private QQ2009 qq2009 = null;
private MyClient myClient = null;
private boolean b_Flag = false;
private boolean b_u = false;
private int fqq = 0;
private int mqq = 0;
private int lqq = 0;
private Thread thread = null;
private String order = null;
private List<MyBean> list = null;
private List<Message> list2 = null;
private List<MyBean> list3 = null;
private MyBean myBean = null;
private JTree jTree = null;
private FriendTree friendTree = null;
private Map<Integer, MyBean> map = new HashMap<Integer, MyBean>();
private Set<Integer> set = new HashSet<Integer>();
private Set<Integer> set2 = new HashSet<Integer>();
private Hashtable<Integer, ChatDialog> table = new Hashtable<Integer, ChatDialog>();
private Hashtable<Integer, FriendInfo> hashTable = new Hashtable<Integer, FriendInfo>();
public Controller() {
}
public Controller(Login login) {
this.login = login;
myClient = new MyClient(this);
}
/**
* 用来判断注册信息
*
* @param nickName
* @param password1
* @param password2
* @param portrait
* @param sex
* @param age
* @param name
* @param info
*/
public void register(String nickName, String password1, String password2,
String portrait, String sex, String age, String name, String info) {
if (nickName.length() < 2 || nickName.length() > 20) {
JOptionPane.showMessageDialog(null, "呢称必须在2 - 20位之间", "注册", 0);
return;
} else {
}
if (password1.equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空", "注册", 0);
return;
} else {
if (password1.length() < 6 || password1.length() > 18) {
JOptionPane.showMessageDialog(null, "密码长度在6 - 18位之间", "注册", 0);
return;
}
}
if (!password1.equals(password2)) {
JOptionPane.showMessageDialog(null, "对不起您两次输入的密码不一致", "注册", 0);
return;
} else {
}
if (info.equals("")) {
JOptionPane.showMessageDialog(null, "个人信息不能为空", "注册", 0);
return;
} else {
if (info.length() > 200 || info.length() < 10) {
JOptionPane
.showMessageDialog(null, "个人信息只能在10-200字以内", "注册", 0);
return;
}
}
if (age.equals("")) {
JOptionPane.showMessageDialog(null, "年龄不能为空", "注册", 0);
return;
} else {
if (age.length() > 2) {
JOptionPane.showMessageDialog(null, "对不起您输入的年龄有误", "注册", 0);
return;
}
}
MyBean myBean = new MyBean();
myBean.setNickName(nickName);
myBean.setPassword(password2);
myBean.setPortrait(portrait);
myBean.setSex(sex);
myBean.setName(name);
myBean.setAge(age);
myBean.setInfo(info);
Embody embody = new Embody();
embody.setOrder(Order.REGISTER);
embody.setObjMessage(myBean);
myClient.send(embody);
}
/**
* 用来判断登录信息
*
* @param qq
* @param password
* @param n
*/
public void login(String qq, String password, int n) {
int qq2 = 0;
if (qq.length() != 4) {
JOptionPane.showMessageDialog(null, "你输入的号码长度有误!", "登录", 0);
return;
} else {
try {
qq2 = Integer.parseInt(qq);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "您输入的号码有误!" + "\n"
+ "请输入数字!", "登录", 0);
return;
}
}
if (password.equals("")) {
JOptionPane.showMessageDialog(null, "请输入你的密码", "登录", 0);
return;
} else {
if (password.length() < 6 || password.length() > 18) {
JOptionPane
.showMessageDialog(null, "密码长度必须在6 - 18位之间", "登录", 0);
return;
}
}
MyBean myBean = new MyBean();
myBean.setQq(qq2);
if (n == 0) {
myBean.setState(Order.STATE_ON);
} else {
myBean.setState(Order.STATE_OFF);
}
myBean.setPassword(password);
myBean.setIp(myClient.getMip());
myBean.setUPort(myClient.getMUPort());
Embody embody = new Embody();
embody.setOrder(Order.LOGIN);
embody.setObjMessage(myBean);
myClient.send(embody);
}
/**
* 用来更改个人信息
*
* @param portrait
*/
public void changeInfo(String nickName, String portrait, String sex,
String age, String name, String info) {
MyBean myBean = new MyBean();
myBean.setNickName(nickName);
myBean.setPortrait(portrait);
myBean.setSex(sex);
myBean.setName(name);
myBean.setAge(age);
myBean.setInfo(info);
Embody embody = new Embody();
embody.setOrder(Order.MODIFY_INFO);
embody.setObjMessage(myBean);
myClient.send(embody);
}
/**
* 用来更改个人状态
*
* @param n
*/
public void changeState(int n) {
Embody embody = new Embody();
MyBean myBean = new MyBean();
myBean.setQq(mqq);
if (n == 0) {
qq2009.getJLabel().setEnabled(true);
myBean.setState(Order.STATE_ON);
embody.setOrder(Order.ME_ON_LINE);
} else {
qq2009.getJLabel().setEnabled(false);
myBean.setState(Order.STATE_OFF);
embody.setOrder(Order.ME_OFF_LINE);
}
embody.setObjMessage(myBean);
myClient.send(embody);
}
/**
* 用来查找好友
*
* @param searchWay
* @param qq
*/
public void searchFriend(String searchWay, String qq) {
if (searchWay.equals(Order.EXACT_SEARCH)) {
int fqq = 0;
if (qq.length() != 4) {
JOptionPane.showMessageDialog(null, "你输入的号码有误!\n请重试!", "查找", 0);
return;
} else {
try {
fqq = Integer.parseInt(qq);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "您输入的号码有误!\n请输入数字!",
"查找", 0);
return;
}
}
MyBean myBean = new MyBean();
myBean.setQq(fqq);
Embody embody = new Embody();
embody.setOrder(Order.EXACT_SEARCH);
embody.setObjMessage(myBean);
myClient.send(embody);
} else {
Embody embody = new Embody();
embody.setOrder(Order.BASIC_SEARCH);
myClient.send(embody);
}
}
/**
* 用来添加好友 添加好友第一步向服务端发出加好友的信息
*
* @param qq
*/
public void addFriend(int qq) {
if (qq == mqq) {
JOptionPane.showMessageDialog(null, "对不起,请不要加自己为好友!", "添加好友",
JOptionPane.WARNING_MESSAGE);
} else {
MyBean myBean = new MyBean();
myBean.setQq(qq);
Embody embody = new Embody();
embody.setOrder(Order.ADD_FRIEND);
embody.setObjMessage(myBean);
myClient.send(embody);
}
}
/**
* 用来删除好友
*
* @param qq
*/
public void delfriend(int qq) {
int Y_N = JOptionPane.showConfirmDialog(qq2009, "你确定删除该好友吗?", "删除好友",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (Y_N == JOptionPane.YES_OPTION) {
friendTree.delfriend(qq);
MyBean myBean = new MyBean();
myBean.setQq(qq);
Embody embody = new Embody();
embody.setOrder(Order.DEL_FRIEND);
embody.setObjMessage(myBean);
myClient.send(embody);
}
}
/**
* 查看好友详细信息
*
* @param qq
*/
public void checkFriendInfo(int qq) {
if (!set2.contains(qq)) {
if (list3 != null) {
for (MyBean bean : list3) {
if (bean.getQq() == qq) {
FriendInfo friendInfo = new FriendInfo(bean);
hashTable.put(qq, friendInfo);
set2.add(qq);
}
}
} else {
for (MyBean bean : list) {
if (bean.getQq() == qq) {
FriendInfo friendInfo = new FriendInfo(bean);
hashTable.put(qq, friendInfo);
set2.add(qq);
}
}
}
} else {
hashTable.get(qq).setVisible(true);
}
}
public void checkFriendIP(int qq) {
if (map.isEmpty()) {
JOptionPane.showMessageDialog(qq2009, "对不起,对方不在线无法查看!", "查看IP",
JOptionPane.WARNING_MESSAGE);
} else {
if (map.get(qq) != null) {
JOptionPane.showMessageDialog(qq2009, "对方IP:"
+ map.get(qq).getIp() + "\n对方端口号:"
+ map.get(qq).getUPort(), "查看IP",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(qq2009, "对不起,对方不在线无法查看!", "查看IP",
JOptionPane.WARNING_MESSAGE);
}
}
}
/**
* 和好友聊天
*
* @param qq
*/
public void chat(int qq) {
if (!set.contains(qq)) {
set.add(qq);
startUDP(qq);
table.get(qq).setVisible(true);
MyBean myBean = new MyBean();
myBean.setQq(qq);
Embody embody = new Embody();
embody.setOrder(Order.CHAT);
embody.setObjMessage(myBean);
myClient.send(embody);
} else {
table.get(qq).setVisible(true);
MyBean myBean = new MyBean();
myBean.setQq(qq);
Embody embody = new Embody();
embody.setOrder(Order.CHAT);
embody.setObjMessage(myBean);
myClient.send(embody);
}
}
/**
* 启动UDP
*
* @param qq
*/
public void startUDP(int qq) {
try {
DatagramSocket dSocket = new DatagramSocket();
ChatDialog chatDialog = new ChatDialog(this, qq, dSocket);
table.put(qq, chatDialog);
if (thread == null) {
thread = new Thread(new AcceptMsg());
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* UDP发送信息
*
* @param qq
* @param dSocket
* @param buf
*/
public void send(int qq, DatagramSocket dSocket, byte[] buf) {
if (!map.isEmpty()) {
if (map.get(qq) != null) {
int fUPort = map.get(qq).getUPort();
InetAddress fip = map.get(qq).getIp();
DatagramPacket dPacket = new DatagramPacket(buf, 0, buf.length,
fip, fUPort);
try {
dSocket.send(dPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 定义一个线程接收好友的信息
*
* @author st0816003
*
*/
public class AcceptMsg implements Runnable {
public void run() {
b_u = true;
byte buf[] = new byte[8192];
DatagramSocket dSocket = null;
DatagramPacket dPacket = null;
try {
dSocket = new DatagramSocket(myClient.getMUPort());
dPacket = new DatagramPacket(buf, buf.length);
while (b_u) {
dSocket.receive(dPacket);
String str = new String(dPacket.getData(), 0, dPacket
.getLength());
lqq = Integer.parseInt(str.substring(20, 24));
ChatDialog chatDialog = table.get(lqq);
JTextArea jTextArea = chatDialog.getJTextArea();
jTextArea.append("\n" + str);
jTextArea.setCaretPosition(jTextArea.getDocument()
.getLength() - 1);
int n = chatDialog.getN();
n++;
chatDialog.setN(n);
chatDialog.setCahtLog();
if (!chatDialog.isVisible()) {// 收到了好友的信息且对话框没有打开,应做出相应的提示
order = Order.H_MSG;
receiveMsg();
}
}
} catch (BindException e) {
System.out.println("Address already in use!");
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 保存聊天记录
*
* @author st0816003
*/
public void saveCahtLog() {
int Y_N = JOptionPane.showConfirmDialog(qq2009, "是否保存聊天记录?", "聊天记录",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (Y_N == JOptionPane.YES_OPTION) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.showSaveDialog(null);
if (fc.getSelectedFile() != null) {
BufferedWriter bw = null;
File file = fc.getSelectedFile();
try {
FileOutputStream fos = new FileOutputStream(file
+ "聊天记录.txt");
bw = new BufferedWriter(new OutputStreamWriter(fos));
String str = ChatLog.chatLog.getJTextArea().getText();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -