TutorialExample2.java
package sos.examples;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import sos.reports.*;
import sos.reports.Element;
public class TutorialExample2
{
public static void main(String[] args)
{
SwingUtilities.invokeLater( new Runnable() {
public void run()
{
Object[][] data = new Object[][] {
{ "Family", "1850 Census", "1860 Census", "1870 Census", "Notes" },
{ "Smith", Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, "" },
{ "Johnson", Boolean.FALSE, Boolean.TRUE, Boolean.TRUE, "In 1860 spelled Johnssen" },
{ "Russell", Boolean.TRUE, Boolean.TRUE, Boolean.TRUE, "" },
{ "Grant", Boolean.FALSE, Boolean.FALSE, Boolean.FALSE, "Not found in Springfield" },
{ "Baker", Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, "" },
};
DefaultTableFormat defaultTableFormat = new DefaultTableFormat();
MutableAttributeSet tableAttributes = new SimpleAttributeSet();
ReportStyleConstants.setHorizontalGridThickness( tableAttributes, 1.0 );
ReportStyleConstants.setVerticalGridThickness( tableAttributes, 1.0 );
StyleConstants.setFontFamily( tableAttributes, "San Serif" );
ReportStyleConstants.setBorderThickness( tableAttributes, 1.0 );
ReportStyleConstants.setWrapped( tableAttributes, false );
defaultTableFormat.setTableAttributes( tableAttributes );
MutableAttributeSet bodyAttributes = new SimpleAttributeSet();
ReportStyleConstants.setRenderer( bodyAttributes, new CheckBoxElementRenderer() );
defaultTableFormat.setColumnCellAttributes( bodyAttributes, 1, DefaultTableFormat.BODY );
defaultTableFormat.setColumnCellAttributes( bodyAttributes, 2, DefaultTableFormat.BODY );
defaultTableFormat.setColumnCellAttributes( bodyAttributes, 3, DefaultTableFormat.BODY );
MutableAttributeSet tierAttributes = new SimpleAttributeSet();
ReportStyleConstants.setFillProportion( tierAttributes, 0 );
defaultTableFormat.setTierAttributes( tierAttributes );
MutableAttributeSet lastColumnAttributes = new SimpleAttributeSet();
ReportStyleConstants.setWrapped( lastColumnAttributes, true );
ReportStyleConstants.setFillProportion( lastColumnAttributes, 1 );
defaultTableFormat.setColumnAttributes( lastColumnAttributes, -1 );
MutableAttributeSet headerAttributes = new SimpleAttributeSet();
StyleConstants.setBold( headerAttributes, true );
MutableAttributeSet headerRowAttributes = new SimpleAttributeSet();
headerRowAttributes.setResolveParent( headerAttributes );
ReportStyleConstants.setBackgroundFill( headerRowAttributes, Fill.YELLOW );
defaultTableFormat.setRowAttributes( headerRowAttributes, DefaultTableFormat.HEADER );
defaultTableFormat.setColumnAttributes( headerAttributes, DefaultTableFormat.HEADER );
StyleContext styleContext = new StyleContext();
Style tableStyle = styleContext.addStyle( "Table", null );
ReportStyleConstants.setTableFormat( tableStyle, defaultTableFormat );
Theme theme = new DefaultTheme( "Default Table Report Template Theme", styleContext );
TableReportTemplate template = new TableReportTemplate( "Census Template", 1, 1, 0, 0 );
Report report = template.createReport( data, theme );
JReportPane reportPane = new JReportPane( report );
JScrollPane scrollPane = new JScrollPane( reportPane );
JFrame frame = new JFrame( "Census Report" );
frame.getContentPane().add( scrollPane );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 500, 300 );
frame.show();
}
} );
}
static class CheckBoxElementRenderer implements ElementRenderer
{
static final private Component BLANK = new JLabel();
private JCheckBox checkBox = new JCheckBox();
public CheckBoxElementRenderer()
{
super();
checkBox.setHorizontalAlignment( JLabel.CENTER );
checkBox.setOpaque( false );
}
public Component getElementRendererComponent(JReportPane reportPane, Element element)
{
Boolean object = (Boolean)element.getObject();
if( object == null )
return BLANK;
boolean selected = object != null && object.booleanValue();
checkBox.setSelected( selected );
return checkBox;
}
}
}