import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Client extends JPanel{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
private static String frameTitle = "Meeting Client";
private static JFrame frame;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
frame = new JFrame(frameTitle);
frame.setContentPane(new Client());
//frame.setContentPane(new Client());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(330,640);
frame.setBounds(100,100,330,640);
//frame.pack();
frame.setVisible(true);
}
private IService service = null;
private TextField serverName,serverPort,userName,password;
private Button connect,disconnect;
private TextField MeetingTitle,MeetingLabel,otherUserName,MeetingStartTime,MeetingEndTime,MeetingID;
private TextField startTime,endTime;
private Button register,add,query,delete,clear,login;
private TextArea fromserver;
private boolean connected = false;
public TextArea getFromserver(){
return fromserver;
}
//无参构造方法
public Client(){
//设置安全管理
System.setSecurityManager(new RMISecurityManager());
setStatus();
//服务器地址
add(new Label("服务器名:"));
serverName = new TextField(20);
//serverName.setEnabled(true);
add(serverName);
serverName.setText("127.0.0.1");
connect = new Button("连接");
//注册监听器
connect.addActionListener(new Connect());
add(connect);
add(new Label("服务端口:"));
serverPort = new TextField(20);
//serverPort.setEnabled(true);
add(serverPort);
serverPort.setText("1099");
disconnect = new Button("断开");
//撤销注册监听器
disconnect.addActionListener(new Disconnect());
add(disconnect);
//用户注册
add(new Label("帐号:"));
userName = new TextField(30);
add(userName);
userName.setText("");
add(new Label("密码:"));
password = new TextField(30);
add(password);
password.setText("");
register = new Button("用户注册");
register.addActionListener(new Register());
add(register);
login = new Button("用户登录");
login.addActionListener(new Login());
add(login);
//增加会议
add(new Label("=====输入会议的相关信息增加新会议====="));
add(new Label("会议名称:"));
MeetingTitle = new TextField(30);
add(MeetingTitle);
MeetingTitle.setText("");
add(new Label("会议标注:"));
MeetingLabel = new TextField(30);
add(MeetingLabel);
MeetingLabel.setText("");
add(new Label("另一用户:"));
otherUserName = new TextField(30);
add(otherUserName);
otherUserName.setText("");
add(new Label("开始时间:"));
MeetingStartTime = new TextField(30);
add(MeetingStartTime);
MeetingStartTime.setText("");
add(new Label("结束时间:"));
MeetingEndTime = new TextField(30);
add(MeetingEndTime);
MeetingEndTime.setText("");
add = new Button("增加会议");
add.addActionListener(new Add());
add(add);
//清除会议
clear = new Button("清除会议");
clear.addActionListener(new Clear());
add(clear);
//查询会议
add(new Label("根据时间段查询会议(起始时间——结束时间):"));
startTime = new TextField(10);
add(startTime);
add(new Label("——"));
endTime = new TextField(10);
add(endTime);
query = new Button("查询");
//注册监听器
query.addActionListener(new Query());
add(query);
//删除会议
add(new Label("请输入想要删除的会议ID:"));
MeetingID = new TextField(10);
add(MeetingID);
delete = new Button("删除");
delete.addActionListener(new Delete());
add(delete);
//显示信息
fromserver = new TextArea(10,40);
add(fromserver);
fromserver.setEditable(false);
//初始化
init();
setVisible(true);
}
private void setStatus(){
if(connected)
frame.setTitle(frameTitle + ":Connected");
else
frame.setTitle(frameTitle + ":Not Connected");
}
private void init(){
serverName.setEnabled(true);
serverPort.setEnabled(true);
userName.setText("");
userName.setEnabled(false);
password.setText("");
password.setEnabled(false);
MeetingTitle.setText("");
MeetingTitle.setEnabled(false);
MeetingLabel.setText("");
MeetingLabel.setEnabled(false);
otherUserName.setText("");
otherUserName.setEnabled(false);
MeetingStartTime.setText("");
MeetingStartTime.setEnabled(false);
MeetingEndTime.setText("");
MeetingEndTime.setEnabled(false);
startTime.setText("");
startTime.setEnabled(false);
endTime.setText("");
endTime.setEnabled(false);
MeetingID.setText("");
MeetingID.setEnabled(false);
connect.setEnabled(true);
disconnect.setEnabled(false);
register.setEnabled(false);
login.setEnabled(false);
add.setEnabled(false);
query.setEnabled(false);
delete.setEnabled(false);
clear.setEnabled(false);
}
private void afterRegister(){
userName.setEnabled(false);
password.setEnabled(false);
MeetingTitle.setEnabled(true);
MeetingLabel.setEnabled(true);
otherUserName.setEnabled(true);
MeetingStartTime.setEnabled(true);
MeetingEndTime.setEnabled(true);
startTime.setEnabled(true);
endTime.setEnabled(true);
MeetingID.setEnabled(true);
register.setEnabled(false);
login.setEnabled(false);
add.setEnabled(true);
query.setEnabled(true);
delete.setEnabled(true);
clear.setEnabled(true);
}
class Connect implements ActionListener {
public void actionPerformed(ActionEvent event) {
try{
service = (IService)Naming.lookup("rmi://" + serverName.getText() + ":" + serverPort.getText() + "/IService");
connected = true;
userName.setEnabled(true);
password.setEnabled(true);
register.setEnabled(true);
login.setEnabled(true);
disconnect.setEnabled(true);
serverName.setEnabled(false);
serverPort.setEnabled(false);
connect.setEnabled(false);
setStatus();
fromserver.append("连接服务器成功,请注册!\n");
}catch(Exception e){
fromserver.append("连接服务器失败,请重新连接!\n");
connected = false;
setStatus();
service = null;
System.out.println(e);
}
}
}
class Disconnect implements ActionListener {
public void actionPerformed(ActionEvent event) {
try{
if(service == null)
return;
service = null;
}catch(Exception e){
fromserver.append("断开连接失败,请重试!\n");
}
finally{
connected = false;
fromserver.append("用户" + userName.getText() + "断开服务器连接!\n");
init();
setStatus();
}
}
}
class Register implements ActionListener {
public void actionPerformed(ActionEvent event) {
try{
if(service.register(userName.getText(), password.getText())){
afterRegister();
fromserver.append("注册成功\n");
}else{
fromserver.append("注册失败——你所使用的用户名已经存在!\n");
}
}catch(Exception e){
fromserver.append("注册失败\n");
}
}
}
class Login implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
if(service.login(userName.getText(), password.getText())){
afterRegister();
fromserver.append("登录成功\n");
}else{
fromserver.ap