Hey Roedy!

I

Ike

Hey Roedy,

Looking over a Usenet reply posting of yours some time ago......I was =
wondering if you could share your AgedTableModel class with me? Thanks, Ike
 
R

Roedy Green

Looking over a Usenet reply posting of yours some time ago......I was =
wondering if you could share your AgedTableModel class with me? Thanks, Ike


package x;
import javax.swing.table.TableModel;

/**
* A TableModel that tracks the freshness of the
* data in each row. It is used in conjunction
* with FadingCellRenderer or DangerCellRenderer.
*
* @author Roedy Green
* @version 1.0
* @since 2002 August 27
*/
public interface AgedTableModel extends TableModel
{

/**
* Get age of the data in a given row.
*
* @param row which row you want the age of.
* @return age of the data in the row in milliseconds
*/
public int age ( int row );
}

==============================================

package x;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
* Cell renderer that fades colours over time.
* final ActionListener refresher = new ActionListener()
* {
* public void actionPerformed( ActionEvent evt )
* {
* tableModel.fireTableDataChanged();
* }
* };
* periodic = new Timer( Config.FADE_UPDATE_FREQUENCY, refresher );
* periodic.start();
* @author Roedy Green
*
* @author Roedy Green
*/
public class FadingCellRenderer extends DefaultTableCellRenderer
implements TableCellRenderer
{

/**
* Constructor
*
* @param freshForeground
* Starting foreground colour
* @param freshBackground
* starting background colour
* @param fadedForeground
* ending foreground colour
* @param fadedBackground
* ending background colour.
* @param timeToHold Time to hold the fresh colours before starting
* the fade process. The longer this number is,
* the less overhead there will be in terms
* of creating and garbage collecting Color.
* in milliseconds.
* @param timeToFade Time for the fade process to complete in
* milliseconds. The longer this number is.
* the more gradual will be the fading.
*/
public FadingCellRenderer( Color freshForeground,
Color freshBackground,
Color fadedForeground,
Color fadedBackground,
int timeToHold,
int timeToFade )
{
this.freshForeground = freshForeground;
this.freshBackground = freshBackground;
this.fadedForeground = fadedForeground;
this.fadedBackground = fadedBackground;
this.timeToHold = timeToHold;
this.timeToFade = timeToFade;

}

/**
* Default contructor.
*/
public FadingCellRenderer()
{
this( Config.FRESH_FOREGROUND,
Config.FRESH_BACKGROUND,
Config.FADED_FOREGROUND,
Config.FADED_BACKGROUND,
Config.TIME_TO_HOLD,
Config.TIME_TO_FADE );
}

private Color freshForeground;
private Color freshBackground;
private Color fadedForeground;
private Color fadedBackground;
private int timeToHold;
private int timeToFade;

/**
* Returns the component used for drawing the cell. This method
is
* used to configure the renderer appropriately before drawing.
* Associated TableModel must implement AgedTableModel.
*
* @param table the <code>JTable</code> that is asking
the
* renderer to draw; can be
<code>null</code>
* @param value the value of the cell to be rendered.
It is
* up to the specific renderer to
interpret
* and draw the value. For example, if
* <code>value</code>
* is the string "true", it could be
rendered as a
* string or it could be rendered as a
check
* box that is checked.
<code>null</code> is a
* valid value
* @param isSelected true if the cell is to be rendered
with the
* selection highlighted; otherwise false
* @param hasFocus if true, render cell appropriately.
For
* example, put a special border on the
cell, if
* the cell can be edited, render in the
color used
* to indicate editing
* @param row the row index of the cell being drawn.
When
* drawing the header, the value of
* <code>row</code> is -1
* @param column the column index of the cell being
drawn
*/
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{

// let original renderer handle text etc. We just modify
colours.

Component cell = super.getTableCellRendererComponent( table,
value,

isSelected,
hasFocus,
row,
column);
// how old is the data in this row.
int age = ((AgedTableModel)(table.getModel())).age( row );

Color foreground;
Color background;
int timeToFinish = timeToFade + timeToHold;
if ( age <= timeToHold )
{
foreground = freshForeground;
background = freshBackground;
}
else
if ( age >= timeToFinish )
{
foreground = fadedForeground;
background = fadedBackground;
}
else
{

// compute intermediate colours by linear interpolation
foreground = new Color ( interpolate(age, timeToHold,
timeToFinish, freshForeground.getRed(), fadedForeground.getRed()),
interpolate(age, timeToHold,
timeToFinish, freshForeground.getGreen(), fadedForeground.getGreen()),
interpolate(age, timeToHold,
timeToFinish, freshForeground.getBlue(), fadedForeground.getBlue()));

background = new Color( interpolate(age, timeToHold,
timeToFinish, freshBackground.getRed(), fadedBackground.getRed()),
interpolate(age, timeToHold,
timeToFinish, freshBackground.getGreen(), fadedBackground.getGreen()),
interpolate(age, timeToHold,
timeToFinish, freshBackground.getBlue(), fadedBackground.getBlue()));
}
cell.setForeground( foreground );
cell.setBackground( background );
return cell;
}

/**
* linearly interpolate the value of y given x with points on
* the line x1,y1 and x2,y2
*
* @param x
* @param x1
* @param x2
* @param y1
* @param y2
*/
private static final int interpolate (int x, int x1, int x2, int
y1, int y2)
{
if ( x2 == x1 )
{
return (y1 + y2) / 2;
}
else
{
return y1 + ( ( y2-y1 ) * (x - x1) ) / (x2 - x1);
}
}

/**
* Install this renderer for all columns of
* the table, for given class.
*
* @param jtable Jtable to install FadingRenderer on.
* @param renderClass
* Class you want rendered in fading style.
* e.g. Float.class
*/
public static void install( JTable jtable, Class renderClass )
{
FadingCellRenderer renderer = new FadingCellRenderer();
jtable.setDefaultRenderer( renderClass, renderer );
}
/**
* Install this renderer for all columns of
* the table, for given class.
*
* @param portalGroup
* PortalGroup containing this Jtablet to be rendered.
*
* @param portalGroupTable
* PortalGroup table to render.
* @param winAttrScheme
* font/colour scheme to use
* @param renderClass
* Class you want rendered in danger level style.
* null or Object.class is everything, Float.class,
String.class
*/

public static void install( PortalGroupTable portalGroupTable,
Class renderClass )
{
JTable jTable = portalGroupTable.getJTable();
FadingCellRenderer renderer = new FadingCellRenderer();
if ( renderClass == Object.class || renderClass == null )
{
/* ignore renderClass and install on each column */
TableColumnModel tm = jTable.getColumnModel();
int columns = tm.getColumnCount();
for ( int i=0; i<columns; i++ )
{
TableColumn tc = tm.getColumn(i);
tc.setCellRenderer( renderer );
}
}
else
{
/* install on renderClass */
jTable.setDefaultRenderer( renderClass, renderer );
}
}
/**
* Install this renderer for just one column of
* the table
*
* @param jTable PortalGroup table to render.
* @param column column 0-based column to install the renderer on.
*/
public static void install( JTable jTable, int column )
{

FadingCellRenderer renderer = new FadingCellRenderer();

TableColumnModel tm = jTable.getColumnModel();

int columns = tm.getColumnCount();
if ( column < 0 || column >= columns )
{
throw new IllegalArgumentException("column " + column + " out
of range in DangerCellRenderer.install");
}
TableColumn tc = tm.getColumn( column );
tc.setCellRenderer( renderer );

}
}
 
M

Mladen Adamovic

????!!!??!?!?!

Roedy Green said:
Ike
* A TableModel that tracks the freshness of the
* data in each row. It is used in conjunction
* with FadingCellRenderer or DangerCellRenderer.
*
* @author Roedy Green
...........................

i don't catch.... the point... of this... Roedy staff...


please enlight me!
 
R

Roedy Green

i don't catch.... the point... of this... Roedy staff...

It is code requested to display tabular data is a way in gradually
fades over time to indicate age. Fresh data is in brighter colours.

It is quite a fun effect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top