iReport Ultimate Guide - Nimsoft Library

iReport Ultimate Guide - Nimsoft Library iReport Ultimate Guide - Nimsoft Library

13.07.2015 Views

iReport Ultimate GuideCode Example 11-7Sample personalized data sourceimport net.sf.jasperreports.engine.*;import java.io.*;public class JRFileSystemDataSource implements JRDataSource{File[] files = null;int index = -1;public JRFileSystemDataSource(String path){File dir = new File(path);if (dir.exists() && dir.isDirectory()){files = dir.listFiles();}}public JRFileSystemDataSource(){this(".");204

Data Sources and Query ExecutersCode Example 11-7Sample personalized data source, continued}public boolean next() throws JRException{index++;if (files != null && index < files.length){return true;}return false;}}public Object getFieldValue(JRField jrField) throws JRException{File f = files[index];if (f == null) return null;if (jrField.getName().equals("FILENAME")){return f.getName();}else if (jrField.getName().equals("IS_DIRECTORY")){return new Boolean(f.isDirectory());}else if (jrField.getName().equals("SIZE")){return new Long(f.length());}// Field not found...return null;}The getFieldValue method will return the requested file information. Your implementation does not use the informationregarding the return type expected by the caller of the method, but it assumes that the name has to be returned as a string, theflag IS_DIRECTORY as a Boolean object, and the file size as a Long object.In the next section, you will learn how to use your personalized data source in iReport and test it.11.6.12 Using a Personalized JRDataSource with iReportiReport provides support for almost all the data sources provided by UR, such as JRXmlDataSource,JRBeanArrayDataSource, and JRBeanCollectionDataSource.To use your personalized data sources, a special connection is provided. It is useful for employing whatever JRDataSourceyou want to use through some kind of factory class that provides an instance of that JRDataSource implementation. Thefactory is just a simple Java class useful to test your data source and to fill a report in iReport. The idea is the same as what youhave seen for the JavaBeans set data source—it is necessary to write a Java class that creates the data source through a static205

Data Sources and Query ExecutersCode Example 11-7Sample personalized data source, continued}public boolean next() throws JRException{index++;if (files != null && index < files.length){return true;}return false;}}public Object getFieldValue(JRField jrField) throws JRException{File f = files[index];if (f == null) return null;if (jrField.getName().equals("FILENAME")){return f.getName();}else if (jrField.getName().equals("IS_DIRECTORY")){return new Boolean(f.isDirectory());}else if (jrField.getName().equals("SIZE")){return new Long(f.length());}// Field not found...return null;}The getFieldValue method will return the requested file information. Your implementation does not use the informationregarding the return type expected by the caller of the method, but it assumes that the name has to be returned as a string, theflag IS_DIRECTORY as a Boolean object, and the file size as a Long object.In the next section, you will learn how to use your personalized data source in <strong>iReport</strong> and test it.11.6.12 Using a Personalized JRDataSource with <strong>iReport</strong><strong>iReport</strong> provides support for almost all the data sources provided by UR, such as JRXmlDataSource,JRBeanArrayDataSource, and JRBeanCollectionDataSource.To use your personalized data sources, a special connection is provided. It is useful for employing whatever JRDataSourceyou want to use through some kind of factory class that provides an instance of that JRDataSource implementation. Thefactory is just a simple Java class useful to test your data source and to fill a report in <strong>iReport</strong>. The idea is the same as what youhave seen for the JavaBeans set data source—it is necessary to write a Java class that creates the data source through a static205

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!