Andy, here's my trick how I did the template design. How does it differ from your template, could we merge it and commit it to spring RCP?
Code:
/**
* @author Geoffrey De Smet
*/
public class FramedJasperReportExportSupport implements MessageSourceAware {
private static final String PARAMETER_CONTENT_TITLE_ID = "CONTENT_TITLE_ID";
private static final String PARAMETER_CONTENT_INPUT_STREAM = "CONTENT_INPUT_STREAM";
private static final String PARAMETER_MESSAGE_SOURCE = "MESSAGE_SOURCE";
private MessageSource messageSource;
private Resource frameReport;
private Resource contentReport;
private String contentTitleId;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public void setFrameReport(Resource frameReport) {
this.frameReport = frameReport;
}
public void setContentReport(Resource contentReport) {
this.contentReport = contentReport;
}
public void setContentTitleId(String contentTitleId) {
this.contentTitleId = contentTitleId;
}
/**
* @TODO the use of MessageSource vs ResourceBundle isn't consistent...
*/
protected Map getParameterMap() {
Map parameterMap = new HashMap();
parameterMap.put(PARAMETER_CONTENT_TITLE_ID, contentTitleId);
Locale locale = Locale.getDefault();
parameterMap.put(JRParameter.REPORT_LOCALE, locale);
parameterMap.put(JRParameter.REPORT_RESOURCE_BUNDLE, new MessageSourceResourceBundle(messageSource, locale));
parameterMap.put(PARAMETER_MESSAGE_SOURCE, messageSource);
try {
parameterMap.put(PARAMETER_CONTENT_INPUT_STREAM, contentReport.getInputStream());
} catch (IOException e) {
throw new JasperReportExportException("Resource contentReport not found: " + contentReport, e);
}
return parameterMap;
}
public void printPreview(Collection collection) {
JasperViewer.viewReport(getJasperPrint(collection), false);
}
public JasperPrint getJasperPrint(Collection collection) {
return getJasperPrint(new JRBeanCollectionDataSource(collection));
}
public JasperPrint getJasperPrint(JRDataSource jrDataSource) {
Assert.notNull(frameReport);
Assert.notNull(contentReport);
InputStream templateReportIn = null;
try {
templateReportIn = frameReport.getInputStream();
} catch (IOException e) {
throw new JasperReportExportException("Resource frameReport not found: " + frameReport, e);
}
try {
return JasperFillManager.fillReport(templateReportIn, getParameterMap(), jrDataSource);
} catch (JRException e) {
throw new JasperReportExportException("Report generation failed", e);
}
}
[code]
public class FramedJasperReportScriplet extends JRDefaultScriptlet {
public String getContentTitle() throws JRScriptletException {
ResourceBundle resourceBundle = ((ResourceBundle) getParameterValue(JRParameter.REPORT_RESOURCE_BUND LE));
String contentTitleId = (String) getParameterValue("CONTENT_TITLE_ID");
String contentTitle = resourceBundle.getString(contentTitleId);
return contentTitle;
}
}
[code]
spring context:
Code:
<bean id="blubExportSupport"
class="ggg.jasperreports.FramedJasperReportExportSupport">
<property name="frameReport" value="classpath:/ggg/common/Report.jasper" />
<property name="contentReport"
value="classpath:/ggg/blub/BlubReport.jasper" />
<property name="contentTitleId" value="blubReport.label" />
</bean>
messages.properties
Code:
blubReport.label = Blub titel
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Report"
pageWidth="595" pageHeight="842" columnWidth="500"
leftMargin="48" rightMargin="47" topMargin="48" bottomMargin="48"
whenNoDataType="BlankPage"
scriptletClass="ggg.jasperreports.FramedJasperReportScriplet">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<import value="ggg.jasperreports.FramedJasperReportScriplet" />
<!-- Styles (keep in sync in ALL reports) -->
<style name="titleStyle"
mode="Transparent" isBlankWhenNull="false"
leftPadding="2" topPadding="2" rightPadding="2" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="20"
isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" />
<style name="heading1Style"
mode="Transparent" isBlankWhenNull="false"
leftPadding="0" topPadding="2" rightPadding="0" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="16"
isBold="true" isItalic="false" isUnderline="true" isStrikeThrough="false" />
<style name="heading2Style"
mode="Transparent" isBlankWhenNull="false"
leftPadding="0" topPadding="2" rightPadding="0" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="14"
isBold="true" isItalic="false" isUnderline="true" isStrikeThrough="false" />
<style name="heading3Style"
mode="Transparent" isBlankWhenNull="false"
leftPadding="0" topPadding="2" rightPadding="0" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="14"
isBold="false" isItalic="false" isUnderline="true" isStrikeThrough="false" />
<style name="heading4Style"
mode="Transparent" isBlankWhenNull="false"
leftPadding="0" topPadding="2" rightPadding="0" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="12"
isBold="false" isItalic="false" isUnderline="true" isStrikeThrough="false" />
<style name="labelStyle"
mode="Transparent" isBlankWhenNull="false"
leftPadding="2" topPadding="2" rightPadding="2" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="12"
isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" />
<style name="fieldStyle"
mode="Transparent" isBlankWhenNull="true"
leftPadding="2" topPadding="2" rightPadding="2" bottomPadding="2"
fontName="Times New Roman" pdfFontName="Times-Roman" fontSize="12"
isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />
<style name="noteStyle"
mode="Transparent" isBlankWhenNull="false"
leftPadding="2" topPadding="2" rightPadding="2" bottomPadding="2"
fontName="Arial" pdfFontName="Helvetica" fontSize="10"
isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />
<style name="footerStyle"
mode="Transparent" isBlankWhenNull="false"
leftPadding="0" topPadding="1" rightPadding="0" bottomPadding="0"
fontName="Arial" pdfFontName="Helvetica" fontSize="10"
isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />
<!-- ### -->
<parameter name="CONTENT_TITLE_ID" isForPrompting="false" class="java.lang.String" />
<parameter name="CONTENT_INPUT_STREAM" isForPrompting="false" class="java.io.InputStream" />
<!-- Set by the scriplet -->
<variable name="CONTENT_TITLE" class="java.lang.String" />
<background>
</background>
<title>
<band height="40" isSplitAllowed="false">
<textField isBlankWhenNull="false" isStretchWithOverflow="true">
<reportElement style="titleStyle" x="100" y="1" width="400" height="0"
positionType="Float" isRemoveLineWhenBlank="true" />
<!-- $R{$P{CONTENT_TITLE_ID}} doesn't work -->
<textFieldExpression>
<![CDATA[((FramedJasperReportScriplet)$P{REPORT_SCRIPTLET}).getContentTitle()]]>
</textFieldExpression>
</textField>
<!-- ... -->
</band>
</title>
<pageHeader>
</pageHeader>
<columnHeader>
</columnHeader>
<detail>
<band height="402" isSplitAllowed="true">
<subreport isUsingCache="true">
<reportElement mode="Opaque" x="0" y="0" width="500" height="360"
stretchType="NoStretch" positionType="FixRelativeToTop"
isPrintWhenDetailOverflows="true" />
<parametersMapExpression><![CDATA[new HashMap($P{REPORT_PARAMETERS_MAP})]]></parametersMapExpression>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
<subreportExpression class="java.io.InputStream">
<![CDATA[$P{CONTENT_INPUT_STREAM}]]></subreportExpression>
</subreport>
</band>
</detail>
<columnFooter>
</columnFooter>
<pageFooter>
<!-- ... -->
</pageFooter>
<summary>
</summary>
</jasperReport>