From fae3584cc1b72483aeceeebd2bb48f7c95443038 Mon Sep 17 00:00:00 2001 From: teo_sarca Date: Mon, 29 Jan 2007 08:49:37 +0000 Subject: [PATCH] bug fix [ 1585369 ] CTable sorting is TOO LAZY (+fixing some comments) --- looks/src/org/compiere/swing/CTable.java | 49 ++++++++++-------------- looks/src/org/compiere/util/MSort.java | 21 ++++++---- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/looks/src/org/compiere/swing/CTable.java b/looks/src/org/compiere/swing/CTable.java index abc1b09517..2f84fff44b 100644 --- a/looks/src/org/compiere/swing/CTable.java +++ b/looks/src/org/compiere/swing/CTable.java @@ -26,11 +26,16 @@ import javax.swing.table.*; import org.compiere.util.*; /** - * Model Independent enhanced JTable. - * Provides sizing and sorting - * - * @author Jorg Janke - * @version $Id: CTable.java,v 1.2 2006/07/30 00:52:24 jjanke Exp $ + * Model Independent enhanced JTable. + * Provides sizing and sorting. + *

+ * Change log: + *

+ * + * @author Jorg Janke + * @version $Id: CTable.java,v 1.2 2006/07/30 00:52:24 jjanke Exp $ */ public class CTable extends JTable { @@ -41,11 +46,7 @@ public class CTable extends JTable { super(new DefaultTableModel()); setColumnSelectionAllowed(false); - /* ARHIPAC: TEO: BEGIN: ORIGINAL: @arhipac.tools.annotation.Bug(228) -------------------------------------------- * setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - /* ARHIPAC: TEO: BEGIN: MODIFIED: @arhipac.tools.annotation.Bug(228) ------------------------------------------- */ - setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); - /* ARHIPAC: TEO: BEGIN: END: @arhipac.tools.annotation.Bug(228) --------------------------------------------------- */ setAutoResizeMode(JTable.AUTO_RESIZE_OFF); getTableHeader().addMouseListener(new CTableMouseListener()); setSurrendersFocusOnKeystroke(true); @@ -201,7 +202,7 @@ public class CTable extends JTable * @param modelColumnIndex model column sort index */ @SuppressWarnings("unchecked") - protected void sort (int modelColumnIndex) + protected void sort (final int modelColumnIndex) { int rows = getRowCount(); if (rows == 0) @@ -224,9 +225,9 @@ public class CTable extends JTable // Prepare sorting DefaultTableModel model = (DefaultTableModel)getModel(); - MSort sort = new MSort(0, null); + final MSort sort = new MSort(0, null); sort.setSortAsc(p_asc); - /* ARHIPAC: teo_sarca: BEGIN: ORIGINAL: @arhipac.tools.annotation.Bug(226) ----------------------------------- * + /* teo_sarca: commented: [ 1585369 ] CTable sorting is TOO LAZY * // while something to sort sorting: while (true) @@ -256,23 +257,15 @@ public class CTable extends JTable // log.config("done"); break; } // while something to sort - /* ARHIPAC: teo_sarca: MODIFIED: @arhipac.tools.annotation.Bug(226) -------------------------------------------- */ - // This comparator is used to sort vectors of data - class ColumnSorter implements Comparator { - int colIndex; - MSort sort; - ColumnSorter(int colIndex, MSort sort) { - this.colIndex = colIndex; - this.sort = sort; - } + */ + // teo_sarca: [ 1585369 ] CTable sorting is TOO LAZY + Collections.sort(model.getDataVector(), new Comparator() { public int compare(Object o1, Object o2) { - Object item1 = ((Vector)o1).get(this.colIndex); - Object item2 = ((Vector)o2).get(this.colIndex); - return this.sort.compare(item1, item2); + Object item1 = ((Vector)o1).get(modelColumnIndex); + Object item2 = ((Vector)o2).get(modelColumnIndex); + return sort.compare(item1, item2); } - }; - Collections.sort(model.getDataVector(), new ColumnSorter(modelColumnIndex, sort)); - /* ARHIPAC: teo_sarca: END: @arhipac.tools.annotation.Bug(226) ---------------------------------------------------- */ + }); // selection clearSelection(); @@ -283,7 +276,7 @@ public class CTable extends JTable if (selected.equals(getValueAt(r, selCol))) { setRowSelectionInterval(r,r); - scrollRectToVisible(getCellRect(r, modelColumnIndex, true)); // @arhipac.tools.annotation.Bug(226) + scrollRectToVisible(getCellRect(r, modelColumnIndex, true)); // teo_sarca: bug fix [ 1585369 ] break; } } diff --git a/looks/src/org/compiere/util/MSort.java b/looks/src/org/compiere/util/MSort.java index ac10a13b17..80b3e2843d 100644 --- a/looks/src/org/compiere/util/MSort.java +++ b/looks/src/org/compiere/util/MSort.java @@ -22,7 +22,14 @@ import java.sql.Timestamp; import java.util.Comparator; /** - * Class to Sort Data + * Class to Sort Data + *

+ * Change log: + *

    + *
  • 2007-01-27 - teo_sarca - [ 1585369 ] CTable sorting is TOO LAZY: + * Fixed {@link #compare(Object, Object)} method for Double comparing support, + * ascending/descending support, comparing strings ignoring case + *
* * @author Jorg Janke * @version $Id: MSort.java,v 1.3 2006/10/06 00:43:09 jjanke Exp $ @@ -70,7 +77,7 @@ public final class MSort implements Comparator, Serializable public int compare (Object o1, Object o2) { // Get Objects to compare - Object cmp1 = o1; // @arhipac.tools.annotation.Bug(226) + Object cmp1 = o1; if (o1 instanceof MSort) cmp1 = ((MSort)o1).data; if (cmp1 instanceof NamePair) @@ -87,10 +94,10 @@ public final class MSort implements Comparator, Serializable { if (cmp2 == null) return 0; - return -1 * m_multiplier; // @arhipac.tools.annotation.Bug(226) + return -1 * m_multiplier; } if (cmp2 == null) - return 1 * m_multiplier; // @arhipac.tools.annotation.Bug(226) + return 1 * m_multiplier; /** * compare different data types @@ -100,7 +107,7 @@ public final class MSort implements Comparator, Serializable if (cmp1 instanceof String && cmp2 instanceof String) { String s = (String)cmp1; - return s.compareToIgnoreCase((String)cmp2) * m_multiplier; // @arhipac.tools.annotation.Bug(226) + return s.compareToIgnoreCase((String)cmp2) * m_multiplier; } // Date else if (cmp1 instanceof Timestamp && cmp2 instanceof Timestamp) @@ -120,18 +127,16 @@ public final class MSort implements Comparator, Serializable Integer d = (Integer)cmp1; return d.compareTo((Integer)cmp2) * m_multiplier; } - /* ARHIPAC: TEO: BEGIN: @arhipac.tools.annotation.Bug(226) ------------------------------ */ // Double else if (cmp1 instanceof Double && cmp2 instanceof Double) { Double d = (Double)cmp1; return d.compareTo((Double)cmp2) * m_multiplier; } - /* ARHIPAC: TEO: END: @arhipac.tools.annotation.Bug(226) -------------------------------- */ // Convert to string value String s = cmp1.toString(); - return s.compareToIgnoreCase(cmp2.toString()) * m_multiplier; // @arhipac.tools.annotation.Bug(226) + return s.compareToIgnoreCase(cmp2.toString()) * m_multiplier; } // compare /**