KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > AlterDesignApp


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 import java.awt.Color JavaDoc;
29 import java.io.File JavaDoc;
30
31 import net.sf.jasperreports.engine.JRDataSource;
32 import net.sf.jasperreports.engine.JRException;
33 import net.sf.jasperreports.engine.JRRectangle;
34 import net.sf.jasperreports.engine.JRStyle;
35 import net.sf.jasperreports.engine.JasperExportManager;
36 import net.sf.jasperreports.engine.JasperFillManager;
37 import net.sf.jasperreports.engine.JasperPrint;
38 import net.sf.jasperreports.engine.JasperPrintManager;
39 import net.sf.jasperreports.engine.JasperReport;
40 import net.sf.jasperreports.engine.util.JRLoader;
41 import net.sf.jasperreports.engine.util.JRSaver;
42
43
44 /**
45  * @author Teodor Danciu (teodord@users.sourceforge.net)
46  * @version $Id: AlterDesignApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
47  */

48 public class AlterDesignApp
49 {
50
51
52     /**
53      *
54      */

55     private static final String JavaDoc TASK_FILL = "fill";
56     private static final String JavaDoc TASK_PRINT = "print";
57     private static final String JavaDoc TASK_PDF = "pdf";
58     
59     
60     /**
61      *
62      */

63     public static void main(String JavaDoc[] args)
64     {
65         String JavaDoc fileName = null;
66         String JavaDoc taskName = null;
67
68         if(args.length == 0)
69         {
70             usage();
71             return;
72         }
73                 
74         int k = 0;
75         while ( args.length > k )
76         {
77             if ( args[k].startsWith("-T") )
78                 taskName = args[k].substring(2);
79             if ( args[k].startsWith("-F") )
80                 fileName = args[k].substring(2);
81             
82             k++;
83         }
84
85         try
86         {
87             long start = System.currentTimeMillis();
88             if (TASK_FILL.equals(taskName))
89             {
90                 File JavaDoc sourceFile = new File JavaDoc(fileName);
91                 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
92                 
93                 JRRectangle rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("first.rectangle");
94                 rectangle.setForecolor(new Color JavaDoc((int)(16000000 * Math.random())));
95                 rectangle.setBackcolor(new Color JavaDoc((int)(16000000 * Math.random())));
96
97                 rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("second.rectangle");
98                 rectangle.setForecolor(new Color JavaDoc((int)(16000000 * Math.random())));
99                 rectangle.setBackcolor(new Color JavaDoc((int)(16000000 * Math.random())));
100
101                 rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("third.rectangle");
102                 rectangle.setForecolor(new Color JavaDoc((int)(16000000 * Math.random())));
103                 rectangle.setBackcolor(new Color JavaDoc((int)(16000000 * Math.random())));
104
105                 JRStyle style = jasperReport.getStyles()[0];
106                 style.setFontSize(16);
107                 style.setItalic(true);
108
109                 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, (JRDataSource)null);
110                 
111                 File JavaDoc destFile = new File JavaDoc(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
112                 JRSaver.saveObject(jasperPrint, destFile);
113                 
114                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
115                 System.exit(0);
116             }
117             else if (TASK_PRINT.equals(taskName))
118             {
119                 JasperPrintManager.printReport(fileName, true);
120                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
121                 System.exit(0);
122             }
123             else if (TASK_PDF.equals(taskName))
124             {
125                 JasperExportManager.exportReportToPdfFile(fileName);
126                 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start));
127                 System.exit(0);
128             }
129             else
130             {
131                 usage();
132                 System.exit(0);
133             }
134         }
135         catch (JRException e)
136         {
137             e.printStackTrace();
138             System.exit(1);
139         }
140         catch (Exception JavaDoc e)
141         {
142             e.printStackTrace();
143             System.exit(1);
144         }
145     }
146
147
148     /**
149      *
150      */

151     private static void usage()
152     {
153         System.out.println( "AlterDesignApp usage:" );
154         System.out.println( "\tjava AlterDesignApp -Ttask -Ffile" );
155         System.out.println( "\tTasks : fill | print | pdf" );
156     }
157
158
159 }
160
Popular Tags