TutorialExample2.java

/*
 * TutorialExample2.java
 *
 * Copyright (C) 2004-05 Side of Software (SOS)
 * All rights reserved.
 *
 *    http://www.sideofsoftware.com
 *    info@sideofsoftware.com
 */

package sos.examples;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import sos.reports.*;
import sos.reports.Element; // takes precedence over javax.swing.text.Element

/**
 * A sample application that displays a formatted report.<p>
 *
 * @author  Side of Software
 */
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, "" },
        };

        // create the table format
        DefaultTableFormat defaultTableFormat = new DefaultTableFormat();
        
        // the table's attributes
        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 );
        
        // use a checkbox renderer for columns 2, 3, and 4
        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 );
        
        // do not let a row or column be wider than its preferred size
        MutableAttributeSet tierAttributes = new SimpleAttributeSet();
        ReportStyleConstants.setFillProportion( tierAttributes, 0 );
        defaultTableFormat.setTierAttributes( tierAttributes );
        
        // the last column can wrap and should take up any extra space
        MutableAttributeSet lastColumnAttributes = new SimpleAttributeSet();
        ReportStyleConstants.setWrapped( lastColumnAttributes, true );
        ReportStyleConstants.setFillProportion( lastColumnAttributes, 1 );
        defaultTableFormat.setColumnAttributes( lastColumnAttributes, -1 );
        
        // text in the header row and column is bold
        MutableAttributeSet headerAttributes = new SimpleAttributeSet();
        StyleConstants.setBold( headerAttributes, true );

        // the header row is yellow
        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();
      }
    } );
  }
  
  
  /**
   * An element renderer that displays Boolean values with
   * a check box.
   */
  static class CheckBoxElementRenderer implements ElementRenderer
  {
    // if the value is null, we'll use a blank label; otherwise, we'll
    // use a checkbox
    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();

      // don't use a checkbox if null
      if( object == null )
        return BLANK;

      // initialize the checkbox appropriately
      boolean selected = object != null && object.booleanValue();
      checkBox.setSelected( selected );
      return checkBox;
    }
  }
}