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  
34  import net.sf.jasperreports.view.JasperViewer;
35  import ar.com.fdvs.dj.domain.DynamicReport;
36  import ar.com.fdvs.dj.domain.Style;
37  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
38  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
39  import ar.com.fdvs.dj.domain.constants.Border;
40  import ar.com.fdvs.dj.domain.constants.Font;
41  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
42  import ar.com.fdvs.dj.domain.constants.Transparency;
43  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
44  
45  public class TemplateStyleReportTest extends BaseDjReportTest {
46  
47  	public DynamicReport buildReport() throws Exception {
48  
49  //		Style detailStyle = new Style();
50  		Style headerStyle = new Style();
51  
52  		headerStyle.setBackgroundColor(new Color(230,230,230));
53  		headerStyle.setBorderBottom(Border.THIN());
54  		headerStyle.setBorderColor(Color.black);
55  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
56  		headerStyle.setTransparency(Transparency.OPAQUE);
57  
58  		/**
59  		 * "titleStyle" exists in the template .jrxml file
60  		 * The title should be seen in a big font size, violet foreground and light green background
61  		 */
62  		Style titleStyle = new Style("titleStyle");
63  
64  		/**
65  		 * "subtitleStyleParent" is meant to be used as a parent style, while
66  		 * "subtitleStyle" is the child.
67  		 */
68  		Style subtitleStyleParent = new Style("subtitleParent");
69  		subtitleStyleParent.setBackgroundColor(Color.CYAN);
70  		subtitleStyleParent.setTransparency(Transparency.OPAQUE);
71  
72  		Style subtitleStyle = Style.createBlankStyle("subtitleStyle","subtitleParent");
73  		subtitleStyle.setFont(Font.GEORGIA_SMALL_BOLD);
74  
75  		Style amountStyle = new Style(); amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
76  
77  		/**
78  		 * Creates the DynamicReportBuilder and sets the basic options for
79  		 * the report
80  		 */
81  		DynamicReportBuilder drb = new DynamicReportBuilder();
82  		drb.setTitle("November " + getYear() +" sales report")
83  			.setSubtitle("The items in this report correspond "
84  					+"to the main products: DVDs, Books, Foods and Magazines")
85  			.setDetailHeight(15)
86  			.setMargins(30, 20, 30, 15)
87  			.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, null)
88  			.addStyle(subtitleStyleParent); //register the parent style
89  
90  
91  		/**
92  		 * Note that we didn't call the build() method yet
93  		 */
94  
95  		/**
96  		 * Column definitions. We use a new ColumnBuilder instance for each
97  		 * column, the ColumnBuilder.getNew() method returns a new instance
98  		 * of the builder
99  		 */
100 		AbstractColumn columnState = ColumnBuilder.getNew()		//creates a new instance of a ColumnBuilder
101 			.setColumnProperty("state", String.class.getName())			//defines the field of the data source that this column will show, also its type
102 			.setTitle("State")											//the title for the column
103 			.setWidth(85)									//the width of the column
104 			.build();													//builds and return a new AbstractColumn
105 
106 		//Create more columns
107 		AbstractColumn columnBranch = ColumnBuilder.getNew()
108 			.setColumnProperty("branch", String.class.getName())
109 			.setTitle("Branch").setWidth(85)
110 			.build();
111 
112 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
113 			.setColumnProperty("productLine", String.class.getName())
114 			.setTitle("Product Line").setWidth(85)
115 			.build();
116 
117 		AbstractColumn columnaItem = ColumnBuilder.getNew()
118 			.setColumnProperty("item", String.class.getName())
119 			.setTitle("Item").setWidth(85)
120 			.build();
121 
122 		AbstractColumn columnCode = ColumnBuilder.getNew()
123 			.setColumnProperty("id", Long.class.getName())
124 			.setTitle("ID").setWidth(40)
125 			.build();
126 
127 		AbstractColumn columnaCantidad = ColumnBuilder.getNew()
128 			.setColumnProperty("quantity", Long.class.getName())
129 			.setTitle("Quantity").setWidth(80)
130 			.build();
131 
132 		AbstractColumn columnAmount = ColumnBuilder.getNew()
133 			.setColumnProperty("amount", Float.class.getName())
134 			.setTitle("Amount").setWidth(90)
135 			.setPattern("$ 0.00")		//defines a pattern to apply to the values swhown (uses TextFormat)
136 			.setStyle(amountStyle)		//special style for this column (align right)
137 			.build();
138 
139 		/**
140 		 * We add the columns to the report (through the builder) in the order
141 		 * we want them to appear
142 		 */
143 		drb.addColumn(columnState);
144 		drb.addColumn(columnBranch);
145 		drb.addColumn(columnaProductLine);
146 		drb.addColumn(columnaItem);
147 		drb.addColumn(columnCode);
148 		drb.addColumn(columnaCantidad);
149 		drb.addColumn(columnAmount);
150 
151 		/**
152 		 * add some more options to the report (through the builder)
153 		 */
154 		drb.setUseFullPageWidth(true);	//we tell the report to use the full width of the page. this rezises
155 										//the columns width proportionally to meet the page width.
156 
157 		//This look for the resource in the classpath
158 		//drb.setTemplateFile("templates/TemplateReportTestWithStyles2.jrxml");
159 
160 		DynamicReport dr = drb.build();	//Finally build the report!
161 
162 		params.put("leftHeader", "My Company - My Area");
163 		params.put("rightHeader", "User: Jack Sparrow");
164 		return dr;
165 	}
166 
167     public static void main(String[] args) throws Exception {
168 		TemplateStyleReportTest test = new TemplateStyleReportTest();
169 		test.testReport();
170 		JasperViewer.viewReport(test.jp);	//finally display the report report
171 	}
172 
173 }