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.Stretching;
52  import ar.com.fdvs.dj.domain.constants.Transparency;
53  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
54  import ar.com.fdvs.dj.domain.entities.DJGroup;
55  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
56  import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;
57  import ar.com.fdvs.dj.test.BaseDjReportTest;
58  
59  public class GroupLabelTest1 extends BaseDjReportTest {
60  
61  	public DynamicReport buildReport() throws Exception {
62  
63  		Style detailStyle = new Style();
64  
65  		Style headerStyle = new Style();
66  		headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
67  		headerStyle.setBorderBottom(Border.PEN_1_POINT);
68  		headerStyle.setBackgroundColor(Color.gray);
69  		headerStyle.setTextColor(Color.white);
70  		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
71  		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
72  		headerStyle.setTransparency(Transparency.OPAQUE);
73  
74  		Style headerVariables = new Style();
75  		headerVariables.setFont(Font.ARIAL_MEDIUM_BOLD);
76  //		headerVariables.setBorder(Border.THIN);
77  		headerVariables.setHorizontalAlign(HorizontalAlign.RIGHT);
78  		headerVariables.setVerticalAlign(VerticalAlign.TOP);
79  
80  		Style titleStyle = new Style();
81  		titleStyle.setFont(new Font(18, Font._FONT_VERDANA, true));
82  		Style importeStyle = new Style();
83  		importeStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
84  		Style oddRowStyle = new Style();
85  		oddRowStyle.setBorder(Border.NO_BORDER);
86  		oddRowStyle.setBackgroundColor(Color.LIGHT_GRAY);
87  		oddRowStyle.setTransparency(Transparency.OPAQUE);
88  
89  		DynamicReportBuilder drb = new DynamicReportBuilder();
90  		Integer margin = new Integer(20);
91  		drb
92  			.setTitleStyle(titleStyle)
93  			.setTitle("November 2006 sales report")					//defines the title of the report
94  			.setSubtitle("The items in this report correspond "
95  					+"to the main products: DVDs, Books, Foods and Magazines")
96  			.setDetailHeight(new Integer(15)).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(true)
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 glabelStyle = new StyleBuilder(false).setFont(Font.ARIAL_SMALL)
153 			.setHorizontalAlign(HorizontalAlign.RIGHT).setBorderTop(Border.THIN)
154 			.setStretchWithOverflow(false)
155 			.build();
156 		DJGroupLabel glabel1 = new DJGroupLabel("Total amount",glabelStyle,LabelPosition.TOP);
157 		DJGroupLabel glabel2 = new DJGroupLabel("Total quantity",glabelStyle,LabelPosition.TOP);
158 		
159 		//		 define the criteria column to group by (columnState)
160 		DJGroup g1 = gb1.setCriteriaColumn((PropertyColumn) columnState)
161 				.addFooterVariable(columnAmount,DJCalculation.SUM,headerVariables, null, glabel1) // tell the group place a variable footer of the column "columnAmount" with the SUM of allvalues of the columnAmount in this group.
162 				.addFooterVariable(columnaQuantity,DJCalculation.SUM,headerVariables, null, glabel2) // idem for the columnaQuantity column
163 				.setGroupLayout(GroupLayout.VALUE_IN_HEADER) // tells the group how to be shown, there are manyposibilities, see the GroupLayout for more.
164 				
165 				.build();
166 
167 		GroupBuilder gb2 = new GroupBuilder(); // Create another group (using another column as criteria)
168 		DJGroup g2 = gb2.setCriteriaColumn((PropertyColumn) columnBranch) // and we add the same operations for the columnAmount and
169 				.addFooterVariable(columnAmount,DJCalculation.SUM) // columnaQuantity columns
170 				.addFooterVariable(columnaQuantity,	DJCalculation.SUM)
171 				.build();
172 
173 		drb.addColumn(columnState);
174 		drb.addColumn(columnBranch);
175 		drb.addColumn(columnaProductLine);
176 //		drb.addColumn(columnaItem);
177 //		drb.addColumn(columnCode);
178 		drb.addColumn(columnaQuantity);
179 		drb.addColumn(columnAmount);
180 		
181 		drb.addGlobalFooterVariable(columnAmount, DJCalculation.SUM, headerVariables, null);
182 		drb.addGlobalFooterVariable(columnaQuantity, DJCalculation.SUM, headerVariables, null);
183 
184 		drb.addGroup(g1); // add group g1
185 //		drb.addGroup(g2); // add group g2
186 
187 		drb.setUseFullPageWidth(true);
188 		drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_SLASH_Y, AutoText.POSITION_FOOTER, AutoText.ALIGNMENT_RIGHT);
189 
190 		DynamicReport dr = drb.build();
191 		return dr;
192 	}
193 
194 	public static void main(String[] args) throws Exception {
195 		GroupLabelTest1 test = new GroupLabelTest1();
196 		test.testReport();
197 		test.exportToJRXML();
198 		JasperViewer.viewReport(test.jp);
199 		JasperDesignViewer.viewReportDesign(test.jr);
200 	}
201 
202 }