KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > view > JasperViewer


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
29 /*
30  * Contributors:
31  * Ryan Johnson - delscovich@users.sourceforge.net
32  * Carlton Moore - cmoore79@users.sourceforge.net
33  */

34 package net.sf.jasperreports.view;
35
36 import java.awt.BorderLayout JavaDoc;
37 import java.awt.Toolkit JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 import net.sf.jasperreports.engine.JRException;
42 import net.sf.jasperreports.engine.JasperPrint;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  * @version $Id: JasperViewer.java 1259 2006-05-15 16:45:29 +0300 (Mon, 15 May 2006) teodord $
48  */

49 public class JasperViewer extends javax.swing.JFrame JavaDoc
50 {
51
52
53     /**
54      *
55      */

56     private JRViewer viewer = null;
57
58     /**
59      *
60      */

61     private boolean isExitOnClose = true;
62     
63     
64     /** Creates new form JasperViewer */
65     public JasperViewer(
66         String JavaDoc sourceFile,
67         boolean isXMLFile
68         ) throws JRException
69     {
70         this(sourceFile, isXMLFile, true);
71     }
72
73
74     /** Creates new form JasperViewer */
75     public JasperViewer(
76         InputStream JavaDoc is,
77         boolean isXMLFile
78         ) throws JRException
79     {
80         this(is, isXMLFile, true);
81     }
82
83
84     /** Creates new form JasperViewer */
85     public JasperViewer(
86         JasperPrint jasperPrint
87         )
88     {
89         this(jasperPrint, true);
90     }
91
92
93     /** Creates new form JasperViewer */
94     public JasperViewer(
95         String JavaDoc sourceFile,
96         boolean isXMLFile,
97         boolean isExitOnClose
98         ) throws JRException
99     {
100         this(sourceFile, isXMLFile, isExitOnClose, null);
101     }
102
103
104     /** Creates new form JasperViewer */
105     public JasperViewer(
106         InputStream JavaDoc is,
107         boolean isXMLFile,
108         boolean isExitOnClose
109         ) throws JRException
110     {
111         this(is, isXMLFile, isExitOnClose, null);
112     }
113
114
115     /** Creates new form JasperViewer */
116     public JasperViewer(
117         JasperPrint jasperPrint,
118         boolean isExitOnClose
119         )
120     {
121         this(jasperPrint, isExitOnClose, null);
122     }
123
124
125     /** Creates new form JasperViewer */
126     public JasperViewer(
127         String JavaDoc sourceFile,
128         boolean isXMLFile,
129         boolean isExitOnClose,
130         Locale JavaDoc locale
131         ) throws JRException
132     {
133         if (locale != null)
134             setLocale(locale);
135
136         this.isExitOnClose = isExitOnClose;
137
138         initComponents();
139
140         this.viewer = new JRViewer(sourceFile, isXMLFile, locale);
141         this.pnlMain.add(this.viewer, BorderLayout.CENTER);
142     }
143
144
145     /** Creates new form JasperViewer */
146     public JasperViewer(
147         InputStream JavaDoc is,
148         boolean isXMLFile,
149         boolean isExitOnClose,
150         Locale JavaDoc locale
151         ) throws JRException
152     {
153         if (locale != null)
154             setLocale(locale);
155
156         this.isExitOnClose = isExitOnClose;
157
158         initComponents();
159
160         this.viewer = new JRViewer(is, isXMLFile, locale);
161         this.pnlMain.add(this.viewer, BorderLayout.CENTER);
162     }
163
164
165     /** Creates new form JasperViewer */
166     public JasperViewer(
167         JasperPrint jasperPrint,
168         boolean isExitOnClose,
169         Locale JavaDoc locale
170         )
171     {
172         if (locale != null)
173             setLocale(locale);
174
175         this.isExitOnClose = isExitOnClose;
176
177         initComponents();
178
179         this.viewer = new JRViewer(jasperPrint, locale);
180         this.pnlMain.add(this.viewer, BorderLayout.CENTER);
181     }
182
183
184     /** This method is called from within the constructor to
185      * initialize the form.
186      * WARNING: Do NOT modify this code. The content of this method is
187      * always regenerated by the Form Editor.
188      */

189     private void initComponents() {//GEN-BEGIN:initComponents
190
pnlMain = new javax.swing.JPanel JavaDoc();
191
192         setTitle("JasperViewer");
193         setIconImage(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF")).getImage());
194         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
195             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
196                 exitForm();
197             }
198         });
199
200         pnlMain.setLayout(new java.awt.BorderLayout JavaDoc());
201
202         getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);
203
204         pack();
205
206         Toolkit JavaDoc toolkit = java.awt.Toolkit.getDefaultToolkit();
207         java.awt.Dimension JavaDoc screenSize = toolkit.getScreenSize();
208         int screenResolution = toolkit.getScreenResolution();
209         float zoom = ((float) screenResolution) / JRViewer.REPORT_RESOLUTION;
210
211         int height = (int) (550 * zoom);
212         if (height > screenSize.getHeight())
213         {
214             height = (int) screenSize.getHeight();
215         }
216         int width = (int) (750 * zoom);
217         if (width > screenSize.getWidth())
218         {
219             width = (int) screenSize.getWidth();
220         }
221
222         java.awt.Dimension JavaDoc dimension = new java.awt.Dimension JavaDoc(width, height);
223         setSize(dimension);
224         setLocation((screenSize.width-width)/2,(screenSize.height-height)/2);
225     }//GEN-END:initComponents
226

227     /** Exit the Application */
228     void exitForm() {//GEN-FIRST:event_exitForm
229

230         if (this.isExitOnClose)
231         {
232             System.exit(0);
233         }
234         else
235         {
236             this.setVisible(false);
237             this.viewer.clear();
238             this.viewer = null;
239             this.getContentPane().removeAll();
240             this.dispose();
241         }
242
243     }//GEN-LAST:event_exitForm
244

245
246     /**
247      *
248      */

249     public void setZoomRatio(float zoomRatio)
250     {
251         viewer.setZoomRatio(zoomRatio);
252     }
253
254
255     /**
256      *
257      */

258     public void setFitWidthZoomRatio()
259     {
260         viewer.setFitWidthZoomRatio();
261     }
262
263
264     /**
265      *
266      */

267     public void setFitPageZoomRatio()
268     {
269         viewer.setFitPageZoomRatio();
270     }
271
272
273     /**
274     * @param args the command line arguments
275     */

276     public static void main(String JavaDoc args[])
277     {
278         String JavaDoc fileName = null;
279         boolean isXMLFile = false;
280
281         if(args.length == 0)
282         {
283             usage();
284             return;
285         }
286
287         int k = 0;
288         while ( args.length > k )
289         {
290             if ( args[k].startsWith("-F") )
291                 fileName = args[k].substring(2);
292             if ( args[k].startsWith("-XML") )
293                 isXMLFile = true;
294
295             k++;
296         }
297
298         try
299         {
300             viewReport(fileName, isXMLFile);
301         }
302         catch (JRException e)
303         {
304             e.printStackTrace();
305             System.exit(1);
306         }
307     }
308
309
310     /**
311      *
312      */

313     private static void usage()
314     {
315         System.out.println( "JasperViewer usage:" );
316         System.out.println( "\tjava JasperViewer -XML -Ffile" );
317     }
318
319
320     /**
321      *
322      */

323     public static void viewReport(
324         String JavaDoc sourceFile,
325         boolean isXMLFile
326         ) throws JRException
327     {
328         viewReport(sourceFile, isXMLFile, true, null);
329     }
330
331     /**
332      *
333      */

334     public static void viewReport(
335         InputStream JavaDoc is,
336         boolean isXMLFile
337         ) throws JRException
338     {
339         viewReport(is, isXMLFile, true, null);
340     }
341
342     /**
343      *
344      */

345     public static void viewReport(
346         JasperPrint jasperPrint
347         )
348     {
349         viewReport(jasperPrint, true, null);
350     }
351
352     /**
353      *
354      */

355     public static void viewReport(
356         String JavaDoc sourceFile,
357         boolean isXMLFile,
358         boolean isExitOnClose
359         ) throws JRException
360     {
361         viewReport(sourceFile, isXMLFile, isExitOnClose, null);
362     }
363
364     /**
365      *
366      */

367     public static void viewReport(
368         InputStream JavaDoc is,
369         boolean isXMLFile,
370         boolean isExitOnClose
371         ) throws JRException
372     {
373         viewReport(is, isXMLFile, isExitOnClose, null);
374     }
375
376     /**
377      *
378      */

379     public static void viewReport(
380         JasperPrint jasperPrint,
381         boolean isExitOnClose
382         )
383     {
384         viewReport(jasperPrint, isExitOnClose, null);
385     }
386
387     /**
388      *
389      */

390     public static void viewReport(
391         String JavaDoc sourceFile,
392         boolean isXMLFile,
393         boolean isExitOnClose,
394         Locale JavaDoc locale
395         ) throws JRException
396     {
397         JasperViewer jasperViewer =
398             new JasperViewer(
399                 sourceFile,
400                 isXMLFile,
401                 isExitOnClose,
402                 locale
403                 );
404         jasperViewer.setVisible(true);
405     }
406
407     /**
408      *
409      */

410     public static void viewReport(
411         InputStream JavaDoc is,
412         boolean isXMLFile,
413         boolean isExitOnClose,
414         Locale JavaDoc locale
415         ) throws JRException
416     {
417         JasperViewer jasperViewer =
418             new JasperViewer(
419                 is,
420                 isXMLFile,
421                 isExitOnClose,
422                 locale
423                 );
424         jasperViewer.setVisible(true);
425     }
426
427     /**
428      *
429      */

430     public static void viewReport(
431         JasperPrint jasperPrint,
432         boolean isExitOnClose,
433         Locale JavaDoc locale
434         )
435     {
436         JasperViewer jasperViewer =
437             new JasperViewer(
438                 jasperPrint,
439                 isExitOnClose,
440                 locale
441                 );
442         jasperViewer.setVisible(true);
443     }
444
445
446     // Variables declaration - do not modify//GEN-BEGIN:variables
447
private javax.swing.JPanel JavaDoc pnlMain;
448     // End of variables declaration//GEN-END:variables
449

450 }
451
Popular Tags