3.7 Presenters

A presenter is an object that draws a graphical representation of a numerical value. (See Cell Presenter in Report Design.) Typically, you assign a presenter to a particular column of a table. However, you can also associate a presenter with an object type, in which case it applies to all objects of that type in a document.

The presenter is a powerful tool that allows you to broadly extend Style Intelligence by rendering any user-defined object value. It has a very simple set of methods, which are defined in the inetsoft.report.Presenter interface. See the API JavaDocs for full details.

Walkthrough

The Style Intelligence package comes with two sample presenters that display a numeric value as a horizontal bar. In the following example you will build a slightly different bar presenter. Instead of a bar growing from left to right, you will create a presenter with bars growing from right to left.

You will 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.

RBarPresenter.java is found in the {InetSoftInstallation}/examples/docExamples/core directory.

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);

}

You 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, define the paint() method for actually painting the bar. You can calculate the width of the bar using the value passed into paint() and the maximum value and presenter area width. Then 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 3. Report using RbarPresenter (PresenterEx.java)

 

<< 3.6.2 User-Defined Actions © 1996-2013 InetSoft Technology Corporation (v11.4) 3.8 Exporting Reports Programmatically >>