📄 detailsview.java
字号:
/*
* 07/14/2004
*
* DetailsView.java - The "Details view" (i.e., table view) for an
* RTextFileChooser.
* Copyright (C) 2004 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui.rtextfilechooser;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
import org.fife.ui.FileExplorerTableModel;
/**
* A "details view" (i.e., a table view) for an <code>RTextFileChooser</code>.
* This is similar to the Details view found in Microsoft Windows file
* choosers.
*
* @author Robert Futrell
* @version 0.1
*/
class DetailsView extends JTable implements RTextFileChooserView {
/**
*
*/
private static final long serialVersionUID = 8869970415045984445L;
private static final int MAX_NAME_COLUMN_SIZE = 150;
private RTextFileChooser chooser; // The chooser this view is in.
// Listeners.
private MouseListener mouseListener;
private SelectionListener selectionListener;
/*****************************************************************************/
/**
* Creates a details view.
*
* @param chooser The file chooser that owns this view.
* @param nameString The title for the "Name" column.
* @param sizeString The title for the "Size" column.
* @param typeString The title for the "Type" column.
* @param statusString The title for the "Status" column.
* @param lastModifiedString The title for the "Date Modified" column.
*/
public DetailsView(RTextFileChooser chooser, String nameString,
String sizeString, String typeString, String statusString,
String lastModifiedString) {
super();
enableEvents(AWTEvent.KEY_EVENT_MASK);
this.chooser = chooser;
// Create the table model, then wrap it in a sorter model.
// FIXME: Combine these two into a single model.
DetailsViewModel dvm = new DetailsViewModel(nameString, sizeString,
typeString, statusString, lastModifiedString);
FileExplorerTableModel sorter = new FileExplorerTableModel(dvm);
setModel(sorter);
sorter.setTable(this);
// Prevent this table from interpreting Enter to mean "move to the
// next row."
InputMap tableInputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
tableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "none");
// Make this table look just a little nicer.
setIntercellSpacing(new Dimension(0,0));
setShowGrid(false);
setColumnSelectionAllowed(false);
// Set the renderer for each column. We must do this even for normal
// columns because otherwise the columns we sort by won't be
// gray-highlighted.
int columnCount = getColumnCount();
TableColumnModel columnModel = getColumnModel();
getColumnModel().getColumn(0).setCellRenderer(new FileTableColumnRenderer());
for (int i=1; i<columnCount-1; i++)
columnModel.getColumn(i).setCellRenderer(new DefaultTableCellRenderer());
columnModel.getColumn(columnCount-1).setCellRenderer(new DateCellRenderer());
sorter.setColumnComparator(File.class, new FileComparator());
// Add any listeners.
mouseListener = new MouseListener(chooser);
addMouseListener(mouseListener);
selectionListener = new SelectionListener(chooser);
getSelectionModel().addListSelectionListener(selectionListener);
}
/*****************************************************************************/
/**
* Clears all files displayed by this view.
*/
public void clearDisplayedFiles() {
((DefaultTableModel)getModel()).setRowCount(0); // Just so the file list gets erased.
}
/*****************************************************************************/
/**
* Makes sure the specified file is visible in the view.
*
* @param file The file that is to be visible.
*/
public void ensureFileIsVisible(File file) {
int row = getRowFor(file);
if (row!=-1)
scrollRectToVisible(getCellRect(row, 0, true));
}
/*****************************************************************************/
/**
* Returns the number of files currently being displayed.
*
* @return The number of files currently being displayed.
*/
public int getDisplayedFileCount() {
return getRowCount();
}
/*****************************************************************************/
/**
* Returns the file at the specified point in the view.
*
* @param p The point at which to look for a file.
* @return The file at that point (or <code>null</code> if there isn't
* one???).
*/
public File getFileAtPoint(Point p) {
int row = rowAtPoint(p);
return (File)getValueAt(row, 0);
}
/*****************************************************************************/
/**
* Returns the row in which the specified file resides in this table
* view.
*
* @param file The file to search for.
* @return The row the specified file is in, or <code>-1</code> if it
* isn't in the view.
*/
private final int getRowFor(File file) {
// FIXME: Is there a better way to find the row of the specified
// file???
// We must do a linear search because the files will usually (always?)
// not be listed in alphabetical order (i.e., folders come first, and
// the user can sort by column).
TableColumnModel columnModel = getColumnModel();
int column = columnModel.getColumn(0).getModelIndex();
TableModel tableModel = getModel();
int rowCount = getRowCount();
for (int i=0; i<rowCount; i++) {
File temp = (File)tableModel.getValueAt(i, column);
if (file.equals(temp))
return i;
}
return -1;
}
/*****************************************************************************/
/**
* Gets the selected file, for use when a single file is selected.
*
* @return The selected file, or <code>null</code> if no file is
* selected.
*/
public File getSelectedFile() {
int index = getSelectedRow();
return (File)getValueAt(index, 0);
}
/*****************************************************************************/
/**
* Returns all selected files in this view.
*
* @return An array of all selected files.
*/
public File[] getSelectedFiles() {
int[] selectedRows = getSelectedRows();
int num = selectedRows.length;
Object[] objArray = new Object[num];
int column = convertColumnIndexToView(0);
for (int i=0; i<num; i++)
objArray[i] = getValueAt(selectedRows[i], column);
int length = objArray.length;
File[] fileArray = new File[length];
System.arraycopy(objArray,0, fileArray,0, length);
return fileArray;
}
/*****************************************************************************/
/**
* Returns the tool tip to display for a given mouse event.
*
* @param e The mouse event.
* @return The tool tip.
*/
public String getToolTipText(MouseEvent e) {
String tip = null;
int row = rowAtPoint(e.getPoint());
if (row==-1)
return null;
File file = (File)getValueAt(row, 0);
if (file==null || file.isDirectory())
return null;
tip = chooser.getToolTipFor(file);
return tip;
}
/*****************************************************************************/
/**
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidthToFit().
*/
private void initFileNameColumnSize() {
TableModel model = getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int maxWidth = 0;
int cellWidth = 0;
TableCellRenderer headerRenderer = getTableHeader().getDefaultRenderer();
int col = convertColumnIndexToView(0);
column = getColumnModel().getColumn(col);
comp = headerRenderer.getTableCellRendererComponent(
this, column.getHeaderValue(),
false, false, 0,0);
headerWidth = comp.getPreferredSize().width;
TableCellRenderer renderer = getDefaultRenderer(model.getColumnClass(0));
int rowCount = getRowCount();
for (int i=0; i<rowCount; i++) {
comp = renderer.getTableCellRendererComponent(
this, getValueAt(i,col),
false, false, i,col);
cellWidth = comp.getPreferredSize().width;
if (maxWidth<cellWidth)
maxWidth = cellWidth;
}
int width = Math.min(Math.max(headerWidth, maxWidth), MAX_NAME_COLUMN_SIZE);
column.setPreferredWidth(width);
column.setWidth(width);
}
/*****************************************************************************/
/**
* Process key presses in this list view. This is overridden so that
* pressing the delete key triggers a delete of the selected file(s).
*
* @param e The key event.
*/
protected void processKeyEvent(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DELETE:
if (e.getID()==KeyEvent.KEY_PRESSED)
chooser.handleDelete();
// Fall through.
default:
super.processKeyEvent(e);
}
}
/*****************************************************************************/
/**
* Removes all listeners this view has created and added to itself. This
* method is here to get around the fact that <code>finalize</code> is
* not going to be called as long as listeners are still registered for
* this view, but nobody else knows about these listeners except for the
* view.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -