View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.table.view;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.extremecomponents.table.bean.Export;
24  import org.extremecomponents.table.model.TableModel;
25  import org.extremecomponents.table.model.TableModelUtils;
26  import org.extremecomponents.table.model.ViewHandler;
27  
28  
29  /***
30   * @author jeff johnston
31   */
32  public class PdfView implements View
33  {
34  	private static Log logger = LogFactory.getLog(PdfView.class);
35  	private StringBuffer xlsfo = new StringBuffer();
36  	
37  	public PdfView(){}
38  	
39  	public void beforeBody(TableModel model) 
40  	{
41  		xlsfo.append(startRoot());
42  		xlsfo.append(regionBefore(model));
43  		xlsfo.append(regionAfter());
44  		xlsfo.append(columnDefinitions(model));
45  		xlsfo.append(header(model));
46  		xlsfo.append(" <fo:table-body> ");
47  	}
48  
49  	public void body(TableModel model, String value, boolean isFirstColumn, boolean isLastColumn) 
50  	{
51  		if (isFirstColumn)
52  		{
53  			xlsfo.append(" <fo:table-row> ");
54  		}
55  		
56  		xlsfo.append(" <fo:table-cell border=\"solid silver .5px\" display-align=\"center\" padding=\"3pt\"> ");
57  		xlsfo.append(" <fo:block>" + value + "</fo:block> ");
58  		xlsfo.append(" </fo:table-cell> ");
59  
60  		if (isLastColumn)
61  		{
62  			xlsfo.append(" </fo:table-row> ");
63  		}
64  	}
65  	
66  	public Object afterBody(TableModel model) 
67  	{
68  		xlsfo.append(" </fo:table-body> ");
69  		xlsfo.append(endRoot());
70  		return xlsfo.toString();
71  	}
72  
73  	public String startRoot()
74  	{
75  		StringBuffer sb = new StringBuffer();
76  
77  		sb.append("<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">");
78  
79  		sb.append(" <fo:layout-master-set> ");
80  		sb.append(" <fo:simple-page-master master-name=\"simple\" ");
81  		sb.append(" page-height=\"8.5in\" ");
82  		sb.append(" page-width=\"11in\" ");
83  		sb.append(" margin-top=\".5in\" ");
84  		sb.append(" margin-bottom=\".25in\" ");
85  		sb.append(" margin-left=\".5in\" ");
86  		sb.append(" margin-right=\".5in\"> ");
87  		sb.append(" <fo:region-body margin-top=\".5in\" margin-bottom=\".25in\"/> ");
88  		sb.append(" <fo:region-before extent=\".5in\"/> ");
89  		sb.append(" <fo:region-after extent=\".25in\"/> ");
90  		sb.append(" </fo:simple-page-master> ");
91  		sb.append(" </fo:layout-master-set> ");
92  
93  		sb.append(" <fo:page-sequence master-reference=\"simple\" initial-page-number=\"1\"> ");
94  
95  		return sb.toString();
96  	}
97  
98  	public String regionBefore(TableModel model)
99  	{
100 		StringBuffer sb = new StringBuffer();
101 
102 		Export export =  model.getExportHandler().getCurrentExport();
103 		String headerBackgroundColor = export.getHeaderBackgroundColor();
104 		
105 		sb.append(" <fo:static-content flow-name=\"xsl-region-before\"> ");
106 
107 		//		sb.append(" <fo:block space-after.optimum=\"12pt\"> ");
108 		//		
109 		//		sb.append(" <fo:inline><fo:external-graphic src=\"url(http://localhost:8080/eXtremeTest/images/clear.gif)\"/></fo:inline> ");		
110 		//
111 		//		sb.append(" </fo:block> ");
112 
113 		sb.append(" <fo:block space-after.optimum=\"15pt\" color=\"" + headerBackgroundColor + "\" font-size=\"17pt\" font-family=\"'Arial'\">" + export.getTitle() + "</fo:block> ");
114 
115 		sb.append(" </fo:static-content> ");
116 
117 		return sb.toString();
118 	}
119 
120 	public String regionAfter()
121 	{
122 		StringBuffer sb = new StringBuffer();
123 
124 		sb.append(" <fo:static-content flow-name=\"xsl-region-after\" display-align=\"after\"> ");
125 
126 		//		sb.append(" <fo:block font-size=\"10pt\" space-after.optimum=\"5pt\"> ");
127 		//			
128 		//		sb.append(" this is the text that goes here ");
129 		//				
130 		//		sb.append(" </fo:block> ");
131 		sb.append(" <fo:block text-align=\"end\">Page <fo:page-number/></fo:block> ");
132 
133 		sb.append(" </fo:static-content> ");
134 
135 		return sb.toString();
136 	}
137 
138 	public String columnDefinitions(TableModel model)
139 	{
140 		StringBuffer sb = new StringBuffer();
141 
142 		sb.append(" <fo:flow flow-name=\"xsl-region-body\"> ");
143 
144 		sb.append(" <fo:block> ");
145 
146 		sb.append(" <fo:table font-size=\"10pt\"> ");
147 
148 		double columnCount = model.getColumnMetaData().columnCount();
149 
150 		double colwidth = 10 / columnCount;
151 
152 		for (int i = 1; i <= columnCount; i++)
153 		{
154 			sb.append(" <fo:table-column column-number=\"" + i + "\" column-width=\"" + colwidth + "in\"/> ");
155 		}
156 
157 		return sb.toString();
158 	}
159 
160 	public String header(TableModel model)
161 	{
162 		StringBuffer sb = new StringBuffer();
163 
164 		Export export = model.getExportHandler().getCurrentExport();
165 		String headerColor = export.getHeaderColor();
166 		String headerBackgroundColor = export.getHeaderBackgroundColor();
167 
168 		sb.append(" <fo:table-header background-color=\"" + headerBackgroundColor + "\" color=\"" + headerColor + "\"> ");
169 
170 		sb.append(" <fo:table-row> ");
171 		
172 		List header = TableModelUtils.getHeader(model, ViewHandler.VALUE);
173 
174 		for (Iterator iter = header.iterator(); iter.hasNext();)
175 		{
176 			sb.append(" <fo:table-cell border=\"solid silver .5px\" text-align=\"center\" display-align=\"center\" padding=\"3pt\"> ");
177 			sb.append(" <fo:block>" + (String) iter.next() + "</fo:block> ");
178 			sb.append(" </fo:table-cell> ");
179 		}
180 
181 		sb.append(" </fo:table-row> ");
182 
183 		sb.append(" </fo:table-header> ");
184 
185 		return sb.toString();
186 	}
187 
188 	public String endRoot()
189 	{
190 		StringBuffer sb = new StringBuffer();
191 
192 		sb.append(" </fo:table> ");
193 
194 		sb.append(" </fo:block> ");
195 
196 		sb.append(" </fo:flow> ");
197 
198 		sb.append(" </fo:page-sequence> ");
199 
200 		sb.append(" </fo:root> ");
201 
202 		return sb.toString();
203 	}
204 }