⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileexplorertablemodel.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 07/12/2004
 *
 * FileExplorerTableModel.java - A table model that simulates the funcationality
 *       found in the table used in Windows' "details view" in Windows Explorer.
 * 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;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.plaf.UIResource;
import javax.swing.table.*;


/**
 * A table model that simulates the functionality found in the table used in
 * Windows' "details view" in Windows explorer.  This is a WIP, and currently is
 * pretty much just a copy of <code>FileExplorerTableModel.java</code> found in
 * java.sun.com's tutorial section.<p>
 * This model currently allows the user to sort by column, and colors the cells
 * of elements in sorted-by columns slightly darker than normal, to signify that
 * the table is sorted by that row.  Future enhancements include a right-click
 * popup menu for the table header that allows you to add or remove columns.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class FileExplorerTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2257612335282974812L;

	protected TableModel tableModel;

	public static final int DESCENDING = -1;
	public static final int NOT_SORTED = 0;
	public static final int ASCENDING = 1;

	private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);

	private static final int DARK_AMOUNT	= 15;	// How much darker columns get when their table is sorted by them.

	/**
	 * Compares two comparable objects by their <code>compareTo</code> method.
	 */
	public static final Comparator COMPARABLE_COMPARATOR = new Comparator() {
		public int compare(Object o1, Object o2) {
			return ((Comparable) o1).compareTo(o2);
		}
	};



	/**
	 * Compares two objects by their string (<code>toString</code>) values.
	 */
	public static final Comparator LEXICAL_COMPARATOR = new Comparator() {
		public int compare(Object o1, Object o2) {
			return o1.toString().compareTo(o2.toString());
		}
	};


	private Row[] viewToModel;
	private int[] modelToView;

	private JTableHeader tableHeader;
	private MouseListener mouseListener;
	private TableModelListener tableModelListener;
	private Map columnComparators = new HashMap();
	private List sortingColumns = new ArrayList();

	private JTable table;

//	private JPopupMenu rightClickMenu;


/*****************************************************************************/


	/**
	 * Constructor.
	 */
	public FileExplorerTableModel() {
		this.mouseListener = new MouseHandler();
		this.tableModelListener = new TableModelHandler();
	}


/*****************************************************************************/


	/**
	 * Constructor.
	 *
	 * @param tableModel ???
	 */
	public FileExplorerTableModel(TableModel tableModel) {
		this();
		setTableModel(tableModel);
	}


/*****************************************************************************/


	/**
	 * Constructor.
	 *
	 * @param tableModel ???
	 * @param tableHeader ???
	 */
	public FileExplorerTableModel(TableModel tableModel, JTableHeader tableHeader) {
		this();
		setTableHeader(tableHeader);
		setTableModel(tableModel);
	}


/*****************************************************************************/


	private void cancelSorting() {
		sortingColumns.clear();
		sortingStatusChanged();
	}


/*****************************************************************************/


	/**
	 * Creates the right-click menu that allows the addition/removal of
	 * columns in the table.
	 */
/*	private void createRightClickMenu() {

		rightClickMenu = new JPopupMenu();

		TableModel model = table.getModel();
		int numColumns = table.getColumnCount();
		for (int i=0; i<numColumns; i++) {

			final int j = i;
			String columnName = model.getColumnName(j);
			JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(new AbstractAction(columnName) {
									public void actionPerformed(ActionEvent e) {
										System.err.println(j);
									}
									});
			rightClickMenu.add(menuItem);

		}


	}
*/

/*****************************************************************************/


	private void clearSortingState() {
		viewToModel = null;
		modelToView = null;
	}


/*****************************************************************************/


	protected Comparator getComparator(int column) {
		Class columnType = tableModel.getColumnClass(column);
		Comparator comparator = (Comparator) columnComparators.get(columnType);
		if (comparator != null)
			return comparator;
		if (Comparable.class.isAssignableFrom(columnType))
			return COMPARABLE_COMPARATOR;
		return LEXICAL_COMPARATOR;
	}


/*****************************************************************************/


	private Directive getDirective(int column) {
		int size = sortingColumns.size();
		for (int i=0; i<size; i++) {
			Directive directive = (Directive)sortingColumns.get(i);
			if (directive.column == column)
				return directive;
		}
		return EMPTY_DIRECTIVE;
	}


/*****************************************************************************/


	protected Icon getHeaderRendererIcon(int column, int size) {
		Directive directive = getDirective(column);
		if (directive == EMPTY_DIRECTIVE)
			return null;
		return new Arrow(directive.direction == DESCENDING, size, sortingColumns.indexOf(directive));
	}


/*****************************************************************************/


	private int[] getModelToView() {
		if (modelToView == null) {
			int n = getViewToModel().length;
			modelToView = new int[n];
			for (int i = 0; i < n; i++) {
				modelToView[modelIndex(i)] = i;
			}
		}
		return modelToView;
	}


/*****************************************************************************/


	public int getSortingStatus(int column) {
		return getDirective(column).direction;
	}


/*****************************************************************************/


	/**
	 * Returns the table header being used.
	 *
	 * @return The table header being used.
	 */
	public JTableHeader getTableHeader() {
		return tableHeader;
	}


/*****************************************************************************/


	/**
	 * Returns the table model being used.
	 *
	 * @return The table model being used.
	 * @see #setTableModel
	 */
	public TableModel getTableModel() {
		return tableModel;
	}


/*****************************************************************************/


	private Row[] getViewToModel() {
		if (viewToModel == null) {
			int tableModelRowCount = tableModel.getRowCount();
			viewToModel = new Row[tableModelRowCount];
			for (int row = 0; row < tableModelRowCount; row++) {
				viewToModel[row] = new Row(row);
			}

			if (isSorting()) {
				Arrays.sort(viewToModel);
			}
		}
		return viewToModel;
	}


/*****************************************************************************/


	public boolean isSorting() {
		return sortingColumns.size() != 0;
	}


/*****************************************************************************/


	public int modelIndex(int viewIndex) {
		return getViewToModel()[viewIndex].modelIndex;
	}


/*****************************************************************************/


	public void setColumnComparator(Class type, Comparator comparator) {
		if (comparator == null)
			columnComparators.remove(type);
		else
			columnComparators.put(type, comparator);
	}


/*****************************************************************************/


	public void setSortingStatus(int column, int status) {

		Directive directive = getDirective(column);

		if (directive != EMPTY_DIRECTIVE)
			sortingColumns.remove(directive);

		if (status != NOT_SORTED)
			sortingColumns.add(new Directive(column, status));

		// Set the color for the columns (darker if sorted).
		if (table!=null) {
			TableColumnModel columnModel = table.getColumnModel();
			Color tableBGColor = table.getBackground();
			Color sortedColor = null;
			int columnCount = table.getColumnCount();
			for (int i=0; i<columnCount; i++) {
				TableColumn col = columnModel.getColumn(table.convertColumnIndexToView(i));
				TableCellRenderer renderer = col.getCellRenderer();
				if (renderer!=null && renderer instanceof Component) {
					Component c = (Component)renderer;
					directive = getDirective(i);
					if (directive.column!=-1 && directive.direction!=NOT_SORTED) {
						if (sortedColor==null) {
							sortedColor = new Color(Math.max(tableBGColor.getRed()-DARK_AMOUNT,   0),
											Math.max(tableBGColor.getGreen()-DARK_AMOUNT, 0),
											Math.max(tableBGColor.getBlue()-DARK_AMOUNT,  0));
						}
						c.setBackground(sortedColor);
					}
					else {
						c.setBackground(tableBGColor);
					}
				}
			}
		}

		sortingStatusChanged();

	}


/*****************************************************************************/


	/**
	 * Sets the table this sorter is the model for.  By setting this to a
	 * non-<code>null</code> value, any columns by which the table is sorted
	 * will be colored with a slightly-darker background (similar to how it's
	 * done in Windows XP).
	 *
	 * @param table The table for which this model is the model.
	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -