001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * Prompter.java
028: *
029: * Created on 4 maggio 2005, 0.02
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.prompt;
034:
035: import it.businesslogic.ireport.*;
036:
037: import java.util.*;
038:
039: /**
040: * @author Administrator
041: */
042: public class Prompter {
043:
044: /**
045: * DOCUMENT ME!
046: *
047: * @param report DOCUMENT ME!
048: * @return DOCUMENT ME!
049: */
050: public static HashMap promptForParameters(Report report) {
051:
052: HashMap hm = new HashMap();
053:
054: for (int i = 0; i < report.getParameters().size(); ++i) {
055:
056: JRParameter param = (JRParameter) (report.getParameters()
057: .elementAt(i));
058:
059: if (param.isIsForPrompting()
060: && param.getClassType() != null
061: && !param.isBuiltin()) {
062:
063: PromptDialog pd = new PromptDialog(
064: it.businesslogic.ireport.gui.MainFrame
065: .getMainInstance(), true);
066: pd.setParameter(param);
067: pd.setVisible(true);
068:
069: boolean isCollection = false;
070:
071: if (pd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
072:
073: Object value = pd.getValue();
074:
075: if (param.getClassType().equals("java.lang.String")) {
076: hm.put(param.getName(), value);
077: } else if (param.getClassType().equals(
078: "java.lang.Integer")) {
079:
080: try {
081: hm.put(param.getName(), new Integer(""
082: + value));
083: } catch (Exception ex) {
084: System.out.println(ex.getMessage());
085: }
086: } else if (param.getClassType().equals(
087: "java.lang.Long")) {
088:
089: try {
090: hm.put(param.getName(),
091: new Long("" + value));
092: } catch (Exception ex) {
093: System.out.println(ex.getMessage());
094: }
095: } else if (param.getClassType().equals(
096: "java.lang.Double")) {
097:
098: try {
099: hm.put(param.getName(), new Double(""
100: + value));
101: } catch (Exception ex) {
102: System.out.println(ex.getMessage());
103: }
104: } else if (param.getClassType().equals(
105: "java.lang.Float")) {
106:
107: try {
108: hm.put(param.getName(), new Float(""
109: + value));
110: } catch (Exception ex) {
111: System.out.println(ex.getMessage());
112: }
113: } else if (param.getClassType().equals(
114: "java.math.BigDecimal")) {
115:
116: try {
117: hm
118: .put(param.getName(),
119: new java.math.BigDecimal(""
120: + value));
121: } catch (Exception ex) {
122: System.out.println(ex.getMessage());
123: }
124: } else if (param.getClassType().equals(
125: "java.lang.Boolean")) {
126:
127: try {
128: hm.put(param.getName(), new Boolean(""
129: + value));
130: } catch (Exception ex) {
131: System.out.println(ex.getMessage());
132: }
133: } else if (param.getClassType().equals(
134: "java.util.Date")) {
135:
136: try {
137:
138: //java.text.SimpleDateFormat sdf =
139: // new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
140: // "dateformat",
141: // "d/M/y"));
142: //hm.put(param.getName(), sdf.parse("" + value));
143: if (value != null)
144: hm.put(param.getName(), value);
145: } catch (Exception ex) {
146: System.out.println(ex.getMessage());
147: }
148: } else if (param.getClassType().equals(
149: "java.sql.Time")) {
150:
151: try {
152:
153: //java.text.SimpleDateFormat sdf =
154: // new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
155: // "timeformat",
156: // "d/M/y H:m:s"));
157: java.util.Date d = (java.util.Date) value; //sdf.parse("" + value);
158: java.sql.Time time = new java.sql.Time(d
159: .getTime());
160: hm.put(param.getName(), time);
161: } catch (Exception ex) {
162: System.out.println(ex.getMessage());
163: }
164: } else if (param.getClassType().equals(
165: "java.sql.Timestamp")) {
166:
167: try {
168:
169: //java.text.SimpleDateFormat sdf =
170: // new java.text.SimpleDateFormat(it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty(
171: // "timeformat",
172: // "d/M/y H:m:s"));
173: java.util.Date d = (java.util.Date) value; // sdf.parse("" + value);
174: java.sql.Timestamp time = new java.sql.Timestamp(
175: d.getTime());
176: hm.put(param.getName(), time);
177: } catch (Exception ex) {
178: System.out.println(ex.getMessage());
179: }
180: } else {
181: try {
182: Class clazz = Class.forName(param
183: .getClassType());
184:
185: if (java.util.Collection.class
186: .isAssignableFrom(clazz)) {
187: isCollection = true;
188: java.util.Collection collection = null;
189: collection = new java.util.ArrayList();
190:
191: if (value != null) {
192: fillCollection(collection, ""
193: + value);
194:
195: param.setLastDefaultValue(""
196: + value);
197: value = collection;
198:
199: try {
200: hm.put(param.getName(),
201: collection);
202: } catch (Exception ex) {
203: System.out.println(ex
204: .getMessage());
205: }
206: }
207: }
208:
209: } catch (Exception ex) {
210: ex.printStackTrace();
211: }
212:
213: }
214:
215: if (value != null && !isCollection) {
216: param.setLastDefaultValue(value);
217: }
218: }
219: }
220: }
221:
222: return hm;
223: }
224:
225: public static void fillCollection(java.util.Collection collection,
226: String str) {
227: if (str == null || str.length() == 0)
228: return;
229:
230: StringTokenizer st = new StringTokenizer(str, ",", false);
231:
232: while (st.hasMoreTokens()) {
233: String s = st.nextToken();
234:
235: s = s.trim();
236: //if (s.startsWith("\"")) s= s.substring(1);
237: //if (s.endsWith("\"")) s = s.substring(0,s.length()-1);
238: collection.add(s);
239: }
240:
241: }
242: }
|