package ch14;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import java.util.Vector;
public class Ftp extends JFrame implements ActionListener{
// 按钮面板组件
//构建用户登入面板
private JPanel btnNorth=new JPanel();
private JLabel userNameLb=new JLabel("用户名:");
private JLabel hostNameLb=new JLabel("FTP服务器地址:");
private JLabel portLb=new JLabel("端口号:");
private JTextField userNameTf=new JTextField("guest",10);
private JTextField hostNameTf=new JTextField("localhost",10);
private JTextField portTf=new JTextField("7777",5);
JButton controlBtn=new JButton("连接");
private JButton exitBtn=new JButton("退出");
//构建文件列表
private JPanel btnSouth = new JPanel();
private JButton browseBtn = new JButton("浏览");
private JButton deleteBtn = new JButton("删除");
private JButton sendBtn = new JButton("传输");
private DefaultListModel fileListModel = new DefaultListModel();
private JList ffileList = new JList(fileListModel);
private JPanel btnWest = new JPanel();
//构建消息面板栏
private JTextArea messageTa = new JTextArea();
private JScrollPane messagePan = new JScrollPane(messageTa);
private String columnNameArray[] = { "状态", "文件名", "文件大小", "速度", "已完成",
"剩余时间" };
private Object data[][] = new Object[0][0];
private AbstractTableModel tableModel = new AbstractTableModel() {
public int getColumnCount() {
return columnNameArray.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int arg0, int arg1) {
return data[arg0][arg1];
}
public String getColumnName(int arg0) {
return columnNameArray[arg0];
}
};
private JTable fileSendProgressTb = new JTable(tableModel);
private JScrollPane fileSendProgressPan = new JScrollPane(
fileSendProgressTb);
private JSplitPane mainPan = new JSplitPane(0);
//构建当前在线用户
private DefaultListModel userListModel = new DefaultListModel();
private JList userList = new JList(userListModel);
private JPanel btnEast = new JPanel();
private FtpClientNet net=null;
//构造函数
public Ftp() {
initComponent();
}
//初始化所有界面组件;
private void initComponent() {
buildAddressPan(); //调用下面的用户面板函数
buildFile(); //调用文件列表栏函数;
buildMessage(); //调用消息面板栏
buildUser(); //调用在线用户栏;
setTitle("Ftp System V1.0Beta");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//用户登入面板
private void buildAddressPan() {
btnNorth.setBorder(BorderFactory.createTitledBorder("用户登录:"));
btnNorth.add(userNameLb);
btnNorth.add(userNameTf);
btnNorth.add(hostNameLb);
btnNorth.add(hostNameTf);
btnNorth.add(portLb);
btnNorth.add(portTf);
btnNorth.add(controlBtn);
btnNorth.add(exitBtn);
add(btnNorth, BorderLayout.NORTH);
//响应事件按钮
controlBtn.addActionListener(this);
exitBtn.addActionListener(this);
}
//响应函数
//构建文件列表栏
private void buildFile() {
ffileList.setFixedCellWidth(50);
JScrollPane tempPan = new JScrollPane(ffileList);
tempPan.setBorder(BorderFactory.createTitledBorder("文件列表"));
btnSouth.add(browseBtn);
btnSouth.add(deleteBtn);
btnSouth.add(sendBtn);
//响应事件按钮
browseBtn.addActionListener(this);
deleteBtn.addActionListener(this);
sendBtn.addActionListener(this);
btnWest.setLayout(new BorderLayout());
btnWest.add(tempPan, BorderLayout.CENTER);
btnWest.add(btnSouth, BorderLayout.SOUTH);
add(btnWest, BorderLayout.WEST);
}
//响应函数;
public void actionPerformed(ActionEvent arg0) {
Object source=arg0.getSource();
if(source==browseBtn) {
JFileChooser fileChooser=new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
fileChooser.showOpenDialog(this);
File[] sendFiles=fileChooser.getSelectedFiles();
if(sendFiles==null || sendFiles.length==0) {
JOptionPane.showMessageDialog(this,"请选中文件!");
return;
}
for(File sendFile:sendFiles)
fileListModel.addElement(sendFile.getAbsolutePath());
} else if(source==deleteBtn) {
fileListModel.removeElement(ffileList.getSelectedValue());
} else if(source==sendBtn) {
Object obj=ffileList.getSelectedValue();
if(obj==null) {
JOptionPane.showMessageDialog(this, "请先选中文件!");
return;
}
if(net.isClosed()) {
JOptionPane.showMessageDialog(this, "请先连接FTP服务器!");
return;
}
net.send(new File((String)obj));
fileListModel.removeElement(obj);
}else if(source==controlBtn) {
String msg=controlBtn.getText();
if("连接".equals(msg)) {
connect();
userNameTf.setEditable(false);
hostNameTf.setEditable(false);
portTf.setEditable(false);
controlBtn.setText("停止");
} else {
stop();
userNameTf.setEditable(true);
hostNameTf.setEditable(true);
portTf.setEditable(true);
controlBtn.setText("连接");
}
} else if(source==exitBtn) {
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(this, "确定退出吗?","退出",JOptionPane.YES_NO_OPTION)) {
System.exit(0);
}
}
}
//构建消息面板栏
private void buildMessage() {
mainPan.setDividerSize(1);
mainPan.setDividerLocation(250);
messagePan.setBorder(BorderFactory.createTitledBorder("消息面板"));
mainPan.setTopComponent(messagePan);
fileSendProgressPan.setBorder(BorderFactory
.createTitledBorder("文件传输进度"));
mainPan.setBottomComponent(fileSendProgressPan);
add(mainPan,BorderLayout.CENTER);
}
//构建当前在线用户
private void buildUser() {
userList.setFixedCellWidth(160);
btnEast.setBorder(BorderFactory.createTitledBorder("当前在线用户"));
btnEast.setLayout(new BorderLayout());
btnEast.add(new JScrollPane(userList));
add(btnEast, BorderLayout.EAST);
}
public void addHint(String s) {
messageTa.append(new Date().toLocaleString()+":"+s+"\r\n");
}
public void refreshUserList(Vector v) {
userListModel.clear();
for(java.util.Iterator it=v.iterator();it.hasNext();) {
userListModel.addElement(it.next());
}
}
//和FTP服务器建立连接
public void connect() {
try{
String hostName=hostNameTf.getText();
int port=Integer.parseInt(portTf.getText());
net=new FtpClientNet(this);
net.connect(userNameTf.getText(),hostNameTf.getText(),port);
addHint("Connected server("+hostName+","+port+")");
}catch(Exception e){
JOptionPane.showMessageDialog(this,"服务器信息输入有误,请重试!");
}
}
//和FTP服务器断开连接
public void stop() {
try{
net.close();
refreshUserList(new Vector(0));
messageTa.setText("");
}catch(Exception e){
JOptionPane.showMessageDialog(this,e.getMessage());
}
}
public static void main(String[] args) {
new Ftp();
}
}