📄 localsourcepanelclass.java
字号:
package FtpLocalSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.util.*;
import java.text.*;
import java.io.*;
/**
* 此类为继承JPanel,包含一个带图标下拉框(类JImagedComboBox)和一个带图标数据表(类JTable),用于查看本地信息。
* @author 张永结
*/
public class LocalSourcePanelClass extends JPanel
{
private static final long serialVersionUID=-8169677449370072828L;
private JButton superior_but;
private Icon superior_ima;
private JTable source_tab;
private JImagedComboBox source_imagecom;
private Vector<ImagedComboBoxItem> items;
private static final FileSystemView filesystem=FileSystemView.getFileSystemView();
private File current;
private File[] disks=filesystem.getRoots()[0].listFiles()[0].listFiles();
/**
* 构造一个LocalSourcePanelClass。
*/
public LocalSourcePanelClass()
{
super(new BorderLayout());
initComponent();
addComponent();
registerEvent();
}
/**
* 该方法用于初始化布局管理器、下拉框、按钮、数据表等。
*/
private void initComponent()
{
// 创建按钮和panel
superior_ima=new ImageIcon("superior.png");
superior_but=new JButton(superior_ima);
superior_but.setToolTipText("返回上一级");
// 创建下拉框,包含盘符
items=new Vector<ImagedComboBoxItem>();
for(int i=0;i<disks.length;i++) // 添加盘符
{
items.add(new ImagedComboBoxItem(filesystem
.getSystemDisplayName(disks[i]),filesystem
.getSystemIcon(disks[i]),1));
}
current=disks[0];
source_imagecom=new JImagedComboBox(items);
// 创建数据表
MyTableModel initModel=new MyTableModel(getFiles(current));
source_tab=new JTable(initModel);
source_tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
source_tab.setShowGrid(false);
source_tab.setFillsViewportHeight(true);
source_tab.getColumnModel().getColumn(0).setCellRenderer(
new DefaultImagedTableCellRenderer());
}
/**
* 该方法将各组件组合一起。
*/
private void addComponent()
{
JPanel top_pan=new JPanel(new BorderLayout(2,2));
top_pan.add(source_imagecom,BorderLayout.CENTER);
top_pan.add(superior_but,BorderLayout.EAST);
JScrollPane scrollPane=new JScrollPane(source_tab);
this.add(top_pan,BorderLayout.NORTH);
this.add(scrollPane,BorderLayout.CENTER);
}
/**
* 该方法实现下拉框、按钮、数据表的事件注册。
*/
private void registerEvent()
{
source_imagecom.addItemListener(new ComboxEvent());
superior_but.addActionListener(new ButtonEvent());
source_tab.addMouseListener(new DoubleClickEvent());
}
/**
* 此类实现双击数据表中的某行时返回其子目录和文件事件。
*/
private class DoubleClickEvent extends MouseAdapter
{
private long time;
public DoubleClickEvent()
{
super();
}
public void mouseClicked(MouseEvent mevent)
{
if(mevent.getButton()==MouseEvent.BUTTON1){
long newTime=new Date().getTime();
if(newTime-time<300){
int row=source_tab.getSelectedRow();
if(row>=0){
File temp;
String name=((ImagedTableCell)source_tab.getValueAt(
row,0)).getText();
temp=new File(current.getAbsolutePath()+'\\'+name);
if(filesystem.isTraversable(temp)){
MyTableModel tempmodel=(MyTableModel)source_tab
.getModel();
tempmodel.setData(getFiles(temp));
current=temp;
}
else{
JOptionPane.showMessageDialog(
LocalSourcePanelClass.this,"您无权限打开该文件夹!",
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
time=newTime;
}
}
}
/**
* 此类实现返回上一级按钮事件。
*/
private class ButtonEvent implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(!filesystem.isDrive(current)){
File parent=current.getParentFile();
if(filesystem.isTraversable(parent)){
MyTableModel tempmodel=(MyTableModel)source_tab.getModel();
tempmodel.setData(getFiles(parent));
current=parent;
}
else{
JOptionPane.showMessageDialog(LocalSourcePanelClass.this,
"您无权限打开该文件夹!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
}
/**
* 此类实现下拉菜单事件。
*/
private class ComboxEvent implements ItemListener
{
public void itemStateChanged(ItemEvent ievent)
{
if(ievent.getStateChange()==ItemEvent.SELECTED){
int i=source_imagecom.getSelectedIndex();
File temp;
temp=disks[i];
if(filesystem.isTraversable(temp)){
MyTableModel tempmodel=(MyTableModel)source_tab.getModel();
tempmodel.setData(getFiles(temp));
current=temp;
}
else{
JOptionPane.showMessageDialog(LocalSourcePanelClass.this,
"您无权限打开该文件夹!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
}
/**
* 返回一个以向量为元素的向量,其中包含file抽象路径名所表示的目录的子目录和文件的信息,其做为数据表信息。
* @param file 抽象路径名。
* @return 一个以向量为元素的向量,其中包含file抽象路径名所表示的目录的子目录和文件的信息,其做为数据表信息。
*/
private Vector<Vector> getFiles(File file)
{
File[] roots=filesystem.getFiles(file,true);
Vector<Vector> source=new Vector<Vector>();
Vector rowsource;
for(int i=0;i<roots.length;i++){
ImagedTableCell cell=new ImagedTableCell(filesystem
.getSystemIcon(roots[i]),filesystem
.getSystemDisplayName(roots[i]));
rowsource=new Vector();
rowsource.add(cell);
rowsource.add(getFileSize(roots[i]));
rowsource.add(filesystem.getSystemTypeDescription(roots[i]));
rowsource.add(getModifiedTime(roots[i]));
source.add(rowsource);
}
return source;
}
/**
* 返回表示由此抽象路径名表示的文件的大小字符串,单位分别为bytes、KB、M,保留小数点后两位。
* @param file 抽象路径名。
* @return 返回表示由此抽象路径名表示的文件的大小字符串,单位分别为bytes、KB、M,保留小数点后两位。
*/
private String getFileSize(File file)
{
if(file.isDirectory()){
return "";
}
else{
String size;
long temp=file.length();
DecimalFormat decformat=new DecimalFormat("0.00");
if(temp>=0&&temp<1024){
size=temp+"bytes";
}
else if(temp>=1024&&temp<1024*1024){
size=decformat.format(temp/1024)+"KB";
}
else{
size=decformat.format(temp/1024/1024)+"M";
}
return size;
}
}
/**
* 返回表示由此抽象路径名表示的文件或目录的本地话的最后修改时间的字符串。
* @param file 抽象路径名。
* @return 表示由此抽象路径名表示的文件或目录的本地话的最后修改时间的字符串。
*/
private String getModifiedTime(File file)
{
Date date=new Date(file.lastModified());
return DateFormat.getDateInstance().format(date);
}
/**
* 返回此抽象路径名的绝对路径名字符串。
* @return 此抽象路径名的绝对路径名字符串。
*/
public String getCurrentDirecory()
{
return current.getAbsolutePath();
}
/**
* 返回当前组件中的JTable对象source_tab。
* @return 当前组件中的JTable对象source_tab。
*/
public JTable getTable()
{
return source_tab;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -