KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > prompt > Prompter


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * Prompter.java
28  *
29  * Created on 4 maggio 2005, 0.02
30  *
31  */

32
33 package it.businesslogic.ireport.gui.prompt;
34
35 import it.businesslogic.ireport.*;
36
37 import java.util.*;
38
39
40 /**
41  * @author Administrator
42  */

43 public class Prompter
44 {
45
46     /**
47      * DOCUMENT ME!
48      *
49      * @param report DOCUMENT ME!
50      * @return DOCUMENT ME!
51      */

52     public static HashMap promptForParameters(Report report)
53     {
54
55         HashMap hm = new HashMap();
56
57         for (int i = 0; i < report.getParameters().size(); ++i)
58         {
59
60             JRParameter param = (JRParameter) (report.getParameters().elementAt(
61                                         i));
62
63             if (param.isIsForPrompting() && param.getClassType() != null &&
64                 !param.isBuiltin())
65             {
66
67                 PromptDialog pd = new PromptDialog(it.businesslogic.ireport.gui.MainFrame.getMainInstance(),
68                                                    true);
69                 pd.setParameter(param);
70                 pd.setVisible(true);
71                 
72                 boolean isCollection = false;
73
74                 if (pd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
75                 {
76
77                     Object JavaDoc value = pd.getValue();
78
79                     if (param.getClassType().equals("java.lang.String"))
80                     {
81                         hm.put(param.getName(), value);
82                     }
83                     else if (param.getClassType().equals("java.lang.Integer"))
84                     {
85
86                         try
87                         {
88                             hm.put(param.getName(), new Integer JavaDoc("" + value));
89                         }
90                         catch (Exception JavaDoc ex)
91                         {
92                             System.out.println(ex.getMessage());
93                         }
94                     }
95                     else if (param.getClassType().equals("java.lang.Long"))
96                     {
97
98                         try
99                         {
100                             hm.put(param.getName(), new Long JavaDoc("" + value));
101                         }
102                         catch (Exception JavaDoc ex)
103                         {
104                             System.out.println(ex.getMessage());
105                         }
106                     }
107                     else if (param.getClassType().equals("java.lang.Double"))
108                     {
109
110                         try
111                         {
112                             hm.put(param.getName(), new Double JavaDoc("" + value));
113                         }
114                         catch (Exception JavaDoc ex)
115                         {
116                             System.out.println(ex.getMessage());
117                         }
118                     }
119                     else if (param.getClassType().equals("java.lang.Float"))
120                     {
121
122                         try
123                         {
124                             hm.put(param.getName(), new Float JavaDoc("" + value));
125                         }
126                         catch (Exception JavaDoc ex)
127                         {
128                             System.out.println(ex.getMessage());
129                         }
130                     }
131                     else if (param.getClassType().equals("java.lang.Boolean"))
132                     {
133
134                         try
135                         {
136                             hm.put(param.getName(), new Boolean JavaDoc("" + value));
137                         }
138                         catch (Exception JavaDoc ex)
139                         {
140                             System.out.println(ex.getMessage());
141                         }
142                     }
143                     else if (param.getClassType().equals("java.util.Date"))
144                     {
145
146                         try
147                         {
148
149                             //java.text.SimpleDateFormat sdf =
150
// new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
151
// "dateformat",
152
// "d/M/y"));
153
//hm.put(param.getName(), sdf.parse("" + value));
154
if (value != null) hm.put(param.getName(), value);
155                         }
156                         catch (Exception JavaDoc ex)
157                         {
158                             System.out.println(ex.getMessage());
159                         }
160                     }
161                     else if (param.getClassType().equals("java.sql.Time"))
162                     {
163
164                         try
165                         {
166
167                             //java.text.SimpleDateFormat sdf =
168
// new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
169
// "timeformat",
170
// "d/M/y H:m:s"));
171
java.util.Date JavaDoc d = (java.util.Date JavaDoc)value; //sdf.parse("" + value);
172
java.sql.Time JavaDoc time = new java.sql.Time JavaDoc(d.getTime());
173                             hm.put(param.getName(), time);
174                         }
175                         catch (Exception JavaDoc ex)
176                         {
177                             System.out.println(ex.getMessage());
178                         }
179                     }
180                     else if (param.getClassType().equals("java.sql.Timestamp"))
181                     {
182
183                         try
184                         {
185
186                             //java.text.SimpleDateFormat sdf =
187
// new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
188
// "timeformat",
189
// "d/M/y H:m:s"));
190
java.util.Date JavaDoc d = (java.util.Date JavaDoc)value; // sdf.parse("" + value);
191
java.sql.Timestamp JavaDoc time = new java.sql.Timestamp JavaDoc(d.getTime());
192                             hm.put(param.getName(), time);
193                         }
194                         catch (Exception JavaDoc ex)
195                         {
196                             System.out.println(ex.getMessage());
197                         }
198                     }
199                     else
200                     {
201                         try {
202                             Class JavaDoc clazz = Class.forName(param.getClassType());
203                             
204                             if ( java.util.Collection JavaDoc.class.isAssignableFrom(clazz) )
205                             {
206                                     isCollection = true;
207                                     java.util.Collection JavaDoc collection = null;
208                                     collection = new java.util.ArrayList JavaDoc();
209                                     
210                                     if (value != null)
211                                     {
212                                         fillCollection( collection, ""+value);
213                                         
214                                         param.setLastDefaultValue("" + value);
215                                         value = collection;
216                                         
217                                         try
218                                         {
219                                             hm.put(param.getName(), collection);
220                                         }
221                                         catch (Exception JavaDoc ex)
222                                         {
223                                             System.out.println(ex.getMessage());
224                                         }
225                                     }
226                             }
227
228                         } catch (Exception JavaDoc ex)
229                         {
230                               ex.printStackTrace();
231                         }
232                         
233                         
234                     }
235
236                     if (value != null && !isCollection)
237                     {
238                         param.setLastDefaultValue( value);
239                     }
240                 }
241             }
242         }
243
244         return hm;
245     }
246     
247     public static void fillCollection( java.util.Collection JavaDoc collection, String JavaDoc str)
248     {
249         if (str == null || str.length() == 0) return;
250         
251         StringTokenizer st = new StringTokenizer(str,",",false);
252         
253         while (st.hasMoreTokens())
254         {
255             String JavaDoc s = st.nextToken();
256             
257             s = s.trim();
258             //if (s.startsWith("\"")) s= s.substring(1);
259
//if (s.endsWith("\"")) s = s.substring(0,s.length()-1);
260
collection.add(s);
261         }
262         
263         
264         
265     }
266 }
267
Popular Tags