package com.exercise.controller;
import com.exercise.model.bean.FileBean;
import com.exercise.model.Model;
import com.exercise.controller.utils.FileUtil;
import com.exercise.controller.utils.FtpUtil;
import com.exercise.controller.utils.StringUtil;
import com.exercise.model.bean.FtpInfoBean;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;
/*
* 说明:程序控制器类,包含视图需要调用的方法
*/
public class Controller {
private Model model; //程序模型实例
//构造方法
public Controller(Model model) {
this.model = model;
}
//刷新本地文件列表
public void refreshLocalFiles(String path) {
FileSystemView fsv = FileSystemView.getFileSystemView();
//如果path为空,就初始化
if (StringUtil.isEmpty(path)) {
path = fsv.getDefaultDirectory().getPath(); // 我的文档默认路径
}
Vector folderData = new Vector();//文件夹数据
Vector fileData = new Vector();//文件数据
File[] files = fsv.getFiles(new File(path), true);
for (int i = 0; i < files.length; i++) {
File file = files[i];
FileBean fb = new FileBean();
fb.setName(file.getName());
fb.setPath(file.getAbsolutePath());
fb.setSize(file.length());
fb.setLastModified(new Date(file.lastModified()));
if (file.isDirectory()) {
fb.setType(FileBean.FileType.folder);
folderData.add(fb);
} else {
fb.setType(FileBean.FileType.file);
fileData.add(fb);
}
}
//所有文件夹,文件数据
Vector listData = new Vector();
listData.addAll(folderData);
listData.addAll(fileData);
//设置模型中的本地文件数据
model.setLocalFiles(listData);
model.setLocalPath(path);
}
//本地文件夹向上按钮事件处理
public void localUpperButtonClickHandler() {
String currentPath = model.getLocalPath();
String separator = File.separator;
int pos = currentPath.lastIndexOf(separator);
if (pos > 0 && pos != currentPath.length()) {
currentPath = currentPath.substring(0, pos);
// System.out.println("路径:" + currentPath);
refreshLocalFiles(currentPath);
} else {
Vector listData = new Vector();//所有文件夹,文件数据
List roots = FileUtil.getFileRoots();
for (int i = 0; i < roots.size(); i++) {
FileBean fb = new FileBean();
String driveName = roots.get(i).replace(File.separator, "");
fb.setName(driveName);
fb.setType(FileBean.FileType.folder);
fb.setPath(driveName);
listData.add(fb);
}
model.setLocalFiles(listData);//设置list数据
model.setLocalPath("/");//更新路径 localPathText
}
}
//上传文件,成功返回true.如果未连接或上传失败,返回false
public String uploadLocalFile(FileBean fb) {
if (model.getFtpInfoBean() == null) {
return "尚未连接服务器!";
}
FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
String localFileName = fb.getPath();
String remoteFileName = model.getRemotePath();
FtpUtil.uploadFile(ftpInfoBean, fb, localFileName, fb.getName());//直接覆盖
model.setLastLogInfo("信息: 上传文件" + fb.getPath() + "完成,大小" + fb.getSize() + "字节");
refreshRemoteFiles();
return "OK";
}
//打开文件夹
public void openLocalFolder(FileBean fb) {
refreshLocalFiles(fb.getPath());
}
//刷新本地文件
public void refreshLocalButtonClickHandler() {
refreshLocalFiles(model.getLocalPath());
}
//初始化服务器侧 JList
public void initRemoteFiles() {
Vector listData = new Vector();
FileBean fb = new FileBean();
fb.setName("无连接...");
fb.setType(FileBean.FileType.info);
listData.add(fb);
model.setRemoteFiles(listData);//设置list数据
model.setRemotePath(".");
}
//登陆ftp服务器,获得服务器文件列表
public void loginRemoteServer(FtpInfoBean ftpInfoBean) {
//如果测试连接成功
if (FtpUtil.isValid(ftpInfoBean)) {
model.setFtpInfoBean(ftpInfoBean);//保存ftp登录信息到模型中
Vector listData = FtpUtil.getTrimedFileList(ftpInfoBean, null);//服务器文件列表
model.setLastLogInfo("信息: 连接服务器[" + ftpInfoBean.getIp() + "]完成");
model.setRemoteFiles(listData);//更新模型数据
model.setRemotePath(".");
} //如果测试连接不成功,更新JList数据
else {
Vector listData = new Vector();
FileBean fb = new FileBean();
fb.setName("连接失败...");
fb.setType(FileBean.FileType.info);
listData.add(fb);
model.setRemoteFiles(listData);
}
}
//刷新服务器文件列表
public void refreshRemoteFiles() {
if (model.getFtpInfoBean() != null) {
String remotePath = model.getRemotePath();
Vector listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
model.setRemoteFiles(listData);
}
}
//服务器 JList 鼠标双击的事件处理方法
public void remoteFileDoubleClickedHandler(FileBean fb) {
if (fb.getType().equals(FileBean.FileType.file)) {
downloadRemoteFile(fb);//下载文件到本地
} else if (fb.getType().equals(FileBean.FileType.folder)) {
openRemoteFolder(fb);//打开文件夹
}
}
//下载文件到本地
private void downloadRemoteFile(FileBean fb) {
FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
String localPath = model.getLocalPath();
FtpUtil.downloadFile(ftpInfoBean, localPath, fb);
model.setLastLogInfo("信息: 下载文件" + fb.getName() + "完成,大小" + fb.getSize() + "字节");
refreshLocalFiles(localPath);
}
//打开服务器文件夹
private void openRemoteFolder(FileBean fb) {
String separator = "/";
String remotePath = fb.getPath();
if (remotePath.equals(separator)) {
remotePath = remotePath + fb.getName();
} else {
remotePath = fb.getPath() + separator + fb.getName();
}
Vector listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
model.setRemoteFiles(listData);
model.setRemotePath(remotePath);
}
//服务器文件路径向上
public void remoteUpperButtonClickHandler() {
String remotePath = model.getRemotePath();
String separator = "/";
int pos = remotePath.lastIndexOf(separator);
if (pos > 0) {
remotePath = remotePath.substring(0, pos);
} else {
remotePath = remotePath.substring(0, pos + 1);
}
Vector listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
model.setRemoteFiles(listData);
model.setRemotePath(remotePath);
}
//断开连接
public void remoteDisconnectButtonClickHandler() {
if (model.getFtpInfoBean() != null) {
FtpUtil.disconnectFromServer();
model.setLastLogInfo("信息: 断开服务器[" + model.getFtpInfoBean().getIp() + "]完成");
model.setFtpInfoBean(null);
initRemoteFiles();
}
}
//删除服务器端文件
public void deleteSeletedRemoteFile(FileBean fb) {
if (model.getFtpInfoBean() != null) {
FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
FtpUtil.deleteFile(ftpInfoBean, fb);
// model.setLastLogInfo("信息: 删除服务器文件"+fb.getName()+"完成,大小"+fb.getSize()+"字节");
refreshRemoteFiles();
}
}
}