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: * PromptDialog.java
028: *
029: * Created on 5 maggio 2005, 0.25
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.prompt;
034:
035: import it.businesslogic.ireport.util.I18n;
036: import java.awt.BorderLayout;
037:
038: import java.awt.Frame;
039: import java.awt.event.KeyAdapter;
040: import java.awt.event.KeyEvent;
041: import java.text.SimpleDateFormat;
042: import java.util.StringTokenizer;
043:
044: import java.util.Vector;
045:
046: import javax.swing.JOptionPane;
047:
048: /**
049: * @author Administrator
050: */
051: public class PromptDialog extends javax.swing.JDialog {
052:
053: static Vector cachedValues = new Vector();
054: private int dialogResult = JOptionPane.CANCEL_OPTION;
055: private Object value = null;
056:
057: private com.michaelbaranov.microba.calendar.DatePicker datePicker = null;
058: private JDateTimePicker datetimePicker = null;
059:
060: private boolean isCollection = false;
061:
062: /**
063: * Creates new form PromptDialog
064: *
065: * @param parent DOCUMENT ME!
066: * @param modal DOCUMENT ME!
067: */
068: public PromptDialog(Frame parent, boolean modal) {
069: super (parent, modal);
070: initComponents();
071: applyI18n();
072: jLabelYouCan.setText(it.businesslogic.ireport.util.I18n
073: .getString("gui.prompt.parameter",
074: "You can provide a value for the parameter:"));
075:
076: for (int i = 0; i < cachedValues.size(); ++i) {
077: this .jComboBox1.addItem(cachedValues.elementAt(i));
078: }
079:
080: this .jComboBox1.getEditor().getEditorComponent()
081: .addKeyListener(new KeyAdapter() {
082: public void keyPressed(KeyEvent evt) {
083: jButton1KeyPressed(evt);
084: }
085:
086: public void keyTyped(KeyEvent evt) {
087: }
088: });
089:
090: it.businesslogic.ireport.util.Misc.centerFrame(this );
091:
092: javax.swing.KeyStroke escape = javax.swing.KeyStroke
093: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
094: false);
095: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
096: public void actionPerformed(java.awt.event.ActionEvent e) {
097: jButton2ActionPerformed(e);
098: }
099: };
100:
101: getRootPane().getInputMap(
102: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
103: escape, "ESCAPE");
104: getRootPane().getActionMap().put("ESCAPE", escapeAction);
105:
106: //to make the default button ...
107: this .getRootPane().setDefaultButton(this .jButton2);
108: }
109:
110: /**
111: * DOCUMENT ME!
112: *
113: * @param param DOCUMENT ME!
114: */
115: public void setParameter(it.businesslogic.ireport.JRParameter param) {
116:
117: Object val = param.getLastDefaultValue();
118:
119: String format = "";
120:
121: if (param.getClassType().equals("java.util.Date")) {
122: format = "";
123: jPanel4.removeAll();
124: datePicker = new com.michaelbaranov.microba.calendar.DatePicker();
125: datePicker.setLocale(I18n.getCurrentLocale());
126: SimpleDateFormat sdf = new SimpleDateFormat();
127: try {
128: if (val instanceof java.util.Date) {
129: datePicker.setDate((java.util.Date) val);
130: }
131: } catch (Exception ex) {
132: ex.printStackTrace();
133: }
134: jPanel4.add(datePicker, BorderLayout.CENTER);
135: } else if (param.getClassType().equals("java.sql.Time")
136: || param.getClassType().equals("java.sql.Timestamp")) {
137: format = "";
138: jPanel4.removeAll();
139: datetimePicker = new JDateTimePicker();
140: datetimePicker.setLocale(I18n.getCurrentLocale());
141:
142: try {
143: if (val instanceof java.util.Date) {
144: datetimePicker.setDate((java.util.Date) val);
145: }
146: } catch (Exception ex) {
147: ex.printStackTrace();
148: }
149: jPanel4.add(datetimePicker, BorderLayout.CENTER);
150: } else {
151: val = (val == null) ? "" : val;
152: this .jComboBox1.setSelectedItem(val);
153: this .jComboBox1.getEditor().selectAll();
154: }
155:
156: jLabelParamName.setText(param.getName());
157:
158: if (param.getDescription() != null
159: && param.getDescription().length() > 0) {
160: jLabelParamName.setToolTipText(param.getDescription());
161: }
162:
163: if (param.getClassType().equals("java.util.Date")) {
164: //format = " (" +
165: // it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
166: // .getProperty("dateformat", "d/M/y") +
167: // ")";
168: } else if (param.getClassType().equals("java.sql.Time")
169: || param.getClassType().equals("java.sql.Timestamp")) {
170: //format = " (" +
171: // it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties()
172: // .getProperty("timeformat", "d/M/y H:m:s") +
173: // ")";
174: } else if (param.getClassType().equals("java.lang.Boolean")) {
175: format = " (true | false)";
176: } else if (param.getClassType().equals("java.lang.String")) {
177:
178: } else {
179:
180: try {
181: Class clazz = Class.forName(param.getClassType());
182: if (java.util.Collection.class.isAssignableFrom(clazz)) {
183: format = " ( foo,bar,test )";
184: }
185:
186: } catch (Exception ex) {
187:
188: }
189: }
190:
191: jLabelClass.setText(it.businesslogic.ireport.util.I18n
192: .getString("gui.prompt.class", "The class type is:")
193: + " " + param.getClassType() + format);
194:
195: if (param.getDescription() != null
196: && param.getDescription().length() > 0) {
197: jTextArea1.setText(param.getDescription());
198: jScrollPane1.setVisible(true);
199: } else {
200: jScrollPane1.setVisible(false);
201: }
202: if (jComboBox1.isVisible())
203: this .jComboBox1.requestFocusInWindow();
204: }
205:
206: /**
207: * This method is called from within the constructor to initialize the
208: * form. WARNING: Do NOT modify this code. The content of this method is
209: * always regenerated by the Form Editor.
210: */
211:
212: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
213: private void initComponents() {
214: java.awt.GridBagConstraints gridBagConstraints;
215:
216: jLabel1 = new javax.swing.JLabel();
217: jPanel1 = new javax.swing.JPanel();
218: jLabelYouCan = new javax.swing.JLabel();
219: jLabelParamName = new javax.swing.JLabel();
220: jPanel4 = new javax.swing.JPanel();
221: jComboBox1 = new javax.swing.JComboBox();
222: jLabelClass = new javax.swing.JLabel();
223: jSeparator1 = new javax.swing.JSeparator();
224: jScrollPane1 = new javax.swing.JScrollPane();
225: jTextArea1 = new javax.swing.JTextArea();
226: jPanel2 = new javax.swing.JPanel();
227: jPanel3 = new javax.swing.JPanel();
228: jButton1 = new javax.swing.JButton();
229: jButton2 = new javax.swing.JButton();
230:
231: getContentPane().setLayout(new java.awt.GridBagLayout());
232:
233: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
234: setTitle("Parameter prompt");
235: jLabel1
236: .setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
237: jLabel1
238: .setIcon(new javax.swing.ImageIcon(
239: getClass()
240: .getResource(
241: "/it/businesslogic/ireport/icons/keyboard.png")));
242: gridBagConstraints = new java.awt.GridBagConstraints();
243: gridBagConstraints.gridx = 0;
244: gridBagConstraints.gridy = 0;
245: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
246: gridBagConstraints.ipadx = 10;
247: gridBagConstraints.ipady = 10;
248: getContentPane().add(jLabel1, gridBagConstraints);
249:
250: jPanel1.setLayout(new java.awt.GridBagLayout());
251:
252: jLabelYouCan.setFont(new java.awt.Font("SansSerif", 0, 12));
253: jLabelYouCan
254: .setText("You can provide a value for the parameter:");
255: jLabelYouCan
256: .setVerticalAlignment(javax.swing.SwingConstants.TOP);
257: gridBagConstraints = new java.awt.GridBagConstraints();
258: gridBagConstraints.gridx = 0;
259: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
260: gridBagConstraints.weightx = 1.0;
261: jPanel1.add(jLabelYouCan, gridBagConstraints);
262:
263: jLabelParamName.setFont(new java.awt.Font("SansSerif", 1, 16));
264: jLabelParamName.setText("Param name");
265: jLabelParamName
266: .setVerticalAlignment(javax.swing.SwingConstants.TOP);
267: gridBagConstraints = new java.awt.GridBagConstraints();
268: gridBagConstraints.gridx = 0;
269: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
270: gridBagConstraints.weightx = 1.0;
271: jPanel1.add(jLabelParamName, gridBagConstraints);
272:
273: jPanel4.setLayout(new java.awt.BorderLayout());
274:
275: jPanel4.setMinimumSize(new java.awt.Dimension(118, 18));
276: jPanel4.setPreferredSize(new java.awt.Dimension(400, 25));
277: jComboBox1.setEditable(true);
278: jComboBox1.setPreferredSize(new java.awt.Dimension(400, 25));
279: jComboBox1.addKeyListener(new java.awt.event.KeyAdapter() {
280: public void keyPressed(java.awt.event.KeyEvent evt) {
281: jComboBox1KeyPressed(evt);
282: }
283: });
284:
285: jPanel4.add(jComboBox1, java.awt.BorderLayout.CENTER);
286:
287: gridBagConstraints = new java.awt.GridBagConstraints();
288: gridBagConstraints.gridx = 0;
289: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
290: gridBagConstraints.weightx = 1.0;
291: jPanel1.add(jPanel4, gridBagConstraints);
292:
293: jLabelClass.setFont(new java.awt.Font("SansSerif", 0, 12));
294: jLabelClass.setText("The class type is:");
295: gridBagConstraints = new java.awt.GridBagConstraints();
296: gridBagConstraints.gridx = 0;
297: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
298: jPanel1.add(jLabelClass, gridBagConstraints);
299:
300: gridBagConstraints = new java.awt.GridBagConstraints();
301: gridBagConstraints.gridx = 0;
302: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
303: gridBagConstraints.insets = new java.awt.Insets(4, 0, 2, 4);
304: jPanel1.add(jSeparator1, gridBagConstraints);
305:
306: jScrollPane1.setBorder(javax.swing.BorderFactory
307: .createEmptyBorder(1, 1, 1, 1));
308: jScrollPane1.setPreferredSize(new java.awt.Dimension(3, 40));
309: jTextArea1.setEditable(false);
310: jTextArea1.setOpaque(false);
311: jScrollPane1.setViewportView(jTextArea1);
312:
313: gridBagConstraints = new java.awt.GridBagConstraints();
314: gridBagConstraints.gridx = 0;
315: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
316: gridBagConstraints.weightx = 1.0;
317: gridBagConstraints.weighty = 1.0;
318: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
319: jPanel1.add(jScrollPane1, gridBagConstraints);
320:
321: gridBagConstraints = new java.awt.GridBagConstraints();
322: gridBagConstraints.gridx = 1;
323: gridBagConstraints.gridy = 0;
324: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
325: gridBagConstraints.ipadx = 1;
326: gridBagConstraints.ipady = 1;
327: gridBagConstraints.weightx = 1.0;
328: gridBagConstraints.weighty = 1.0;
329: getContentPane().add(jPanel1, gridBagConstraints);
330:
331: jPanel2.setLayout(new java.awt.GridBagLayout());
332:
333: jPanel2.setMinimumSize(new java.awt.Dimension(50, 40));
334: jPanel2.setPreferredSize(new java.awt.Dimension(40, 40));
335: gridBagConstraints = new java.awt.GridBagConstraints();
336: gridBagConstraints.weightx = 1.0;
337: jPanel2.add(jPanel3, gridBagConstraints);
338:
339: jButton1.setText("Ok");
340: jButton1.setPreferredSize(new java.awt.Dimension(100, 26));
341: jButton1.addActionListener(new java.awt.event.ActionListener() {
342: public void actionPerformed(java.awt.event.ActionEvent evt) {
343: jButton1ActionPerformed(evt);
344: }
345: });
346: jButton1.addKeyListener(new java.awt.event.KeyAdapter() {
347: public void keyPressed(java.awt.event.KeyEvent evt) {
348: jButton1KeyPressed(evt);
349: }
350:
351: public void keyTyped(java.awt.event.KeyEvent evt) {
352: jButton1KeyTyped(evt);
353: }
354: });
355:
356: gridBagConstraints = new java.awt.GridBagConstraints();
357: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
358: jPanel2.add(jButton1, gridBagConstraints);
359:
360: jButton2.setText("Use default");
361: jButton2.setPreferredSize(new java.awt.Dimension(100, 26));
362: jButton2.addActionListener(new java.awt.event.ActionListener() {
363: public void actionPerformed(java.awt.event.ActionEvent evt) {
364: jButton2ActionPerformed(evt);
365: }
366: });
367:
368: gridBagConstraints = new java.awt.GridBagConstraints();
369: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
370: jPanel2.add(jButton2, gridBagConstraints);
371:
372: gridBagConstraints = new java.awt.GridBagConstraints();
373: gridBagConstraints.gridx = 0;
374: gridBagConstraints.gridy = 1;
375: gridBagConstraints.gridwidth = 2;
376: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
377: getContentPane().add(jPanel2, gridBagConstraints);
378:
379: pack();
380: }// </editor-fold>//GEN-END:initComponents
381:
382: private void jComboBox1KeyPressed(java.awt.event.KeyEvent evt//GEN-FIRST:event_jComboBox1KeyPressed
383: ) {
384: }//GEN-LAST:event_jComboBox1KeyPressed
385:
386: private void jButton1KeyPressed(java.awt.event.KeyEvent evt//GEN-FIRST:event_jButton1KeyPressed
387: ) {
388:
389: if (evt.getKeyCode() == evt.VK_ENTER) {
390: jComboBox1
391: .setSelectedItem(jComboBox1.getEditor().getItem());
392: this .jButton1.requestFocusInWindow();
393: jButton1ActionPerformed(null);
394: }
395: }//GEN-LAST:event_jButton1KeyPressed
396:
397: private void jButton1KeyTyped(java.awt.event.KeyEvent evt//GEN-FIRST:event_jButton1KeyTyped
398: ) {
399: }//GEN-LAST:event_jButton1KeyTyped
400:
401: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt//GEN-FIRST:event_jButton2ActionPerformed
402: ) {
403: setDialogResult(javax.swing.JOptionPane.CANCEL_OPTION);
404: setValue(null);
405: setVisible(false);
406: dispose();
407: }//GEN-LAST:event_jButton2ActionPerformed
408:
409: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt//GEN-FIRST:event_jButton1ActionPerformed
410: ) {
411: setDialogResult(javax.swing.JOptionPane.OK_OPTION);
412:
413: if (jPanel4.getComponent(0) == jComboBox1) {
414: if (jComboBox1.getSelectedItem() == null) {
415: setValue(null);
416: } else {
417: setValue(jComboBox1.getSelectedItem());
418: if (!cachedValues.contains(getValue())) {
419: cachedValues.addElement(getValue());
420: }
421: }
422: } else if (jPanel4.getComponent(0) == datePicker) {
423: setValue(datePicker.getDate());
424: } else if (jPanel4.getComponent(0) == datetimePicker) {
425: setValue(datetimePicker.getDate());
426: }
427:
428: setVisible(false);
429: dispose();
430:
431: }//GEN-LAST:event_jButton1ActionPerformed
432:
433: /**
434: * DOCUMENT ME!
435: *
436: * @return DOCUMENT ME!
437: */
438: public int getDialogResult() {
439:
440: return dialogResult;
441: }
442:
443: /**
444: * DOCUMENT ME!
445: *
446: * @param dialogResult DOCUMENT ME!
447: */
448: public void setDialogResult(int dialogResult) {
449: this .dialogResult = dialogResult;
450: }
451:
452: /**
453: * DOCUMENT ME!
454: *
455: * @return DOCUMENT ME!
456: */
457: public Object getValue() {
458:
459: return value;
460: }
461:
462: /**
463: * DOCUMENT ME!
464: *
465: * @param value DOCUMENT ME!
466: */
467: public void setValue(Object value) {
468: this .value = value;
469: }
470:
471: // Variables declaration - do not modify//GEN-BEGIN:variables
472: private javax.swing.JButton jButton1;
473: private javax.swing.JButton jButton2;
474: private javax.swing.JComboBox jComboBox1;
475: private javax.swing.JLabel jLabel1;
476: private javax.swing.JLabel jLabelClass;
477: private javax.swing.JLabel jLabelParamName;
478: private javax.swing.JLabel jLabelYouCan;
479: private javax.swing.JPanel jPanel1;
480: private javax.swing.JPanel jPanel2;
481: private javax.swing.JPanel jPanel3;
482: private javax.swing.JPanel jPanel4;
483: private javax.swing.JScrollPane jScrollPane1;
484: private javax.swing.JSeparator jSeparator1;
485: private javax.swing.JTextArea jTextArea1;
486:
487: // End of variables declaration//GEN-END:variables
488: public void applyI18n() {
489: // Start autogenerated code ----------------------
490: jButton1.setText(I18n.getString("promptDialog.button1", "Ok"));
491: jButton2.setText(I18n.getString("promptDialog.button2",
492: "Use default"));
493: jLabelClass.setText(I18n.getString("promptDialog.labelClass",
494: "The class type is:"));
495: jLabelParamName.setText(I18n.getString(
496: "promptDialog.labelParamName", "Param name"));
497: // End autogenerated code ----------------------
498:
499: this .setTitle(I18n.getString("promptDialog.title",
500: "Parameter prompt"));
501: jLabelYouCan.setText(I18n.getString("promptDialog.labelYouCan",
502: "You can provide a value for the parameter:"));
503: }
504: }
|