sortingdatagridmodel.java

来自「一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码」· Java 代码 · 共 294 行

JAVA
294
字号
/*
 *    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
 *    (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Created on Mar 29, 2005 By TWang.
 * 
 * A TableModel that enables column sorting by clicking the column based on
 * Java's Collection class sort method against the TableModel's dataVector.
 * 
 * To use: need to first add listerner to the data model:
 * model.addMouseListenerToHeaderInTable(table);
 */
package eti.bi.alphaminer.operation.result.datamodel;

import java.awt.Component;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public class SortingDataGridModel extends DataGridModel implements Comparator {

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

	protected int currCol;

	protected Vector<Integer> ascendCol; // sorting (ascending or descending) state of
								// each column

	protected Integer ascendingYes = new Integer(1);

	protected Integer ascendingFalse = new Integer(-1);
	
	private static String TOOLTIPS = "Click to sort the column";

	private String MISSING = "NaN"; // MissingValue.DISPLAY_VALUE; //special value to be
												// considerred while sorting

	/**
	 * @param a_TableContent
	 * @param a_TableHeader
	 * @param a_Types
	 */
	public SortingDataGridModel(Object[][] a_TableContent,
			Object[] a_TableHeader, Class[] a_Types) {
		super(a_TableContent, a_TableHeader, a_Types);
		ascendCol = new Vector<Integer>();
		for (int i = 0; i < a_TableHeader.length; i++) {
			ascendCol.add(ascendingYes);
		}
	}

	public void addColumn(Object columnName, Vector columnData) {
		super.addColumn(columnName, columnData);
		ascendCol.add(ascendingYes);
	}

	public int compare(Object v1, Object v2) {

		int ascending = ascendCol.get(currCol).intValue();
		if (v1 == null && v2 == null) {
			return 0;
		} else if (v1 == null) { // Define null less than everything.
			return -1 * ascending;
		} else if (v2 == null) {
			return 1 * ascending;
		}

		Object o1 = ((Vector) v1).get(currCol);
		Object o2 = ((Vector) v2).get(currCol);

		Class type;
		if (o1 != null)
			type = o1.getClass();
		else if (o2 != null)
			type = o2.getClass();
		else
			type = java.lang.Object.class;

		// If both values are null, return 0.
		if (o1 == null && o2 == null) {
			return 0;
		} else if (o1 == null) { // Define null less than everything.
			return -1 * ascending;
		} else if (o2 == null) {
			return 1 * ascending;
		}

		// If both values are missing value, return 0.
		if (o1.toString().equals(MISSING)
				&& o2.toString().equals(MISSING)) {
			return 0;
		} else if (o1.toString().equals(MISSING)) { // Define null less
														 // than everything.
			return -1 * ascending;
		} else if (o2.toString().equals(MISSING)) {
			return 1 * ascending;
		}

		if (type.getSuperclass() == java.lang.Number.class) {
			Number n1 = (Number) o1;
			double d1 = n1.doubleValue();
			Number n2 = (Number) o2;
			double d2 = n2.doubleValue();

			if (d1 < d2) {
				return -1 * ascending;
			} else if (d1 > d2) {
				return 1 * ascending;
			} else {
				return 0;
			}
		} else if (type == java.util.Date.class) {
			Date d1 = (Date) o1;
			long n1 = d1.getTime();
			Date d2 = (Date) o2;
			long n2 = d2.getTime();

			if (n1 < n2) {
				return -1 * ascending;
			} else if (n1 > n2) {
				return 1 * ascending;
			} else {
				return 0;
			}
		} else if (type == String.class) {
			String s1 = (String) o1;
			String s2 = (String) o2;
			int result = s1.compareTo(s2);

			if (result < 0) {
				return -1 * ascending;
			} else if (result > 0) {
				return 1 * ascending;
			} else {
				return 0;
			}
		} else if (type == Boolean.class) {
			Boolean bool1 = (Boolean) o1;
			boolean b1 = bool1.booleanValue();
			Boolean bool2 = (Boolean) o2;
			boolean b2 = bool2.booleanValue();

			if (b1 == b2) {
				return 0;
			} else if (b1) { // Define false < true
				return 1 * ascending;
			} else {
				return -1 * ascending;
			}
		} else {
			String s1 = o1.toString();
			String s2 = o2.toString();
			int result = s2.compareTo(s1);

			if (result < 0) {
				return -1 * ascending;
			} else if (result > 0) {
				return 1 * ascending;
			} else {
				return 0;
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void sort() {
		Collections.sort(dataVector, this);
		Integer val = ascendCol.get(currCol);
		ascendCol.remove(currCol);
		if (val.equals(ascendingYes))
			ascendCol.add(currCol, ascendingFalse);
		else
			ascendCol.add(currCol, ascendingYes);
	}

	public void sortByColumn(int column) {
		this.currCol = column;
		sort();
		fireTableChanged(new TableModelEvent(this));
	}

	public void addMouseListenerToHeader(JTable table) {
		final SortingDataGridModel sorter = this;
		final JTable tableView = table;
		tableView.setColumnSelectionAllowed(false);
		MouseAdapter listMouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				TableColumnModel columnModel = tableView.getColumnModel();
				int viewColumn = columnModel.getColumnIndexAtX(e.getX());
				int column = tableView.convertColumnIndexToModel(viewColumn);
				if (e.getClickCount() == 1 && column != -1) { 
					sorter.sortByColumn(column);
				}
			}
		};
		JTableHeader th = tableView.getTableHeader();

		ButtonHeaderRenderer renderer = new ButtonHeaderRenderer();
		TableColumnModel model = table.getColumnModel();

		int n = model.getColumnCount();
		for (int i = 0; i < n; i++) {
			model.getColumn(i).setHeaderRenderer(renderer);
		}
		th.addMouseListener(new HeaderListener(th, renderer));
		th.addMouseListener(listMouseListener);
		th.setToolTipText(SortingDataGridModel.TOOLTIPS);
	}

	/**
	 * Header listener which listens to the mouse event, and set the render attributes.
	 * @author twang 
	 */
	class HeaderListener extends MouseAdapter {
		JTableHeader header; 
		ButtonHeaderRenderer renderer;

		HeaderListener(JTableHeader header, ButtonHeaderRenderer renderer) {
			this.header = header;
			this.renderer = renderer;
		}

		public void mousePressed(MouseEvent e) {
			int col = header.columnAtPoint(e.getPoint());
			renderer.setPressedColumn(col);
			header.repaint(); 
		}

		public void mouseReleased(MouseEvent e) {
			renderer.setPressedColumn(-1); // clear
		 	header.repaint(); //need to repaint again
		}
	}

	/**
	 * A Button like JTable header render.
	 * 
	 * @author twang 
	 */ 
	class ButtonHeaderRenderer extends JButton implements TableCellRenderer {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		int pushedColumn;

		public ButtonHeaderRenderer() {
			pushedColumn = -1;
			setMargin(new Insets(0, 0, 0, 0));
		}

		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			setText((value == null) ? "" : value.toString());
			boolean isPressed = (column == pushedColumn);
			getModel().setPressed(isPressed);
			getModel().setArmed(isPressed);
			return this;
		}

		public void setPressedColumn(int col) {
			pushedColumn = col;
		}
	}
}

⌨️ 快捷键说明

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