View Full Version : Page Headers/ Functions

07-03-2002, 10:29 AM

I'm very new to JFreeReport and I would appreciate your answers to a couple of potentially stupid questions.

1) We would like to print our reports in duplex with a header only appearing at the top of every other page i.e on the front. Is there a simple way of doing this?

2) I'm pretty confused about Functions, is the return from them always that acheived by the getValue method and is it always a simple integer or string? Could you condition a whole block of report output on the outcome of a function? What methods get called?

Also how does the FontChangeFunction get invoked in report4 of the JFreeReportDemo get invoked it appears to be defined in the report4.xml but I can't see where it is named in the main body of the report definition.

Thank you for any illumination


07-03-2002, 04:22 PM

1) No, currently not. You may trigger all elements of the page header/footer invisible, but there is currently no easy way of disabling the printing of bands. But this Idea is good and is added to the ToDo list.

2) The functions get calculated once, but the value may be needed more than once and can be potentially queried everywhere in the process of printing the elements, so a function returns its last calculated value on getValue().

The function is called when the report processing proceeds using the functions defined in the com.jrefinery.report.function.Function interface.
The called functions are originally defined in the ReportListener interface (package com.jrefinery.report.event), Function extends the ReportListener interface.

What kind of Value you return is up to you, we place no constraints, as long as the target element is able to handle the value. You cannot return bands or elements, the returnvalue is interpreted/displayed by an element itself.

The returnvalue of the function must be understandable to the target element. JFreeReport supports Number, java.util.Date, String, java.awt.Shape (and in the CVS java.awt.Image). If the function returns objects not suitable for the element, you can add filters to make the object suitable.

The return value of the function can be filtered (converted) to fit the needs of your element, filters are defined in the packaged com.jrefinery.report.filter.
You can use custom filters to make any object printable:

Lets see an example for filters:
Assume you have a data object "Customer", and you want to print the customers address in an multilined text field:

The data object for customer:
// dont care about style :)
class Customer {
public String name;
public String street;
public String town;

class CustomerAddressFilter implements DataFilter {
... // implement getDataSource/setDataSource

public Object getValue ()
DataSource ds = getDataSource();
if (ds == null)
return getNullValue();
Object o = ds.getValue();
if (o == null) return getNullValue();
if (o instanceof Customer)
Customer c = (Customer) o;
StringBuffer b = new StringBuffer ();
b.append (c.name);
b.append ("\n");
b.append (c.street);
b.append ("\n");
b.append (c.town);
b.append ("\n");
return b.toString();
return getNullValue()

The CustomerAddressFilter will accept Customer objects and will transform them into an AddressString. And while none of JFreeReports default Elements does understand how to print customers, using this filter, all TextElements will accept Customer-Objects and print the customers address.

3) The font change function is defined in the function tag:


<function name="fontmod" class="com.jrefinery.report.demo.FontChangeFunction">
<property name="element">SampleElement</property>

and referenced in the ItemBand:

<items height="30" fontname="Monospaced" fontstyle="plain" fontsize="10">
<string-field x="0" y="0" width="40%" height="50%" alignment="left" fieldname="fontname" />
<string-field name="SampleElement" x="10%" y="50%" width="90%" height="50%" alignment="left" fieldname="sample"/>

The element with the name "SampleElement" is referenced by the functions property with the name "element", the function searches an element with this name in the ItemBand and will alter the font of the element.

Have more fun,
said Thomas