?? foranesourcetableclass.java
字號:
package FtpForaneSource;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import com.jcat.ftp.*;
/**
* 此類為一個第一列帶圖標和字符串的數據表。
* @author 張永結。
*
*/
public class ForaneSourceTableClass extends JTable
{
private final Icon iconDirectory = new ImageIcon("Directory.png");
private final Icon iconFile = new ImageIcon("File.png");
/**
* 無參構造函數,用于設置數據表第一列渲染器等。
*/
public ForaneSourceTableClass()
{
super(new MyTableModel());
setShowGrid(false);
getColumnModel().getColumn(0).setCellRenderer(new DefaultImagedTableCellRenderer());
setFillsViewportHeight(true);
}
/**
* 由一個抽象路徑名數組創建一個JTable實例。
* @param file 抽象路徑名數組。
*/
public ForaneSourceTableClass(FTPFile[] file)
{
this();
showFTPFile(file);
}
/**
* 構造一個JTable來顯示 FTPFile[]中抽象路徑名的信息。
* @param file 抽象路徑名數組。
*/
public void showFTPFile(FTPFile[] file)
{
MyTableModel tempModel = (MyTableModel)getModel();
tempModel.setData(getInformation(file));
}
/**
* 返回一個以Vector為元素的Vector,它的每個元素包含有其抽象路徑名表示的文件或目錄的圖標、名稱、大小、類型、最后修改時間,作為數據表信息。
* @param file 抽象路徑名數組。
* @return 返回一個以Vector為元素的Vector,它的每個元素包含有其抽象路徑名表示的文件或目錄的圖標、名稱、大小、類型、最后修改時間,作為數據表信息。
*/
private Vector<Vector> getInformation(FTPFile[] file)
{
Vector<Vector> source = new Vector<Vector>();
for(int i=0; i<file.length; i++)
{
ImagedTableCell cell = new ImagedTableCell(getFileIcon(file[i]), file[i].getName());
Vector rowsource = new Vector();
rowsource.addElement(cell);
rowsource.addElement((getFileSize(file[i])));
rowsource.addElement(getFileType(file[i]));
rowsource.addElement(getModifiedTime(file[i]));
source.addElement(rowsource);
}
return source;
}
/**
* 返回表示由此抽象路徑名表示的文件或目錄的圖標,僅分為文件夾和文件兩種圖標。
* @param file 抽象路徑名。
* @return 表示由此抽象路徑名表示的文件或目錄的圖標,僅分為文件夾和文件兩種圖標。
*/
private Icon getFileIcon(FTPFile file)
{
if(file.isDirectory())
{
return iconDirectory;
}
else
return iconFile;
}
/**
* 返回由此抽象路徑名表示的文件或目錄的類型,若是為文件夾,返回類型“文件夾”;若是為無擴展名的文件,則返回“未知文件”;
* 其余文件返回由其擴展名加上字符“文件”所表示的字符串。
* @param file 抽象路徑名。
* @return 由此抽象路徑名表示的文件或目錄的類型,若是為文件夾,返回類型“文件夾”;若是為無擴展名的文件,則返回“未知文件”;
* 其余文件返回由其擴展名加上字符“文件”所表示的字符串。
*/
private String getFileType(FTPFile file)
{
if(file.isDirectory())
{
return "文件夾";
}
else
{
int a = file.getName().lastIndexOf(".");
if(a == -1)
{
return "未知文件";
}
else
{
return file.getName().substring(a+1) + "文件";
}
}
}
/**
* 返回表示由此抽象路徑名表示的文件的大小字符串,單位分別為bytes、KB、M,保留小數點后兩位。
* @param file 抽象路徑名。
* @return 返回表示由此抽象路徑名表示的文件的大小字符串,單位分別為bytes、KB、M,保留小數點后兩位。
*/
private String getFileSize(FTPFile file)
{
if(file.isDirectory())
{
return "";
}
else
{
String size;
long temp = file.length();
DecimalFormat decformat = new DecimalFormat("0.00");
if(temp>=0 &&temp <1024)
{
return size = temp +"bytes";
}
else if(temp>=1024 && temp <1024*1024)
{
return size = decformat.format(temp/1024) + "KB";
}
else
{
return size = decformat.format(temp/1024/1024) + "M";
}
}
}
/**
* 返回表示由此抽象路徑名表示的文件或目錄的本地話的最后修改時間的字符串。
* @param file 抽象路徑名。
* @return 表示由此抽象路徑名表示的文件或目錄的本地話的最后修改時間的字符串。
*/
private String getModifiedTime(FTPFile file)
{
Date date = new Date(file.lastModified());
return DateFormat.getDateInstance().format(date);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -