📄 remotefiletree.java
字号:
package ftpclient;
import javax.swing.tree.*;
import javax.swing.filechooser.*;
import javax.swing.event.*;
import java.awt.Cursor;
import java.awt.Component;
import java.awt.Font;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import cmd.*;
public class RemoteFileTree
extends JTree {
protected DefaultTreeModel treeModel;
protected FileSystemView fileSystemView; //建立文件系统视类对象
protected DefaultMutableTreeNode root;
protected Vector filesname;
public RemoteFileTree(){
Font myFont = new Font("宋体", 11, 12);
root =
new DefaultMutableTreeNode(new File("/"));
treeModel = new DefaultTreeModel(root);
this.setModel(treeModel);
this.setFont(myFont);
this.setRootVisible(true);
this.setRowHeight(20);
new PopInitial(this);
}
public void createNodes(DefaultMutableTreeNode top,Vector filesname) {
DefaultMutableTreeNode file = null;
for (int i = 2; i < filesname.size(); i++) {
file = new DefaultMutableTreeNode(new File( (String) filesname.elementAt(
i)));
top.add(file);
if(((String)filesname.elementAt(i)).indexOf(".")!=-1){
file.setAllowsChildren(false);
}else{
DefaultMutableTreeNode useless=new DefaultMutableTreeNode(new File("."));
file.add(useless);
}
}
}
}
class TreeMouseListener
extends MouseAdapter {
private RemoteFileTree rft;
private JPopupMenu jpp;
public TreeMouseListener(RemoteFileTree rft,JPopupMenu jpp) {
this.rft = rft;
this.jpp=jpp;
}
public void mouseClicked(MouseEvent e) {
int count = e.getClickCount();
DefaultMutableTreeNode selectedNode
= (DefaultMutableTreeNode) rft.getLastSelectedPathComponent();
if((selectedNode!=null)&&(e.getButton() == e.BUTTON1)&&(count==1)&&!selectedNode.isRoot()&&!selectedNode.isLeaf()){
String directory=selectedNode.getParent().toString();
Vector clist = new Vector();
Vector alist = new Vector();
CommandArgument ca = new CommandArgument();
ca.setArgument(directory,"");
clist.addElement("Cwd");
alist.addElement(ca);
TransactionCommand tc = new TransactionCommand(clist, alist);
CommandManager cm = new CommandManager(tc);
cm.runCommands();
}
if ((selectedNode!=null)&&(e.getButton() == e.BUTTON1)&&count==2) {
rft.updateUI();
TreePath nodePath = rft.getSelectionModel().getSelectionPath();
Object[] ob = nodePath.getPath();
String filename = ob[nodePath.getPathCount() - 1].toString().trim();
if ((selectedNode!=null)&&(nodePath.getLastPathComponent()!=null)&&nodePath.getPathCount()>=1) {
//得到子文件
Vector clist = new Vector();
Vector alist = new Vector();
CommandArgument ca = new CommandArgument();
CommandArgument ca1 = new CommandArgument();
ca.setArgument(ob[nodePath.getPathCount() - 1].toString().trim(),"");
ca1.setArgument("", "");
clist.addElement("Cwd");
clist.addElement("List");
alist.addElement(ca);
alist.addElement(ca1);
TransactionCommand tc = new TransactionCommand(clist, alist);
CommandManager cm = new CommandManager(tc);
cm.runCommands();
Vector names = CommandReceiver.filesname;
selectedNode.removeAllChildren();
for (int i = 2; i < names.size(); i++) {
DefaultMutableTreeNode newNode= new DefaultMutableTreeNode(new File(names.elementAt(i).toString().trim()));
if(names.elementAt(i).toString().trim().indexOf(".")==-1){
DefaultMutableTreeNode uselessNode = new DefaultMutableTreeNode(new File("."));
System.out.println("这里"+uselessNode);
newNode.setAllowsChildren(true);
newNode.add(uselessNode);
if(newNode.isLeaf())System.out.println("要命啊");
}
selectedNode.add(newNode);
//System.out.println(names.elementAt(i).toString());
}
rft.updateUI();
}
}
if(e.getButton()==e.BUTTON3) {
int x = e.getX();
int y = e.getY();
int row = rft.getRowForLocation(x, y);
jpp.show(rft,x,y);
}
}
}
class PopInitial{
private RemoteFileTree rft1;
public PopInitial(RemoteFileTree rft){
rft1=rft;
JPopupMenu jpp = new JPopupMenu();
JMenuItem ii = new JMenuItem("下载");
JMenuItem i2 = new JMenuItem("续传");
jpp.add(ii);
jpp.add(i2);
rft.add(jpp);
rft.addMouseListener(new TreeMouseListener(rft,jpp));
ii.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
TreePath nodePath=rft1.getSelectionModel().getSelectionPath();
Object[] ob=nodePath.getPath();
//System.out.println(ob[2].toString().trim());
Vector clist=new Vector();
Vector alist=new Vector();
CommandArgument ca= new CommandArgument();
ca.setArgument(ob[nodePath.getPathCount()-1].toString().trim(),"");
clist.addElement("Get");
alist.addElement(ca);
TransactionCommand tc = new TransactionCommand(clist, alist);
CommandManager cm = new CommandManager(tc);
cm.runCommands();
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -