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: package net.sf.jasperreports.view;
030:
031: import java.io.InputStream;
032: import java.util.Collection;
033:
034: import javax.swing.JOptionPane;
035:
036: import net.sf.jasperreports.engine.JRException;
037: import net.sf.jasperreports.engine.JRReport;
038: import net.sf.jasperreports.engine.JasperCompileManager;
039: import net.sf.jasperreports.engine.design.JRValidationException;
040: import net.sf.jasperreports.engine.design.JasperDesign;
041: import net.sf.jasperreports.engine.util.JRLoader;
042: import net.sf.jasperreports.engine.xml.JRXmlLoader;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: JRDesignViewer.java 1797 2007-07-30 09:38:35Z teodord $
047: */
048: public class JRDesignViewer extends JRViewer {
049:
050: /** Creates new form JRDesignViewer */
051: public JRDesignViewer(String fileName, boolean isXML)
052: throws JRException {
053: super (fileName, isXML);
054: loadReport(fileName, isXML);
055: //reconfigureReloadButton();
056: hideUnusedComponents();
057: }
058:
059: /** Creates new form JRDesignViewer */
060: public JRDesignViewer(InputStream is, boolean isXML)
061: throws JRException {
062: super (is, isXML);
063: loadReport(is, isXML);
064: //reconfigureReloadButton();
065: hideUnusedComponents();
066: }
067:
068: /** Creates new form JRDesignViewer */
069: public JRDesignViewer(JRReport report) throws JRException {
070: super (new JRPreviewBuilder(report).getJasperPrint());
071: //reconfigureReloadButton();
072: hideUnusedComponents();
073: }
074:
075: private void hideUnusedComponents() {
076: btnFirst.setVisible(false);
077: btnLast.setVisible(false);
078: btnPrevious.setVisible(false);
079: btnNext.setVisible(false);
080: txtGoTo.setVisible(false);
081: pnlStatus.setVisible(false);
082: }
083:
084: void btnReloadActionPerformed(java.awt.event.ActionEvent evt) {
085: if (this .type == TYPE_FILE_NAME) {
086: try {
087: loadReport(reportFileName, isXML);
088: forceRefresh();
089: } catch (JRException e) {
090: e.printStackTrace();
091: JOptionPane
092: .showMessageDialog(this ,
093: "Error loading report design. See console for details.");
094: }
095: }
096: }
097:
098: /**
099: */
100: private void verifyDesign(JasperDesign jasperDesign)
101: throws JRException {
102: /* */
103: Collection brokenRules = JasperCompileManager
104: .verifyDesign(jasperDesign);
105: if (brokenRules != null && brokenRules.size() > 0) {
106: throw new JRValidationException(brokenRules);
107: }
108: }
109:
110: /**
111: */
112: protected void loadReport(String fileName, boolean isXmlReport)
113: throws JRException {
114: if (isXmlReport) {
115: JasperDesign jasperDesign = JRXmlLoader.load(fileName);
116: setReport(jasperDesign);
117: } else {
118: setReport((JRReport) JRLoader.loadObject(fileName));
119: }
120: this .type = TYPE_FILE_NAME;
121: this .isXML = isXmlReport;
122: this .reportFileName = fileName;
123: }
124:
125: /**
126: */
127: protected void loadReport(InputStream is, boolean isXmlReport)
128: throws JRException {
129: if (isXmlReport) {
130: JasperDesign jasperDesign = JRXmlLoader.load(is);
131: setReport(jasperDesign);
132: } else {
133: setReport((JRReport) JRLoader.loadObject(is));
134: }
135: this .type = TYPE_INPUT_STREAM;
136: this .isXML = isXmlReport;
137: }
138:
139: /**
140: */
141: protected void loadReport(JRReport rep) throws JRException {
142: setReport(rep);
143: this .type = TYPE_OBJECT;
144: this .isXML = false;
145: }
146:
147: private void setReport(JRReport report) throws JRException {
148: if (report instanceof JasperDesign) {
149: verifyDesign((JasperDesign) report);
150: }
151: this .jasperPrint = new JRPreviewBuilder(report)
152: .getJasperPrint();
153: }
154: }
|