001: /*
002: * Copyright (C) 2005-2007 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * DesignVerifyerThread.java
023: *
024: * Created on February 27, 2007, 1:56 PM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport;
031:
032: import it.businesslogic.ireport.gui.JReportFrame;
033: import it.businesslogic.ireport.gui.MainFrame;
034: import it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedEvent;
035: import it.businesslogic.ireport.gui.event.ReportDocumentStatusChangedListener;
036: import it.businesslogic.ireport.gui.logpane.ProblemItem;
037: import it.businesslogic.ireport.compiler.xml.SourceLocation;
038: import it.businesslogic.ireport.compiler.xml.SourceTraceDigester;
039: import java.io.ByteArrayInputStream;
040: import java.io.ByteArrayOutputStream;
041: import java.util.Collection;
042: import java.util.Iterator;
043: import javax.swing.SwingUtilities;
044: import net.sf.jasperreports.engine.JasperCompileManager;
045: import net.sf.jasperreports.engine.design.JRValidationFault;
046: import net.sf.jasperreports.engine.design.JasperDesign;
047:
048: /**
049: *
050: * @author gtoffoli
051: */
052: public class DesignVerifyerThread implements Runnable,
053: ReportDocumentStatusChangedListener {
054:
055: private boolean reportChanged = true;
056: private Thread this Thread = null;
057: private boolean stop = false;
058:
059: private JReportFrame jReportFrame = null;
060:
061: /** Creates a new instance of DesignVerifyerThread */
062: public DesignVerifyerThread(JReportFrame jReportFrame) {
063: this .jReportFrame = jReportFrame;
064:
065: this .jReportFrame.getReport()
066: .addReportDocumentStatusChangedListener(this );
067:
068: this Thread = new Thread(this );
069: }
070:
071: public void start() {
072: this Thread.start();
073: }
074:
075: public void stop() {
076: setStop(true);
077: }
078:
079: public void run() {
080:
081: while (!isStop()) {
082: try {
083: Thread.sleep(2000);
084: } catch (Exception ex) {
085: }
086:
087: if (MainFrame.getMainInstance().getProperties()
088: .getProperty("RealTimeValidation", "true").equals(
089: "true")
090: && getJReportFrame() != null
091: && isReportChanged()
092: && MainFrame.getMainInstance()
093: .getActiveReportFrame() == this
094: .getJReportFrame()) {
095: setReportChanged(false);
096: verifyDesign();
097: }
098: }
099: }
100:
101: public JReportFrame getJReportFrame() {
102: return jReportFrame;
103: }
104:
105: public void setJReportFrame(JReportFrame jReportFrame) {
106: this .jReportFrame = jReportFrame;
107: }
108:
109: public boolean isReportChanged() {
110: return reportChanged;
111: }
112:
113: public void setReportChanged(boolean reportChanged) {
114: this .reportChanged = reportChanged;
115: }
116:
117: public void reportDocumentStatusChanged(
118: ReportDocumentStatusChangedEvent evt) {
119: setReportChanged(true);
120: }
121:
122: public boolean isStop() {
123: return stop;
124: }
125:
126: public void setStop(boolean stop) {
127: this .stop = stop;
128: }
129:
130: public void verifyDesign() {
131: // Remove all the WARNINGS...
132: for (int i = 0; i < getJReportFrame().getReportProblems()
133: .size(); ++i) {
134: ProblemItem pii = (ProblemItem) getJReportFrame()
135: .getReportProblems().get(i);
136: if (pii.getProblemType() == ProblemItem.WARNING) {
137: getJReportFrame().getReportProblems().remove(i);
138: i--;
139: }
140: }
141: //getJReportFrame().getReportProblems().clear();
142:
143: try {
144: SourceTraceDigester digester = IReportCompiler
145: .createDigester();
146: ReportWriter rw = new ReportWriter(getJReportFrame()
147: .getReport());
148: ByteArrayOutputStream baos = new ByteArrayOutputStream();
149: rw.writeToOutputStream(baos);
150: JasperDesign jd = IReportCompiler.loadJasperDesign(
151: new ByteArrayInputStream(baos.toByteArray()),
152: digester);
153:
154: Collection ls = JasperCompileManager.verifyDesign(jd);
155: Iterator iterator = ls.iterator();
156: while (iterator.hasNext()) {
157: JRValidationFault fault = (JRValidationFault) iterator
158: .next();
159: String s = fault.getMessage();
160: SourceLocation sl = digester.getLocation(fault
161: .getSource());
162: getJReportFrame().getReportProblems().add(
163: new ProblemItem(ProblemItem.WARNING, s, fault
164: .getSource(), sl.getXPath()));
165: }
166:
167: } catch (Exception ex) {
168: getJReportFrame().getReportProblems().add(
169: new ProblemItem(ProblemItem.WARNING, ex
170: .getMessage(), null, null));
171: }
172:
173: Runnable runner = new Runnable() {
174: public void run() {
175: MainFrame.getMainInstance().getLogPane()
176: .getProblemsPanel().setProblems(
177: getJReportFrame().getReportProblems());
178: }
179: };
180:
181: SwingUtilities.invokeLater(runner);
182: }
183: }
|