Tuesday, April 17, 2012

Creating and using external Jasper Report styles with JasperWave


Creating and using external Jasper Report styles with JasperWave

The JasperWave team just released version 0.9.5 of their JasperReports Designer. Among the main new features are External Style Templates, JavaBean data source and custom Java classes.

Having worked in the past a lot with external styles I can tell a story or two about how challenging it sometimes was to go down this route. The “challenging” bit only refers to actually using external and internal styles with the designer GUI.
It is needless to say that using external style sheets is the most effective way to go in large scale projects. Creating these external style sheets is fairly easy, but the problem arises when your report designer GUI does not offer a nice integration. The JasperWave team identified this as a shortcoming in current designers and hence had their own take at implementing this feature. Now let’s have a look if they succeeded in their goal:

Download the latest version of the JasperWave report designer from here. You have the option to use a standalone client (this is the easiest setup) or to use the Eclipse plugin. If you go down the Eclipse route, make sure that you first install GEF (as highlighted on the downloads page).

Once installed successfully, fire up JasperWave Report Designer. In case you are using the standalone version, you will find the Browser in the bottom left hand corner. Navigate to a convenient folder to store our report. Right click on this folder and choose New Report:

Specify a filename and click on Finish. Your screen should now look like this:
Now let’s create two new external style sheets for the purpose of this tutorial. Click on the Plus button in the Style Templates area in the Properties panel (right hand side):

In quotation marks write table_external_styles.jrtx:
Click OK.
In the same fashion create another external stylesheet called title_external_styles.jrtx. Remember to enclose this in quotation marks!

Time to save our report! Click on the Save button (or alternatively depending on OS CTRL+S or CMD+S)

In the Browser right click on your folder and choose Refresh. The folder content should now look like this:

As you can see, JasperWave created the external style sheets for us.

Now it is time to introduce you to our new best friend, the Styles Manager. This is the one place where you can manage all your styles, be it external or internal styles, one or many external style sheets. This is extremely convenient, as it means you don’t have to wade through many different files to get an overview of your styles.
You can access the styles manager either by clicking on the style button in the top tool bar or if you click on a report element (like a Text Field) you can find the style button next to each property in the properties panel. The latter enables a very convenient access to a specific style.

So let’s create some styles:
  1. Click on the Style button in the top tool bar:
  2. In the Styles Manager, click on New Style:
  3. Name the style report_title
  4. Next we can define where we want the new style to be stored. In the Location pull down menu choose the external style sheet we created earlier on: title_external_styles.jrtx. Note: Embedded means that the style will be stored within the report file (internal style).
  5. Set a few properties for this style like Font, Font size, Bold etc. Note how the greyed out circular arrow button turns blue once you set the style. This is a very good indicator as you can easily spot which styles have been set and which ones not! If you ever want to set the Default value for a particular style, just click this button!
    Also watch the
    Style preview as you add properties!
  6. Next create a new style called table_header_string. Set the Location to table_external_styles and set a few properties for this style like Font, Font size, Bold etc.
  7. Finally create an internal style called custom_subtitle. Set the Location to Embedded and set a few properties for this style like Font, Font size, Bold etc.
  8. If you take a look now at the styles listing, you will see that each style is suffixed by its location in case of an external style:

Now let’s quickly have a look on how to use these styles. Add 2 static text elements to the report title section and one static text element to the column header. Populate them with some text.

Mark the first static text element and expand the Style section in the properties panel. Choose report_title:

Assign the custom_subtitle style to the next static text element and finally the table_header_string style to the last static text element. Your report should now look something like this:
As you can see, the styles are instantly applied in the report design area.

Now we realize that we actually want the table heading to have a different foreground color. I show you now another way to access the Styles Manager: Mark the table header static text element and click on the Style button in the Style properties section:

You will see that in the Styles Manager this style is automatically selected. Change now the foreground color to green.

In the course of this short tutorial you learnt how easy it is with JasperWave to use external as well as internal styles. I have to say that this implementation of styles in a Jasper Reports Designer is the best I have seen so far! I do sincerely hope that this project gets the community support it deserves! You can find a more detailed documentation on using styles with Jasperwave on their website.