📄 detailsview.java
字号:
public void removeAllListeners() {
removeMouseListener(mouseListener);
getSelectionModel().removeListSelectionListener(selectionListener);
}
/*****************************************************************************/
/**
* Selects the file at the specified point in the view. If no file
* exists at that point, the selection should be cleared.
*
* @param p The point at which a file should be selected.
*/
public void selectFileAtPoint(Point p) {
int row = rowAtPoint(p); // -1 if p isn't actually in table.
setRowSelectionInterval(row, row);
//ensureIndexIsVisible(row); // Not necessary for JTable.
}
/*****************************************************************************/
/**
* Sets the files displayed by this view.
*
* @param files A vector containing the files to display.
*/
public void setDisplayedFiles(Vector files) {
DetailsViewModel tableModel =
(DetailsViewModel)((FileExplorerTableModel)getModel()).getTableModel();
// The setData() call would be faster, but would require us
// to reset column 0's renderer to our custome one. So, for
// now, we're leaving it as the two substitute lines below.
// Although, we could substitute them and just re-add the
// renderer. The real bottleneck though is the File.length()
// calls...
//tableModel.setData(dirList);
tableModel.setRowCount(0);
tableModel.addRows(files);
initFileNameColumnSize();
}
/*****************************************************************************/
/**
* Sets whether or not this view allows the selection of multiple files.
*
* @param enabled Whether or not to allow the selection of multiple
* files.
*/
public void setMultiSelectionEnabled(boolean enabled) {
getSelectionModel().setSelectionMode(
enabled ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION :
ListSelectionModel.SINGLE_SELECTION);
}
/*****************************************************************************/
/**
* Selects the specified files in the view.
*
* @param files The files to select. If any of the files are not in
* the file chooser's <code>currentDirectory</code>, then
* they are not selected.
*/
public void setSelectedFiles(File[] files) {
int num = files.length;
if(num>0) {
int[] rows = new int[num];
for (int i=0; i<num; i++) {
rows[i] = getRowFor(files[i]);
if (rows[i] != -1)
addRowSelectionInterval(rows[i], rows[i]);
}
}
}
/*****************************************************************************/
/******************* PRIVATE INNER CLASSES ***********************************/
/*****************************************************************************/
/**
* Renders the "last modified" column.
*/
static class DateCellRenderer extends DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 3687931069714280385L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
setText(Utilities.getLastModifiedString(
((Long)value).longValue()));
return this;
}
}
/*****************************************************************************/
/**
* Table model for the details view.
*/
class DetailsViewModel extends DefaultTableModel {
/**
*
*/
private static final long serialVersionUID = -1227004305526022729L;
Vector tempVector;
public DetailsViewModel(String nameHeader, String sizeHeader,
String typeHeader, String statusHeader,
String lastModifiedHeader) {
super();
String[] columnNames = new String[4];
columnNames[0] = nameHeader;
//columnNames[1] = sizeHeader; // Huge performance hit for dirs with many files in Table view...
columnNames[1] = typeHeader;
columnNames[2] = statusHeader;
columnNames[3] = lastModifiedHeader;
setColumnIdentifiers(columnNames);
}
/**
* Appends a bunch of rows to the end of the table data. This method
* is added because <code>DefaultTableModel</code> has no means of
* adding multiple (i.e., many thousands of) rows without repeatedly
* calling <code>addRow</code> or <code>insertRow</code>, which
* adds a lot of overhead (e.g., notifies listeners of each line
* added instead of just all of them at once, etc.).
*/
public void addRows(Vector data) {
int dataSize = data.size();
if (dataSize==0)
return; // For example, if our filter filters out all files in this dir.
int dataVectorSize = dataVector.size();
dataVector.setSize(dataVectorSize + dataSize); // Ensure we have enough values.
for (int i=0; i<dataSize; i++) {
dataVector.set(dataVectorSize++,
getTableObjectVectorForFile((File)data.get(i)));
}
}
public Class getColumnClass(int column) {
switch (column) {
case 0: // File name.
return File.class;
case 3: // Last modified.
return Long.class;
default:
return Object.class;
}
}
private final Vector getTableObjectVectorForFile(final File file) {
// boolean isDirectory = file.isDirectory();
// String length = getFileSizeStringFor(file); // Invalid if file is a directory.
String description = chooser.getDescription(file);
boolean canRead = file.canRead();
boolean canWrite = file.canWrite();
String status = "";
if (canRead) {
if (canWrite)
status = chooser.readWriteString;
else
status = chooser.readString;
}
else if (canWrite)
status = chooser.writeString;
Vector tempVector = new Vector(5);//tempVector.clear();
tempVector.add(0, file);
// tempVector.add(isDirectory ? "" : length);
tempVector.add(1, description);
tempVector.add(2, status);
tempVector.add(3, new Long(file.lastModified()));
return tempVector;
}
public boolean isCellEditable(int row, int column) {
return false;
}
/**
* Takes a Vector (of files), and sets the data represented by this
* table to be suitable for the "Details View."
*/
public void setData(Vector dataVector) {
int size = dataVector.size(); // Convert file values to TableObject values.
for (int i=0; i<size; i++)
dataVector.setElementAt(getTableObjectVectorForFile((File)dataVector.get(i)), i);
setDataVector(dataVector, columnIdentifiers);
}
}
/*****************************************************************************/
/**
* Sorts two <code>File</code> objects as follows:<br>
* If one is a file and the other is a directory, returns that the
* directory object comes "before" the file object. If they are either
* both files or both directories, then it uses <code>File</code>'s
* standard <code>compareTo</code> method.
*/
static class FileComparator implements Comparator {
public int compare(Object o1, Object o2) {
File f1 = (File)o1;
File f2 = (File)o2;
boolean f1IsDir = f1.isDirectory();
boolean f2IsDir = f2.isDirectory();
if (f1IsDir) {
if (!f2IsDir)
return -1;
else // Both are directories.
return ((Comparable)f1).compareTo(f2);
}
else { // f1 isn't a directory.
if (f2IsDir)
return 1;
else // Both are regular files.
return ((Comparable)f1).compareTo(f2);
}
}
};
/*****************************************************************************/
/**
* Renderer used for columns displaying <code>File</code>s in a
* <code>JTable</code>.
*/
class FileTableColumnRenderer extends DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = -4961100472447021466L;
private Rectangle paintTextR = new Rectangle();
private Rectangle paintIconR = new Rectangle();
private Rectangle paintViewR = new Rectangle();
private boolean isAlreadyOpened;
public void paintComponent(Graphics g) {
String text = getText();
Icon icon = getIcon();
FontMetrics fm = g.getFontMetrics();
paintViewR.x = paintViewR.y = 0;
paintViewR.width = getWidth();
paintViewR.height = getHeight();
g.setColor(getBackground());
g.fillRect(paintViewR.x,paintViewR.y, paintViewR.width,paintViewR.height);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
String clippedText =
SwingUtilities.layoutCompoundLabel(this,
fm,
text,
icon,
getVerticalAlignment(),
getHorizontalAlignment(),
getVerticalTextPosition(),
getHorizontalTextPosition(),
paintViewR,
paintIconR,
paintTextR,
getIconTextGap());
if (icon != null)
icon.paintIcon(this, g, paintIconR.x, paintIconR.y);
if (text != null) {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();
g.setColor(getForeground());
g.drawString(clippedText, textX,textY);
if (isAlreadyOpened)
g.drawLine(textX, textY+2, textX+paintTextR.width, textY+2);
}
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
File file = (File)value;
String fileName = file.getName();
isAlreadyOpened = chooser.isUnderlinedFile(file);
setText(fileName);
// Set the image according to the file type.
FileTypeInfo info = chooser.getFileTypeInfoFor(file);
setIcon(info.icon);
if (!isSelected) {
if (chooser.getShowHiddenFiles() && file.isHidden())
setForeground(chooser.getHiddenFileColor());
else
setForeground(info.labelTextColor);
}
return this;
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -