[Schmitzm-commits] r1756 - in trunk/schmitzm-core/src/main/java/de/schmitzm: lang swing swing/table temp
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Oct 12 14:52:01 CEST 2011
Author: mojays
Date: 2011-10-12 14:51:58 +0200 (Wed, 12 Oct 2011)
New Revision: 1756
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/NumberComparator.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DoubleRenderer.java
trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java
Log:
SortableJTable: Automatically handle numeric column sort
BaseTypeUtil: Treat super-class Number as "numeric"
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/NumberComparator.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/NumberComparator.java 2011-10-11 22:41:32 UTC (rev 1755)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/NumberComparator.java 2011-10-12 12:51:58 UTC (rev 1756)
@@ -0,0 +1,105 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * 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 Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ ******************************************************************************/
+package de.schmitzm.lang;
+
+import java.util.Comparator;
+
+/**
+ * {@code Comparator} um beliebige {@link Number}-Objekte ueber ihren {@code double}-Wert
+ * zu vergleichen. Der Vergleich wird nur auf eine bestimmte Anzahl an Nachkommastellen
+ * vorgenommen!!
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ * @version 1.0
+ */
+public class NumberComparator implements Comparator<Object> {
+
+ /** Standard-Instanz dieses {@code Comparator} um ganze Zahlen zu
+ * vergleichen. */
+ public static final NumberComparator INTEGER = new NumberComparator();
+ /** Standard-Instanz dieses {@code Comparator} um Dezimalzahlen auf
+ * zwei Nachkommastellen zu vergleichen. */
+ public static final NumberComparator DOUBLE2 = new NumberComparator(2);
+
+ private int roundFactor = 0;
+
+ /**
+ * Erzeugt einen neuen {@code Comparator}.
+ */
+ public NumberComparator() {
+ this(0);
+ }
+
+ /**
+ * Erzeugt einen neuen {@code Comparator}.
+ * @param digits Genauigkeit (Nachkommastellen) auf die Verglichen wird
+ */
+ public NumberComparator(int digits) {
+ this.roundFactor = (int)Math.pow(10, digits);
+ }
+
+ /**
+ * Fuehrt den Vergleich aus.
+ * @param obj1 ein Objekt
+ * @param obj2 ein anders Objekt
+ * @return ein negativer Wert fuer {@code obj1 < obj2}, 0 für {@code obj1 = obj2}
+ * oder ein positiver Wert fuer {@code obj1 > obj2}
+ */
+ @Override
+ public int compare(Object obj1, Object obj2) {
+ if ( obj1 == null && obj2 == null )
+ return 0;
+ if ( obj1 == null || !(obj1 instanceof Number) )
+ return Integer.MAX_VALUE;
+ if ( obj2 == null || !(obj2 instanceof Number) )
+ return Integer.MIN_VALUE;
+ return (int)Math.round( (((Number)obj1).doubleValue() - ((Number)obj2).doubleValue()) * roundFactor );
+ }
+
+ /**
+ * Liefert einen {@link Comparator}, der die umgekehrte Reihenfolge eines
+ * {@link Comparator} definiert. {@code null}-Werte sind trotzdem groesser
+ * als alles andere und werden somit auch in der invertierten Reihenfolge
+ * hinten angehaengt.
+ * @param comparator ein Comparator
+ */
+ public static <T> Comparator<T> invert(final Comparator<T> comparator) {
+ return new Comparator<T>() {
+ public int compare(T obj1, T obj2) {
+ if ( obj1 == null && obj2 == null )
+ return 0;
+ if ( obj1 == null )
+ return Integer.MAX_VALUE;
+ if ( obj2 == null )
+ return Integer.MIN_VALUE;
+ return comparator.compare(obj2,obj1);
+ }
+ };
+ }
+}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java 2011-10-11 22:41:32 UTC (rev 1755)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java 2011-10-12 12:51:58 UTC (rev 1756)
@@ -31,13 +31,18 @@
import java.util.Vector;
+import javax.swing.DefaultRowSorter;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
+import javax.swing.RowSorter;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.apache.log4j.Logger;
+import de.schmitzm.lang.NumberComparator;
+import de.schmitzm.temp.BaseTypeUtil;
+
/**
* Extends the {@link JTable} with automatic sort functionality.
* This class adds the following methods so that manually calling
@@ -123,6 +128,36 @@
}
/**
+ * Sets {@link NumberComparator} in the {@link RowSorter} for all
+ * numeric columns.
+ * Calls {@link #setAutoCreateRowSorter(boolean)} if no {@link RowSorter}
+ * is set yet.
+ * @param digits digits for decimal comparation
+ * @see NumberComparator
+ */
+ public void setAutoCreatedNumericRowSorter(int digits) {
+ // set Sorter for numeric columns
+ TableModel model = getModel();
+ DefaultRowSorter rowSorter = (DefaultRowSorter)getRowSorter();
+ NumberComparator decimalComparator = new NumberComparator(digits);
+ if ( rowSorter == null )
+ setAutoCreateRowSorter(true);
+ for (int i=0; i<getModel().getColumnCount(); i++) {
+ Class colClass = model.getColumnClass(i);
+ if ( !BaseTypeUtil.isNumeric(colClass) )
+ continue;
+ if ( BaseTypeUtil.isDecimal(colClass) )
+ rowSorter.setComparator(i, decimalComparator);
+ else
+ rowSorter.setComparator(i, NumberComparator.INTEGER);
+ }
+
+
+ }
+
+
+
+ /**
* Returns the index of the currently edited row according to
* the table model.
*/
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DoubleRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DoubleRenderer.java 2011-10-11 22:41:32 UTC (rev 1755)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DoubleRenderer.java 2011-10-12 12:51:58 UTC (rev 1756)
@@ -127,7 +127,7 @@
setText( "" );
setToolTipText( "<null>" );
} else {
- setText( cellFormatter != null ? cellFormatter.format(value) : value.toString() );
+ setText( cellFormatter != null && value instanceof Number ? cellFormatter.format(value) : value.toString() );
setToolTipText( tooltipFormatter != null ? tooltipFormatter.format(value) : value.toString() );
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java 2011-10-11 22:41:32 UTC (rev 1755)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/temp/BaseTypeUtil.java 2011-10-12 12:51:58 UTC (rev 1756)
@@ -338,8 +338,9 @@
* korrespondierende Klasse (z.B. <code>java.lang.Integer</code>) darstellt.
*/
public static boolean isNumeric(Class c) {
- return isByte(c) || isShort(c) || isInteger(c) || isLong(c)
- || isFloat(c) || isDouble(c);
+ return Number.class.isAssignableFrom(c) ||
+ isByte(c) || isShort(c) || isInteger(c) ||
+ isLong(c) || isFloat(c) || isDouble(c);
}
/**
More information about the Schmitzm-commits
mailing list