Creating UDOs
This topic introduces the objects and interfaces for creating UDOs. It also presents two examples to illustrate how you can create UDOs for specific purposes.
This topic contains the following sections:
- Objects and Interfaces for Creating UDOs
- Example 1: Accessing a Record for a UDO and Registering the UDO with the Report System
- Example 2: Making a Group Based on a UDO
Objects and Interfaces for Creating UDOs
The following objects and interfaces are necessary for creating a UDO. Refer to the corresponding class or interface in the Report Java API Documentation.
- Jet.report.JRObjectTemplate
This is an object that Report provides. You can use it to derive a UDO. JRObjectTemplate contains several predefined properties of the standard Report object, including: X, Y, Height, and Width. You can add more properties that Report supports to your own UDO, which you can view and modify in the Report Inspector at design time.You can only add Report properties or properties inherited from them, such as JetNumber, JetColor, JetString, and JetEnumeration. For more information, see the jet.controls package in the Report Java API Documentation.
- jet.datastream.JRVisiableResult
This is another object Report provides. You need to define an object inherited from this object, so that you can specify the methods for saving and restoring the UDO object. JRVisiableResult provides methods for saving and restoring UDOs. - jet.datastream.JRObjectResult
If the UDO you define is not used for displaying, that is, it does not appear inside the report on the screen or on paper, then it can be inherited from the JRObjectResult object. - jet.udo.JRObjectResultCreator
This is an interface. It takes Report's Report Record as input parameters and produces the UDO's JRObjectResult. A definition of the Report Record is in the jet.connect package. - jet.udo.JRObjectRender
This is another interface. It provides a method used for painting the UDO to the report. The report can then be shown on the screen or printed on a printer. - jet.udo.JRObjectEditor
This is an optional interface. If specified, Report Engine uses it to handle interactive events (key events, mouse events, paint, and so on) at design time. If you do not implement this interface, Report Engine applies the default one. - jet.udo.JRGroupListener
This interface is used when you define a UDO with a value that is calculated based on a group of data.
Then, to create a UDO manually with the objects and interfaces, take the following steps:
- Inherit from the JROblectTemplate class to create a template file.
- Inherit from the JRVisiableResult or JRObjectResult class to create a result file.
- Implement JRObjectResultCreator to create a result creator file.
- Implement the JRObjectRender interface to create a result render file. If you want to display a UDO in the design area, implement the JRObjectEditor interface. If your UDO is a group level object, implement the JRGroupListener interface.
- Modify udo.ini in
<install_root>\lib
by adding the following:Begin
name=MyDbField
template=myudo.MyDbFld
resultobject=myudo.MyDbFldRst
resultcreator=myudo.MyDbFldCreator
resultviewer=myudo.MyDbFldRender
End - Compile the Java files, add the classes to the ADDCLASSPATH variable of setenv.bat in
<install_root>\bin
.
If you want to use class files for the UDO that you created in previous versions, you should recompile the source files.
Example 1: Accessing a Record for a UDO and Registering the UDO with the Report System
This example describes how to access a record for your UDO and how to register your UDO with the report system.
- A UDO needs to implement at least four classes from Report. The four classes are for template, result, creator, and render files respectively.
- MyDbFld.java
//Template
package myudo;
import jet.report.*;
import jet.controls.*;
/**
* This class extends form JRObjectTemplate, add two properties "ColumnName" and
* "TextColor".
*/
public class MyDbFld extends JRObjectTemplate {
/**
* This property is for the column name of MyDbFld.
*/
public JetColumnName columnName = new JetColumnName(this, "ColumnName");
/**
* This property is for the background color of MyDbFld..
*/
public JetColor backColor = new JetColor(this, "TextColor", null, true);
public MyDbFld() {
super();
columnName.setEditable(true);
// set the default size.
set("Width", 40); // before build575 should be width.set(40);
set("Height", 20); // before build575 should be height.set(20);
// add the property to group then they can displayed in ReportInspector.
addPropertyToGroup("ColumnName", "Others");
addPropertyToGroup("TextColor", "Color");
}
/**
* Return the instance name prefix. >
*/
public String getInstancePrefix() {
return "MyDbField";
}
} - MyDbFldRst.java
//Result
package myudo;
import jet.connect.Record;
import jet.connect.DbValue;
import jet.util.*;
import jet.datastream.*;
public class MyDbFldRst extends JRVisiableResult {
public MyDbFldRst() {
}
// retrieve value from column.
DbValue getValue() {
// get the column name.
String colName = (String) getPropertyByName("ColumnName").getObject();
// get the report record.
Record record = getRecord();
// return the column value.
return record.getCell(colName);
}
// make the text will be displayed.
String getText() {
String sRet;
DbValue value = getValue();
if (value != null && !value.isNull()) {
sRet = value.toString();
} else {
sRet = "NULL";
}
return sRet;
}
} - MyDbFldCreator.java
//Creator
package myudo;
import java.awt.*;
import java.util.*;
import jet.report.*;
import jet.controls.*;
import jet.connect.*;
import jet.udo.*;
import jet.datastream.*;
import guitools.Painter;
import jet.util.*;
/**
* Implements JRObjectResultCreator for creating JRObjectResult in Report* Engine Bean.
*/
public class MyDbFldCreator implements JRObjectResultCreator {
public MyDbFldCreator() {
}
public JRObjectResult createJRObjectResult(JRObjectTemplate rptobj, Record record) {
MyDbFld rpt = (MyDbFld) rptobj;
MyDbFldRst dsField = new MyDbFldRst();
dsField.setTemplate(rpt);
return dsField;
}
} - MyDbFldRender.java
//Render
package myudo;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import jet.datastream.*;
import jet.connect.DbValue;
import jet.connect.Record;
import jet.udo.*;
public class MyDbFldRender extends Component implements JRObjectRender {
String text = null;
Color color;
Color background;
/**
* The default constructor.
*/
public MyDbFldRender() {
}
// retrieve property and data from JRObjectResult.
public void setProperty(jet.util.PropertySetable dsPropSet) {
text = ((MyDbFldRst) dsPropSet).getText();
color = (Color) dsPropSet.getPropertyByName("TextColor").getObject();
background = (Color) dsPropSet.getPropertyByName("Background").getObject();
setBounds(((MyDbFldRst) dsPropSet).getBounds());
}
/**
* Paint the text.
*/
public void paint(Graphics g) {
Dimension dim = getSize();
if (background != null) {
g.setColor(background);
g.fillRect(0, 0, dim.width, dim.height);
}
if (text != null) {
if (color == null)
color = Color.black;
g.setColor(color);
g.drawString(text, 10, 10);
}
}
}Since the compiling process of Designer differs with that of Server, when you run UDOs in these two applications, you should pay attention to some minor differences. For example, to get width and height, in Designer, you should use:
w = guitools.toolkit.Unit.convertUnitToPixel(((Integer)propertySetable.getPropertyByName ("Width").getObject()).intValue());
h = guitools.toolkit.Unit.convertUnitToPixel(((Integer)propertySetable.getPropertyByName("Height").getObject()).intValue());While on Server, you should use the following instead:
JRObjectResult obj = (JRObjectResult)propertySetable;
w = guitools.toolkit.Unit.convertUnitToPixel(obj.getTemplate().getWidth(obj));
h = guitools.toolkit.Unit.convertUnitToPixel(obj.getTemplate().getHeight(obj));
- MyDbFld.java
- Compile the Java files. To compile these four Java files, you should add report.jar and JREngine.jar with their path into the class path (make sure that the path of the file JREngine.jar is before that of the file report.jar). For example, use the following command:
Javac -classpath "C:\LogiReport\Designer\lib\JRengine.jar;C:\LogiReport\Designer\lib\report.jar;C:\test "MyDbFld.java
Here, it is assumed that you have installed Designer to
C:\LogiReport\Designer
. The Java files for the example are inC:\test\myudo
. - Modify udo.ini in the
<install_root>\lib
directory by appending the four classes.Begin
name=MyDbField
template=myudo.MyDbFld
resultobject=myudo.MyDbFldRst
resultcreator=myudo.MyDbFldCreator
resultviewer=myudo.MyDbFldRender
End - Edit setenv.bat in
<install_root>\bin
by appending the path of the four classes to the batch file's ADDCLASSPATH variable. Assume that the four classes are located inD:\test\myudo
.set ADDCLASSPATH=%JAVAHOME%\lib\tools.jar;D:\test;
- Start Designer.
- Open a page report that contains a banded object using a query resource.
- Select a banded panel.
- Select Insert > UDO. You can now find a new UDO object MyDbFld in the drop-down list of the Insert UDO dialog box.
Example 2: Making a Group Based on a UDO
This example describes how to make a group based on a UDO. This sample is similar to a summary field. This example includes the implementation of four classes and shares a class with Example 1.
- Create the template, result, creator, and render files.
- MySumFld.java
//Template
package myudo;
import jet.report.*;
import jet.controls.*;
/**
* This class extends from JRObjectTemplate, add two properties "ColumnName" and
* "TextColor".
*/
public class MySumFld extends MyDbFld {
public MySumFld() {
super();
}
/**
* Return the instance name prefix.
*/
public String getInstancePrefix() {
return "MySumField";
}
public boolean isGroupListener() {
return true;
}
} - MySumFldRst.java
//Result
package myudo;
import jet.JRStopEngineException;
import jet.connect.Record;
import jet.connect.DbValue;
import jet.util.*;
import jet.datastream.*;
import java.io.*;
public class MySumFldRst extends MyDbFldRst {
double value;
public MySumFldRst() {
}
// make the text will be displayed.
String getText() {
return "" + value;
}
/**
* Read the data from DataInput.
* UDO can override this method to restore its own data.
* @throws JRStopEngineException
* */
protected void readProperties(DataInput in, DSDataStreamable ds)
throws IOException, JRStopEngineException {
super.readProperties(in, ds);
value = in.readDouble();
}
/**
* Write the data to DataInput.
* UDO can override this method to save its own data.
* */
protected void writeProperties(DataOutput out) throws IOException {
super.writeProperties(out);
out.writeDouble(value);
}
} - MySumFldCreator.java
//Creator
package myudo;
import java.awt.*;
import java.util.*;
import jet.report.*;
import jet.controls.*;
import jet.connect.*;
import jet.udo.*;
import jet.datastream.*;
import guitools.Painter;
import jet.util.*;
/**
* * Implements JRObjectResultCreator for creating JRObjectResult in Report*
* Engine Bean.
*/
public class MySumFldCreator implements JRObjectResultCreator, JRGroupListener {
// This interface must implement by JRObjectCreator.
MySumFld rpt;
MySumFldRst dsField;
double value;
public MySumFldCreator() {
}
public void setTemplate(JRObjectTemplate rptobj) {
rpt = (MySumFld) rptobj;
}
public JRObjectResult createJRObjectResult(JRObjectTemplate rptobj,Record record) {
rpt = (MySumFld) rptobj;
MySumFldRst dsField = new MySumFldRst();
dsField.setTemplate(rpt);
dsField.value = value;
return dsField;
}
/** * This method will be called before the first record. */
public void prepareFetchRecords() {
value = 0.0;
}
/** * This method will be called after the last record. */
public void finishFetchRecords() {
}
/** * This method will be called each record. */
public void fetchNewRecord(Record record) {
String colName = (String) rpt.getPropertyByName("ColumnName").getObject();
// return the column value.
DbNumber num = (DbNumber) record.getCell(colName);
if (!num.isNull()) {
value += num.intValue();
}
}
} - MyDbFldRender.java (See Example 1 for more information.)
- MySumFld.java
- Compile the four Java files in the same way as Example 1.
- Modify udo.ini in
<install_root>\lib\
by appending the four classes.Begin
name=MySumFld
template=myudo.MySumFld
resultobject=myudo.MySumFldRst
resultcreator=myudo.MySumFldCreator
resultviewer=myudo.MyDbFldRender
End - Edit setenv.bat in
<install_root>\bin
by appending the path of the four classes to the batch file's ADDCLASSPATH variable. Assume that the four classes are located inD:\test\myudo
.set ADDCLASSPATH=%JAVAHOME%\lib\tools.jar;D:\test;
- Start Designer.
- Open a page report that contains a banded object created using a query resource.
- Select a banded panel.
- Navigate to Insert > UDO. You can now find a new UDO object MySumFld in the drop-down list of the Insert UDO dialog box.