KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > fill > JRThreadSubreportRunner


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 package net.sf.jasperreports.engine.fill;
29
30 import net.sf.jasperreports.engine.JRException;
31 import net.sf.jasperreports.engine.JRRuntimeException;
32
33
34 /**
35  * Thread-based {@link net.sf.jasperreports.engine.fill.JRSubreportRunner JRSubreportRunner}
36  * implementation.
37  * <p>
38  * The subreport fill is launched in a new thread which coordinates suspend/resume actions with
39  * the master thread.
40  *
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: JRThreadSubreportRunner.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
43  */

44 public class JRThreadSubreportRunner extends JRSubreportRunnable implements JRSubreportRunner
45 {
46     private final JRBaseFiller subreportFiller;
47     
48     private Thread JavaDoc fillThread;
49     
50     public JRThreadSubreportRunner(JRFillSubreport fillSubreport, JRBaseFiller subreportFiller)
51     {
52         super(fillSubreport);
53         this.subreportFiller = subreportFiller;
54     }
55
56     public boolean isFilling()
57     {
58         return fillThread != null;
59     }
60
61     public JRSubreportRunResult start()
62     {
63         fillThread = new Thread JavaDoc(this, subreportFiller.getJasperReport().getName() + " subreport filler");
64         fillThread.start();
65
66         return waitResult();
67     }
68
69     public JRSubreportRunResult resume()
70     {
71         //notifing the subreport fill thread that it can continue on the next page
72
subreportFiller.notifyAll();
73
74         return waitResult();
75     }
76
77     protected JRSubreportRunResult waitResult()
78     {
79         try
80         {
81             // waiting for the subreport fill thread to fill the current page
82
subreportFiller.wait(); // FIXME maybe this is useless since you cannot enter
83
// the synchornized bloc if the subreport filler hasn't
84
// finished the page and passed to the wait state.
85
}
86         catch (InterruptedException JavaDoc e)
87         {
88             throw new JRRuntimeException("Error encountered while waiting on the report filling thread.", e);
89         }
90         
91         return runResult();
92     }
93
94     public void reset()
95     {
96         fillThread = null;
97     }
98     
99     public void cancel() throws JRException
100     {
101         // notifying the subreport filling thread that it can continue.
102
// it will stop anyway when trying to fill the current band
103
subreportFiller.notifyAll();
104
105         if (isRunning())
106         {
107             try
108             {
109                 //waits until the master filler notifies it that can continue with the next page
110
subreportFiller.wait();
111             }
112             catch(InterruptedException JavaDoc e)
113             {
114                 throw new JRException("Error encountered while waiting on the subreport filling thread.", e);
115             }
116         }
117     }
118
119     public void suspend() throws JRException
120     {
121         //signals to the master filler that is has finished the page
122
subreportFiller.notifyAll();
123
124         try
125         {
126             //waits until the master filler notifies it that can continue with the next page
127
subreportFiller.wait();
128         }
129         catch(InterruptedException JavaDoc e)
130         {
131             throw new JRException("Error encountered while waiting on the subreport filling thread.", e);
132         }
133     }
134
135     public void run()
136     {
137         super.run();
138
139         synchronized (subreportFiller)
140         {
141             //main filler notified that the subreport has finished
142
subreportFiller.notifyAll();
143         }
144
145 /*
146         if (error != null)
147         {
148             synchronized (subreportFiller)
149             {
150                 //if an exception occured then we should notify the main filler that we have finished the subreport
151                 subreportFiller.notifyAll();
152             }
153         }
154         */

155     }
156 }
157
Popular Tags