📄 localfiletree.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.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.*;
import cmd.*;
public class LocalFileTree
extends JTree implements TreeExpansionListener, TreeSelectionListener,
MouseListener {
protected DefaultTreeModel treeModel;
protected FileSystemView fileSystemView; //建立文件系统视类对象
protected FileNode root;
public LocalFileTree() {
Font myFont = new Font("宋体", 11, 12);
fileSystemView = FileSystemView.getFileSystemView();
root = new FileNode(fileSystemView.getRoots()[0]);
root.explore();
treeModel = new DefaultTreeModel(root);
this.setModel(treeModel); //设定树形菜单
this.addTreeExpansionListener(this); //打开/关闭节点事件
this.addTreeSelectionListener(this); //选择的事件
this.setCellRenderer(new MyTreeCellRenderer()); //生成图标
this.setFont(myFont);
this.setRootVisible(true);
this.setRowHeight(18);
this.addMouseListener(this);
}
protected class MyTreeCellRenderer
extends JLabel implements TreeCellRenderer {
public MyTreeCellRenderer() {
//this.setBackground(UIManager.getColor("Tree.textBackground"));
this.setOpaque(true);
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
String stringValue = tree.convertValueToText(value, sel, expanded, leaf,
row, hasFocus);
setFont(tree.getFont());
//设置图标为系统的文件类型图标
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
setIcon(fileSystemView.getSystemIcon( ( (FileNode) value).getFile()));
setText(stringValue);
setBackground(hasFocus ? Color.LIGHT_GRAY: UIManager.getColor("Tree.textBackground"));
return this;
}
}
//节点张开事件
public void treeExpanded(TreeExpansionEvent event) {
//判断是否是叶节点
//if (this.getLastSelectedPathComponent() == null) {
//System.out.println("ok");
//return;
//}
setCursor(new Cursor(Cursor.WAIT_CURSOR));
TreePath path = event.getPath();
//System.out.println(path.toString());
FileNode node = (FileNode) path.getLastPathComponent();
node.explore();
treeModel.nodeStructureChanged(node);
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
//节点闭合事件
public void treeCollapsed(TreeExpansionEvent event) {
}
//文件节点类
protected class FileNode
extends DefaultMutableTreeNode {
private boolean isSelected = false;
private boolean explored = false;
public FileNode(File file) {
this(file, false);
}
public FileNode(File file, boolean bool) {
super(file);
this.isSelected = bool;
}
//
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
/*if (children != null) {
Enumeration enu = children.elements();
while (enu.hasMoreElements()) {
FileNode node = (FileNode) enu.nextElement();
node.setSelected(isSelected);
}
}*/
}
//
public boolean getAllowsChildren() {
return isDirectory();
}
public boolean isLeaf() {
return!isDirectory();
}
public File getFile() {
return (File) getUserObject();
}
public boolean isExplored() {
return explored;
}
public void setExplored(boolean b) {
explored = b;
}
public boolean isDirectory() {
return getFile().isDirectory();
}
public String toString() {
File file = (File) getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length() - 1)
? filename.substring(index + 1) : filename;
}
public void explore() {
if (!isExplored()) {
File file = getFile();
File[] children = file.listFiles();
if (children == null || children.length == 0)
return;
for (int i = 0; i < children.length; ++i) {
File f = children[i];
add(new FileNode(children[i], isSelected));
}
explored = true;
}
}
}
/**
* 选择节点触发的事件
* 继承或是直接引用需要重新写此方法
* @param e
*/
public void valueChanged(TreeSelectionEvent e) {
//文件路径
String sFilePath = "";
Object myobj = this.getLastSelectedPathComponent();
if (myobj != null) {
sFilePath = ( (File) ( ( (DefaultMutableTreeNode) (myobj)).getUserObject())).
getPath();
}
//System.out.println(sFilePath);
}
/**
* Invoked when the mouse button has been clicked (pressed and released) on a
* component.
*
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseClicked(MouseEvent e) {
int count = e.getClickCount();
if(e.getButton()==e.BUTTON3){
JPopupMenu jpp = new JPopupMenu();
JMenuItem ii = new JMenuItem("上传");
jpp.add(ii);
this.add(jpp);
int x = e.getX();
int y = e.getY();
int row = this.getRowForLocation(x, y);
jpp.show(this,x,y);
ii.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
popListen();
}
});
}
if(e.getButton()==e.BUTTON1&&count==1){
int x = e.getX();
int y = e.getY();
int row = this.getRowForLocation(x, y);
TreePath path = this.getPathForRow(row);
if (path != null) {
FileNode node = (FileNode) path.getLastPathComponent();
//boolean isSelected = ! (node.isSelected());
node.setSelected(true);
//( (DefaultTreeModel)this.getModel()).nodeChanged(node);
}
}
if (count == 2) {
int x = e.getX();
int y = e.getY();
int row = this.getRowForLocation(x, y);
TreePath path = this.getPathForRow(row);
if (path != null) {
FileNode node = (FileNode) path.getLastPathComponent();
boolean isSelected = ! (node.isSelected());
node.setSelected(isSelected);
( (DefaultTreeModel)this.getModel()).nodeChanged(node);
}
}
}
public void popListen() {
TreePath nodePath = this.getSelectionModel().getSelectionPath();
Object[] ob = nodePath.getPath();
String file="";
if(nodePath.getPathCount()>3){
int tmp=(ob[2].toString()).indexOf("\\");
file=ob[2].toString().substring(0,tmp)+"/";
for (int i = 3; i < nodePath.getPathCount() - 1; i++) {
file += ob[i].toString().trim()+"/";
}
file += ob[nodePath.getPathCount() - 1].toString().trim();
}
System.out.println(file);
Vector clist = new Vector();
Vector alist = new Vector();
CommandArgument ca = new CommandArgument();
ca.setArgument(file,"");
clist.addElement("PutFile");
alist.addElement(ca);
TransactionCommand tc = new TransactionCommand(clist, alist);
CommandManager cm = new CommandManager(tc);
cm.runCommands();
}
/**
* Invoked when a mouse button has been pressed on a component.
*
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mousePressed(MouseEvent e) {
}
/**
* Invoked when a mouse button has been released on a component.
*
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseReleased(MouseEvent e) {
}
/**
* Invoked when the mouse enters a component.
*
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseEntered(MouseEvent e) {
}
/**
* Invoked when the mouse exits a component.
*
* @param e MouseEvent
* @todo Implement this java.awt.event.MouseListener method
*/
public void mouseExited(MouseEvent e) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -