java做一个简单的文件管理器(二)

时间:2022-05-05 16:39:16

继续上一篇,得到路径之后的操作该是什么样的呢?是对node的操作还是路径的操作。这里是需要思考的问题。由于路径是从node中一步一步得到,所以我们还是得从node去操作这些。

  1. 实现操作的过程。
//节点的系列操作 比如是保存节点还是删除节点还是其它操作等等 因为其实对文件的操作本质上就是对节点的操作
public class FileNodeOperation {
private I_Node node = null;
private Vector<Vector<I_Node>> copiedList = new Vector<Vector<I_Node>>();//存储复制的文件节点
private Vector<I_Node> copyFile = null;
private Vector<I_Node> multiSelectedNodeList = new Vector<I_Node>();//存储选中的节点
private Vector<String> pastedFilePath = new Vector<String>(); //存储复制文件的路径
private static Vector<I_Node> searchNodeList = new Vector<I_Node>();//存储搜索结果的节点
int index = 0; //标记是否有
private int pos = 0; //标记文件名在路径中的位置
private int newNodeIndex = 0;
private static I_Node tempNode = new FileNode();

public static I_Node getTempNode() {
return tempNode;
}

public FileNodeOperation() {

}
//获取复制文件列表
private void setCopyList(I_Node copiedNode) {
if (index++ == 0) {
copyFile = new Vector<I_Node>();
copyFile.add(copiedNode);
copiedList.add(copyFile);
pos = copiedNode.getPath().lastIndexOf(File.separator);//获取文件节点名开始的索引
}
File[] copieFiles = copiedNode.getFile().listFiles();
if (copieFiles != null) {
for (File file : copieFiles) {
if (file.isDirectory()) {
copyFile = new Vector<I_Node>();
I_Node Tnode = new FileNode(file);
copyFile.add(Tnode);
copiedList.add(copyFile);
setCopyList(Tnode); //递归
} else {
copyFile = new Vector<I_Node>();
I_Node Tnode = new FileNode(file);
copyFile.add(Tnode);
copiedList.add(copyFile);
}
}
}
}
//压缩文件
public int zipfiles(String zipfilenode1,String zipfilenode2){
try{
//String zippath = zipfilenode1.getPath();
//String zippath1 = zipfilenode2.getPath();
File file = new File(zipfilenode1);
File zipfiles = new File(zipfilenode2);
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipfiles));
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i = 0; i < files.length; ++i){
input = new FileInputStream(files[i]);
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
}
}
zipOut.close();
}catch(Exception e){
e.printStackTrace();
}
return 1;
}
//解压
public int jzipfiles(String jzipfile1,String jzipfile2){
try {
File file = new File(jzipfile1);
File outFile = null;
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream output = null;
while((entry = zipInput.getNextEntry()) != null){
System.out.println("解压缩" + entry.getName() + "文件");
outFile = new File(jzipfile2 + File.separator + entry.getName());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if(!outFile.exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}

//复制文件节点
public int copy(I_Node copiedFileNode, I_Node pastedFileNode) {
try {
setCopyList(copiedFileNode); //获取复制节点中的所有文件
for (Vector<I_Node> copyFile : copiedList) {
String copyPath = copyFile.get(0).getPath();
String pastePath = pastedFileNode.getPath()
+ copyPath.substring(pos);
File file=new File(pastePath);
if(file.exists()){
JOptionPane.showMessageDialog(null,
file.getName()+"已存在", null,
JOptionPane.ERROR_MESSAGE, null);

return 0;
}
else{
pastedFilePath.add(copyPath.substring(pos));
if (copyFile.get(0).getFile().isFile()) {
copyFile(copyPath, pastePath);
} else if (copyFile.get(0).getFile().isDirectory()) {
new File(pastePath).mkdir();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 1;
}
//复制文件
private void copyFile(String copiedPath, String pastedPath) {
try {
File file = new File(copiedPath);
FileInputStream inputStream = new FileInputStream(file);
File pastedFile = new File(pastedPath);
FileOutputStream outputStream = new FileOutputStream(pastedFile);
byte[] buffer = new byte[10240];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//清空复制文件节点列表
public void resetCopiedList() {
copiedList.removeAllElements();
}
//清空粘贴地址
public void resetPastedFilePath() {
pastedFilePath.removeAllElements();
}
//获取粘贴节点
public Vector<I_Node> getPastedNode(I_Node pastedFileNode) {
Vector<I_Node> pastedNodeList = new Vector<I_Node>();

if (pastedNodeList.size() != 0) {
pastedNodeList.removeAllElements();
}
for (String path : pastedFilePath) {
String pastedNodePath = pastedFileNode.getPath() + path;
if (path.lastIndexOf(File.separator) == 0) {
I_Node pastedNode = new FileNode(new File(pastedNodePath));
pastedNodeList.add(pastedNode);
}
}
return pastedNodeList;
}
//设置文件节点
public void setFileNode(I_Node node) {
resetIndex();
if (copiedList.size() != 0) {
copiedList.removeAllElements();
}

this.node = node;
}
//重设文件节点为空
public void resetFileNode() {
this.node = null;
}
//添加复制节点
public void setFileNodeList(I_Node node) {
multiSelectedNodeList.add(node);
}
//判断剪切板是否为空
public boolean isClipboardEmpty() {
return (this.node == null && multiSelectedNodeList.size() == 0);
}
//添加搜索节点
public static void setSearchNodeList(I_Node node) {
searchNodeList.add(node);
}
//获取搜索节点列表
public static Vector<I_Node> getSearchNodeList() {
return searchNodeList;
}
//清空选择节点列表
public void removeAllFileNode() {
if (multiSelectedNodeList.size() != 0) {
multiSelectedNodeList.removeAllElements();
}
}
//重设索引为0
public void resetIndex() {
index = 0;
}
//获取选中复制节点列表
public Vector<I_Node> getFileNodeList() {
return this.multiSelectedNodeList;
}
//获取当前节点
public I_Node getFileNode() {
return node;
}
//删除节点文件
public void delete(I_Node node) {
File file = node.getFile();
deleteFile(file);
file.delete();
}
//重命名节点文件并返回新的路径
public String rename(I_Node node, String newName) {
if (node != null) {
File file = node.getFile();
String path = ((I_Node) node.getParent()).getPath()
+ File.separator + newName;
file.renameTo(new File(path));
return path;
} else {
return "";
}
}

private void deleteFile(File deletedFile) {
if (deletedFile.isDirectory() && deletedFile.listFiles() != null) {
File[] fileList = deletedFile.listFiles();

for (File file : fileList) {
if (file.isDirectory()) {
deleteFile(new File(file.getPath()));
} else {
file.delete();
}

file.delete();
}
} else {
deletedFile.delete();
}
}
//新建文件夹
public void createNewFolder() {
String name = node.getPath() + File.separator + "新建文件夹";
System.out.println(name);
int index = getNewFolderIndex(name);

if (index != 0) {
name = name + "(" + (index + 1) + ")";
}
File folder = new File(name);
folder.mkdir();
I_Node newFolder = new FileNode(folder);
node.addChild(newFolder);
newNodeIndex = 0;
}
//获取已新建文件夹最大索引
private int getNewFolderIndex(String folder) {
File file = new File(folder);

if (file.exists()) {
newNodeIndex++;
if (file.getName().indexOf("(") > 0) { //判断文件名中是否有括号,即是不是第一个
int postion = file.getPath().lastIndexOf("(");
String path = file.getPath().substring(0, postion) + "("
+ (newNodeIndex + 1) + ")";
getNewFolderIndex(path);//
} else {
String path = file.getPath() + "(" + (newNodeIndex + 1) + ")";
getNewFolderIndex(path);//递归判断加1后的索引存在不
}
}
return newNodeIndex;
}
//新建文件
public void createNewFile(String type) {
String name = null;
if (type.equals("文本文档")) {
name = node.getPath() + File.separator + "新建文本文档"
+ ".txt";
} else if (type.equals("Word文档")) {
name = node.getPath() + File.separator + "新建 Word文档"
+ ".doc";
} else if (type.equals("Excel文档")) {
name = node.getPath() + File.separator
+ "新建 Excel文档" + ".xls";
}

int index = getNewFileIndex(name);
if (index != 0) {
if (type.equals("文本文档")) {
name = name.split(".txt")[0] + "(" + (index + 1) + ")" + ".txt";
} else if (type.equals("Word文档")) {
name = name.split(".doc")[0] + "(" + (index + 1) + ")" + ".doc";
} else if (type.equals("Excel文档")) {
name = name.split(".xls")[0] + "(" + (index + 1) + ")" + ".xls";
}
}

File file = new File(name);
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
I_Node newFile = new FileNode(file);
node.addChild(newFile);
newNodeIndex = 0;
}
//获取已新建同类型文件的最大索引
private int getNewFileIndex(String filePath) {
File file = new File(filePath);

if (file.exists()) {
newNodeIndex++;
if (file.getName().indexOf("(") > 0) {
int postion = file.getPath().lastIndexOf("(");
String path = file.getPath().substring(0, postion) + "("
+ (newNodeIndex + 1) + ")"
+ filePath.substring(filePath.lastIndexOf("."));
getNewFileIndex(path);
} else {
String path = file.getPath().substring(0,
file.getPath().lastIndexOf("."));
path = path
+ "("
+ (newNodeIndex + 1)
+ ")"
+ file.getPath().substring(
file.getPath().lastIndexOf("."));
getNewFileIndex(path);
}
}
return newNodeIndex;
}
//获取图标
public Icon getIcon(String iconName) {
String currdir = null;
try {
currdir = new File(".").getCanonicalPath();
} catch (Exception e) {
e.printStackTrace();
}
return new ImageIcon(this.getClass().getResource("/icon/" + iconName));//新建icon包中的图标
}
}

当我们在界面中显示每一个个文件信息记录的时候,该如何对这些记录进行反应呢?这个时候我们需要接触到对于按键和鼠标左键右键的事件。
2. 事件响应类:(事件响应类太多,这里拿出了解密加密的类展示,更多的可到文章末尾下载工程)

//解密事件
public class PasswordOperationDecryptEvent implements ActionListener {
private String fileUrl = null;
private String tempUrl;
private String key = "yuanshengtao";
private FileList fileSystemList = null;
private FileNodeOperation fileNodeOperation = null;
public PasswordOperationDecryptEvent(FileNodeOperation fileNodeOperation,
FileList fileSystemList) {
// TODO Auto-generated constructor stub
this.fileNodeOperation = fileNodeOperation;
this.fileSystemList = fileSystemList;
}
public void execute() throws Exception{
I_Node node = (I_Node) fileSystemList.getSelectedValue();
int a = node.getPath().length();
int b = node.toString().length();
tempUrl = node.getPath().substring(0, a-b)+"decry.txt";
decrypt(node.getPath(),tempUrl,key.length());

}
public static String decrypt(String fileUrl, String tempUrl, int keyLength) throws Exception{
File file = new File(fileUrl);
if (!file.exists()) {
return null;
}
File dest = new File(tempUrl);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}

InputStream is = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(tempUrl);

byte[] buffer = new byte[1024];
byte[] buffer2=new byte[1024];
byte bMax=(byte)255;
long size = file.length() - keyLength;
int mod = (int) (size%1024);
int div = (int) (size>>10);
int count = mod==0?div:(div+1);
int k = 1, r;
while ((k <= count && ( r = is.read(buffer)) > 0)) {
if(mod != 0 && k==count) {
r = mod;
}

for(int i = 0;i < r;i++)
{
byte b=buffer[i];
buffer2[i]=b==0?bMax:--b;
}
out.write(buffer2, 0, r);
k++;
}
out.close();
is.close();
return tempUrl;
}
//判断文件是否加密
public static String readFileLastByte(String fileName, int keyLength) {
File file = new File(fileName);
if(!file.exists())return null;
StringBuffer str = new StringBuffer();
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
for(int i = keyLength ; i>=1 ; i--){
randomFile.seek(fileLength-i);
str.append((char)randomFile.read());
}
randomFile.close();
return str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
execute();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
//加密事件
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

import Fileabout.FileList;
import Fileabout.FileNodeOperation;
import Fileabout.I_Node;

public class PasswordOperationEncryptEvent implements ActionListener {
private FileList fileSystemList = null;
private FileNodeOperation fileNodeOperation = null;
private String key = "yuanshengtao";
public PasswordOperationEncryptEvent(FileNodeOperation fileNodeOperation,
FileList fileSystemList) {
// TODO Auto-generated constructor stub
this.fileNodeOperation = fileNodeOperation;
this.fileSystemList = fileSystemList;
}
public void execute() throws Exception{
I_Node node = (I_Node) fileSystemList.getSelectedValue();

encrypt(node.getPath(),key);
}
public static void encrypt(String fileUrl, String key) throws Exception {
File file = new File(fileUrl);
String path = file.getPath();
if(!file.exists()){
return;
}
int index = path.lastIndexOf("\\");
String destFile = path.substring(0, index)+"\\"+"abc";
File dest = new File(destFile);
InputStream in = new FileInputStream(fileUrl);
OutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int r;
byte[] buffer2=new byte[1024];
while (( r= in.read(buffer)) > 0) {
for(int i=0;i<r;i++)
{
byte b=buffer[i];
buffer2[i]=b==255?0:++b;
}
out.write(buffer2, 0, r);
out.flush();
}
in.close();
out.close();
file.delete();
dest.renameTo(new File(fileUrl));
appendMethodA(fileUrl, key);
System.out.println("加密成功");
}

public static void appendMethodA(String fileName, String content) {
try {
// 打开一个随机访问文件流,按读写方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
//将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
execute();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}

右键响应:

ublic class RightClickEvent extends MouseAdapter {
private FileList fileSystemList = null;
private JPopupMenu listPopMenu = null;//菜单栏
private FileNodeOperation fileNodeOperation = null;
private static String viewFlg = ""; //
private static String nullFlg = "";
private static String copyType=null; //复制类型,复制或剪切
public static JMenuItem cutFile=null;
public static JMenuItem copyFile = null;
public static JMenuItem deleteFile = null;
public static JMenuItem pasteFile = null;
public static JMenuItem zipFile = null;
public static JMenuItem JzipFile = null;
public static JMenuItem passwordFile = null;
public static JMenuItem JpasswordFile = null;
public static JMenu createObj = null;
public static JMenuItem openFile=null;
public static JMenuItem rename=null;
//public static JMenuItem properties=null;
public static void setViewFlg(String value) {
viewFlg = value;
}

public static String getViewFlg() {
return viewFlg;
}

public static void setNullFlg(String value) {
nullFlg = value;
}
public static String getNullFlg() {
return nullFlg;
}
//文件列表上的右击事件
public RightClickEvent(FileNodeOperation fileNodeOperation,
FileList fileSystemList) {
this.fileSystemList = fileSystemList;
listPopMenu = new JPopupMenu();
this.fileNodeOperation = fileNodeOperation;
//打开菜单键
openFile=new JMenuItem("打开");
openFile.setEnabled(false);
if (fileSystemList.getSelectedValues().length>1) {
openFile.setEnabled(false);
}else{
openFile.addActionListener(new OpenFileEvent(fileSystemList));
//openFile.setMnemonic(KeyEvent.VK_O);//添加快捷键助记符
//openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+c
}
//剪切菜单键
cutFile=new JMenuItem("剪切");
cutFile.setEnabled(false);
cutFile.addActionListener(new CopyEvent(fileNodeOperation,
fileSystemList,"cut"));
//cutFile.setMnemonic(KeyEvent.VK_X);//添加快捷键助记符
//cutFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+c

zipFile = new JMenuItem("压缩");
zipFile.setEnabled(false);
//添加事件响应
zipFile.addActionListener(new ZipFilesEvent(fileNodeOperation, fileSystemList));

JzipFile = new JMenuItem("解压");
JzipFile.setEnabled(false);
//添加事件响应
JzipFile.addActionListener(new JZipFilesEvent(fileNodeOperation, fileSystemList));


passwordFile = new JMenuItem("加密");
passwordFile.setEnabled(false);
//添加时间响应
passwordFile.addActionListener(new PasswordOperationEncryptEvent(fileNodeOperation, fileSystemList));

JpasswordFile = new JMenuItem("解密");
JpasswordFile.setEnabled(false);
//添加时间响应
JpasswordFile.addActionListener(new PasswordOperationDecryptEvent(fileNodeOperation, fileSystemList));

//复制菜单键
copyFile = new JMenuItem("复制");
copyFile.setEnabled(false);
copyFile.addActionListener(new CopyEvent(fileNodeOperation,
fileSystemList));
//copyFile.setMnemonic(KeyEvent.VK_C);//添加快捷键助记符
//copyFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+c
//粘贴菜单键
pasteFile = new JMenuItem("粘贴");
if (RightClickEvent.getViewFlg()
.equals("Search Model View.")) {
pasteFile.setEnabled(false);
} else {
pasteFile.addActionListener(new PasteEvent(fileNodeOperation,
fileSystemList));
//pasteFile.setMnemonic(KeyEvent.VK_V);
//pasteFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+v
}
//刷新菜单键
JMenuItem refresh = new JMenuItem("刷新");
refresh.addActionListener(new RefreshEvent(fileNodeOperation,
fileSystemList));
//refresh.setMnemonic(KeyEvent.VK_E);
//refresh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+S
//重命名菜单键
rename = new JMenuItem("重命名");
rename.setEnabled(false);
rename.addActionListener(new RenameEvent(fileNodeOperation,
fileSystemList));
//rename.setMnemonic(KeyEvent.VK_R);
//rename.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
//KeyEvent.CTRL_MASK)); //添加快捷键Ctrl+R
//删除菜单键
deleteFile = new JMenuItem("删除");
deleteFile.setEnabled(false);
deleteFile.addActionListener(new DeleteEvent(fileNodeOperation,
fileSystemList));
//deleteFile.setMnemonic(KeyEvent.VK_D);
//deleteFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
//KeyEvent.CTRL_MASK, true)); //添加快捷键Ctrl+D
//新建菜单键
createObj = new JMenu("新建");
if (RightClickEvent.getViewFlg()
.equals("Search Model View.")) {
createObj.setEnabled(false);
}
else {
//新建文件夹
JMenuItem newFolder = new JMenuItem("文件夹");
newFolder.addActionListener(new NewFolderEvent(
fileNodeOperation, fileSystemList));
//新建文本文档
JMenuItem newTextDocument = new JMenuItem(
"新建 文本文档");
newTextDocument.addActionListener(new NewFileEvent(
fileNodeOperation, fileSystemList,
"文本文档"));
//新建Word文档
JMenuItem newWordDocument = new JMenuItem(
"新建 Word文档");
newWordDocument.addActionListener(new NewFileEvent(
fileNodeOperation, fileSystemList,
"Word文档"));
//新建Excel文档
JMenuItem newExcelDocument = new JMenuItem(
"新建 Excel文档");
newExcelDocument.addActionListener(new NewFileEvent(
fileNodeOperation, fileSystemList,
"Excel文档"));
createObj.add(newFolder);
createObj.addSeparator();
createObj.add(newTextDocument);
createObj.add(newWordDocument);
createObj.add(newExcelDocument);
}
//属性菜单键
//properties = new JMenuItem("属性");
//properties.addActionListener(new PropertyEvent(fileNodeOperation,
// fileSystemList));
listPopMenu.add(openFile);
listPopMenu.add(cutFile);
listPopMenu.add(copyFile);
listPopMenu.add(pasteFile);
listPopMenu.add(zipFile);
listPopMenu.add(JzipFile);
listPopMenu.add(passwordFile);
listPopMenu.add(JpasswordFile);
listPopMenu.addSeparator();
listPopMenu.add(refresh);
listPopMenu.add(rename);
listPopMenu.add(deleteFile);
listPopMenu.addSeparator();
listPopMenu.add(createObj);
listPopMenu.addSeparator();
//listPopMenu.add(properties);
}

public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {

//搜索模式
if (RightClickEvent.getViewFlg().equals(
"Search Model View.")) {
pasteFile.setEnabled(false);
createObj.setEnabled(false);
}
//普通模式
else if (RightClickEvent.getViewFlg().equals(
"Common Model View.")) {
//选择多个文件
if (fileSystemList.getSelectedValues().length>1) {
openFile.setEnabled(false);
rename.setEnabled(false);
zipFile.setEnabled(true);
JzipFile.setEnabled(true);
passwordFile.setEnabled(true);
JpasswordFile.setEnabled(true);
}else{
openFile.setEnabled(true);
rename.setEnabled(true);
zipFile.setEnabled(true);
JzipFile.setEnabled(true);
passwordFile.setEnabled(true);
JpasswordFile.setEnabled(true);

}
//没有复制文件就设粘贴菜单为无效
if (fileNodeOperation.isClipboardEmpty()) {
pasteFile.setEnabled(false);
}
else {
pasteFile.setEnabled(true);
}

if (createObj != null) {
createObj.setEnabled(true);
}
if (fileSystemList.getSelectedValue()!=null) {
I_Node node = (I_Node) fileSystemList.getSelectedValue(); //选中的节点
I_Node parent=(I_Node)node.getParent();
if(!node.getFile().getName().equals("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")&&
!node.getFile().getName().equals("::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}")&&
!node.getFile().getName().equals("::{031E4825-7B94-4DC3-B131-E946B44C8DD5}")&&
!node.getFile().getName().equals("C:\\Users\\jone")&&
!parent.getFile().getName().equals("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")&&
!parent.getFile().getName().equals("::{031E4825-7B94-4DC3-B131-E946B44C8DD5}")){
RightClickEvent.copyFile.setEnabled(true);
RightClickEvent.cutFile.setEnabled(true);
RightClickEvent.rename.setEnabled(true);
RightClickEvent.deleteFile.setEnabled(true);
RightClickEvent.createObj.setEnabled(true);
RightClickEvent.zipFile.setEnabled(true);
RightClickEvent.JzipFile.setEnabled(true);
RightClickEvent.passwordFile.setEnabled(true);
RightClickEvent.JpasswordFile.setEnabled(true);
}
else if (parent.getFile().getName().equals("C:\\Users\\jone")) {

RightClickEvent.createObj.setEnabled(false);
}
else {
MainFrame.newFile.setEnabled(false);
MainFrame.nMenu.setEnabled(false);
RightClickEvent.copyFile.setEnabled(false);
RightClickEvent.cutFile.setEnabled(false);
RightClickEvent.rename.setEnabled(false);
RightClickEvent.deleteFile.setEnabled(false);
RightClickEvent.createObj.setEnabled(false);
//RightClickEvent.properties.setEnabled(false);
RightClickEvent.zipFile.setEnabled(false);
RightClickEvent.JzipFile.setEnabled(false);
RightClickEvent.passwordFile.setEnabled(false);
RightClickEvent.JpasswordFile.setEnabled(false);
}
}else {
I_Node node = SelectedNode.getSelectedNode();
if(node.getFile().getName().equals("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")||
node.getFile().getName().equals("::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}")||
node.getFile().getName().equals("::{031E4825-7B94-4DC3-B131-E946B44C8DD5}")||
node.getFile().getName().equals("C:\\Users\\jone")){
//RightClickEvent.properties.setEnabled(false);
RightClickEvent.createObj.setEnabled(false);
}
RightClickEvent.openFile.setEnabled(false);
RightClickEvent.rename.setEnabled(false);

}

}
if (listPopMenu != null) {
listPopMenu.show(fileSystemList, me.getX(), me.getY());
}
}
}
}
  1. 主界面:(直接上代码)
public class MainFrame {
private JScrollPane treeScrollPane = null;
private JScrollPane listScrollPane = null;
private JFrame mainFrame = null;
private JLabel lblAddress = null;
public static JTextField tfdAddress = null;
public static JButton returnButton=null;
public static JButton forwardButton=null;
public static JButton openFile=null;//选中之后用来打开的按钮
public static JButton newFile=null;//新建文件夹按钮
public static JButton refreshButton=null;
public static JTextField searchField=null;
public static JMenu nMenu;
public static JMenuItem oItem;
public static JMenuItem nItem;
public static JMenuItem dItem;
public static JMenuItem mItem;
//public static JMenuItem pItem;
public static JMenuItem xItem;
public static JMenuItem cItem;
public static JMenuItem zItem;
public static JMenuItem zipItem;
public static JMenuItem JzipItem;
public static JMenuItem passwordItem;
public static JMenuItem JpasswordItem;
public static JLabel pLabel=null;
private JToolBar toolBar = null;//JToolBar 提供了一个用来显示常用的 Action 或控件的组件
private JSplitPane pnlMain = null;
private FileSystemView fileSystemView = FileSystemView.getFileSystemView();//列举系统文件夹,获取系统图标
public void show() {
FileNodeOperation fileNodeOperation = new FileNodeOperation();
//文件列表结构
FileList fileSystemList = new FileList(fileNodeOperation);
Icon returnIcon = fileNodeOperation.getIcon("return.png");//返回按钮的添加
returnButton=new JButton(returnIcon);
returnButton.setToolTipText("返回");
returnButton.setBounds(5, 0, 40, 30);
returnButton.setEnabled(false);
returnButton.addActionListener(new ReturnEvent(fileSystemList));// 添加一个返回事件的监听器
Icon refreshIcon = fileNodeOperation.getIcon("refresh.png");//前进按钮的添加
/*forwardButton=new JButton(refreshIcon);
forwardButton.setToolTipText("前进");
forwardButton.setBounds(45, 0, 40, 30);
forwardButton.setEnabled(false);*/

//forwardButton.addActionListener(new ForwardEvent(fileSystemList));
//菜单栏
JMenu fMenu=new JMenu("文件(F)"); //菜单栏中相关的文件栏菜单fMenu
//为这个新建文件设置热键 相当于按下 Alt-F按钮的时候便弹出打开文件选项
//fMenu.setMnemonic(KeyEvent.VK_F);//设置当前模型上的键盘助记符。助记符是某种键,它与外观的无鼠标修饰符(通常是 Alt)组合时(如果焦点被包含在此按钮祖先窗口中的某个地方)将激活此按钮
oItem=new JMenuItem("打开");
oItem.setEnabled(false);
oItem.addActionListener(new OpenFileEvent(fileSystemList));//添加打开文件的监听器
nMenu=new JMenu("新建");
JMenuItem nItem1=new JMenuItem("文件夹");//菜单中的项的实现。菜单项本质上是位于列表中的按钮。当用户选择“按钮”时,将执行与菜单项关联的操作。
nItem1.addActionListener(new NewFolderEvent(fileNodeOperation, fileSystemList));
JMenuItem nItem2=new JMenuItem("文本文档");
nItem2.addActionListener(new NewFileEvent(fileNodeOperation, fileSystemList, "文本文档"));
JMenuItem nItem3=new JMenuItem("Word文档");
nItem3.addActionListener(new NewFileEvent(fileNodeOperation, fileSystemList, "Word文档"));
JMenuItem nItem4=new JMenuItem("Excel文档");
nItem4.addActionListener(new NewFileEvent(fileNodeOperation, fileSystemList, "Excel文档"));
nMenu.add(nItem1);//新建文件的时候涉及到新建不同类型的文件 要把这些都添加到nMenu上去
nMenu.add(nItem2);
nMenu.add(nItem3);
nMenu.add(nItem4);
dItem=new JMenuItem("删除");
dItem.setEnabled(false);
dItem.addActionListener(new DeleteEvent(fileNodeOperation, fileSystemList));//添加删除文件的监听器
mItem=new JMenuItem("重命名");
mItem.setEnabled(false);
mItem.addActionListener(new RenameEvent(fileNodeOperation, fileSystemList));
//pItem=new JMenuItem("属性");
//pItem.setEnabled(false);
//pItem.addActionListener(new PropertyEvent(fileNodeOperation, fileSystemList));
fMenu.add(oItem);
fMenu.add(nMenu);
JMenuItem eItem=new JMenuItem("退出(X)"/*,KeyEvent.VK_X*/);//按下Alt-X快捷键的时候便退出当前运行
eItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);//点击退出按钮的时候 便退出整个程序
}
});
//eItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));//设置组合键,它能直接调用菜单项的操作侦听器而不必显示菜单的层次结构。 表示当我们输入Ctrl-X的时候便退出 相当于是一个快捷键
fMenu.add(dItem);
fMenu.add(mItem);
//fMenu.add(pItem);
fMenu.add(eItem);
JMenu eMenu=new JMenu("编辑(E)"); //另外一个菜单栏“编辑”
//fMenu.setMnemonic(KeyEvent.VK_E);//按下快捷键crtl-E的时候便弹出编辑选项
xItem=new JMenuItem("剪切");
xItem.setEnabled(false);
xItem.addActionListener(new CopyEvent(fileNodeOperation, fileSystemList,"cut"));
cItem=new JMenuItem("复制");
cItem.setEnabled(false);
cItem.addActionListener(new CopyEvent(fileNodeOperation, fileSystemList));
zItem=new JMenuItem("粘贴");
zItem.setEnabled(false);
zItem.addActionListener(new PasteEvent(fileNodeOperation, fileSystemList));
/*zipItem = new JMenuItem("压缩");
zipItem.setEnabled(false);
zipItem.addActionListener(new ZipFilesEvent(fileNodeOperation, fileSystemList));
//添加压缩实现模块

JzipItem = new JMenuItem("解压");
JzipItem.setEnabled(false);
JzipItem.addActionListener(new JZipFilesEvent(fileNodeOperation, fileSystemList));
//添加解压缩实现模块

passwordItem = new JMenuItem("加密");
passwordItem.setEnabled(false);
//添加加密实现模块

JpasswordItem = new JMenuItem("解密");
JpasswordItem.setEnabled(false);*/

//添加解密实现模块


eMenu.add(xItem);
eMenu.add(cItem);
eMenu.add(zItem);
/*eMenu.add(JpasswordItem);
eMenu.add(passwordItem);
eMenu.add(JzipItem);
eMenu.add(zipItem);*/


/*JMenu vMenu=new JMenu("查看(V)");
vMenu.setMnemonic(KeyEvent.VK_V);//按下快捷键Alt-V的时候便弹出查看选项
JMenuItem rItem=new JMenuItem("刷新");
rItem.addActionListener(new RefreshEvent(fileNodeOperation, fileSystemList));
vMenu.add(rItem);*/



/*JMenu hMenu=new JMenu("帮助(H)");
JMenuItem aItem1=new JMenuItem("关于");//在一个菜单中添加多个菜单项
//aItem1.addActionListener( new AboutExplorerEvent());
JMenuItem aItem2=new JMenuItem("关于作者");
//aItem2.addActionListener(new AboutAuthorEvent());
hMenu.setMnemonic(KeyEvent.VK_H);//按下快捷键Alt-H的时候便弹出帮助选项
hMenu.add(aItem1);
hMenu.add(aItem2);*/



JMenuBar menuBar=new JMenuBar();//主菜单选项把四个分菜单选项添加到一起
menuBar.add(fMenu);
menuBar.add(eMenu);
//menuBar.add(vMenu);
//menuBar.add(hMenu);
menuBar.setBounds(170, 30, 60, 25);
//打开按钮
openFile=new JButton("打开");//打开按钮 下面的
openFile.setToolTipText("打开选中文件");
openFile.setBounds(5,0,60,25);
openFile.setEnabled(false);


//新建文件夹按钮
newFile=new JButton("新建文件夹");//添加新建文件夹事件
newFile.setToolTipText("创建一个空的文件夹");
newFile.setBounds(65, 0, 100, 25);
pLabel=new JLabel();
pLabel.setToolTipText("选中文件属性");
pLabel.setBounds(350, 0, 400, 25);


listScrollPane = new JScrollPane(fileSystemList);//设置滚动条
listScrollPane.setPreferredSize(new Dimension(
350, 300));
fileNodeOperation = new FileNodeOperation();
fileSystemList.addMouseListener(new RightClickEvent(
fileNodeOperation, fileSystemList));//添加鼠标右击响应事件

refreshButton = new JButton(refreshIcon);//刷新按钮
refreshButton.setToolTipText("刷新");
refreshButton.setBounds(420,0,40,30);
refreshButton.addActionListener(new RefreshEvent(fileNodeOperation, fileSystemList));
//地址文本编辑框
tfdAddress = new JTextField();
tfdAddress.setBounds(90,0,330,30);


openFile.addActionListener(new OpenFileEvent(fileSystemList));//添加打开文件响应事件
newFile.addActionListener(new NewFolderEvent(fileNodeOperation, fileSystemList));//添加新建文件夹事件
//文件树结构
FileTree fileSystemTree = new FileTree(fileSystemList);
treeScrollPane = new JScrollPane(fileSystemTree);
treeScrollPane.setPreferredSize(new Dimension(
150, 300));
//添加到分隔面板
//JSplitPane 用于分隔两个(只能两个)Component。两个 Component 图形化分隔以外观实现为基础,并且这两个 Component 可以由用户交互式调整大小
JSplitPane explorerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
treeScrollPane, listScrollPane);
explorerPane.setDividerLocation(180);//设置分隔条的位置。
explorerPane.setDividerSize(8);//设置分隔条的大小。
/*searchField=new JTextField();//搜索栏
searchField.setBounds(480,0,260,30);
searchField.setToolTipText("输入在左侧地址下搜索的完整文件名");
//搜索按钮
Icon searchIcon = fileNodeOperation
.getIcon("Search.jpg");
JButton btnSearch = new JButton(searchIcon);
btnSearch.setBounds(740,0,40,30);
btnSearch.setToolTipText("搜索");*/

//btnSearch.addActionListener(new SearchEvent(fileSystemList));//添加搜索响应事件
tfdAddress.setText(fileSystemView.getHomeDirectory().getPath());//fileSystemView 列举系统文件夹,获取系统图标
tfdAddress.setPreferredSize(new Dimension(300, 25));//设置此组件的首选大小。如果 preferredSize 为 null,则要求 UI 提供首选大小

JPanel p1=new JPanel();//添加一个JPanel 将这些按钮等都添加到一起
p1.setPreferredSize(new Dimension(750,30));
p1.setLayout(null);
p1.add(returnButton);
//p1.add(forwardButton);

p1.add(tfdAddress);
p1.add(refreshButton);
//p1.add(searchField);
//p1.add(btnSearch);
JPanel p2=new JPanel();//把新建文件夹 、打开文件 和显示文件信息的标签添加到一起
p2.setPreferredSize(new Dimension(750,25));
p2.setLayout(null);
p2.add(openFile);
p2.add(newFile);
p2.add(pLabel);
pnlMain = new JSplitPane(JSplitPane.VERTICAL_SPLIT,p1,p2);//将这两部分组件用分隔符分隔开
pnlMain.setDividerLocation(30);
pnlMain.setDividerSize(5);//然后再把合成的部分与刚才前面的浏览具体文件列表的部分合到一起
JSplitPane mainPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
pnlMain, explorerPane);
mainFrame = new JFrame();
mainFrame.setTitle("文件管理器");//添加完标题 这样的话就算是把整个的资源管理器界面布局打理好了
mainFrame.add(mainPane);
mainFrame.setBounds(100, 50, 800, 600);
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击右上角的按钮也可以退出
}

class JMenuDemo extends JMenuBar implements ActionListener{
JMenuItem item1;
public JMenuDemo(){
add(createJMenuone());
}
public JMenu createJMenuone(){
JMenu menu=new JMenu("文件(F)");
menu.setMnemonic(KeyEvent.VK_F);
JMenuItem item=new JMenuItem("新建(N)",KeyEvent.VK_N);
//表示键盘或等效输入设置上的键操作的 KeyStroke。KeyStroke 仅能对应于按下或释放某个特定的键
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));//CTRL_MASK是Ctrl 修饰符
// getKeyStroke 返回 KeyStroke 的共享实例,前者表示指定字符的 KEY_TYPED 事件
//setAccelerator设置组合键,它能直接调用菜单项的操作侦听器而不必显示菜单的层次结构。
menu.add(item);
item1=new JMenuItem("退出(X)",KeyEvent.VK_X);
item1.addActionListener((ActionListener) this);
item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
menu.add(item1);
return menu;
}
public void actionPerformed(ActionEvent e) {
// 自动生成方法存根
if(e.getSource()==item1){//得到最初发生 Event 的对象
System.exit(0);
}
}
}
public static void main(String[] args)
{
MainFrame mf=new MainFrame();
mf.show();
}
}