From: Pavel V. <va...@us...> - 2002-08-16 13:09:29
|
Update of /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views In directory usw-pr-cvs1:/tmp/cvs-serv12913 Modified Files: TableClickSorter.java AllocTableClickSorter.java Log Message: sorting prepared Index: TableClickSorter.java =================================================================== RCS file: /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views/TableClickSorter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** TableClickSorter.java 8 Aug 2002 06:45:04 -0000 1.1 --- TableClickSorter.java 16 Aug 2002 13:09:27 -0000 1.2 *************** *** 51,57 **** */ public class TableClickSorter extends TableMap { ! private int indexes[] = new int[0]; private int usedCount = 0; //number uf used buckets in the indexes[] ! private ArrayList sortingColumns = new ArrayList(); /** Creates a new instance of TableClickSorter */ --- 51,58 ---- */ public class TableClickSorter extends TableMap { ! private int indexes[] = null; private int usedCount = 0; //number uf used buckets in the indexes[] ! private final LinkedList sortingColumns = new LinkedList(); ! private boolean ascendColumns[] = null; /** Creates a new instance of TableClickSorter */ *************** *** 59,62 **** --- 60,64 ---- super( model ); //adds listener, etc. initIndexes(); + initColumns(); } *************** *** 78,84 **** --- 80,91 ---- super.setModel(model); initIndexes(); + initColumns(); } public void tableChanged(TableModelEvent e) { + /* if (e.getFirstRow() == TableModelEvent.HEADER_ROW) { + initColumns(); + } + switch (e.getType()) { case TableModelEvent.INSERT: *************** *** 90,99 **** case TableModelEvent.UPDATE: //nothing special ! } super.tableChanged( e ); } ! // Add a mouse listener to the Table to trigger a table sort ! // when a column heading is clicked in the JTable. public void addMouseListenerToHeaderInTable(JTable table) { final TableClickSorter sorter = this; --- 97,107 ---- case TableModelEvent.UPDATE: //nothing special ! }*/ super.tableChanged( e ); } ! /** Add a mouse listener to the Table to trigger a table sort ! * when a column heading is clicked in the JTable. ! */ public void addMouseListenerToHeaderInTable(JTable table) { final TableClickSorter sorter = this; *************** *** 107,113 **** if (e.getClickCount() == 1 && column != -1) { int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; ! boolean ascending = (shiftPressed == 0); ! //PENDING ! //sorter.sortByColumn(column); } } --- 115,127 ---- if (e.getClickCount() == 1 && column != -1) { int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; ! synchronized (this) { ! if (shiftPressed != 0) { ! ascendColumns[column] = !ascendColumns[column]; ! } ! sorter.sortByColumn(column); ! } ! } ! else if(e.getClickCount() == 1 && column != -1) { ! initColumns(); } } *************** *** 120,132 **** //---------------- SORTING ! //comparator public int compare( Object v1, Object v2 ) { //PENDING return 0; } ! public void sortByColumn( int column, boolean ascending ) { ! //PENDING sort care about column ! super.tableChanged( new TableModelEvent(this) ); } --- 134,227 ---- //---------------- SORTING ! public synchronized void sort() { ! //PENDING ! super.tableChanged( new TableModelEvent(this) ); ! } ! ! /**Comparator interface, it compares rows in the table. ! */ public int compare( Object v1, Object v2 ) { + //PENDING return 0; } ! /** This method is used by compare() for comparsion of objects in the cells. ! * Override this to add comparsion for new class. ! */ ! public int compareObjs( Object o1, Object o2 ) { ! ! // 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; ! else if (o2 == null) ! return 1; ! ! ! if (o1 instanceof java.lang.Number && o2 instanceof java.lang.Number) { ! Number n1 = (Number) o1; ! double d1 = n1.doubleValue(); ! Number n2 = (Number) o2; ! double d2 = n2.doubleValue(); ! ! if (d1 < d2) ! return -1; ! else if (d1 > d2) ! return 1; ! else ! return 0; ! } ! else if (o1 instanceof java.lang.Boolean && o2 instanceof java.lang.Boolean) { ! 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; ! else ! return -1; ! } //default case: ! else if (o1 instanceof java.lang.Comparable && o2 instanceof java.lang.Comparable) { ! int result = ((Comparable) o1).compareTo( o2 ); ! if (result < 0) ! return -1; ! else if (result > 0) ! return 1; ! else ! return 0; ! } ! else { //lexicografic comparsion ! String s1 = o1.toString(); ! String s2 = o1.toString(); ! ! int result = s1.compareTo( s2 ); ! if (result < 0) ! return -1; ! else if (result > 0) ! return 1; ! else ! return 0; ! } ! } ! ! public void sortByColumn( int column ) { ! sortByColumn( column, ascendColumns[ column ] ); ! } ! ! public synchronized void sortByColumn( int column, boolean ascending ) { ! Integer col = new Integer( column ); ! //sortingColumns.clear(); ! ! if ( sortingColumns.contains( col ) ) { ! sortingColumns.remove( col ); ! } ! sortingColumns.addFirst( col ); ! ! sort(); } *************** *** 134,142 **** //---------------- handling events from TableModel listener private synchronized void initIndexes() { int i; usedCount = getRowCount(); ! indexes = new int[ (int) Math.ceil( usedCount * 1.1 ) ]; for (i=0; i<usedCount; i++ ) { indexes[i] = i; --- 229,247 ---- //---------------- handling events from TableModel listener + private synchronized void initColumns() { + int i; + + int colNum = getColumnCount(); + ascendColumns = new boolean[ colNum ]; + for (i=0; i<colNum; i++ ) { + ascendColumns[i] = true; + } + } + private synchronized void initIndexes() { int i; usedCount = getRowCount(); ! indexes = new int[ preferedArraySize( usedCount ) ]; for (i=0; i<usedCount; i++ ) { indexes[i] = i; *************** *** 147,151 **** int i; ! if (first < usedCount) { //repair broken mapping for (i=0; i<usedCount; i++ ) if (indexes[i] >= first ) { --- 252,256 ---- int i; ! /* if (first < usedCount) { //repair broken mapping for (i=0; i<usedCount; i++ ) if (indexes[i] >= first ) { *************** *** 159,163 **** } usedCount = last-first+1 + usedCount; ! } private synchronized void delRows( int first, int last ) { --- 264,268 ---- } usedCount = last-first+1 + usedCount; ! */ } private synchronized void delRows( int first, int last ) { *************** *** 174,186 **** condReallocateIndexes( usedCount ); } ! ! //PENDING deside whether allocate new array private synchronized void condReallocateIndexes( int newSize ) { int i; ! ! int indexes2[] = new int[ (int) Math.ceil(newSize * 1.1) ]; ! System.arraycopy( indexes, 0, indexes2, 0, usedCount ); ! indexes = indexes2; } } --- 279,296 ---- condReallocateIndexes( usedCount ); } ! private synchronized void condReallocateIndexes( int newSize ) { int i; ! int pref = preferedArraySize( newSize ); ! ! if ( pref > indexes.length || pref <= indexes.length * 0.9 ) { ! int indexes2[] = new int[ pref ]; ! System.arraycopy( indexes, 0, indexes2, 0, usedCount ); ! indexes = indexes2; ! } } + private synchronized int preferedArraySize( int size ) { + return (int) Math.ceil( Math.pow( 2, Math.log(size)/Math.log(2) )); + } } Index: AllocTableClickSorter.java =================================================================== RCS file: /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views/AllocTableClickSorter.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -r1.1 -r1.2 *** AllocTableClickSorter.java 8 Aug 2002 06:45:04 -0000 1.1 --- AllocTableClickSorter.java 16 Aug 2002 13:09:27 -0000 1.2 *************** *** 22,25 **** --- 22,27 ---- import javax.swing.table.TableModel; + + import net.sourceforge.javaprofiler.jpi.AllocStat; /** AllocTableClickSorter is TableClickSorter, in other words: It sorts TableModel for JTable * when you click on column header in JTable instance. *************** *** 39,45 **** } ! public int compare(Object v1, Object v2) { ! //PENDING ! return 0; } --- 41,64 ---- } ! public int compareObjs(Object o1, Object o2) { ! if (o1 == null && o2 == null) ! return 0; ! else if (o1 == null) // Define null less than everything. ! return -1; ! else if (o2 == null) ! return 1; ! ! if ( o1 instanceof AllocStat && o2 instanceof AllocStat ) { ! long d1 = ((AllocStat) o1).getLiveInstancesCount(); ! long d2 = ((AllocStat) o2).getLiveInstancesCount(); ! if (d1 < d2) ! return -1; ! else if (d1 > d2) ! return 1; ! else ! return 0; ! } ! ! return super.compareObjs( o1, o2 ); } |