InetSoft Product How-To: Customizing a Report with Presenters

Presenter is a powerful concept that allows unlimited extensibility of InetSoft products for customizing reports by providing any user-defined rendering of any object value. It has a very simple set of methods, which are defined in the inetsoft.report.Presenter interface. See the API JavaDoc for the details.

The InetSoft package comes with two presenters that display a numeric value as a horizontal bar. In this example we build a slightly different bar presenter. Instead of a bar growing from left to right, we write a presenter to have bars growing from right to left.

We first define a few constructors, a default constructor and a constructor that takes a maximum value and color as parameters. The maximum value is used to calculate the size of the bar. The size is the proportion of the actual value to the maximum value.

public class RBarPresenter implements Presenter {


   public RBarPresenter() {
   }


   public RBarPresenter(double max, Color color) {
      this.max = max;
      this.color = color;
   }

Pick a default size for the bar presenter and allow the size to be changed by users:

public Dimension getPreferredSize(Object v) {

   return psize;
}


public void setPreferredSize(Dimension psize) {
   psize = new Dimension(psize);
}

We also need to define the isPresenterOf() method to check if a type of object can be presented by this presenter. This is used when a presenter is assigned to a table column to avoid using it on the wrong types of objects, if there is more than one type in the column.

public boolean isPresenterOf(Class type) {
   return Number.class.isAssignableFrom(type);
}

Last, we define the paint() method for actually painting the bar. We can calculate the width of the bar using the value passed into paint() and the maximum value and presenter area width. Then we align the bar to the right of the area and paint.

public void paint(Graphics g, Object v, int x, int y, int w, int h) {
   if(v != null && v instanceof Number) {
      Rectangle clip = g.getClipBounds();
      Color oc = g.getColor();
      g.setClip(x, y, w, h);
      double n = ((Number) v).doubleValue();
      g.setColor(color);
      int width = (int) (n * w / max);
      g.fillRect(x+w-width, y+2, width, h-4);
      g.setColor(oc);
      g.setClip(clip);
   }
}

Figure 10. Report using RbarPresenter (PresenterEx.java)

customized report

Customized Report Summary

We have discussed the API behind the table report element. We followed this with a description of the various table lens classes, which adapt data so that it is correctly formatted for the InetSoft table report element. This was followed by a description of the Transformer classes, which are used to project different views onto other table lens classes. We concluded by describing some advanced formatting capabilities such as object presenters, which display graphic representations of numeric data.

BI DemoRegister

Previous: Report Programming - Table Transformer