All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fife.ui.search.FindInFilesTable Maven / Gradle / Ivy

Go to download

RText is a powerful, cross-platform programmer's text editor written in Java. It is designed to be easy to use, highly customizable and flexible. Part of RText's design is for the source code to be simple, easy to understand, and well documented, so that other programmers can look into its inner-workings and figure out how RText ticks with ease. A good place to start (besides the source code) is the Javadoc for all classes used in the project.

There is a newer version: 2.0.7
Show newest version
/*
 * 10/03/2005
 *
 * FindInFilesTable.java - A table listing search results in a Find in Files
 * dialog.
 * Copyright (C) 2005 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.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.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.ResourceBundle;
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.ui.FileExplorerTableModel;
import org.fife.ui.RListSelectionModel;


/**
 * The table used to display search results in a
 * FindInFilesDialog.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class FindInFilesTable extends JTable implements ResultsComponent {

	private FileExplorerTableModel sorter;
	private DefaultTableModel tableModel;
	private ArrayList matchDatas;

	private TableCellRenderer verboseCellRenderer;

	private static final String MSG = "org.fife.ui.search.FindInFilesTable";


	/**
	 * Constructor.
	 */
	public FindInFilesTable() {

		ResourceBundle msg = ResourceBundle.getBundle(MSG);

		// Create the table model, and make it sortable.
		// Keep a pointer to the "real" table model since it's a
		// DefaultTableModel and is easy to modify.
		tableModel = createTableModel(msg);
		sorter = new FileExplorerTableModel(tableModel);
		setModel(sorter);
		sorter.setTable(this);

		setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		setSelectionModel(new RListSelectionModel());
		setRowSelectionAllowed(true);
		setShowGrid(false);

		initColumnWidths();

		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 (posJScrollPane's
	 * viewport.
	 */
	public boolean getScrollableTracksViewportWidth() {
 		Container parent = getParent();
		if (parent instanceof JViewport) {
			return parent.getSize().getWidth()>getPreferredSize().getWidth();
		}
		return super.getScrollableTracksViewportWidth();
	}


	/**
	 * Initializes the column widths.
	 */
	protected void initColumnWidths() {
		TableColumnModel columnModel = getColumnModel();
		columnModel.getColumn(0).setPreferredWidth(80);
		columnModel.getColumn(1).setPreferredWidth(40);
		columnModel.getColumn(2).setPreferredWidth(180);
	}


	/**
	 * 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 false always.
	 */
	public boolean isCellEditable(int row, int column) {
		return false;
	}


	private static final boolean isFileSeparatorChar(char ch) {
		return ch=='\\' || ch=='/';
	}


	/**
	 * Allows the results component to update its appearance after
	 * having lots of data added to it.
	 */
	public void prettyUp() {
		refreshColumnWidths();
		revalidate();
	}


	/**
	 * Resizes the columns of the table to accomodate their data.
	 */
	private void refreshColumnWidths() {

		TableColumnModel columnModel = getColumnModel();
		int columnCount = getColumnCount();
		int width;
		int rowCount = getRowCount();

		for (int j=0; jwidth)
					width = w;
			}

			// Set the size of the column.
			// NOTE: Why do we need to add a small amount to prevent "..."?
			column.setPreferredWidth(width + 20);

		}

	}


	/**
	 * Renderer for "verbose information" and "error" cells.
	 */
	private 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) {
				MatchData data = getMatchDataForRow(row);
				if (data.isVerboseSearchInfo()) {
					setForeground(Color.GRAY);
				}
				else if (data.isError()) {
					setForeground(Color.RED);
				}
			}
			return this;
		}

	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy