ar.com.fdvs.dj.test.ConditionalStylesReportTest.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.fdvs.dj.test.ConditionalStylesReportTest.java

Source

/*
 * DynamicJasper: A library for creating reports dynamically by specifying
 * columns, groups, styles, etc. at runtime. It also saves a lot of development
 * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
 *
 * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 *
 * License as published by the Free Software Foundation; either
 *
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 */

package ar.com.fdvs.dj.test;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import net.sf.jasperreports.view.JasperViewer;

import org.apache.commons.beanutils.BeanUtils;

import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;
import ar.com.fdvs.dj.domain.entities.conditionalStyle.StatusLightCondition;

public class ConditionalStylesReportTest extends BaseDjReportTest {

    public DynamicReport buildReport() throws Exception {

        Style detailStyle = new Style();
        Style headerStyle = new Style();
        headerStyle.setFont(Font.ARIAL_BIG_BOLD);
        headerStyle.setBorderBottom(Border.PEN_2_POINT);
        headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
        headerStyle.setBackgroundColor(Color.LIGHT_GRAY);
        headerStyle.setTextColor(Color.WHITE);
        headerStyle.setTransparency(Transparency.OPAQUE);

        Style titleStyle = new Style();
        titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
        Style amountStyle = new Style();
        amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
        Style oddRowStyle = new Style();
        oddRowStyle.setBorder(Border.NO_BORDER);
        Color veryLightGrey = new Color(230, 230, 230);
        oddRowStyle.setBackgroundColor(veryLightGrey);
        oddRowStyle.setTransparency(Transparency.OPAQUE);

        DynamicReportBuilder drb = new DynamicReportBuilder();
        Integer margin = new Integer(20);
        drb.setTitle("November 2006 sales report")
                .setSubtitle("The items in this report correspond "
                        + "to the main products: DVDs, Books, Foods and Magazines")
                .setTitleStyle(titleStyle).setTitleHeight(new Integer(30)).setSubtitleHeight(new Integer(20))
                .setDetailHeight(new Integer(15)).setLeftMargin(margin).setRightMargin(margin).setTopMargin(margin)
                .setBottomMargin(margin).setPrintBackgroundOnOddRows(true).setOddRowBackgroundStyle(oddRowStyle)
                .setColumnsPerPage(new Integer(1)).setColumnSpace(new Integer(5));

        AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
                .setTitle("State").setWidth(new Integer(85)).setStyle(detailStyle).setHeaderStyle(headerStyle)
                .build();

        AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
                .setTitle("Branch").setWidth(new Integer(85)).setStyle(detailStyle).setHeaderStyle(headerStyle)
                .build();

        AbstractColumn columnaProductLine = ColumnBuilder.getNew()
                .setColumnProperty("productLine", String.class.getName()).setTitle("Product Line")
                .setWidth(new Integer(85)).setStyle(detailStyle).setHeaderStyle(headerStyle).build();

        AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
                .setTitle("Item").setWidth(new Integer(85)).setStyle(detailStyle).setHeaderStyle(headerStyle)
                .build();

        AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
                .setTitle("ID").setWidth(new Integer(40)).setStyle(amountStyle).setHeaderStyle(headerStyle).build();

        AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
                .setTitle("Quantity").setWidth(new Integer(80)).setStyle(amountStyle).setHeaderStyle(headerStyle)
                .build();

        //Define Conditional Styles

        ArrayList conditionalStyles = createConditionalStyles(amountStyle);

        AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
                .setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
                .addConditionalStyles(conditionalStyles).setStyle(amountStyle).setHeaderStyle(headerStyle).build();

        drb.addColumn(columnState);
        drb.addColumn(columnBranch);
        drb.addColumn(columnaProductLine);
        drb.addColumn(columnaItem);
        drb.addColumn(columnCode);
        drb.addColumn(columnaCantidad);
        drb.addColumn(columnAmount);

        drb.setUseFullPageWidth(true);

        DynamicReport dr = drb.build();
        return dr;
    }

    /**
     * Create a Conditional Styles that redefines the text color of the values of the "amount" column
     * @param baseStyle
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    private ArrayList createConditionalStyles(Style baseStyle) throws IllegalAccessException,
            InstantiationException, InvocationTargetException, NoSuchMethodException {
        Style style0 = (Style) BeanUtils.cloneBean(baseStyle);
        style0.setTextColor(Color.RED);
        style0.setFont(Font.GEORGIA_MEDIUM_BOLD);
        Style style1 = (Style) BeanUtils.cloneBean(baseStyle);
        style1.setTextColor(new Color(128, 128, 0));
        Style style2 = (Style) BeanUtils.cloneBean(baseStyle);
        style2.setTextColor(new Color(0, 128, 0)); //dark green
        style2.setFont(Font.ARIAL_SMALL_BOLD);

        StatusLightCondition status0 = new StatusLightCondition(new Double(0), new Double(3000)); //TODO ENHANCEMENT make it come from a parameter??? $P{...}
        StatusLightCondition status1 = new StatusLightCondition(new Double(5000), new Double(6000));
        StatusLightCondition status2 = new StatusLightCondition(new Double(6000), new Double(100000));

        ConditionalStyle condition0 = new ConditionalStyle(status0, style0);
        ConditionalStyle condition1 = new ConditionalStyle(status1, style1);
        ConditionalStyle condition2 = new ConditionalStyle(status2, style2);

        ArrayList conditionalStyles = new ArrayList();
        conditionalStyles.add(condition0);
        conditionalStyles.add(condition1);
        conditionalStyles.add(condition2);
        return conditionalStyles;
    }

    public static void main(String[] args) throws Exception {
        ConditionalStylesReportTest test = new ConditionalStylesReportTest();
        test.testReport();
        test.exportToJRXML();
        JasperViewer.viewReport(test.jp);
    }

}