KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > locale > LocaleResourceFilesDialog


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  * LocaleResourceFilesDialog.java
28  *
29  * Created on 29 settembre 2004, 0.56
30  *
31  */

32
33 package it.businesslogic.ireport.gui.locale;
34 import it.businesslogic.ireport.gui.*;
35 import it.businesslogic.ireport.*;
36 import javax.swing.tree.*;
37 import javax.swing.table.*;
38 import javax.swing.*;
39 import it.businesslogic.ireport.chart.*;
40 import java.util.*;
41 import java.io.*;
42 import javax.swing.event.*;
43 import it.businesslogic.ireport.util.I18n;
44 /**
45  *
46  * @author Administrator
47  */

48 public class LocaleResourceFilesDialog extends javax.swing.JDialog JavaDoc {
49    
50     private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
51     private JReportFrame jReportFrame = null;
52     
53     /** Creates new form IReportChartDialog */
54     public LocaleResourceFilesDialog(java.awt.Dialog JavaDoc parent, boolean modal) {
55         super(parent, modal);
56         initComponents();
57         initFrame();
58     }
59     
60     public void initFrame()
61     {
62         javax.swing.DefaultListModel JavaDoc dlm = new javax.swing.DefaultListModel JavaDoc() ;
63      
64         this.setSize(500,400);
65         it.businesslogic.ireport.util.Misc.centerFrame(this);
66         
67          DefaultListSelectionModel dlsm = (DefaultListSelectionModel)this.jTable1.getSelectionModel();
68         dlsm.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
69             public void valueChanged(ListSelectionEvent e) {
70                 jTable1ListSelectionValueChanged(e);
71             }
72         });
73         
74         javax.swing.KeyStroke JavaDoc escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
75         javax.swing.Action JavaDoc escapeAction = new javax.swing.AbstractAction JavaDoc() {
76             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
77                 jButtonCloseActionPerformed(e);
78             }
79         };
80        
81         getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
82         getRootPane().getActionMap().put("ESCAPE", escapeAction);
83
84         applyI18n();
85         //to make the default button ...
86
this.getRootPane().setDefaultButton(this.jButtonClose);
87     }
88     
89     public void jTable1ListSelectionValueChanged(javax.swing.event.ListSelectionEvent JavaDoc e)
90     {
91         if (this.jTable1.getSelectedRowCount() > 0) {
92             this.jButtonModifyFile.setEnabled(true);
93             this.jButtonDeleteFile.setEnabled(true);
94         }
95         else {
96             this.jButtonModifyFile.setEnabled(false);
97             this.jButtonDeleteFile.setEnabled(false);
98         }
99     }
100     
101     public LocaleResourceFilesDialog(java.awt.Frame JavaDoc parent, boolean modal) {
102         super(parent, modal);
103         initComponents();
104         initFrame();
105     }
106     
107     public void updateFileList()
108     {
109         DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
110         dtm.setRowCount(0);
111         if (getJReportFrame() == null) { jTable1.updateUI(); return; }
112         
113         Report report = getJReportFrame().getReport();
114         // Looking for locale files for this report...
115
if (report.getFilename() == null ||
116             report.getFilename().trim().equals("") )
117         {
118             return;
119         }
120         
121         if (report.getResourceBundleBaseName() == null ||
122             report.getResourceBundleBaseName().trim().equals(""))
123         {
124             return;
125         }
126         else
127         {
128             String JavaDoc basename = report.getResourceBundleBaseName();
129             File f = new File(report.getFilename());
130             File dir = f.getParentFile();
131             String JavaDoc[] files = dir.list();
132             for (int i=0; i<files.length; ++i)
133             {
134                 if (files[i].startsWith(basename) &&
135                     files[i].endsWith(".properties"))
136                 {
137                     dtm.addRow(new Object JavaDoc[]{getLocaleName(basename, files[i]),files[i]});
138                 }
139             }
140         }
141         
142         jTable1.updateUI();
143     }
144
145     public JReportFrame getJReportFrame() {
146         
147         return jReportFrame;
148     }
149
150     public void setJReportFrame(JReportFrame jReportFrame) {
151
152         this.jReportFrame = jReportFrame;
153         updateFileList();
154     }
155
156     public int getDialogResult() {
157         return dialogResult;
158     }
159
160     public void setDialogResult(int dialogResult) {
161         this.dialogResult = dialogResult;
162     }
163     
164     /** This method is called from within the constructor to
165      * initialize the form.
166      * WARNING: Do NOT modify this code. The content of this method is
167      * always regenerated by the Form Editor.
168      */

169     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
170
private void initComponents() {
171         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
172
173         jPanelData = new javax.swing.JPanel JavaDoc();
174         jScrollPane3 = new javax.swing.JScrollPane JavaDoc();
175         jTable1 = new javax.swing.JTable JavaDoc();
176         jPanel2 = new javax.swing.JPanel JavaDoc();
177         jButtonCreateNewLocale = new javax.swing.JButton JavaDoc();
178         jButtonModifyFile = new javax.swing.JButton JavaDoc();
179         jButtonDeleteFile = new javax.swing.JButton JavaDoc();
180         jPanel3 = new javax.swing.JPanel JavaDoc();
181         jButtonClose = new javax.swing.JButton JavaDoc();
182
183         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
184         setTitle("Report Imports");
185         jPanelData.setLayout(new java.awt.GridBagLayout JavaDoc());
186
187         jTable1.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
188             new Object JavaDoc [][] {
189
190             },
191             new String JavaDoc [] {
192                 "Locale", "File name"
193             }
194         ) {
195             boolean[] canEdit = new boolean [] {
196                 false, false
197             };
198
199             public boolean isCellEditable(int rowIndex, int columnIndex) {
200                 return canEdit [columnIndex];
201             }
202         });
203         jTable1.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
204             public void mouseClicked(java.awt.event.MouseEvent JavaDoc evt) {
205                 jTable1MouseClicked(evt);
206             }
207         });
208
209         jScrollPane3.setViewportView(jTable1);
210
211         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
212         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
213         gridBagConstraints.weightx = 1.0;
214         gridBagConstraints.weighty = 1.0;
215         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 4, 4, 4);
216         jPanelData.add(jScrollPane3, gridBagConstraints);
217
218         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
219
220         jPanel2.setMinimumSize(new java.awt.Dimension JavaDoc(140, 151));
221         jPanel2.setPreferredSize(new java.awt.Dimension JavaDoc(180, 100));
222         jButtonCreateNewLocale.setText("Create new locale");
223         jButtonCreateNewLocale.addActionListener(new java.awt.event.ActionListener JavaDoc() {
224             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
225                 jButtonCreateNewLocaleActionPerformed(evt);
226             }
227         });
228
229         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
230         gridBagConstraints.gridx = 0;
231         gridBagConstraints.gridy = 1;
232         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
233         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
234         gridBagConstraints.weightx = 1.0;
235         jPanel2.add(jButtonCreateNewLocale, gridBagConstraints);
236
237         jButtonModifyFile.setText(" Modify file");
238         jButtonModifyFile.setActionCommand("Modify locale");
239         jButtonModifyFile.setEnabled(false);
240         jButtonModifyFile.addActionListener(new java.awt.event.ActionListener JavaDoc() {
241             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
242                 jButtonModifyFileActionPerformed(evt);
243             }
244         });
245
246         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
247         gridBagConstraints.gridx = 0;
248         gridBagConstraints.gridy = 4;
249         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
250         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
251         gridBagConstraints.weightx = 1.0;
252         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 0, 0);
253         jPanel2.add(jButtonModifyFile, gridBagConstraints);
254
255         jButtonDeleteFile.setText("Delete file");
256         jButtonDeleteFile.setEnabled(false);
257         jButtonDeleteFile.addActionListener(new java.awt.event.ActionListener JavaDoc() {
258             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
259                 jButtonDeleteFileActionPerformed(evt);
260             }
261         });
262
263         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
264         gridBagConstraints.gridx = 0;
265         gridBagConstraints.gridy = 5;
266         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
267         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
268         gridBagConstraints.weightx = 1.0;
269         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 0, 0);
270         jPanel2.add(jButtonDeleteFile, gridBagConstraints);
271
272         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
273         gridBagConstraints.gridx = 0;
274         gridBagConstraints.gridy = 10;
275         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
276         gridBagConstraints.weightx = 1.0;
277         gridBagConstraints.weighty = 1.0;
278         jPanel2.add(jPanel3, gridBagConstraints);
279
280         jButtonClose.setText("Close");
281         jButtonClose.addActionListener(new java.awt.event.ActionListener JavaDoc() {
282             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
283                 jButtonCloseActionPerformed(evt);
284             }
285         });
286
287         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
288         gridBagConstraints.gridx = 0;
289         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
290         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
291         gridBagConstraints.weightx = 1.0;
292         jPanel2.add(jButtonClose, gridBagConstraints);
293
294         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
295         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
296         gridBagConstraints.insets = new java.awt.Insets JavaDoc(4, 0, 4, 4);
297         jPanelData.add(jPanel2, gridBagConstraints);
298
299         getContentPane().add(jPanelData, java.awt.BorderLayout.CENTER);
300
301         pack();
302     }// </editor-fold>//GEN-END:initComponents
303

304     private void jTable1MouseClicked(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jTable1MouseClicked
305
if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1)
306         {
307             jButtonModifyFileActionPerformed(null);
308             
309         }
310     }//GEN-LAST:event_jTable1MouseClicked
311

312     private void jButtonDeleteFileActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonDeleteFileActionPerformed
313

314         if (jTable1.getSelectedRow() >= 0)
315         {
316             Report report = getJReportFrame().getReport();
317             File f = new File(report.getFilename());
318             File dir = f.getParentFile();
319             File localeFile = new File(dir,""+jTable1.getValueAt( jTable1.getSelectedRow(), 1) );
320             try {
321                 localeFile.delete();
322             } catch (Exception JavaDoc ex) {}
323             updateFileList();
324         }
325         
326     }//GEN-LAST:event_jButtonDeleteFileActionPerformed
327

328     private void jButtonModifyFileActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonModifyFileActionPerformed
329

330         if (jTable1.getSelectedRow() >= 0)
331         {
332             Report report = getJReportFrame().getReport();
333             File f = new File(report.getFilename());
334             File dir = f.getParentFile();
335             File localeFile = new File(dir,""+jTable1.getValueAt( jTable1.getSelectedRow(), 1) );
336             LocaleEditorDialog led = new LocaleEditorDialog(this.getJReportFrame().getMainFrame(), false);
337             led.setFile( localeFile );
338             led.setVisible(true);
339             try {
340                 led.setTitle( localeFile.getName() );
341             } catch (Exception JavaDoc ex) {}
342         }
343     }//GEN-LAST:event_jButtonModifyFileActionPerformed
344

345     private void jButtonCreateNewLocaleActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCreateNewLocaleActionPerformed
346

347         String JavaDoc suffix = "";
348         
349         Report report = getJReportFrame().getReport();
350         if (report.getFilename() == null ||
351             report.getFilename().trim().equals("") )
352         {
353            
354             javax.swing.JOptionPane.showMessageDialog(this,
355                     I18n.getString("messages.localeResourceFilesDialog.saveFile","To create a new locale file please save your report first.")
356                     );
357             
358             return;
359         }
360         
361         File f = new File(report.getFilename());
362         
363         
364         NewLocaleFileDialog fd = new NewLocaleFileDialog(this, true);
365         fd.setVisible(true);
366         File localeFile = null;
367         if (fd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
368         {
369             try {
370             suffix = fd.getText();
371         
372             if (report.getResourceBundleBaseName() == null ||
373                 report.getResourceBundleBaseName().trim().equals("") )
374         {
375                 String JavaDoc filename = f.getName();
376                 int index = filename.lastIndexOf(".");
377                 if (index >= 0) {
378                             filename = filename.substring(0,index);
379                 }
380                 report.setResourceBundleBaseName(filename);
381         }
382             
383             File dir = f.getParentFile();
384             localeFile = new File(dir, report.getResourceBundleBaseName() + ".properties");
385             // 1. Try to load default...
386
String JavaDoc str = "# Locale " + suffix + " for report " + f.getName() +"\n";
387             try {
388                 FileInputStream fis = new FileInputStream(localeFile);
389                 byte[] buffer = new byte[1024];
390                 int read = 0;
391                 while ((read = fis.read(buffer)) > 0)
392                 {
393                     str += new String JavaDoc(buffer,0,read);
394                 }
395                 fis.close();
396             } catch (Exception JavaDoc ex){}
397                 
398             localeFile = new File(dir, report.getResourceBundleBaseName() + suffix +".properties");
399             FileOutputStream fos = new FileOutputStream( localeFile );
400             fos.write( str.getBytes());
401             fos.close();
402             
403             updateFileList();
404             
405             } catch (Exception JavaDoc ex) {
406               
407               javax.swing.JOptionPane.showMessageDialog(this,
408                     I18n.getFormattedString("messages.localeResourceFilesDialog.errorCreatingNewLocale","Error creating new locale {0}:\n{1}",
409                     new Object JavaDoc[]{localeFile.getName(),ex.getMessage()})
410                     );
411             }
412         }
413     }//GEN-LAST:event_jButtonCreateNewLocaleActionPerformed
414

415     private void jButtonCloseActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
416

417         this.setDialogResult( javax.swing.JOptionPane.OK_OPTION);
418         this.setVisible(false);
419     }//GEN-LAST:event_jButtonCloseActionPerformed
420

421
422     
423     // Variables declaration - do not modify//GEN-BEGIN:variables
424
private javax.swing.JButton JavaDoc jButtonClose;
425     private javax.swing.JButton JavaDoc jButtonCreateNewLocale;
426     private javax.swing.JButton JavaDoc jButtonDeleteFile;
427     private javax.swing.JButton JavaDoc jButtonModifyFile;
428     private javax.swing.JPanel JavaDoc jPanel2;
429     private javax.swing.JPanel JavaDoc jPanel3;
430     private javax.swing.JPanel JavaDoc jPanelData;
431     private javax.swing.JScrollPane JavaDoc jScrollPane3;
432     private javax.swing.JTable JavaDoc jTable1;
433     // End of variables declaration//GEN-END:variables
434

435     public static String JavaDoc getLocaleName(String JavaDoc baseName, String JavaDoc propertiesName)
436     {
437         String JavaDoc language = "";
438         String JavaDoc country = "";
439         String JavaDoc variant = "";
440         Locale locale = Locale.getDefault();
441         
442         if (!propertiesName.endsWith(".properties")) return "";
443         if (!propertiesName.startsWith(baseName)) return "";
444         
445         propertiesName = propertiesName.substring(baseName.length(), propertiesName.length() - ".properties".length());
446         
447         if (propertiesName.length() == 0) return "Default";
448         if (propertiesName.startsWith("_")) propertiesName = propertiesName.substring(1);
449         if (propertiesName.indexOf("_") > 0)
450         {
451             language = propertiesName.substring(0,propertiesName.indexOf("_"));
452             propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1);
453             
454             if (propertiesName.indexOf("_") > 0)
455             {
456                 country = propertiesName.substring(0,propertiesName.indexOf("_"));
457                 propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1);
458                 
459                 if (propertiesName.indexOf("_") > 0)
460                 {
461                     variant = propertiesName.substring(0,propertiesName.indexOf("_"));
462                     propertiesName = propertiesName.substring(propertiesName.indexOf("_")+1);
463                 }
464                 else
465                 {
466                     variant = propertiesName;
467                 }
468             }
469             else
470             {
471                 country = propertiesName;
472             }
473         }
474         else
475         {
476             language = propertiesName;
477         }
478         
479         locale = new Locale(language,country,variant);
480         
481         return locale.getDisplayName();
482         
483      }
484     
485     public void applyI18n(){
486                 // Start autogenerated code ----------------------
487
jButtonClose.setText(I18n.getString("localeResourceFilesDialog.buttonClose","Close"));
488                 jButtonCreateNewLocale.setText(I18n.getString("localeResourceFilesDialog.buttonCreateNewLocale","Create new locale"));
489                 jButtonDeleteFile.setText(I18n.getString("localeResourceFilesDialog.buttonDeleteFile","Delete file"));
490                 jButtonModifyFile.setText(I18n.getString("localeResourceFilesDialog.buttonModifyFile"," Modify file"));
491                 // End autogenerated code ----------------------
492

493                 jTable1.getColumnModel().getColumn(0).setHeaderValue( I18n.getString("localeResourceFilesDialog.tablecolumn.locale","Locale") );
494                 jTable1.getColumnModel().getColumn(1).setHeaderValue( I18n.getString("localeResourceFilesDialog.tablecolumn.fileName","File name") );
495
496     }
497 }
498
Popular Tags