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.groups.labels;
31  
32  import java.awt.Color;
33  
34  import net.sf.jasperreports.view.JasperDesignViewer;
35  import net.sf.jasperreports.view.JasperViewer;
36  import ar.com.fdvs.dj.domain.AutoText;
37  import ar.com.fdvs.dj.domain.DJCalculation;
38  import ar.com.fdvs.dj.domain.DJGroupLabel;
39  import ar.com.fdvs.dj.domain.DynamicReport;
40  import ar.com.fdvs.dj.domain.ImageBanner;
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.builders.GroupBuilder;
45  import ar.com.fdvs.dj.domain.builders.StyleBuilder;
46  import ar.com.fdvs.dj.domain.constants.Border;
47  import ar.com.fdvs.dj.domain.constants.Font;
48  import ar.com.fdvs.dj.domain.constants.GroupLayout;
49  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
50  import ar.com.fdvs.dj.domain.constants.LabelPosition;
51  import ar.com.fdvs.dj.domain.constants.Transparency;
52  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
53  import ar.com.fdvs.dj.domain.entities.DJGroup;
54  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
55  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
56  import ar.com.fdvs.dj.test.BaseDjReportTest;
57  
58  public class GroupLabelTest3 extends BaseDjReportTest {
59  
60  	public DynamicReport buildReport() throws Exception {
61  
62  		Style detailStyle = new Style();
63  
64  		Style headerStyle = new Style();
65  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
66  		headerStyle.setBorderBottom(Border.PEN_1_POINT());
67  		headerStyle.setBackgroundColor(Color.gray);
68  		headerStyle.setTextColor(Color.white);
69  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
70  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
71  		headerStyle.setTransparency(Transparency.OPAQUE);
72  
73  		Style headerVariables = new Style();
74  		headerVariables.setFont(Font.ARIAL_MEDIUM_BOLD);
75  		//headerVariables.setBorder(Border.THIN());
76  		headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
77  		headerVariables.setVerticalAlign(VerticalAlign.MIDDLE);
78  
79  		Style titleStyle = new Style();
80  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
81  		Style importeStyle = new Style();
82  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
83  		Style oddRowStyle = new Style();
84  		oddRowStyle.setBorder(Border.NO_BORDER());
85  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
86  		oddRowStyle.setTransparency(Transparency.OPAQUE);
87  
88  		DynamicReportBuilder drb = new DynamicReportBuilder();
89  		Integer margin = new Integer(20);
90  		drb
91  			.setTitleStyle(titleStyle)
92  			.setTitle("November 2006 sales report")					//defines the title of the report
93  			.setSubtitle("The items in this report correspond "
94  					+"to the main products: DVDs, Books, Foods and Magazines")
95  			.setDetailHeight(new Integer(15))
96  			.setLeftMargin(margin)
97  			.setRightMargin(margin).setTopMargin(margin).setBottomMargin(margin)
98  			.setPrintBackgroundOnOddRows(false)
99  			.setGrandTotalLegend("Grand Total")
100 			.setGrandTotalLegendStyle(headerVariables)
101 			.setDefaultStyles(titleStyle, null, headerStyle, detailStyle)
102 			.setPrintColumnNames(false)
103 			.addImageBanner(System.getProperty("user.dir") +"/target/test-classes/images/logo_fdv_solutions_60.jpg", new Integer(100), new Integer(30), ImageBanner.ALIGN_RIGHT)
104 			.setOddRowBackgroundStyle(oddRowStyle);
105 
106 
107 		AbstractColumn columnState = ColumnBuilder.getNew()
108 				.setColumnProperty("state", String.class.getName())
109 				.setTitle("State").setWidth(new Integer(85))
110 				//.setStyle(titleStyle).setHeaderStyle(titleStyle)
111 				.build();
112 
113 		AbstractColumn columnBranch = ColumnBuilder.getNew()
114 				.setColumnProperty("branch", String.class.getName())
115 				.setTitle("Branch").setWidth(new Integer(85))
116 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
117 				.build();
118 
119 		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
120 				.setColumnProperty("productLine", String.class.getName())
121 				.setTitle("Product Line").setWidth(new Integer(85))
122 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
123 				.build();
124 
125 		AbstractColumn columnaItem = ColumnBuilder.getNew()
126 				.setColumnProperty("item", String.class.getName())
127 				.setTitle("Item").setWidth(new Integer(85))
128 				.setStyle(detailStyle).setHeaderStyle(headerStyle)
129 				.build();
130 
131 		AbstractColumn columnCode = ColumnBuilder.getNew()
132 				.setColumnProperty("id", Long.class.getName())
133 				.setTitle("ID").setWidth(new Integer(40))
134 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
135 				.build();
136 
137 		AbstractColumn columnaQuantity = ColumnBuilder.getNew()
138 				.setColumnProperty("quantity", Long.class.getName())
139 				.setTitle("Quantity").setWidth(new Integer(80))
140 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
141 				.build();
142 
143 		AbstractColumn columnAmount = ColumnBuilder.getNew()
144 				.setColumnProperty("amount", Float.class.getName())
145 				.setTitle("Amount").setWidth(new Integer(90)).setPattern("$ 0.00")
146 				.setStyle(importeStyle).setHeaderStyle(headerStyle)
147 				.build();
148 
149 
150 		GroupBuilder gb1 = new GroupBuilder();
151 
152 		Style glabelStyle2 = new StyleBuilder(false).setFont(Font.ARIAL_SMALL)
153 		.setHorizontalAlign(HorizontalAlign.RIGHT)
154 		.setVerticalAlign(VerticalAlign.MIDDLE)
155 		.setPadding(new Integer(0))
156 		.setStretchWithOverflow(false)
157 		.build();
158 		
159 		DJGroupLabel glabel3 = new DJGroupLabel("Subtotal"  ,glabelStyle2,LabelPosition.TOP);
160 		
161 		//		 define the criteria column to group by (columnState)
162 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
163 				//.addHeaderVariable(columnAmount,DJCalculation.SUM,headerVariables) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
164 //				.addHeaderVariable(columnaQuantity,DJCalculation.SUM,headerVariables) // idem for the columnaQuantity column
165 				.addFooterVariable(columnAmount,DJCalculation.SUM,headerVariables) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
166 				.addFooterVariable(columnaQuantity,DJCalculation.SUM,headerVariables) // idem for the columnaQuantity column
167 //				.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
168 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER_WITH_HEADERS) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
169 				.setFooterLabel(glabel3)
170 //				.setFooterVariablesHeight(new Integer(30))
171 				.build();
172 
173 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
174 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
175 				.addFooterVariable(columnAmount,DJCalculation.SUM) // columnaQuantity columns
176 				.addFooterVariable(columnaQuantity,	DJCalculation.SUM)
177 				.build();
178 
179 		drb.addColumn(columnState);
180 		drb.addColumn(columnBranch);
181 		drb.addColumn(columnaProductLine);
182 //		drb.addColumn(columnaItem);
183 //		drb.addColumn(columnCode);
184 		drb.addColumn(columnaQuantity);
185 		drb.addColumn(columnAmount);
186 
187 		drb.addGroup(g1); // add group g1
188 //		drb.addGroup(g2); // add group g2
189 
190 		drb.setUseFullPageWidth(true);
191 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
192 
193 		DynamicReport dr = drb.build();
194 		return dr;
195 	}
196 
197 	public static void main(String[] args) throws Exception {
198 		GroupLabelTest3 test = new GroupLabelTest3();
199 		test.testReport();
200 		test.exportToJRXML();
201 		JasperViewer.viewReport(test.jp);
202 		JasperDesignViewer.viewReportDesign(test.jr);
203 	}
204 
205 }