View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.test;
31  
32  import java.awt.Color;
33  import java.lang.reflect.InvocationTargetException;
34  import java.util.ArrayList;
35  
36  import net.sf.jasperreports.view.JasperViewer;
37  
38  import org.apache.commons.beanutils.BeanUtils;
39  
40  import ar.com.fdvs.dj.domain.DynamicReport;
41  import ar.com.fdvs.dj.domain.Style;
42  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
43  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
44  import ar.com.fdvs.dj.domain.constants.Border;
45  import ar.com.fdvs.dj.domain.constants.Font;
46  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
47  import ar.com.fdvs.dj.domain.constants.Transparency;
48  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
49  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
50  import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;
51  import ar.com.fdvs.dj.domain.entities.conditionalStyle.StatusLightCondition;
52  
53  public class ConditionalStylesReportTest extends BaseDjReportTest {
54  
55  	public DynamicReport buildReport() throws Exception {
56  
57  		Style detailStyle = new Style();
58  		Style headerStyle = new Style();
59  		headerStyle.setFont(Font.ARIAL_BIG_BOLD);
60  		headerStyle.setBorderBottom(Border.PEN_2_POINT());
61  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
62  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
63  		headerStyle.setBackgroundColor(Color.LIGHT_GRAY);
64  		headerStyle.setTextColor(Color.WHITE);
65  		headerStyle.setTransparency(Transparency.OPAQUE);
66  
67  		Style titleStyle = new Style();
68  		titleStyle.setFont(new Font(18,Font._FONT_VERDANA,true));
69  		Style amountStyle = new Style();
70  		amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
71  		Style oddRowStyle = new Style();
72  		oddRowStyle.setBorder(Border.NO_BORDER());
73  		Color veryLightGrey = new Color(230,230,230);
74  		oddRowStyle.setBackgroundColor(veryLightGrey);oddRowStyle.setTransparency(Transparency.OPAQUE);
75  
76  		DynamicReportBuilder drb = new DynamicReportBuilder();
77  		Integer margin = new Integer(20);
78  		drb.setTitle("November " + getYear() +" sales report")
79  			.setSubtitle("The items in this report correspond "
80  				+"to the main products: DVDs, Books, Foods and Magazines")
81  			.setTitleStyle(titleStyle).setTitleHeight(new Integer(30))
82  			.setSubtitleHeight(new Integer(20))
83  			.setDetailHeight(new Integer(15))
84  			.setLeftMargin(margin)
85  			.setRightMargin(margin)
86  			.setTopMargin(margin)
87  			.setBottomMargin(margin)
88  			.setPrintBackgroundOnOddRows(true)
89  			.setOddRowBackgroundStyle(oddRowStyle)
90  			.setColumnsPerPage(new Integer(1))
91  			.setColumnSpace(new Integer(5));
92  
93  		AbstractColumn columnState = ColumnBuilder.getNew().setColumnProperty("state", String.class.getName())
94  			.setTitle("State").setWidth(new Integer(85))
95  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
96  
97  		AbstractColumn columnBranch = ColumnBuilder.getNew().setColumnProperty("branch", String.class.getName())
98  			.setTitle("Branch").setWidth(new Integer(85))
99  			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
100 
101 		AbstractColumn columnaProductLine = ColumnBuilder.getNew().setColumnProperty("productLine", String.class.getName())
102 			.setTitle("Product Line").setWidth(new Integer(85))
103 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
104 
105 		AbstractColumn columnaItem = ColumnBuilder.getNew().setColumnProperty("item", String.class.getName())
106 			.setTitle("Item").setWidth(new Integer(85))
107 			.setStyle(detailStyle).setHeaderStyle(headerStyle).build();
108 
109 		AbstractColumn columnCode = ColumnBuilder.getNew().setColumnProperty("id", Long.class.getName())
110 			.setTitle("ID").setWidth(new Integer(40))
111 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
112 
113 		AbstractColumn columnaCantidad = ColumnBuilder.getNew().setColumnProperty("quantity", Long.class.getName())
114 			.setTitle("Quantity").setWidth(new Integer(80))
115 			.setStyle(amountStyle).setHeaderStyle(headerStyle).build();
116 
117 
118 		//Define Conditional Styles
119 
120 		ArrayList conditionalStyles = createConditionalStyles(amountStyle);
121 
122 		AbstractColumn columnAmount = ColumnBuilder.getNew().setColumnProperty("amount", Float.class.getName())
123 			.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
124 			.addConditionalStyles(conditionalStyles)
125 			.setStyle(amountStyle)
126 			.setHeaderStyle(headerStyle).build();
127 
128 		drb.addColumn(columnState);
129 		drb.addColumn(columnBranch);
130 		drb.addColumn(columnaProductLine);
131 		drb.addColumn(columnaItem);
132 		drb.addColumn(columnCode);
133 		drb.addColumn(columnaCantidad);
134 		drb.addColumn(columnAmount);
135 
136 		drb.setUseFullPageWidth(true);
137 
138 		DynamicReport dr = drb.build();
139 		return dr;
140 	}
141 
142 	/**
143 	 * Create a Conditional Styles that redefines the text color of the values of the "amount" column
144 	 * @param baseStyle
145 	 * @return
146 	 * @throws IllegalAccessException
147 	 * @throws InstantiationException
148 	 * @throws InvocationTargetException
149 	 * @throws NoSuchMethodException
150 	 */
151 	private ArrayList createConditionalStyles(Style baseStyle) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
152 		Style style0 = (Style) BeanUtils.cloneBean(baseStyle);
153 		style0.setTextColor(Color.RED);
154 		style0.setFont(Font.GEORGIA_MEDIUM_BOLD);
155 		Style style1 = (Style) BeanUtils.cloneBean(baseStyle);
156 		style1.setTextColor(new Color(128,128,0));
157 		Style style2 = (Style) BeanUtils.cloneBean(baseStyle);
158 		style2.setTextColor(new Color(0,128,0)); //dark green
159 		style2.setFont(Font.ARIAL_SMALL_BOLD);
160 
161 		StatusLightCondition status0 = new StatusLightCondition(new Double(0), new Double(3000)); //TODO ENHANCEMENT make it come from a parameter??? $P{...}
162 		StatusLightCondition status1 = new StatusLightCondition(new Double(5000), new Double(6000));
163 		StatusLightCondition status2 = new StatusLightCondition(new Double(6000),new Double(100000));
164 
165 		ConditionalStyle condition0 = new ConditionalStyle(status0,style0);
166 		ConditionalStyle condition1 = new ConditionalStyle(status1,style1);
167 		ConditionalStyle condition2 = new ConditionalStyle(status2,style2);
168 
169 		ArrayList conditionalStyles = new ArrayList();
170 		conditionalStyles.add(condition0);
171 		conditionalStyles.add(condition1);
172 		conditionalStyles.add(condition2);
173 		return conditionalStyles;
174 	}
175 
176 	public static void main(String[] args) throws Exception {
177 		ConditionalStylesReportTest test = new ConditionalStylesReportTest();
178 		test.testReport();
179 		test.exportToJRXML();
180 		JasperViewer.viewReport(test.jp);
181 	}
182 
183 }