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.HorizontalAlign;
41  import ar.com.fdvs.dj.domain.constants.Transparency;
42  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
43  
44  public class FixedColumnWidhTest extends BaseDjReportTest {
45  
46  	public DynamicReport buildReport() throws Exception {
47  
48  		Style detailStyle = new Style();
49  			detailStyle.setBorder(Border.THIN());detailStyle.setBorderColor(Color.BLACK);detailStyle.setStretchWithOverflow(false);
50  //			detailStyle.setTransparency(Transparency.OPAQUE);
51  		Style headerStyle = new Style(); headerStyle.setBackgroundColor(new Color(230,230,230));headerStyle.setTransparency(Transparency.OPAQUE);
52  
53  		Style titleStyle = new Style();
54  		Style subtitleStyle = new Style();
55  		Style amountStyle = new Style(); amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
56  
57  		/**
58  		 * Creates the DynamicReportBuilder and sets the basic options for
59  		 * the report
60  		 */
61  		DynamicReportBuilder drb = new DynamicReportBuilder();
62  		drb.setTitle("November " + getYear() +" sales report")					//defines the title of the report
63  			.setSubtitle("The items in this report correspond "
64  					+"to the main products: DVDs, Books, Foods and Magazines")
65  			.setDetailHeight(17)						//defines the height for each record of the report
66  			.setMargins(30, 20, 30, 15)							//define the margin space for each side (top, bottom, left and right)
67  			.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
68  			.setColumnsPerPage(1);						//defines columns per page (like in the telephone guide)
69  
70  		/**
71  		 * Note that we still didn't call the build() method
72  		 */
73  
74  		/**
75  		 * Column definitions. We use a new ColumnBuilder instance for each
76  		 * column, the ColumnBuilder.getNew() method returns a new instance
77  		 * of the builder
78  		 */
79  		AbstractColumn columnState = ColumnBuilder.getNew()		//creates a new instance of a ColumnBuilder
80  			.setColumnProperty("state", String.class.getName())			//defines the field of the data source that this column will show, also its type
81  			.setTitle("State")											//the title for the column
82  			.setWidth(85)												//the width of the column
83  			.build();													//builds and return a new AbstractColumn
84  
85  		//Create more columns
86  		AbstractColumn columnBranch = ColumnBuilder.getNew()
87  			.setColumnProperty("branch", String.class.getName())
88  			.setTitle("Branch").setWidth(85).setTruncateSuffix("...")
89  			.build();
90  
91  		AbstractColumn columnaProductLine = ColumnBuilder.getNew()
92  			.setColumnProperty("productLine", String.class.getName())
93  			.setTitle("Product Line").setWidth(250).setFixedWidth(true)
94  			.build();
95  
96  		AbstractColumn columnaItem = ColumnBuilder.getNew()
97  			.setColumnProperty("item", String.class.getName())
98  			.setTitle("Item").setWidth(85)
99  			.build();
100 
101 		AbstractColumn columnCode = ColumnBuilder.getNew()
102 			.setColumnProperty("id", Long.class.getName())
103 			.setTitle("ID").setWidth(40)
104 			.build();
105 
106 		AbstractColumn columnaCantidad = ColumnBuilder.getNew()
107 			.setColumnProperty("quantity", Long.class.getName())
108 			.setTitle("Quantity").setWidth(80)
109 			.build();
110 
111 		AbstractColumn columnAmount = ColumnBuilder.getNew()
112 			.setColumnProperty("amount", Float.class.getName())
113 			.setTitle("Amount").setWidth(90)
114 			.setFixedWidth(true) // <--- FIXED COLUMN WIDTH
115 			.setPattern("$ 0.00")		//defines a pattern to apply to the values swhown (uses TextFormat)
116 			.build();
117 
118 		/**
119 		 * We add the columns to the report (through the builder) in the order
120 		 * we want them to appear
121 		 */
122 		drb.addColumn(columnState);
123 		drb.addColumn(columnBranch);
124 		drb.addColumn(columnaProductLine);
125 		drb.addColumn(columnaItem);
126 		drb.addColumn(columnCode);
127 		drb.addColumn(columnaCantidad);
128 		drb.addColumn(columnAmount);
129 
130 		/**
131 		 * add some more options to the report (through the builder)
132 		 */
133 		drb.setUseFullPageWidth(true);	//we tell the report to use the full width of the page. this rezises
134 										//the columns width proportionally to meat the page width.
135 
136 
137 		DynamicReport dr = drb.build();	//Finally build the report!
138 
139 		//JRProperties.setProperty(JRTextElement.PROPERTY_TRUNCATE_SUFFIX, "...");
140 		
141 		return dr;
142 	}
143 
144 	public static void main(String[] args) throws Exception {
145 		FixedColumnWidhTest test = new FixedColumnWidhTest();
146 		test.testReport();
147 		JasperViewer.viewReport(test.jp);	//finally display the report report
148 	}
149 
150 }