📄 remotefilelist.java
字号:
package ftpclient;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.Component;
import java.io.*;
import java.util.*;
import java.awt.Color;
import java.awt.event.*;
import cmd.TransactionCommand;
import cmd.CommandArgument;
import cmd.CommandManager;
import cmd.CommandReceiver;
public class RemoteFileList
extends JList {
public RemoteFileList(Vector files) {
//Object[]files={"aa","bb","cc"};
super(files);
ListRenderer lr = new ListRenderer();
this.setCellRenderer(lr);
new PopMenu(this);
}
protected class ListRenderer
extends JLabel implements ListCellRenderer {
public ListRenderer() {
this.setOpaque(true);
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected,
boolean cellHasFocus) {
setFont(list.getFont());
//设置图标为系统的文件类型图标
FileSystemView fileSystemView = FileSystemView.getFileSystemView();
String root = RemoteFileList.class.getResource("").getFile();
File file1 = fileSystemView.createFileObject(root);
Icon icon1 = fileSystemView.getSystemIcon(file1);
File file2 = fileSystemView.getHomeDirectory();
Icon icon2 = fileSystemView.getSystemIcon(file2);
String text= value.toString();
if(text.indexOf(".")>0&&!text.equals("..")){
setIcon(icon2);
}else{
setIcon(icon1);
}
setText(value.toString());
setBackground(isSelected ? Color.LIGHT_GRAY :
UIManager.getColor("List.textBackground"));
return this;
}
}
}
class JListMouseListener
extends MouseAdapter {
private RemoteFileList rfl;
private JPopupMenu jpp;
public JListMouseListener(RemoteFileList rfl,JPopupMenu jpp){
this.rfl=rfl;
this.jpp=jpp;
}
public void mouseClicked(MouseEvent e){
int index=rfl.getSelectedIndex();
int len=rfl.getModel().getSize();
String file="";
if(-1<index&&index<len)file =rfl.getModel().getElementAt(index).toString();
if((-1<index&&index<len)&&(e.getButton()==e.BUTTON1)&&(e.getClickCount()==2)){
if(file.equals("."))file="/";
Vector clist = new Vector();
Vector alist = new Vector();
CommandArgument ca = new CommandArgument();
CommandArgument ca1 = new CommandArgument();
ca.setArgument(file,"");
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();
rfl.updateUI();
}
if(-1<index&&index<len&&e.getButton()==e.BUTTON3){
jpp.show(rfl,e.getX(),e.getY());
}
}
}
class PopMenu{
private RemoteFileList rfl1;
private JPopupMenu jpp;
public PopMenu(RemoteFileList rfl) {
rfl1 = rfl;
jpp = new JPopupMenu();
JMenuItem ii = new JMenuItem("下载");
JMenuItem i2 = new JMenuItem("续传");
jpp.add(ii);
jpp.add(i2);
rfl.add(jpp);
rfl.addMouseListener(new JListMouseListener(rfl, jpp));
ii.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
jpp.setVisible(false);
int index=rfl1.getSelectedIndex();
String file=rfl1.getModel().getElementAt(index).toString();
Vector clist=new Vector();
Vector alist=new Vector();
CommandArgument ca= new CommandArgument();
ca.setArgument(file,"");
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 + -