KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrinterApplet


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.io.PrintWriter JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import javax.swing.JOptionPane JavaDoc;
33
34 import net.sf.jasperreports.engine.JasperPrint;
35 import net.sf.jasperreports.engine.JasperPrintManager;
36 import net.sf.jasperreports.engine.util.JRLoader;
37
38
39 /**
40  * @author Teodor Danciu (teodord@users.sourceforge.net)
41  * @version $Id: PrinterApplet.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
42  */

43 public class PrinterApplet extends javax.swing.JApplet JavaDoc
44 {
45
46
47     /**
48      *
49      */

50     private URL JavaDoc url = null;
51     private JasperPrint jasperPrint = null;
52
53
54     /** Creates new form AppletViewer */
55     public PrinterApplet()
56     {
57         initComponents();
58     }
59
60
61     /**
62     *
63     */

64     public void init()
65     {
66         String JavaDoc strUrl = getParameter("REPORT_URL");
67         if (strUrl != null)
68         {
69             try
70             {
71                 url = new URL JavaDoc(getCodeBase(), strUrl);
72             }
73             catch (Exception JavaDoc e)
74             {
75                 StringWriter JavaDoc swriter = new StringWriter JavaDoc();
76                 PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
77                 e.printStackTrace(pwriter);
78                 JOptionPane.showMessageDialog(this, swriter.toString());
79             }
80         }
81         else
82         {
83         JOptionPane.showMessageDialog(this, "Source URL not specified");
84         }
85     }
86
87
88     /** This method is called from within the constructor to
89      * initialize the form.
90      * WARNING: Do NOT modify this code. The content of this method is
91      * always regenerated by the Form Editor.
92      */

93     private void initComponents() {//GEN-BEGIN:initComponents
94
pnlMain = new javax.swing.JPanel JavaDoc();
95         btnPrint = new javax.swing.JButton JavaDoc();
96         btnView = new javax.swing.JButton JavaDoc();
97
98         btnPrint.setText("Print the report");
99         btnPrint.addActionListener(new java.awt.event.ActionListener JavaDoc() {
100             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
101                 btnPrintActionPerformed(evt);
102             }
103         });
104
105         pnlMain.add(btnPrint);
106
107         btnView.setText("View the report");
108         btnView.addActionListener(new java.awt.event.ActionListener JavaDoc() {
109             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
110                 btnViewActionPerformed(evt);
111             }
112         });
113
114         pnlMain.add(btnView);
115
116         getContentPane().add(pnlMain, java.awt.BorderLayout.WEST);
117
118     }//GEN-END:initComponents
119

120     protected void btnViewActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_btnViewActionPerformed
121
// Add your handling code here:
122
if (url != null)
123         {
124             try
125             {
126                 if (jasperPrint == null)
127                 {
128                     jasperPrint = (JasperPrint)JRLoader.loadObject(url);
129                 }
130                 if (jasperPrint != null)
131                 {
132                     ViewerFrame viewerFrame = new ViewerFrame(this.getAppletContext(), jasperPrint);
133                     viewerFrame.show();
134                 }
135                 else
136                 {
137                     JOptionPane.showMessageDialog(this, "Empty report.");
138                 }
139             }
140             catch (Exception JavaDoc e)
141             {
142                 StringWriter JavaDoc swriter = new StringWriter JavaDoc();
143                 PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
144                 e.printStackTrace(pwriter);
145                 JOptionPane.showMessageDialog(this, swriter.toString());
146             }
147         }
148         else
149         {
150             JOptionPane.showMessageDialog(this, "Source URL not specified");
151         }
152     }//GEN-LAST:event_btnViewActionPerformed
153

154     protected void btnPrintActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_btnPrintActionPerformed
155
// Add your handling code here:
156
if (url != null)
157         {
158             if (jasperPrint == null)
159             {
160                 try
161                 {
162                     jasperPrint = (JasperPrint)JRLoader.loadObject(url);
163                 }
164                 catch (Exception JavaDoc e)
165                 {
166                     StringWriter JavaDoc swriter = new StringWriter JavaDoc();
167                     PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
168                     e.printStackTrace(pwriter);
169                     JOptionPane.showMessageDialog(this, swriter.toString());
170                 }
171             }
172             
173             if (jasperPrint != null)
174             {
175                 final JasperPrint print = jasperPrint;
176                 
177                 Thread JavaDoc thread = new Thread JavaDoc
178                     (
179                         new Runnable JavaDoc()
180                         {
181                             public void run()
182                             {
183                                 try
184                                 {
185                                     JasperPrintManager.printReport(print, true);
186                                 }
187                                 catch (Exception JavaDoc e)
188                                 {
189                                     StringWriter JavaDoc swriter = new StringWriter JavaDoc();
190                                     PrintWriter JavaDoc pwriter = new PrintWriter JavaDoc(swriter);
191                                     e.printStackTrace(pwriter);
192                                     JOptionPane.showMessageDialog(null, swriter.toString());
193                                 }
194                             }
195                         }
196                     );
197                 
198                 thread.start();
199             }
200             else
201             {
202                 JOptionPane.showMessageDialog(this, "Empty report.");
203             }
204         }
205         else
206         {
207             JOptionPane.showMessageDialog(this, "Source URL not specified");
208         }
209     }//GEN-LAST:event_btnPrintActionPerformed
210

211     
212     // Variables declaration - do not modify//GEN-BEGIN:variables
213
private javax.swing.JPanel JavaDoc pnlMain;
214     private javax.swing.JButton JavaDoc btnView;
215     private javax.swing.JButton JavaDoc btnPrint;
216     // End of variables declaration//GEN-END:variables
217

218 }
219
Popular Tags