📄 findinfilestable.java
字号:
/*
* 10/03/2005
*
* FindInFilesTable.java - A table listing search results in a Find in Files
* dialog.
* Copyright (C) 2005 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.search;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.fife.RListSelectionModel;
/**
* The table used to display search results in a
* <code>FindInFilesDialog</code>.
*
* @author Robert Futrell
* @version 1.0
*/
public class FindInFilesTable extends JTable {
private DefaultTableModel tableModel;
private ArrayList matchDatas;
private TableCellRenderer verboseCellRenderer;
/*****************************************************************************/
/**
* Constructor.
*/
public FindInFilesTable() {
super();
tableModel = new DefaultTableModel();
tableModel.addColumn("File");
tableModel.addColumn("Line");
tableModel.addColumn("Text");
setModel(tableModel);
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setSelectionModel(new RListSelectionModel());
setRowSelectionAllowed(true);
setShowGrid(false);
TableColumnModel columnModel = getColumnModel();
columnModel.getColumn(0).setPreferredWidth(80);
columnModel.getColumn(1).setPreferredWidth(40);
columnModel.getColumn(2).setPreferredWidth(180);
matchDatas = new ArrayList();
}
/*****************************************************************************/
/**
* Adds data on a match to the table.
*
* @param matchData The data.
* @param dirName The "root directory" searching was done in. This is
* used so all file paths displayed in the table are abbreviated
* to be relative to this directory.
* @see #clear()
*/
public void addMatchData(MatchData matchData, String dirName) {
// Make the displayed filename be in a path relative to the
// directory typed into the Find in Files dialog.
int pos = 0;
String fileName = matchData.getFileName().toLowerCase();
dirName = dirName.toLowerCase();
int dirNameLength = dirName.length();
while (pos<dirNameLength &&
(fileName.charAt(pos)==dirName.charAt(pos) ||
isFileSeparatorChar(fileName.charAt(pos)))) {
pos++;
}
if (isFileSeparatorChar(fileName.charAt(pos)))
pos++;
fileName = matchData.getFileName().substring(pos);
// We create and pass a Vector since that's what DefaultTableModel
// uses internally anyway. This saves, say, creating an Object[]
// array to pass in.
Vector v = new Vector(3);
v.add(fileName);
v.add(matchData.getLineNumber());
v.add(matchData.getLineText());
tableModel.addRow(v);
matchDatas.add(matchData);
}
/*****************************************************************************/
/**
* Clears all match results from the table.
*
* @see #addMatchData
*/
public void clear() {
tableModel.setRowCount(0);
matchDatas.clear();
}
/*****************************************************************************/
/**
* Returns the preferred size of this table.
*
* @return The preferred size of this table.
*/
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(100, getRowHeight()*8);
}
/*****************************************************************************/
/**
* Overridden in a "hack" to ensure that the table's contents are
* always at least as large as the enclosing <code>JScrollPane</code>'s
* viewport.
*/
public boolean getScrollableTracksViewportWidth() {
Container parent = getParent();
if (parent instanceof JViewport) {
return parent.getSize().getWidth()>getPreferredSize().getWidth();
}
return super.getScrollableTracksViewportWidth();
}
/*****************************************************************************/
/**
* Returns the renderer to use for the given cell.
*
* @param row The row of the cell.
* @param column The column of the cell.
* @return The renderer.
*/
public TableCellRenderer getCellRenderer(int row, int column) {
if (getMatchDataForRow(row).isVerboseSearchInfo()) {
if (verboseCellRenderer==null)
verboseCellRenderer = new VerboseCellRenderer();
return verboseCellRenderer;
}
else {
return super.getCellRenderer(row, column);
}
}
/*****************************************************************************/
/**
* Returns the match data displayed in the specified row.
*
* @param row The row.
* @return The match data.
*/
public MatchData getMatchDataForRow(int row) {
return (MatchData)matchDatas.get(row);
}
/*****************************************************************************/
/**
* This method always returns false, as match data is immutable.
*
* @param row The row of the cell.
* @param column The column of the cell.
* @return <code>false</code> always.
*/
public boolean isCellEditable(int row, int column) {
return false;
}
/*****************************************************************************/
private static boolean isFileSeparatorChar(char ch) {
return ch=='\\' || ch=='/';
}
/*****************************************************************************/
/**
* Resizes the columns of the table to accomodate their data.
*/
public void refreshColumnWidths() {
TableColumnModel columnModel = getColumnModel();
int columnCount = getColumnCount();
int width;
int rowCount = getRowCount();
for (int j=0; j<columnCount; j++) {
// Initialize width of column to best width for its header.
TableColumn column = columnModel.getColumn(j);
//column.sizeWidthToFit();
//width = column.getWidth();
Component c = getTableHeader().getDefaultRenderer().
getTableCellRendererComponent(null,
column.getHeaderValue(), false, false, 0, 0);
width = c.getPreferredSize().width;
// Loop through all cells in the column to find the longest.
for (int i=0; i<rowCount; i++) {
TableCellRenderer renderer = getCellRenderer(i, j);
Component comp = renderer.getTableCellRendererComponent(
this, getValueAt(i, j), false, false,
i, j);
int w = comp.getPreferredSize().width;
if (w>width)
width = w;
}
// Set the size of the column.
// NOTE: Why do we need to add a small amount to prevent "..."?
column.setPreferredWidth(width + 20);
}
}
/*****************************************************************************/
/************************* INNER CLASSES *************************************/
/*****************************************************************************/
/**
* Renderer for "verbose information" cells.
*/
static class VerboseCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
if (!isSelected)
setForeground(Color.GRAY);
return this;
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -