ScheduleExample.java

/*
 * ScheduleExample.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.*;

/**
 * Sample application that uses Side of Software's Report Library
 * and illustrates cell straddling and gradient coloring.<p>
 *
 * @author  Side of Software
 */
public class ScheduleExample
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater( new Runnable() {
      public void run()
      {
        Object m = TableReportTemplate.STRADDLE_PREVIOUS_COLUMN;
        final Object[][] data = new Object[][] {
          { null, "12:00", m, "12:30", m, "1:00", m, "1:30", m, "2:00", m, "2:30", m, "3:00", m, "3:30", m, },
          { "Amanda", null, m, null, m, null, m, null, "", m, m, m, m, m, m, m, m },
          { "Bethany", null, "", m, null, "", m, null, m, "", m, m, null, null, m, null, m },
          { "Craig", "", m, m, m, m, null, null, "", m, m, null, m, null, "", m, null },
          { "John", null, "", m, null, null, m, "", m, null, m, null, m, "", m, m, m },
          { "Justin", "", m, m, null, "", null, "", null, "", m, null, "", null, m, null, m },
          { "Kathy", null, m, null, m, null, m, null, m, null, "", m, m, m, null, null, m },
          { "Lori", null, "", null, m, "", m, m, m, null, "", m, m, null, m, null, "" },
          { "Mark", "", m, m, null, null, m, null, "", m, null, null, m, null, m, null, m },
          { "Patty", null, m, null, "", m, m, m, null, null, m, "", m, null, m, "", m },
          { "Ralph", "", m, null, m, null, m, null, m, null, "", m, m, m, m, null, m },
        };

        // store the data in a table model so that we can specify the style
        // name of the cell content
        ReportTableModel tableModel = new AbstractReportTableModel() {
          public Object getValueAt( int row, int column )
          {
            return data[row][column];
          }
          
          public int getRowCount()
          {
            return data.length;
          }
          
          public int getColumnCount()
          {
            return data[0].length;
          }
          
          public String getStyleNameAt( int row, int column )
          {
            Object value = getValueAt( row, column );
            if( value == "" )
              return "Booked";
            else
              return super.getStyleNameAt( row, column );
          }
        };
        
        // 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 );
        ReportStyleConstants.setRowCushionBefore( tableAttributes, 2.0 );
        ReportStyleConstants.setRowCushionAfter( tableAttributes, 2.0 );
        defaultTableFormat.setTableAttributes( tableAttributes );
        
        // text in the header row and column is bold
        MutableAttributeSet headerAttributes = new SimpleAttributeSet();
        StyleConstants.setBold( headerAttributes, true );

        // the header row is yellow
        MutableAttributeSet headerRowCellAttributes = new SimpleAttributeSet();
        Color color = new Color( 153, 0, 102 );
        ReportStyleConstants.setBackgroundFill( headerRowCellAttributes, new DefaultFill( color.brighter(), color, SwingConstants.SOUTH ));
        ReportStyleConstants.setVerticalAlignment( headerRowCellAttributes, StyleConstants.ALIGN_CENTER );
        ReportStyleConstants.setHorizontalAlignment( headerRowCellAttributes, StyleConstants.ALIGN_CENTER );
        StyleConstants.setForeground( headerRowCellAttributes, Color.white );
        defaultTableFormat.setColumnCellAttributes( headerRowCellAttributes, DefaultTableFormat.HEADER );
        
        defaultTableFormat.setRowAttributes( headerAttributes, DefaultTableFormat.HEADER );
        defaultTableFormat.setColumnAttributes( headerAttributes, DefaultTableFormat.HEADER );

        StyleContext styleContext = new StyleContext();
        Style tableStyle = styleContext.addStyle( "Table", null );
        ReportStyleConstants.setTableFormat( tableStyle, defaultTableFormat );

        Color c = new Color( 0, 153, 255 );
        Fill fill = new DefaultFill( c, c.brighter(), SwingConstants.EAST );
        Style bookedStyle = styleContext.addStyle( "Booked", null );
        ReportStyleConstants.setBackgroundFill( bookedStyle, fill );
        
        Theme theme = new DefaultTheme( "Default Table Report Template Theme", styleContext );
        
        TableReportTemplate template = new TableReportTemplate( "Schedule Template", 1, 1, 0, 0 );
        Report report = makeTitledReport( "Schedule\nMonday Afternoon", tableModel, template, theme );

        JReportPane reportPane = new JReportPane( report );
        reportPane.setBackground( new Color( 253, 245, 230 ));

        JScrollPane scrollPane = new JScrollPane( reportPane );
        JFrame frame = new JFrame( "Schedule" );
        frame.getContentPane().add( scrollPane );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize( 500, 300 );
        frame.show();
      }
    } );
  }
  
  public static Report makeTitledReport( String title, Object body, ReportTemplate bodyTemplate, Theme bodyTheme )
  {
    StyleContext styleContext = new StyleContext();
    
    // the title resides in cell 0,0
    //
    // font 16; bold; centered; 6 points below
    //
    Style titleStyle = styleContext.addStyle( "Cell0,0Object", null );
    StyleConstants.setFontSize( titleStyle, 16 );
    StyleConstants.setBold( titleStyle, true );
    StyleConstants.setAlignment( titleStyle, StyleConstants.ALIGN_CENTER );
    StyleConstants.setSpaceBelow( titleStyle, 6.0f );
    StyleConstants.setFontFamily( titleStyle, "San Serif" );

    Style rowStyle = styleContext.addStyle( "Row", null );
    ReportStyleConstants.setFillProportion( rowStyle, 0 );
    styleContext.addStyle( "Row0", rowStyle );
    styleContext.addStyle( "Row1", rowStyle );
    
    // the content resides in cell 1,0
    //
    // use a nested report template and theme
    //
    Style bodyStyle = styleContext.addStyle( "Cell1,0Object", null );
    ReportStyleConstants.setReportTemplate( bodyStyle, bodyTemplate );
    ReportStyleConstants.setTheme( bodyStyle, bodyTheme );
    
    Theme theme = new DefaultTheme( styleContext );
    
    Object[] content = new Object[] { title, body };
    ReportTemplate template = new TableReportTemplate();
    Report report = template.createReport( content, theme );
    return report;
  }
}