📄 filelist.java
字号:
package javazip;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Arrays;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javazip.base.FileInfo;
/**
* This class is a talbe used for display the files under a folder.
*/
public class FileList extends JTable {
private Object[][] cells;
private String[] columnNames = { "名称", "大小", "压缩率", "类型", "修改时间" };
public String path = "C:\\";
private File currentPath;
private String[] fileList;
private DefaultTableModel model;
private SortFilterModel sorter;
/**
* the Constructor with a parameter String path.
*/
public FileList() {
super();
this.init();
}
/**
* Initalize the FileList
*/
private void init() {
this.currentPath = new File(path);
this.fileList = currentPath.list();
this.cells = this.getFileList();
// set up click handler for column headers
this.getTableHeader().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
// find column of click and
int tableColumn = columnAtPoint(event.getPoint());
// translate to table model index and sort
int modelColumn = convertColumnIndexToModel(tableColumn);
sorter.sort(modelColumn);
}
});
this.model = new DefaultTableModel(cells, columnNames);
this.sorter = new SortFilterModel(model);
this.setModel(sorter);
}
/**
* @return the filelist under the current folder
*/
private Object[][] getFileList() {
Object[][] fileListTable = new Object[fileList.length][5];
for (int i = 0; i < fileList.length; i++) {
fileListTable[i] = getFileProperty(fileList[i]);
}
return fileListTable;
}
private Object[] getFileProperty(String fileName) {
Object[] property = new Object[5];
File file = new File(path + "\\" + fileName);
FileInfo fileInfo = new FileInfo(file);
property[0] = fileInfo.getFileName();
property[1] = fileInfo.getFileLength();
if (fileInfo.getZippedLength() == null) {
property[2] = "UnKnown";
} else {
double temp1 = 0, temp2 = 0;
temp1 = fileInfo.getFileLength().longValue();
temp2 = fileInfo.getZippedLength().longValue();
if (temp2 > 0) {
int ratio = (int) ((temp1 / temp2) * 100);
property[2] = ratio + "%";
}
else {
property[2] = "UnKnown";
}
}
property[3] = fileInfo.getFileType();
property[4] = fileInfo.getModifyDate();
return property;
}
/**
* Set the new file list by a
*
* @param newPath
*/
public void setPath(String newPath) {
this.path = newPath;
this.init();
}
public File[] getSelectedFiles() {
int[] selectedRowsIndexs = this.getSelectedRows();
File[] files = new File[selectedRowsIndexs.length];
for (int i = 0; i < files.length; i++) {
files[i] = new File(this.path + "\\"
+ this.getValueAt(selectedRowsIndexs[i], 0).toString());
}
return files;
}
public int getSelectedFileNums(){
return this.getSelectedRows().length;
}
public int getDirsNum(){
int dirNums=0;
File[] files = this.currentPath.listFiles();
for (int i=0;i<files.length;i++){
if(files[i].isDirectory())dirNums++;
}
return dirNums;
}
public int getFilesNum(){
return this.fileList.length-this.getDirsNum();
}
public long getTotalSize(){
long size=0;
File[] files = this.currentPath.listFiles();
for (int i=0;i<files.length;i++){
size+=files[i].length();
}
return size;
}
}
/**
* This table model takes an existing table model and produces a new model that
* sorts the rows so that the entries in a particular column are sorted.
*/
class SortFilterModel extends AbstractTableModel {
/**
* Constructs a sort filter model.
*
* @param m
* the table model to filter
*/
private TableModel model;
private int sortColumn;
private Row[] rows;
public SortFilterModel(TableModel m) {
model = m;
rows = new Row[model.getRowCount()];
for (int i = 0; i < rows.length; i++) {
rows[i] = new Row();
rows[i].index = i;
}
}
/**
* Sorts the rows.
*
* @param c
* the column that should become sorted
*/
public void sort(int c) {
sortColumn = c;
Arrays.sort(rows);
fireTableDataChanged();
}
// Compute the moved row for the three methods that access
// model elements
public Object getValueAt(int r, int c) {
return model.getValueAt(rows[r].index, c);
}
/*
* Can't edit the value now,but can be added "Rename" later
*/
public boolean isCellEditable(int r, int c) {
return false;
}
public void setValueAt(Object aValue, int r, int c) {
model.setValueAt(aValue, rows[r].index, c);
}
// delegate all remaining methods to the model
public int getRowCount() {
return model.getRowCount();
}
public int getColumnCount() {
return model.getColumnCount();
}
public String getColumnName(int c) {
return model.getColumnName(c);
}
public Class getColumnClass(int c) {
return model.getColumnClass(c);
}
/**
* This inner class holds the index of the model row Rows are compared by
* looking at the model row entries in the sort column.
*/
private class Row implements Comparable {
public int index;
public int compareTo(Object other) {
Row otherRow = (Row) other;
Object a = model.getValueAt(index, sortColumn);
Object b = model.getValueAt(otherRow.index, sortColumn);
if (a instanceof Comparable)
return ((Comparable) a).compareTo(b);
else
return a.toString().compareTo(b.toString());
// return index - otherRow.index;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -