From: Pavel V. <va...@us...> - 2002-08-08 06:45:10
|
Update of /cvsroot/javaprofiler/module/net/sourceforge/javaprofiler/module/views In directory usw-pr-cvs1:/tmp/cvs-serv1728 Added Files: TableMap.java TableClickSorter.java AllocTableClickSorter.java Log Message: models with sorting for JTable --- NEW FILE: TableMap.java --- /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version * 1.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is available at http://www.sun.com/ * * The Original Code is the Java Profiler module. * The Initial Developers of the Original Code are Jan Stola, Pavel Vacha, * Michal Pise, Petr Luner, Lukas Petru and Marek Przeczek. * Portions created by Jan Stola are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Pavel Vacha are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Michal Pise are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Petr Luner are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Lukas Petru are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Marek Przeczek are Copyright (C) 2000-2001. All Rights Reserved. * * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. */ package net.sourceforge.javaprofiler.module.views; import javax.swing.table.TableModel; import javax.swing.table.DefaultTableModel; import javax.swing.event.TableModelListener; import javax.swing.event.TableModelEvent; /**TableMap is TableModel that forwards all method calls to its model. * Events from the model forwards to its listeners. It designed as super class for * your map models with other funkcionality e.g. sorting. * * @author Pavel Vacha */ public class TableMap extends DefaultTableModel implements TableModelListener { //implementation issue protected TableModel model; /** Creates a new instance of TableMap */ public TableMap( TableModel model ) { this.model = model; this.model.addTableModelListener( this ); } public TableModel getModel() { return model; } public void setModel( TableModel model ) { this.model.removeTableModelListener( this ); this.model = model; this.model.addTableModelListener( this ); } //TableModel interface - forwarding public Object getValueAt(int row, int column) { return model.getValueAt( row, column ); } public void setValueAt(Object aValue, int row, int column) { model.setValueAt( aValue, row, column ); } public String getColumnName(int columnIndex) { return model.getColumnName(columnIndex); } public Class getColumnClass(int columnIndex) { return model.getColumnClass( columnIndex ); } public int getColumnCount() { return model.getColumnCount(); } public int getRowCount() { return model.getRowCount(); } public boolean isCellEditable(int rowIndex, int columnIndex) { return model.isCellEditable( rowIndex, columnIndex ); } //methods for adding/removing listeners inherited from DefaultTableModel //TableModelListener interface public void tableChanged(TableModelEvent e) { fireTableChanged(e); //forward event to our listeners } } --- NEW FILE: TableClickSorter.java --- /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version * 1.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is available at http://www.sun.com/ * * The Original Code is the Java Profiler module. * The Initial Developers of the Original Code are Jan Stola, Pavel Vacha, * Michal Pise, Petr Luner, Lukas Petru and Marek Przeczek. * Portions created by Jan Stola are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Pavel Vacha are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Michal Pise are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Petr Luner are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Lukas Petru are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Marek Przeczek are Copyright (C) 2000-2001. All Rights Reserved. * * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. */ package net.sourceforge.javaprofiler.module.views; import net.sourceforge.javaprofiler.module.views.TableMap; import javax.swing.JTable; import javax.swing.table.*; import javax.swing.event.TableModelEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.InputEvent; import java.util.*; /**TableClickSorter is TableMap model that provides sorting capabalities. * TableClickSorter doesn't keep model data, it stores only array of indexes and refernce to the model. * This array is reordered during sorting. Request made through getVauleAt(row, col) * are redirected to model via mapping array. * Sorting take place only when user clicks on column; newly added rows goes to * the bottom of the table. * !! You have to register listner on the JTable with * <code> addMouseListenerToHeaderInTable(JTable table) </code> method * * Usage : * * TableModel model1 = new AllocHistogramTableModel(); //or create any another TableModel that you wish * TableClickSorter model = new TableClickSorter(model1); * JTable table = new JTable(model); * model.addMouseListenerToHeaderInTable(table); * * @author Pavel Vacha */ 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 */ public TableClickSorter( TableModel model ) { super( model ); //adds listener, etc. initIndexes(); } public Object getValueAt(int row, int column) { return model.getValueAt(indexes[row], column); } public boolean isCellEditable(int row, int column) { return model.isCellEditable( indexes[row], column ); } public void setValueAt(Object value, int row, int column) { model.setValueAt( value, row, column ); } /** Attention when use this, you need care about synchronization. */ public void setModel(TableModel model) { super.setModel(model); initIndexes(); } public void tableChanged(TableModelEvent e) { switch (e.getType()) { case TableModelEvent.INSERT: addRows( e.getFirstRow(), e.getLastRow() ); break; case TableModelEvent.DELETE: delRows( e.getFirstRow(), e.getLastRow() ); break; 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; 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) { int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; boolean ascending = (shiftPressed == 0); //PENDING //sorter.sortByColumn(column); } } }; JTableHeader th = tableView.getTableHeader(); th.addMouseListener(listMouseListener); } //---------------- 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) ); } //---------------- 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; } } private synchronized void addRows( int first, int last ) { int i; if (first < usedCount) { //repair broken mapping for (i=0; i<usedCount; i++ ) if (indexes[i] >= first ) { indexes[i] += last-first+1; //number of added items } } //add new items condReallocateIndexes( last-first+1 + usedCount ); for ( i=usedCount; i<last-first+1 + usedCount; i++ ) { indexes[i] = first + (usedCount-i); } usedCount = last-first+1 + usedCount; } private synchronized void delRows( int first, int last ) { int i; for (i=0; i<usedCount; i++ ) if (indexes[i] >= first ) { indexes[i] -= last-first+1; //number of deleted items } System.arraycopy( indexes, first, indexes, last+1, usedCount-(last-first+1) ); for( i=usedCount-(last-first+1); i<usedCount; i++ ) indexes[i] = 0; usedCount = usedCount-(last-first+1); 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; } } --- NEW FILE: AllocTableClickSorter.java --- /* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version * 1.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is available at http://www.sun.com/ * * The Original Code is the Java Profiler module. * The Initial Developers of the Original Code are Jan Stola, Pavel Vacha, * Michal Pise, Petr Luner, Lukas Petru and Marek Przeczek. * Portions created by Jan Stola are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Pavel Vacha are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Michal Pise are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Petr Luner are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Lukas Petru are Copyright (C) 2000-2001. All Rights Reserved. * Portions created by Marek Przeczek are Copyright (C) 2000-2001. All Rights Reserved. * * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner, * Lukas Petru and Marek Przeczek. */ package net.sourceforge.javaprofiler.module.views; import javax.swing.table.TableModel; /** AllocTableClickSorter is TableClickSorter, in other words: It sorts TableModel for JTable * when you click on column header in JTable instance. * (Actually it sorts only indexes mapping to TableModel). The differnce is that * AllocTableClickSorter can sort AllocStat objects therefore you have to you use this with * AllocHistogramTable and AllocHistogramModel. * * * @author Pavel Vacha * @see TableClickSorter */ public class AllocTableClickSorter extends TableClickSorter { /** Creates a new instance of AllocTableClickSorter */ public AllocTableClickSorter( TableModel model ) { super( model ); } public int compare(Object v1, Object v2) { //PENDING return 0; } } |