PDA

View Full Version : Getting boxes around report entries



Anonymous
08-04-2002, 04:11 AM
Hi,

I'm trying to make a report where the entries are boxed, i.e in a grid. I'm looking for a solution that doesn't depend on the grid having rows of equal height. Here's a test program that almost does it - the only problem is that the last row is not boxed. The vertical lines are not drawn and I can't figure out why (the code doesn't draw the last horizontal line anyway, but I'll live with that). Anyone have any ideas?

Here's the code:

-----------

import com.jrefinery.report.*;
import com.jrefinery.report.preview.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import javax.swing.*;
import javax.swing.table.*;

import java.util.Comparator;
import java.util.Arrays;

public class BoxedReport {


TableModel model = new TestModel();


public static void main(String[] args) {
new BoxedReport();
}

public BoxedReport() {
JFreeReport rep = getReport();

rep.setData (model);

PreviewFrame frame1 = new PreviewFrame (rep);
frame1.pack ();
frame1.setVisible (true);
frame1.requestFocus ();

}



JFreeReport getReport() {
JFreeReport rep = new JFreeReport();

rep.setItemBand(makeItemBandFromTableModel(model));
rep.setGroups(makeGroupsFromTableModel(model));

return rep;
}


GroupList makeGroupsFromTableModel(TableModel model) {
GroupList list = new GroupList();
list.add(makeTableHeadingGroup(model));
return list;
}

Group makeTableHeadingGroup(TableModel model) {
Group group = new Group();
group.setName("Universal");

GroupHeader band = new GroupHeader();
band.setDefaultPaint(getDefaultFontColor());
Font df = getDefaultFont();
Font f = df.deriveFont(Font.BOLD,df.getSize()+2);
band.setDefaultFont(f);
band.setHeight(f.getSize()+8);
int offset = (int) ((band.getHeight()-f.getSize())/2);
band.addElement(
ItemFactory.createRectangleShapeElement(
"background",
Color.decode("#DFDFDF"),
new BasicStroke (0),
new Rectangle2D.Float (0,0,-100,-100)
)
);


int colCount = model.getColumnCount();
int colwidth = getUsableWidth()/colCount;
int xpc = 0;
for (int i = 0; i < colCount; i++) {
String colName = model.getColumnName(i);
band.addElement(
ItemFactory.createLabelElement(
"Code Element",
new Rectangle2D.Float(xpc+4,offset,xpc+colwidth-5,10),
null,
Element.LEFT,
null,
colName
)
);
xpc += colwidth;

}
band.addElement(
ItemFactory.createLineShapeElement(
"hline-head",
getDefaultFontColor(),
new BasicStroke(1),
new Line2D.Float(0,0, getUsableWidth(), 0)
)
);


group.setHeader(band);
return group;


}

ItemBand makeItemBandFromTableModel(TableModel model) {
ItemBand items = new ItemBand ();
items.setDefaultPaint(getDefaultFontColor());
Font f = getDefaultFont();
items.setDefaultFont(f);
int height = (int) f.getSize()+ 4;
items.setHeight(height);
int colCount = model.getColumnCount();
int colwidth = getUsableWidth()/colCount;
int xpc = 0;
for (int i = 0; i < colCount; i++) {
String colName = model.getColumnName(i);
items.addElement(
ItemFactory.createStringElement(
"Col Element" + i,
new Rectangle2D.Float(xpc+4,2,colwidth-5,f.getSize()),
null,
Element.LEFT,
f,
"",
colName
)
);
items.addElement(
ItemFactory.createLineShapeElement(
"line"+i,
getDefaultFontColor(),
new BasicStroke(1),
new Line2D.Float(xpc,0, xpc, -100)
)
);

xpc += colwidth;

}
items.addElement(
ItemFactory.createLineShapeElement(
"line"+colCount,
getDefaultFontColor(),
new BasicStroke(1),
new Line2D.Float(xpc-1,0, xpc-1, -100)
)
);
items.addElement(
ItemFactory.createLineShapeElement(
"hline",
getDefaultFontColor(),
new BasicStroke(1),
new Line2D.Float(0,0, getUsableWidth(), 0)
)
);


return items;
}

int getUsableWidth() {
return (int) (new PageFormat().getImageableWidth());
}

Font getDefaultFont() {
return new Font("SansSerif", Font.PLAIN, 12);
}

Color getDefaultFontColor() {
return Color.black;
}

}

class TestModel extends AbstractTableModel {

String[] cols = new String[]{"Col1", "Col2", "Col3"};

int rowCount = 9;

/**
* Returns the number of rows in the table model.
*/
public int getRowCount ()
{
return rowCount;
}

/**
* Returns the number of columns in the table model.
*/
public int getColumnCount ()
{
return cols.length;
}

/**
* Returns the class of the data in the specified column.
*/
public Class getColumnClass (int columnIndex)
{
return String.class;
}

/**
* Returns the name of the specified column.
*/
public String getColumnName (int columnIndex)
{
return cols[columnIndex];
}

/**
* Returns the data value at the specified row and column.
*/
public Object getValueAt (int row, int column)
{
return "Data: " + row + ":" + column;;
}


}

Anonymous
08-05-2002, 09:19 AM
I haven't got a complete solution for you, but I can at least see what is going wrong. When you create the line elements, you are using a height of -100. In the XML parsing there is some code that converts negative dimensions to percentages of the height or width...but that isn't happening here, because you are creating the elements directly. If you change the value to something positive, you will see the lines being drawn for every row (but no vertical lines in the header).

I like what you are trying to do. Perhaps it would make sense to modify the RectangleShapeElement so that it can draw a border...currently this is disabled by the override on the shouldDraw() method.

Regards,

DG.

Anonymous
08-08-2002, 09:42 AM
Hi,

the Rectangle-Element has 2 new properties: shouldDraw (xml attribute: boolean: "draw") and shouldFill (xml attribute: boolean: "fill") which control how the element is drawn.

There is no need to override the shouldDraw method anymore.

Have more fun,
said Thomas