001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * Ryan Johnson - delscovich@users.sourceforge.net
032: * Carlton Moore - cmoore79@users.sourceforge.net
033: */
034: package net.sf.jasperreports.view;
035:
036: import java.awt.BorderLayout;
037: import java.awt.Toolkit;
038: import java.io.InputStream;
039: import java.util.Locale;
040:
041: import net.sf.jasperreports.engine.JRException;
042: import net.sf.jasperreports.engine.JasperPrint;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: JasperViewer.java 1753 2007-06-14 07:57:56Z shertage $
047: */
048: public class JasperViewer extends javax.swing.JFrame {
049:
050: /**
051: *
052: */
053: protected JRViewer viewer = null;
054:
055: /**
056: *
057: */
058: private boolean isExitOnClose = true;
059:
060: /** Creates new form JasperViewer */
061: public JasperViewer(String sourceFile, boolean isXMLFile)
062: throws JRException {
063: this (sourceFile, isXMLFile, true);
064: }
065:
066: /** Creates new form JasperViewer */
067: public JasperViewer(InputStream is, boolean isXMLFile)
068: throws JRException {
069: this (is, isXMLFile, true);
070: }
071:
072: /** Creates new form JasperViewer */
073: public JasperViewer(JasperPrint jasperPrint) {
074: this (jasperPrint, true);
075: }
076:
077: /** Creates new form JasperViewer */
078: public JasperViewer(String sourceFile, boolean isXMLFile,
079: boolean isExitOnClose) throws JRException {
080: this (sourceFile, isXMLFile, isExitOnClose, null);
081: }
082:
083: /** Creates new form JasperViewer */
084: public JasperViewer(InputStream is, boolean isXMLFile,
085: boolean isExitOnClose) throws JRException {
086: this (is, isXMLFile, isExitOnClose, null);
087: }
088:
089: /** Creates new form JasperViewer */
090: public JasperViewer(JasperPrint jasperPrint, boolean isExitOnClose) {
091: this (jasperPrint, isExitOnClose, null);
092: }
093:
094: /** Creates new form JasperViewer */
095: public JasperViewer(String sourceFile, boolean isXMLFile,
096: boolean isExitOnClose, Locale locale) throws JRException {
097: if (locale != null)
098: setLocale(locale);
099:
100: this .isExitOnClose = isExitOnClose;
101:
102: initComponents();
103:
104: this .viewer = new JRViewer(sourceFile, isXMLFile, locale);
105: this .pnlMain.add(this .viewer, BorderLayout.CENTER);
106: }
107:
108: /** Creates new form JasperViewer */
109: public JasperViewer(InputStream is, boolean isXMLFile,
110: boolean isExitOnClose, Locale locale) throws JRException {
111: if (locale != null)
112: setLocale(locale);
113:
114: this .isExitOnClose = isExitOnClose;
115:
116: initComponents();
117:
118: this .viewer = new JRViewer(is, isXMLFile, locale);
119: this .pnlMain.add(this .viewer, BorderLayout.CENTER);
120: }
121:
122: /** Creates new form JasperViewer */
123: public JasperViewer(JasperPrint jasperPrint, boolean isExitOnClose,
124: Locale locale) {
125: if (locale != null)
126: setLocale(locale);
127:
128: this .isExitOnClose = isExitOnClose;
129:
130: initComponents();
131:
132: this .viewer = new JRViewer(jasperPrint, locale);
133: this .pnlMain.add(this .viewer, BorderLayout.CENTER);
134: }
135:
136: /** This method is called from within the constructor to
137: * initialize the form.
138: * WARNING: Do NOT modify this code. The content of this method is
139: * always regenerated by the Form Editor.
140: */
141: private void initComponents() {//GEN-BEGIN:initComponents
142: pnlMain = new javax.swing.JPanel();
143:
144: setTitle("JasperViewer");
145: setIconImage(new javax.swing.ImageIcon(getClass().getResource(
146: "/net/sf/jasperreports/view/images/jricon.GIF"))
147: .getImage());
148: addWindowListener(new java.awt.event.WindowAdapter() {
149: public void windowClosing(java.awt.event.WindowEvent evt) {
150: exitForm();
151: }
152: });
153:
154: pnlMain.setLayout(new java.awt.BorderLayout());
155:
156: getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);
157:
158: pack();
159:
160: Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
161: java.awt.Dimension screenSize = toolkit.getScreenSize();
162: int screenResolution = toolkit.getScreenResolution();
163: float zoom = ((float) screenResolution)
164: / JRViewer.REPORT_RESOLUTION;
165:
166: int height = (int) (550 * zoom);
167: if (height > screenSize.getHeight()) {
168: height = (int) screenSize.getHeight();
169: }
170: int width = (int) (750 * zoom);
171: if (width > screenSize.getWidth()) {
172: width = (int) screenSize.getWidth();
173: }
174:
175: java.awt.Dimension dimension = new java.awt.Dimension(width,
176: height);
177: setSize(dimension);
178: setLocation((screenSize.width - width) / 2,
179: (screenSize.height - height) / 2);
180: }//GEN-END:initComponents
181:
182: /** Exit the Application */
183: void exitForm() {//GEN-FIRST:event_exitForm
184:
185: if (this .isExitOnClose) {
186: System.exit(0);
187: } else {
188: this .setVisible(false);
189: this .viewer.clear();
190: this .viewer = null;
191: this .getContentPane().removeAll();
192: this .dispose();
193: }
194:
195: }//GEN-LAST:event_exitForm
196:
197: /**
198: *
199: */
200: public void setZoomRatio(float zoomRatio) {
201: viewer.setZoomRatio(zoomRatio);
202: }
203:
204: /**
205: *
206: */
207: public void setFitWidthZoomRatio() {
208: viewer.setFitWidthZoomRatio();
209: }
210:
211: /**
212: *
213: */
214: public void setFitPageZoomRatio() {
215: viewer.setFitPageZoomRatio();
216: }
217:
218: /**
219: * @param args the command line arguments
220: */
221: public static void main(String args[]) {
222: String fileName = null;
223: boolean isXMLFile = false;
224:
225: if (args.length == 0) {
226: usage();
227: return;
228: }
229:
230: int k = 0;
231: while (args.length > k) {
232: if (args[k].startsWith("-F"))
233: fileName = args[k].substring(2);
234: if (args[k].startsWith("-XML"))
235: isXMLFile = true;
236:
237: k++;
238: }
239:
240: try {
241: viewReport(fileName, isXMLFile);
242: } catch (JRException e) {
243: e.printStackTrace();
244: System.exit(1);
245: }
246: }
247:
248: /**
249: *
250: */
251: private static void usage() {
252: System.out.println("JasperViewer usage:");
253: System.out.println("\tjava JasperViewer -XML -Ffile");
254: }
255:
256: /**
257: *
258: */
259: public static void viewReport(String sourceFile, boolean isXMLFile)
260: throws JRException {
261: viewReport(sourceFile, isXMLFile, true, null);
262: }
263:
264: /**
265: *
266: */
267: public static void viewReport(InputStream is, boolean isXMLFile)
268: throws JRException {
269: viewReport(is, isXMLFile, true, null);
270: }
271:
272: /**
273: *
274: */
275: public static void viewReport(JasperPrint jasperPrint) {
276: viewReport(jasperPrint, true, null);
277: }
278:
279: /**
280: *
281: */
282: public static void viewReport(String sourceFile, boolean isXMLFile,
283: boolean isExitOnClose) throws JRException {
284: viewReport(sourceFile, isXMLFile, isExitOnClose, null);
285: }
286:
287: /**
288: *
289: */
290: public static void viewReport(InputStream is, boolean isXMLFile,
291: boolean isExitOnClose) throws JRException {
292: viewReport(is, isXMLFile, isExitOnClose, null);
293: }
294:
295: /**
296: *
297: */
298: public static void viewReport(JasperPrint jasperPrint,
299: boolean isExitOnClose) {
300: viewReport(jasperPrint, isExitOnClose, null);
301: }
302:
303: /**
304: *
305: */
306: public static void viewReport(String sourceFile, boolean isXMLFile,
307: boolean isExitOnClose, Locale locale) throws JRException {
308: JasperViewer jasperViewer = new JasperViewer(sourceFile,
309: isXMLFile, isExitOnClose, locale);
310: jasperViewer.setVisible(true);
311: }
312:
313: /**
314: *
315: */
316: public static void viewReport(InputStream is, boolean isXMLFile,
317: boolean isExitOnClose, Locale locale) throws JRException {
318: JasperViewer jasperViewer = new JasperViewer(is, isXMLFile,
319: isExitOnClose, locale);
320: jasperViewer.setVisible(true);
321: }
322:
323: /**
324: *
325: */
326: public static void viewReport(JasperPrint jasperPrint,
327: boolean isExitOnClose, Locale locale) {
328: JasperViewer jasperViewer = new JasperViewer(jasperPrint,
329: isExitOnClose, locale);
330: jasperViewer.setVisible(true);
331: }
332:
333: // Variables declaration - do not modify//GEN-BEGIN:variables
334: private javax.swing.JPanel pnlMain;
335: // End of variables declaration//GEN-END:variables
336:
337: }
|