bug fix [ 1585369 ] CTable sorting is TOO LAZY

(+fixing some comments)
This commit is contained in:
teo_sarca 2007-01-29 08:49:37 +00:00
parent 7dd2dce38b
commit fae3584cc1
2 changed files with 34 additions and 36 deletions

View File

@ -26,11 +26,16 @@ import javax.swing.table.*;
import org.compiere.util.*; import org.compiere.util.*;
/** /**
* Model Independent enhanced JTable. * Model Independent enhanced JTable.
* Provides sizing and sorting * Provides sizing and sorting.
* * <p>
* @author Jorg Janke * Change log:
* @version $Id: CTable.java,v 1.2 2006/07/30 00:52:24 jjanke Exp $ * <ul>
* <li>2007-01-27 - teo_sarca - [ 1585369 ] CTable sorting is TOO LAZY
* </ul>
*
* @author Jorg Janke
* @version $Id: CTable.java,v 1.2 2006/07/30 00:52:24 jjanke Exp $
*/ */
public class CTable extends JTable public class CTable extends JTable
{ {
@ -41,11 +46,7 @@ public class CTable extends JTable
{ {
super(new DefaultTableModel()); super(new DefaultTableModel());
setColumnSelectionAllowed(false); setColumnSelectionAllowed(false);
/* ARHIPAC: TEO: BEGIN: ORIGINAL: @arhipac.tools.annotation.Bug(228) -------------------------------------------- *
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 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); setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getTableHeader().addMouseListener(new CTableMouseListener()); getTableHeader().addMouseListener(new CTableMouseListener());
setSurrendersFocusOnKeystroke(true); setSurrendersFocusOnKeystroke(true);
@ -201,7 +202,7 @@ public class CTable extends JTable
* @param modelColumnIndex model column sort index * @param modelColumnIndex model column sort index
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected void sort (int modelColumnIndex) protected void sort (final int modelColumnIndex)
{ {
int rows = getRowCount(); int rows = getRowCount();
if (rows == 0) if (rows == 0)
@ -224,9 +225,9 @@ public class CTable extends JTable
// Prepare sorting // Prepare sorting
DefaultTableModel model = (DefaultTableModel)getModel(); DefaultTableModel model = (DefaultTableModel)getModel();
MSort sort = new MSort(0, null); final MSort sort = new MSort(0, null);
sort.setSortAsc(p_asc); 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 // while something to sort
sorting: sorting:
while (true) while (true)
@ -256,23 +257,15 @@ public class CTable extends JTable
// log.config("done"); // log.config("done");
break; break;
} // while something to sort } // while something to sort
/* ARHIPAC: teo_sarca: MODIFIED: @arhipac.tools.annotation.Bug(226) -------------------------------------------- */ */
// This comparator is used to sort vectors of data // teo_sarca: [ 1585369 ] CTable sorting is TOO LAZY
class ColumnSorter<T> implements Comparator<T> { Collections.sort(model.getDataVector(), new Comparator<Object>() {
int colIndex;
MSort sort;
ColumnSorter(int colIndex, MSort sort) {
this.colIndex = colIndex;
this.sort = sort;
}
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
Object item1 = ((Vector)o1).get(this.colIndex); Object item1 = ((Vector)o1).get(modelColumnIndex);
Object item2 = ((Vector)o2).get(this.colIndex); Object item2 = ((Vector)o2).get(modelColumnIndex);
return this.sort.compare(item1, item2); return sort.compare(item1, item2);
} }
}; });
Collections.sort(model.getDataVector(), new ColumnSorter<Object>(modelColumnIndex, sort));
/* ARHIPAC: teo_sarca: END: @arhipac.tools.annotation.Bug(226) ---------------------------------------------------- */
// selection // selection
clearSelection(); clearSelection();
@ -283,7 +276,7 @@ public class CTable extends JTable
if (selected.equals(getValueAt(r, selCol))) if (selected.equals(getValueAt(r, selCol)))
{ {
setRowSelectionInterval(r,r); setRowSelectionInterval(r,r);
scrollRectToVisible(getCellRect(r, modelColumnIndex, true)); // @arhipac.tools.annotation.Bug(226) scrollRectToVisible(getCellRect(r, modelColumnIndex, true)); // teo_sarca: bug fix [ 1585369 ]
break; break;
} }
} }

View File

@ -22,7 +22,14 @@ import java.sql.Timestamp;
import java.util.Comparator; import java.util.Comparator;
/** /**
* Class to Sort Data * Class to Sort Data
* <p>
* Change log:
* <ul>
* <li>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
* </ul>
* *
* @author Jorg Janke * @author Jorg Janke
* @version $Id: MSort.java,v 1.3 2006/10/06 00:43:09 jjanke Exp $ * @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) public int compare (Object o1, Object o2)
{ {
// Get Objects to compare // Get Objects to compare
Object cmp1 = o1; // @arhipac.tools.annotation.Bug(226) Object cmp1 = o1;
if (o1 instanceof MSort) if (o1 instanceof MSort)
cmp1 = ((MSort)o1).data; cmp1 = ((MSort)o1).data;
if (cmp1 instanceof NamePair) if (cmp1 instanceof NamePair)
@ -87,10 +94,10 @@ public final class MSort implements Comparator, Serializable
{ {
if (cmp2 == null) if (cmp2 == null)
return 0; return 0;
return -1 * m_multiplier; // @arhipac.tools.annotation.Bug(226) return -1 * m_multiplier;
} }
if (cmp2 == null) if (cmp2 == null)
return 1 * m_multiplier; // @arhipac.tools.annotation.Bug(226) return 1 * m_multiplier;
/** /**
* compare different data types * compare different data types
@ -100,7 +107,7 @@ public final class MSort implements Comparator, Serializable
if (cmp1 instanceof String && cmp2 instanceof String) if (cmp1 instanceof String && cmp2 instanceof String)
{ {
String s = (String)cmp1; String s = (String)cmp1;
return s.compareToIgnoreCase((String)cmp2) * m_multiplier; // @arhipac.tools.annotation.Bug(226) return s.compareToIgnoreCase((String)cmp2) * m_multiplier;
} }
// Date // Date
else if (cmp1 instanceof Timestamp && cmp2 instanceof Timestamp) else if (cmp1 instanceof Timestamp && cmp2 instanceof Timestamp)
@ -120,18 +127,16 @@ public final class MSort implements Comparator, Serializable
Integer d = (Integer)cmp1; Integer d = (Integer)cmp1;
return d.compareTo((Integer)cmp2) * m_multiplier; return d.compareTo((Integer)cmp2) * m_multiplier;
} }
/* ARHIPAC: TEO: BEGIN: @arhipac.tools.annotation.Bug(226) ------------------------------ */
// Double // Double
else if (cmp1 instanceof Double && cmp2 instanceof Double) else if (cmp1 instanceof Double && cmp2 instanceof Double)
{ {
Double d = (Double)cmp1; Double d = (Double)cmp1;
return d.compareTo((Double)cmp2) * m_multiplier; return d.compareTo((Double)cmp2) * m_multiplier;
} }
/* ARHIPAC: TEO: END: @arhipac.tools.annotation.Bug(226) -------------------------------- */
// Convert to string value // Convert to string value
String s = cmp1.toString(); String s = cmp1.toString();
return s.compareToIgnoreCase(cmp2.toString()) * m_multiplier; // @arhipac.tools.annotation.Bug(226) return s.compareToIgnoreCase(cmp2.toString()) * m_multiplier;
} // compare } // compare
/** /**