Xoetrope

 Wiki : XTable4

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
Oldest known version of this page was edited on 2008-02-18 11:21:02 by LuanO []
Page view:
XUI 4.0

XTable Changes


In XUI 4.0 the XTable class is given a major enhancement through the merging of the XBaseTable functionality from XuiPro. The functionality is not a complete match as some methods and features have been removed in the process. Most significantly the data binding and table rendering have been updated as some of the features required by the new data bindings were difficult or awkward to implement with the previous setup.

New Features


Table Sorting
Table sorting is available through the JTable infrastructure in the later JDKs and can be added to an XTable instance using the following code:

	TableColumnModel mainTCM = mainTable.getColumnModel();
	TableRowSorter rowSorter = (TableRowSorter)mainTable.getRowSorter();
	XTableNumberSorter numberSorter = new XTableNumberSorter();
	rowSorter.addRowSorterListener( this );
	int numCols = mainTCM.getColumnCount();
	for ( int i = 0; i  < numCols; i++ ) {
	  // Java 6!
	  if ( Number.class.isAssignableFrom( tableModel.getColumnClass( i )))
	    rowSorter.setStringConverter( numberSorter );
	}


where the XTableNumberSorter is implemented as follows:

package com.xoetrope.swing.table;

import java.util.Comparator;
import javax.swing.table.TableModel;
import javax.swing.table.TableStringConverter;

/**
 * A sorter for columns of number values
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * @author luano
 */
public class XTableNumberSorter extends TableStringConverter implements Comparator
{
  public int compare( Object o1, Object o2 )
  {
	if (( o1 == null ) && ( o2 == null )) 
	  return 0;    
	else if (( o1 == null ) && ( o2 != null )) 
	  return -1;
	else if (( o1 != null ) && ( o2 == null )) 
	  return 1;
	
	if ( o1 instanceof Long )       
	  return ((Long)o1).compareTo( (Long)o2 );    
	else if ( o1 instanceof Integer ) 
	  return ((Integer)o1).compareTo( (Integer)o2 );
	else if ( o1 instanceof Double ) 
	  return ((Double)o1).compareTo( (Double)o2 );
	else if ( o1 instanceof Float ) 
	  return ((Float)o1).compareTo( (Float)o2 );
	
	return -1;
  }

  public String toString( TableModel model, int row, int column )
  {
	Object o1 = model.getValueAt(row, column);
	if ( o1 instanceof Long )       
	  return ((Long)o1).toString();    
	else if ( o1 instanceof Integer ) 
	  return ((Integer)o1).toString();    
	else if ( o1 instanceof Double ) 
	  return ((Double)o1).toString();    
	else if ( o1 instanceof Float ) 
	  return ((Float)o1).toString();    
	
	return o1.toString();
  }
}


This class is provided as otherwise exceptions are thrown if the sorter encounters number objects. The class requries java 6 and is therefore not yet included in the XUI SVN repository.

Table Badges
To add a badge to the table header to indicate the sort order add an entry to the startup.properties file

TableBadgePainter=com.xoetrope.swing.table.XSortingBadgePainter


The default table header renderer for XTable will then create an instance of the badge painter. The painter should implement the new XTableBadgePainter interface. A badge for showing the table sort order can be implemented as follows:

package com.xoetrope.swing.table;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import net.xoetrope.swing.table.XTableBadgePainter;
import net.xoetrope.swing.table.XTableHeaderRenderer;

/**
 * A painter for the sorting badge of a table.
 * <p> Copyright (c) Xoetrope Ltd., 2001-2008, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * @author luano
 */
public class XSortingBadgePainter implements XTableBadgePainter
{
  public void paint( Graphics2D g, JTable table, XTableHeaderRenderer renderer, int column, int width, int height )
  {
	SortOrder sortOrder = SortOrder.UNSORTED;
	
	RowSorter sorter = table.getRowSorter();
	if ( sorter != null ) {
	  List list = sorter.getSortKeys();
	  if ( list.size() > 0 ) {
	    int columnOffset = 0;
	    Object o = table.getClientProperty( "offset" );
	    if ( o != null )
	      columnOffset = ((Integer)o).intValue();
	    
	    RowSorter.SortKey sk = (RowSorter.SortKey)list.get( 0 );
	    if ( sk.getColumn() == column + columnOffset ) {
	      sortOrder = sk.getSortOrder();
	    }
	  }
	}
	
	Rectangle rect = renderer.getBounds();
	
	int inc = 0;
	int x = rect.width -8;
	int y = rect.y + rect.height / 2;
	if ( sortOrder == SortOrder.ASCENDING ) 
	  inc = 2;
	else if ( sortOrder == SortOrder.DESCENDING )
	  inc = -2;
	  
	if ( inc != 0 ) {
	  g.drawLine( x-2, y -inc , x + 3, y - inc );
	  g.drawLine( x-1, y, x + 2, y );      
	  g.drawLine( x, y + inc, x + 1, y + inc );
	}
  }
}
Page was generated in 0.1187 seconds