KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JRAbstractScriptlet


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;
29
30 import java.util.Map JavaDoc;
31
32 import net.sf.jasperreports.engine.fill.JRFillField;
33 import net.sf.jasperreports.engine.fill.JRFillGroup;
34 import net.sf.jasperreports.engine.fill.JRFillParameter;
35 import net.sf.jasperreports.engine.fill.JRFillVariable;
36
37
38 /**
39  * Defines an abstract representation of a report scriptlet. Scriptlets are useful when a specific behaviour is needed
40  * in certain moments of the report filling process, such as report, column or group initialization. Scriptlets must implement
41  * the abstract methods that define the behaviour at the specified moments.
42  * @author Teodor Danciu (teodord@users.sourceforge.net)
43  * @version $Id: JRAbstractScriptlet.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
44  */

45 public abstract class JRAbstractScriptlet
46 {
47
48
49     /**
50      *
51      */

52     protected Map JavaDoc parametersMap = null;
53     protected Map JavaDoc fieldsMap = null;
54     protected Map JavaDoc variablesMap = null;
55     protected JRFillGroup[] groups = null;
56
57
58     /**
59      *
60      */

61     public JRAbstractScriptlet()
62     {
63     }
64
65
66     /**
67      *
68      */

69     public void setData(
70         Map JavaDoc parsm,
71         Map JavaDoc fldsm,
72         Map JavaDoc varsm,
73         JRFillGroup[] grps
74         )
75     {
76         parametersMap = parsm;
77         fieldsMap = fldsm;
78         variablesMap = varsm;
79         groups = grps;
80     }
81
82
83     /**
84      *
85      */

86     public Object JavaDoc getParameterValue(String JavaDoc parameterName) throws JRScriptletException
87     {
88         JRFillParameter parameter = (JRFillParameter)this.parametersMap.get(parameterName);
89         if (parameter == null)
90         {
91             throw new JRScriptletException("Parameter not found : " + parameterName);
92         }
93         return parameter.getValue();
94     }
95
96
97     /**
98      *
99      */

100     public Object JavaDoc getFieldValue(String JavaDoc fieldName) throws JRScriptletException
101     {
102         JRFillField field = (JRFillField)this.fieldsMap.get(fieldName);
103         if (field == null)
104         {
105             throw new JRScriptletException("Field not found : " + fieldName);
106         }
107         return field.getValue();
108     }
109
110
111     /**
112      *
113      */

114     public Object JavaDoc getVariableValue(String JavaDoc variableName) throws JRScriptletException
115     {
116         JRFillVariable variable = (JRFillVariable)this.variablesMap.get(variableName);
117         if (variable == null)
118         {
119             throw new JRScriptletException("Variable not found : " + variableName);
120         }
121         return variable.getValue();
122     }
123
124
125     /**
126      *
127      */

128     public void setVariableValue(String JavaDoc variableName, Object JavaDoc value) throws JRScriptletException
129     {
130         JRFillVariable variable = (JRFillVariable)this.variablesMap.get(variableName);
131         if (variable == null)
132         {
133             throw new JRScriptletException("Variable not found : " + variableName);
134         }
135         
136         if (value != null && !variable.getValueClass().isInstance(value) )
137         {
138             throw new JRScriptletException("Incompatible value assigned to variable " + variableName + ". Expected " + variable.getValueClassName() + ".");
139         }
140         
141         variable.setValue(value);
142     }
143
144
145     /**
146      *
147      */

148     public void callBeforeReportInit() throws JRScriptletException
149     {
150         this.beforeReportInit();
151         this.beforePageInit();
152         this.beforeColumnInit();
153
154         if(groups != null && groups.length > 0)
155         {
156             for(int i = 0; i < groups.length; i++)
157             {
158                 this.beforeGroupInit( groups[i].getName() );
159             }
160         }
161     }
162
163
164     /**
165      *
166      */

167     public void callAfterReportInit() throws JRScriptletException
168     {
169         if(groups != null && groups.length > 0)
170         {
171             for(int i = groups.length - 1; i >= 0; i--)
172             {
173                 this.afterGroupInit( groups[i].getName() );
174             }
175         }
176
177         this.afterColumnInit();
178         this.afterPageInit();
179         this.afterReportInit();
180     }
181
182
183     /**
184      *
185      */

186     public void callBeforePageInit() throws JRScriptletException
187     {
188         this.beforePageInit();
189         this.beforeColumnInit();
190     }
191
192
193     /**
194      *
195      */

196     public void callAfterPageInit() throws JRScriptletException
197     {
198         this.afterColumnInit();
199         this.afterPageInit();
200     }
201
202
203     /**
204      *
205      */

206     public void callBeforeColumnInit() throws JRScriptletException
207     {
208         this.beforeColumnInit();
209     }
210
211
212     /**
213      *
214      */

215     public void callAfterColumnInit() throws JRScriptletException
216     {
217         this.afterColumnInit();
218     }
219
220
221     /**
222      *
223      */

224     public void callBeforeGroupInit() throws JRScriptletException
225     {
226         if(groups != null && groups.length > 0)
227         {
228             JRFillGroup group = null;
229             for(int i = 0; i < groups.length; i++)
230             {
231                 group = groups[i];
232                 if (group.hasChanged())
233                 {
234                     this.beforeGroupInit(group.getName());
235                 }
236             }
237         }
238     }
239
240
241     /**
242      *
243      */

244     public void callAfterGroupInit() throws JRScriptletException
245     {
246         if(groups != null && groups.length > 0)
247         {
248             JRFillGroup group = null;
249             for(int i = groups.length - 1; i >= 0; i--)
250             {
251                 group = groups[i];
252                 if (group.hasChanged())
253                 {
254                     this.afterGroupInit(group.getName());
255                 }
256             }
257         }
258     }
259
260
261     /**
262      *
263      */

264     public void callBeforeDetailEval() throws JRScriptletException
265     {
266         this.beforeDetailEval();
267     }
268
269
270     /**
271      *
272      */

273     public void callAfterDetailEval() throws JRScriptletException
274     {
275         this.afterDetailEval();
276     }
277
278
279     /**
280      * Called before the report is initialized.
281      */

282     public abstract void beforeReportInit() throws JRScriptletException;
283
284
285     /**
286      * Called after the report is initialized.
287      */

288     public abstract void afterReportInit() throws JRScriptletException;
289
290
291     /**
292      * Called before each page is initialized.
293      */

294     public abstract void beforePageInit() throws JRScriptletException;
295
296
297     /**
298      * Called after each page is initialized.
299      */

300     public abstract void afterPageInit() throws JRScriptletException;
301
302
303     /**
304      * Called before each column is initialized.
305      */

306     public abstract void beforeColumnInit() throws JRScriptletException;
307
308
309     /**
310      * Called after each column is initialized.
311      */

312     public abstract void afterColumnInit() throws JRScriptletException;
313
314
315     /**
316      * Called before a group is initialized.
317      * @param groupName the group name
318      */

319     public abstract void beforeGroupInit(String JavaDoc groupName) throws JRScriptletException;
320
321
322     /**
323      * Called after a group is initialized.
324      * @param groupName the group name
325      */

326     public abstract void afterGroupInit(String JavaDoc groupName) throws JRScriptletException;
327
328
329     /**
330      * Called before evaluating each detail.
331      */

332     public abstract void beforeDetailEval() throws JRScriptletException;
333
334
335     /**
336      * Called after evaluating each detail.
337      */

338     public abstract void afterDetailEval() throws JRScriptletException;
339
340
341 }
342
Popular Tags