Jump to content
We've recently updated our Privacy Statement, available here ×

thangalin

Members
  • Posts

    322
  • Joined

  • Last visited

Community Answers

  1. thangalin's post in Create Java archive of report templates and dependencies was marked as the answer   
    This answer makes the following assumptions:
    The class files and resource bundles are located in the "build" directory (should created by default when writing custom Java classes). The distribution directory for the Java archive will be named "dist". The images used by the reports are in an "images" directory and in SVG format. The report templates have been compiled and are in the main project folder. Now:
    In the project folder create a file named "build_dist.xml" Copy the following content into build_dist.xml: <?xml version="1.0"?><project name="DistributionLibrary" default="dist" basedir=".">  <description>Archives templates, bundles, class files.</description>   <property name="build" location="build" />  <property name="dist" location="dist" />   <target name="dist" description="Build distribution archive.">    <mkdir dir="${dist}" />     <jar destfile="${dist}/templates.jar">      <fileset dir="${build}" includes="**/*" />      <fileset dir="." includes="images/*.svg" />      <fileset dir="." includes="**/*.jasper"/>    </jar>  </target></project> Right-click on the project name. Select Properties. Select Builders. Click New. Select Ant Builder. Click OK to display the Edit Configuration dialog. Set Buildfile to: ${project_loc}/build_dist.xml Set Base Directory to: ${project_loc} Click the Targets tab. Click Set Targets beside "After a 'Clean'". Uncheck dist, if it is checked. Click OK. Click Set Targets beside "Manual Build". Check dist, if it is unchecked. Click OK. Click OK to close the Edit Configuration dialog. Click OK to close the Project properties dialog. The archive is now bulit automatically after each successful project build. Try it as follows:
    Select Project >> Clean. Click OK. In the Console panel you should see BUILD SUCCESSFUL.
    Once the build is successful, the dist directory will contain a file called templates.jar that contains an archive that can be used as a resource for reports (in a Java application).
  2. thangalin's post in Canonical step-by-step guide to global date format in Jaspersoft Studio 6.x was marked as the answer   
    The following seems to work: Click Window >> Prefereces Select Capabilities Check Development Click OK Next:
    Open the Project Explorer Right-click Project name Select Properties Select Java Build Path Click the Source tab Click Add Folder Select Create New Folder Set Folder Name to: src Click Finish Select src Click OK Set Default output folder: Project Name/build Click OK Create a report as usual (with a text field that uses a date, either a parameter or a field), then:
    Select the report in the Outline panel Open the Properties panel Set Format Factory Class to: com.company.reports.ReportFormatFactory Next, create some source code inside the "src" directory in a package (folder) named "com.company.reports". Paste the following into a file named "ReportFormatFactory.java" that is saved in that directory:
    import java.text.DateFormat;import java.util.Locale;import java.util.TimeZone; import net.sf.jasperreports.engine.util.DefaultFormatFactory; /** * Delegates creation of date and number formatters to JasperReports' default * formatters. This class ensures that dates are formatted consistently across * all reports. */public class ReportFormatFactory extends DefaultFormatFactory {  /**   * Returns a DateFormat instance that creates dates in YYYY/MM/dd format.   *   * @param pattern Unused.   * @param locale Passed to the DefaultFormatFactory instance.   * @param timezone Passed to the DefaultFormatFactory instance.   *   * @return An object that can format dates.   */  @Override  public DateFormat createDateFormat(    String pattern, Locale locale, TimeZone timezone ) {    return super.createDateFormat( "YYYY/MM/dd", locale, timezone );  }}When you run the report, the date should be formatted as YYYY/MM/dd.
    If you want to change the format (e.g., to "dd/MM/YYYY"), update the date format line in the source code and then restart Jaspersoft Studio (the classloader does not seem to reload the ReportFormatFactory class after modification).
    To avoid having to restart each time the date format changes, use a resource bundle:
    Create a new project folder called i18n​Right-click on the project name Select New >> Folder Set Folder name to i18n Click Finish Right-click on i18n Select New >> Other Expand Messages Editor Select ResourceBundle Click Next Set the name to ReportsLocale Add a Locale (e.g., en_US) Click Finish Add the i18n directory to the build process:
    Right-click i18n Select Build Path >> Configure Build Path Click Add Folder Check i18n Click OK Click OK again Change the createDateFormat method:
      @Override  public DateFormat createDateFormat(    String pattern, Locale locale, TimeZone timezone ) {    String dateFormat = DATE_FORMAT_DEFAULT;    try {   ResourceBundle rb = ResourceBundle.getBundle( "ReportsBundle" );     String df = rb.getString( DATE_FORMAT );       if( df != null ) {       dateFormat = df;     }  }  catch( Exception e ) {   // If the resource bundle isn't found, use the default date format.   // TODO: Pass this exception into a logger.  }     return super.createDateFormat( dateFormat, locale, timezone );  }And add these constants to the class definition (immediately after the publc class declaration, around line 15/16):
      private final static String DATE_FORMAT = "date.format";  private final static String DATE_FORMAT_DEFAULT = "YYYY/MM/dd"; Restart Jaspersoft Studio, then: Edit the ReportsLocale file Add a date.format property Set the property value to: dd/MM/YYYY Set the property value for all locales. When the report is run, the date should look like 29/02/1976, for example.
×
×
  • Create New...