KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > wizard > UserTemplatesDialog


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  * UserTemplatesDialog.java
28  *
29  * Created on 22 aprile 2005, 18.05
30  *
31  */

32
33 package it.businesslogic.ireport.gui.wizard;
34
35 import it.businesslogic.ireport.Report;
36 import it.businesslogic.ireport.gui.*;
37 import it.businesslogic.ireport.util.I18nOptionPane;
38 import it.businesslogic.ireport.util.Misc;
39 import it.businesslogic.ireport.util.ReportGenerator;
40 import java.awt.event.MouseAdapter JavaDoc;
41 import java.awt.event.MouseEvent JavaDoc;
42 import java.io.File JavaDoc;
43 import javax.swing.DefaultListModel JavaDoc;
44 import java.util.*;
45 import javax.swing.JCheckBox JavaDoc;
46 import it.businesslogic.ireport.util.I18n;
47 import javax.swing.JFileChooser JavaDoc;
48 import javax.swing.JOptionPane JavaDoc;
49 /**
50  *
51  * @author Administrator
52  */

53 public class UserTemplatesDialog extends javax.swing.JDialog JavaDoc {
54
55     private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
56     private boolean modifiedList = false;
57     /** Creates new form ClassPathDialog */
58     public UserTemplatesDialog(java.awt.Frame JavaDoc parent, boolean modal) {
59         super(parent, modal);
60         initComponents();
61         applyI18n();
62         jList1.setModel(new DefaultListModel JavaDoc());
63         it.businesslogic.ireport.util.Misc.centerFrame( this );
64
65
66         javax.swing.KeyStroke JavaDoc escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
67         javax.swing.Action JavaDoc escapeAction = new javax.swing.AbstractAction JavaDoc() {
68             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
69                 jButtonCancelActionPerformed(e);
70             }
71         };
72
73         getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
74         getRootPane().getActionMap().put("ESCAPE", escapeAction);
75
76         
77         DefaultListModel JavaDoc dlm = new DefaultListModel JavaDoc();
78         jList1.setModel(dlm);
79
80          
81          java.util.List JavaDoc list = MainFrame.getMainInstance().getUserChoicesWizardTemplates();
82          
83          for (int i=0; i<list.size(); ++i)
84          {
85              dlm.addElement( list.get(i) );
86          }
87         
88          if (list.size() > 0)
89          {
90              jList1.setSelectedIndex(0);
91          }
92          
93         jList1.updateUI();
94          
95         //to make the default button ...
96
this.getRootPane().setDefaultButton(this.jButtonSave);
97     }
98
99     /** This method is called from within the constructor to
100      * initialize the form.
101      * WARNING: Do NOT modify this code. The content of this method is
102      * always regenerated by the Form Editor.
103      */

104     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
105
private void initComponents() {
106         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
107
108         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
109         jList1 = new javax.swing.JList JavaDoc();
110         jPanel1 = new javax.swing.JPanel JavaDoc();
111         jButtonCreateReports = new javax.swing.JButton JavaDoc();
112         jButtonDeleteTemplate = new javax.swing.JButton JavaDoc();
113         jButtonSave = new javax.swing.JButton JavaDoc();
114         jButtonCancel = new javax.swing.JButton JavaDoc();
115
116         getContentPane().setLayout(new java.awt.GridBagLayout JavaDoc());
117
118         setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
119         setTitle("Classpath");
120         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
121             public void windowClosed(java.awt.event.WindowEvent JavaDoc evt) {
122                 formWindowClosed(evt);
123             }
124             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
125                 formWindowClosing(evt);
126             }
127         });
128
129         jScrollPane2.setPreferredSize(new java.awt.Dimension JavaDoc(258, 300));
130         jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
131             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
132                 jList1ValueChanged(evt);
133             }
134         });
135
136         jScrollPane2.setViewportView(jList1);
137
138         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
139         gridBagConstraints.gridx = 0;
140         gridBagConstraints.gridy = 1;
141         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
142         gridBagConstraints.weightx = 1.0;
143         gridBagConstraints.weighty = 1.0;
144         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
145         getContentPane().add(jScrollPane2, gridBagConstraints);
146
147         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
148
149         jPanel1.setMinimumSize(new java.awt.Dimension JavaDoc(120, 10));
150         jPanel1.setPreferredSize(new java.awt.Dimension JavaDoc(120, 10));
151         jButtonCreateReports.setText("Create report(s)");
152         jButtonCreateReports.setToolTipText("Create report from selected template");
153         jButtonCreateReports.setEnabled(false);
154         jButtonCreateReports.addActionListener(new java.awt.event.ActionListener JavaDoc() {
155             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
156                 jButtonDeselectAllActionPerformed1(evt);
157             }
158         });
159
160         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
161         gridBagConstraints.gridx = 0;
162         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
163         gridBagConstraints.weightx = 1.0;
164         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
165         jPanel1.add(jButtonCreateReports, gridBagConstraints);
166
167         jButtonDeleteTemplate.setText("Delete temlplate");
168         jButtonDeleteTemplate.setToolTipText("");
169         jButtonDeleteTemplate.setEnabled(false);
170         jButtonDeleteTemplate.addActionListener(new java.awt.event.ActionListener JavaDoc() {
171             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
172                 jButtonCreateReportsjButtonDeselectAllActionPerformed1(evt);
173             }
174         });
175
176         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
177         gridBagConstraints.gridx = 0;
178         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
179         gridBagConstraints.weightx = 1.0;
180         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
181         jPanel1.add(jButtonDeleteTemplate, gridBagConstraints);
182
183         jButtonSave.setText("Save");
184         jButtonSave.addActionListener(new java.awt.event.ActionListener JavaDoc() {
185             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
186                 jButtonSaveActionPerformed(evt);
187             }
188         });
189
190         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
191         gridBagConstraints.gridx = 0;
192         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
193         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
194         gridBagConstraints.weightx = 1.0;
195         gridBagConstraints.weighty = 1.0;
196         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 4, 0);
197         jPanel1.add(jButtonSave, gridBagConstraints);
198
199         jButtonCancel.setText("Cancel");
200         jButtonCancel.addActionListener(new java.awt.event.ActionListener JavaDoc() {
201             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
202                 jButtonCancelActionPerformed(evt);
203             }
204         });
205
206         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
207         gridBagConstraints.gridx = 0;
208         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
209         gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
210         gridBagConstraints.weightx = 1.0;
211         jPanel1.add(jButtonCancel, gridBagConstraints);
212
213         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
214         gridBagConstraints.gridx = 1;
215         gridBagConstraints.gridy = 1;
216         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
217         gridBagConstraints.weighty = 1.0;
218         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
219         getContentPane().add(jPanel1, gridBagConstraints);
220
221         pack();
222     }// </editor-fold>//GEN-END:initComponents
223

224     private void jList1ValueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jList1ValueChanged
225

226         if (jList1.getSelectedIndex() >=0)
227         {
228             jButtonCreateReports.setEnabled(true);
229             jButtonDeleteTemplate.setEnabled(true);
230         }
231         else
232         {
233             jButtonCreateReports.setEnabled(false);
234             jButtonDeleteTemplate.setEnabled(false);
235         }
236         
237     }//GEN-LAST:event_jList1ValueChanged
238

239     private void jButtonSaveActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
240

241         
242         List list = MainFrame.getMainInstance().getUserChoicesWizardTemplates();
243         list.clear();
244         DefaultListModel JavaDoc dlm = (DefaultListModel JavaDoc)jList1.getModel();
245         for (int i=0; i<dlm.getSize(); ++i)
246         {
247             list.add(dlm.getElementAt(i));
248         }
249         
250         UserChoicesWizardTemplate.storeWizardTemplates(list);
251         
252         setDialogResult(javax.swing.JOptionPane.OK_OPTION);
253         setVisible(false);
254     }//GEN-LAST:event_jButtonSaveActionPerformed
255

256     private void jButtonCreateReportsjButtonDeselectAllActionPerformed1(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCreateReportsjButtonDeselectAllActionPerformed1
257

258         DefaultListModel JavaDoc dlm = (DefaultListModel JavaDoc)jList1.getModel();
259         int[] indexes = jList1.getSelectedIndices();
260         for (int i = indexes.length-1; i>=0; --i)
261         {
262             dlm.remove(indexes[i]);
263             modifiedList = true;
264         }
265         
266     }//GEN-LAST:event_jButtonCreateReportsjButtonDeselectAllActionPerformed1
267

268     private void jButtonDeselectAllActionPerformed1(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonDeselectAllActionPerformed1
269

270         
271         Object JavaDoc[] templates = jList1.getSelectedValues();
272         if (templates.length == 0) return;
273         
274         
275         int filesCreated = 0;
276         
277         // Ask for destination directory...
278

279         javax.swing.JFileChooser JavaDoc jfc = new javax.swing.JFileChooser JavaDoc( MainFrame.getMainInstance().getCurrentDirectory());
280
281         jfc.setDialogTitle("Select output directory");
282         jfc.setDialogTitle(it.businesslogic.ireport.util.I18n.getString("selectOutputDirectory", "Select output directory"));//I18N
283

284         jfc.setAcceptAllFileFilterUsed(true);
285         jfc.setFileSelectionMode( jfc.DIRECTORIES_ONLY );
286         /*jfc.addChoosableFileFilter( new javax.swing.filechooser.FileFilter() {
287             public boolean accept(java.io.File file) {
288                 String filename = file.getName();
289                 return (filename.toLowerCase().endsWith(".jar") || file.isDirectory() ||
290                         filename.toLowerCase().endsWith(".zip")
291                         ) ;
292             }
293             public String getDescription() {
294                 return "*.jar, *.zip";
295             }
296         });
297          */

298
299         jfc.setMultiSelectionEnabled(false);
300
301         jfc.setDialogType( javax.swing.JFileChooser.OPEN_DIALOG);
302         if (jfc.showOpenDialog( this) == javax.swing.JOptionPane.OK_OPTION) {
303             java.io.File JavaDoc directory = jfc.getSelectedFile();
304
305             MainFrame.getMainInstance().setCurrentDirectory(directory, true);
306             
307             for (int i = templates.length-1; i>=0; --i)
308             {
309                 UserChoicesWizardTemplate template = (UserChoicesWizardTemplate)templates[i];
310                 File JavaDoc file = new File JavaDoc(directory, Misc.string_replace("_"," ", template.getName() ) + ".jrxml");
311                 
312                 if (file.exists())
313                 {
314                     //is different file to save?
315
//confirm overwrite
316
int ret = JOptionPane.showConfirmDialog(this,
317                             I18n.getFormattedString("userTemplatesDialog.message.overwrite","The file {0} already exists.\nDo you want overwrite it?",new Object JavaDoc[]{""+file}),
318                                     "", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
319                     
320                             
321                     if (ret == JOptionPane.CANCEL_OPTION) break;
322                     if (ret == JOptionPane.NO_OPTION)
323                     {
324                         // Select a new File...
325
jfc = new javax.swing.JFileChooser JavaDoc(MainFrame.getMainInstance().getCurrentDirectory());
326                         jfc.setDialogTitle("Save report as XML jasperreports file...");
327
328                         // Handling the new File, that gets saved for the first time
329
// Propose a new file name based on the report name
330

331                         jfc.setSelectedFile( file ) ;
332
333                         jfc.setFileFilter( new javax.swing.filechooser.FileFilter JavaDoc()
334                         {
335                             public boolean accept(java.io.File JavaDoc file)
336                             {
337                                 String JavaDoc filename = file.getName();
338                                 return (filename.toLowerCase().endsWith(".xml") || file.isDirectory() || filename.toLowerCase().endsWith(".jrxml")) ;
339                             }
340                             public String JavaDoc getDescription()
341                             {
342                                 return "JasperReports XML *.xml, *.jrxml";
343                             }
344                         });
345                         jfc.setMultiSelectionEnabled(false);
346                         jfc.setDialogType( javax.swing.JFileChooser.SAVE_DIALOG);
347
348                         // Show the dialog:
349
if (jfc.showSaveDialog(this) != javax.swing.JOptionPane.OK_OPTION)
350                         {
351                             return;
352                         }
353
354                         file = jfc.getSelectedFile();
355                     }
356                 }
357                 
358                 try {
359                     Report report = ReportGenerator.createReport(template);
360                     report.saveXMLFile( ""+file );
361                     filesCreated++;
362                 } catch (Exception JavaDoc ex)
363                 {
364                    JOptionPane.showMessageDialog(this,ex.getMessage(), I18n.getString("message.title.exception","Exception"), JOptionPane.ERROR_MESSAGE);
365                    break;
366                 }
367             }
368             
369             JOptionPane.showMessageDialog(this,I18n.getFormattedString("userTemplatesDialog.message.createdReports","{0} report(s) created successful.", new Object JavaDoc[]{new Integer JavaDoc(filesCreated)}), "",JOptionPane.INFORMATION_MESSAGE);
370         }
371         
372         
373         
374         
375     }//GEN-LAST:event_jButtonDeselectAllActionPerformed1
376

377     private void formWindowClosed(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_formWindowClosed
378

379     }//GEN-LAST:event_formWindowClosed
380

381     private void formWindowClosing(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_formWindowClosing
382

383         // Save all...
384
if (modifiedList)
385         {
386             int ret = javax.swing.JOptionPane.showConfirmDialog(this, it.businesslogic.ireport.util.I18n.getString("userTemplatesDialog.saveTemplatesList", "Do you want save the changes ?"));
387
388             if (ret == javax.swing.JOptionPane.CANCEL_OPTION)
389             {
390                 return;
391             }
392
393             jButtonSaveActionPerformed(null);
394         }
395         else
396         {
397             setVisible(false);
398         }
399     }//GEN-LAST:event_formWindowClosing
400

401     private void jButtonCancelActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
402
setVisible(false);
403     }//GEN-LAST:event_jButtonCancelActionPerformed
404

405     /**
406      * @param args the command line arguments
407      */

408     public static void main(String JavaDoc args[]) {
409         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
410             public void run() {
411                 new ClassPathDialog(new javax.swing.JFrame JavaDoc(), true).setVisible(true);
412             }
413         });
414     }
415
416    
417
418
419
420     public int getDialogResult() {
421         return dialogResult;
422     }
423
424     public void setDialogResult(int dialogResult) {
425         this.dialogResult = dialogResult;
426     }
427
428     // Variables declaration - do not modify//GEN-BEGIN:variables
429
private javax.swing.JButton JavaDoc jButtonCancel;
430     private javax.swing.JButton JavaDoc jButtonCreateReports;
431     private javax.swing.JButton JavaDoc jButtonDeleteTemplate;
432     private javax.swing.JButton JavaDoc jButtonSave;
433     private javax.swing.JList JavaDoc jList1;
434     private javax.swing.JPanel JavaDoc jPanel1;
435     private javax.swing.JScrollPane JavaDoc jScrollPane2;
436     // End of variables declaration//GEN-END:variables
437

438     public void applyI18n(){
439                 // Start autogenerated code ----------------------
440
jButtonCancel.setText(I18n.getString("jRFieldDialog.buttonCancel","Cancel"));
441         jButtonCreateReports.setText(I18n.getString("userTemplatesDialog.buttonGenerateReport","Generate report(s)"));
442         jButtonDeleteTemplate.setText(I18n.getString("userTemplatesDialog.buttonRemoveTemplate","Remove template"));
443         jButtonSave.setText(I18n.getString("userTemplatesDialog.buttonSave","Save changes"));
444         
445         this.setTitle(I18n.getString("userTemplatesDialog.title","User choices templates"));
446                         // End autogenerated code ----------------------
447
}
448 }
449
Popular Tags