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: * LocaleResourceFilesDialog.java
028: *
029: * Created on 29 settembre 2004, 0.56
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.locale;
034:
035: import it.businesslogic.ireport.gui.*;
036: import it.businesslogic.ireport.*;
037: import javax.swing.tree.*;
038: import javax.swing.table.*;
039: import javax.swing.*;
040: import it.businesslogic.ireport.chart.*;
041: import java.util.*;
042: import java.io.*;
043: import javax.swing.event.*;
044: import it.businesslogic.ireport.util.I18n;
045:
046: /**
047: *
048: * @author Administrator
049: */
050: public class LocaleResourceFilesDialog extends javax.swing.JDialog {
051:
052: private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
053: private JReportFrame jReportFrame = null;
054:
055: /** Creates new form IReportChartDialog */
056: public LocaleResourceFilesDialog(java.awt.Dialog parent,
057: boolean modal) {
058: super (parent, modal);
059: initComponents();
060: initFrame();
061: }
062:
063: public void initFrame() {
064: javax.swing.DefaultListModel dlm = new javax.swing.DefaultListModel();
065:
066: this .setSize(500, 400);
067: it.businesslogic.ireport.util.Misc.centerFrame(this );
068:
069: DefaultListSelectionModel dlsm = (DefaultListSelectionModel) this .jTable1
070: .getSelectionModel();
071: dlsm
072: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
073: public void valueChanged(ListSelectionEvent e) {
074: jTable1ListSelectionValueChanged(e);
075: }
076: });
077:
078: javax.swing.KeyStroke escape = javax.swing.KeyStroke
079: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
080: false);
081: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
082: public void actionPerformed(java.awt.event.ActionEvent e) {
083: jButtonCloseActionPerformed(e);
084: }
085: };
086:
087: getRootPane().getInputMap(
088: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
089: escape, "ESCAPE");
090: getRootPane().getActionMap().put("ESCAPE", escapeAction);
091:
092: applyI18n();
093: //to make the default button ...
094: this .getRootPane().setDefaultButton(this .jButtonClose);
095: }
096:
097: public void jTable1ListSelectionValueChanged(
098: javax.swing.event.ListSelectionEvent e) {
099: if (this .jTable1.getSelectedRowCount() > 0) {
100: this .jButtonModifyFile.setEnabled(true);
101: this .jButtonDeleteFile.setEnabled(true);
102: } else {
103: this .jButtonModifyFile.setEnabled(false);
104: this .jButtonDeleteFile.setEnabled(false);
105: }
106: }
107:
108: public LocaleResourceFilesDialog(java.awt.Frame parent,
109: boolean modal) {
110: super (parent, modal);
111: initComponents();
112: initFrame();
113: }
114:
115: public void updateFileList() {
116: DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
117: dtm.setRowCount(0);
118: if (getJReportFrame() == null) {
119: jTable1.updateUI();
120: return;
121: }
122:
123: Report report = getJReportFrame().getReport();
124: // Looking for locale files for this report...
125: if (report.getFilename() == null
126: || report.getFilename().trim().equals("")) {
127: return;
128: }
129:
130: if (report.getResourceBundleBaseName() == null
131: || report.getResourceBundleBaseName().trim().equals("")) {
132: return;
133: } else {
134: String basename = report.getResourceBundleBaseName();
135: File f = new File(report.getFilename());
136: File dir = f.getParentFile();
137: String[] files = dir.list();
138: for (int i = 0; i < files.length; ++i) {
139: if (files[i].startsWith(basename)
140: && files[i].endsWith(".properties")) {
141: dtm
142: .addRow(new Object[] {
143: getLocaleName(basename, files[i]),
144: files[i] });
145: }
146: }
147: }
148:
149: jTable1.updateUI();
150: }
151:
152: public JReportFrame getJReportFrame() {
153:
154: return jReportFrame;
155: }
156:
157: public void setJReportFrame(JReportFrame jReportFrame) {
158:
159: this .jReportFrame = jReportFrame;
160: updateFileList();
161: }
162:
163: public int getDialogResult() {
164: return dialogResult;
165: }
166:
167: public void setDialogResult(int dialogResult) {
168: this .dialogResult = dialogResult;
169: }
170:
171: /** This method is called from within the constructor to
172: * initialize the form.
173: * WARNING: Do NOT modify this code. The content of this method is
174: * always regenerated by the Form Editor.
175: */
176: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
177: private void initComponents() {
178: java.awt.GridBagConstraints gridBagConstraints;
179:
180: jPanelData = new javax.swing.JPanel();
181: jScrollPane3 = new javax.swing.JScrollPane();
182: jTable1 = new javax.swing.JTable();
183: jPanel2 = new javax.swing.JPanel();
184: jButtonCreateNewLocale = new javax.swing.JButton();
185: jButtonModifyFile = new javax.swing.JButton();
186: jButtonDeleteFile = new javax.swing.JButton();
187: jPanel3 = new javax.swing.JPanel();
188: jButtonClose = new javax.swing.JButton();
189:
190: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
191: setTitle("Report Imports");
192: jPanelData.setLayout(new java.awt.GridBagLayout());
193:
194: jTable1.setModel(new javax.swing.table.DefaultTableModel(
195: new Object[][] {
196:
197: }, new String[] { "Locale", "File name" }) {
198: boolean[] canEdit = new boolean[] { false, false };
199:
200: public boolean isCellEditable(int rowIndex, int columnIndex) {
201: return canEdit[columnIndex];
202: }
203: });
204: jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
205: public void mouseClicked(java.awt.event.MouseEvent evt) {
206: jTable1MouseClicked(evt);
207: }
208: });
209:
210: jScrollPane3.setViewportView(jTable1);
211:
212: gridBagConstraints = new java.awt.GridBagConstraints();
213: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
214: gridBagConstraints.weightx = 1.0;
215: gridBagConstraints.weighty = 1.0;
216: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
217: jPanelData.add(jScrollPane3, gridBagConstraints);
218:
219: jPanel2.setLayout(new java.awt.GridBagLayout());
220:
221: jPanel2.setMinimumSize(new java.awt.Dimension(140, 151));
222: jPanel2.setPreferredSize(new java.awt.Dimension(180, 100));
223: jButtonCreateNewLocale.setText("Create new locale");
224: jButtonCreateNewLocale
225: .addActionListener(new java.awt.event.ActionListener() {
226: public void actionPerformed(
227: java.awt.event.ActionEvent evt) {
228: jButtonCreateNewLocaleActionPerformed(evt);
229: }
230: });
231:
232: gridBagConstraints = new java.awt.GridBagConstraints();
233: gridBagConstraints.gridx = 0;
234: gridBagConstraints.gridy = 1;
235: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
236: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
237: gridBagConstraints.weightx = 1.0;
238: jPanel2.add(jButtonCreateNewLocale, gridBagConstraints);
239:
240: jButtonModifyFile.setText(" Modify file");
241: jButtonModifyFile.setActionCommand("Modify locale");
242: jButtonModifyFile.setEnabled(false);
243: jButtonModifyFile
244: .addActionListener(new java.awt.event.ActionListener() {
245: public void actionPerformed(
246: java.awt.event.ActionEvent evt) {
247: jButtonModifyFileActionPerformed(evt);
248: }
249: });
250:
251: gridBagConstraints = new java.awt.GridBagConstraints();
252: gridBagConstraints.gridx = 0;
253: gridBagConstraints.gridy = 4;
254: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
255: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
256: gridBagConstraints.weightx = 1.0;
257: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
258: jPanel2.add(jButtonModifyFile, gridBagConstraints);
259:
260: jButtonDeleteFile.setText("Delete file");
261: jButtonDeleteFile.setEnabled(false);
262: jButtonDeleteFile
263: .addActionListener(new java.awt.event.ActionListener() {
264: public void actionPerformed(
265: java.awt.event.ActionEvent evt) {
266: jButtonDeleteFileActionPerformed(evt);
267: }
268: });
269:
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.gridy = 5;
273: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
274: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
275: gridBagConstraints.weightx = 1.0;
276: gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
277: jPanel2.add(jButtonDeleteFile, gridBagConstraints);
278:
279: gridBagConstraints = new java.awt.GridBagConstraints();
280: gridBagConstraints.gridx = 0;
281: gridBagConstraints.gridy = 10;
282: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
283: gridBagConstraints.weightx = 1.0;
284: gridBagConstraints.weighty = 1.0;
285: jPanel2.add(jPanel3, gridBagConstraints);
286:
287: jButtonClose.setText("Close");
288: jButtonClose
289: .addActionListener(new java.awt.event.ActionListener() {
290: public void actionPerformed(
291: java.awt.event.ActionEvent evt) {
292: jButtonCloseActionPerformed(evt);
293: }
294: });
295:
296: gridBagConstraints = new java.awt.GridBagConstraints();
297: gridBagConstraints.gridx = 0;
298: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
299: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
300: gridBagConstraints.weightx = 1.0;
301: jPanel2.add(jButtonClose, gridBagConstraints);
302:
303: gridBagConstraints = new java.awt.GridBagConstraints();
304: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
305: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
306: jPanelData.add(jPanel2, gridBagConstraints);
307:
308: getContentPane().add(jPanelData, java.awt.BorderLayout.CENTER);
309:
310: pack();
311: }// </editor-fold>//GEN-END:initComponents
312:
313: private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
314: if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) {
315: jButtonModifyFileActionPerformed(null);
316:
317: }
318: }//GEN-LAST:event_jTable1MouseClicked
319:
320: private void jButtonDeleteFileActionPerformed(
321: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteFileActionPerformed
322:
323: if (jTable1.getSelectedRow() >= 0) {
324: Report report = getJReportFrame().getReport();
325: File f = new File(report.getFilename());
326: File dir = f.getParentFile();
327: File localeFile = new File(dir, ""
328: + jTable1.getValueAt(jTable1.getSelectedRow(), 1));
329: try {
330: localeFile.delete();
331: } catch (Exception ex) {
332: }
333: updateFileList();
334: }
335:
336: }//GEN-LAST:event_jButtonDeleteFileActionPerformed
337:
338: private void jButtonModifyFileActionPerformed(
339: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModifyFileActionPerformed
340:
341: if (jTable1.getSelectedRow() >= 0) {
342: Report report = getJReportFrame().getReport();
343: File f = new File(report.getFilename());
344: File dir = f.getParentFile();
345: File localeFile = new File(dir, ""
346: + jTable1.getValueAt(jTable1.getSelectedRow(), 1));
347: LocaleEditorDialog led = new LocaleEditorDialog(this
348: .getJReportFrame().getMainFrame(), false);
349: led.setFile(localeFile);
350: led.setVisible(true);
351: try {
352: led.setTitle(localeFile.getName());
353: } catch (Exception ex) {
354: }
355: }
356: }//GEN-LAST:event_jButtonModifyFileActionPerformed
357:
358: private void jButtonCreateNewLocaleActionPerformed(
359: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateNewLocaleActionPerformed
360:
361: String suffix = "";
362:
363: Report report = getJReportFrame().getReport();
364: if (report.getFilename() == null
365: || report.getFilename().trim().equals("")) {
366:
367: javax.swing.JOptionPane
368: .showMessageDialog(
369: this ,
370: I18n
371: .getString(
372: "messages.localeResourceFilesDialog.saveFile",
373: "To create a new locale file please save your report first."));
374:
375: return;
376: }
377:
378: File f = new File(report.getFilename());
379:
380: NewLocaleFileDialog fd = new NewLocaleFileDialog(this , true);
381: fd.setVisible(true);
382: File localeFile = null;
383: if (fd.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
384: try {
385: suffix = fd.getText();
386:
387: if (report.getResourceBundleBaseName() == null
388: || report.getResourceBundleBaseName().trim()
389: .equals("")) {
390: String filename = f.getName();
391: int index = filename.lastIndexOf(".");
392: if (index >= 0) {
393: filename = filename.substring(0, index);
394: }
395: report.setResourceBundleBaseName(filename);
396: }
397:
398: File dir = f.getParentFile();
399: localeFile = new File(dir, report
400: .getResourceBundleBaseName()
401: + ".properties");
402: // 1. Try to load default...
403: String str = "# Locale " + suffix + " for report "
404: + f.getName() + "\n";
405: try {
406: FileInputStream fis = new FileInputStream(
407: localeFile);
408: byte[] buffer = new byte[1024];
409: int read = 0;
410: while ((read = fis.read(buffer)) > 0) {
411: str += new String(buffer, 0, read);
412: }
413: fis.close();
414: } catch (Exception ex) {
415: }
416:
417: localeFile = new File(dir, report
418: .getResourceBundleBaseName()
419: + suffix + ".properties");
420: FileOutputStream fos = new FileOutputStream(localeFile);
421: fos.write(str.getBytes());
422: fos.close();
423:
424: updateFileList();
425:
426: } catch (Exception ex) {
427:
428: javax.swing.JOptionPane
429: .showMessageDialog(
430: this ,
431: I18n
432: .getFormattedString(
433: "messages.localeResourceFilesDialog.errorCreatingNewLocale",
434: "Error creating new locale {0}:\n{1}",
435: new Object[] {
436: localeFile
437: .getName(),
438: ex.getMessage() }));
439: }
440: }
441: }//GEN-LAST:event_jButtonCreateNewLocaleActionPerformed
442:
443: private void jButtonCloseActionPerformed(
444: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
445:
446: this .setDialogResult(javax.swing.JOptionPane.OK_OPTION);
447: this .setVisible(false);
448: }//GEN-LAST:event_jButtonCloseActionPerformed
449:
450: // Variables declaration - do not modify//GEN-BEGIN:variables
451: private javax.swing.JButton jButtonClose;
452: private javax.swing.JButton jButtonCreateNewLocale;
453: private javax.swing.JButton jButtonDeleteFile;
454: private javax.swing.JButton jButtonModifyFile;
455: private javax.swing.JPanel jPanel2;
456: private javax.swing.JPanel jPanel3;
457: private javax.swing.JPanel jPanelData;
458: private javax.swing.JScrollPane jScrollPane3;
459: private javax.swing.JTable jTable1;
460:
461: // End of variables declaration//GEN-END:variables
462:
463: public static String getLocaleName(String baseName,
464: String propertiesName) {
465: String language = "";
466: String country = "";
467: String variant = "";
468: Locale locale = Locale.getDefault();
469:
470: if (!propertiesName.endsWith(".properties"))
471: return "";
472: if (!propertiesName.startsWith(baseName))
473: return "";
474:
475: propertiesName = propertiesName.substring(baseName.length(),
476: propertiesName.length() - ".properties".length());
477:
478: if (propertiesName.length() == 0)
479: return "Default";
480: if (propertiesName.startsWith("_"))
481: propertiesName = propertiesName.substring(1);
482: if (propertiesName.indexOf("_") > 0) {
483: language = propertiesName.substring(0, propertiesName
484: .indexOf("_"));
485: propertiesName = propertiesName.substring(propertiesName
486: .indexOf("_") + 1);
487:
488: if (propertiesName.indexOf("_") > 0) {
489: country = propertiesName.substring(0, propertiesName
490: .indexOf("_"));
491: propertiesName = propertiesName
492: .substring(propertiesName.indexOf("_") + 1);
493:
494: if (propertiesName.indexOf("_") > 0) {
495: variant = propertiesName.substring(0,
496: propertiesName.indexOf("_"));
497: propertiesName = propertiesName
498: .substring(propertiesName.indexOf("_") + 1);
499: } else {
500: variant = propertiesName;
501: }
502: } else {
503: country = propertiesName;
504: }
505: } else {
506: language = propertiesName;
507: }
508:
509: locale = new Locale(language, country, variant);
510:
511: return locale.getDisplayName();
512:
513: }
514:
515: public void applyI18n() {
516: // Start autogenerated code ----------------------
517: jButtonClose.setText(I18n.getString(
518: "localeResourceFilesDialog.buttonClose", "Close"));
519: jButtonCreateNewLocale.setText(I18n.getString(
520: "localeResourceFilesDialog.buttonCreateNewLocale",
521: "Create new locale"));
522: jButtonDeleteFile.setText(I18n.getString(
523: "localeResourceFilesDialog.buttonDeleteFile",
524: "Delete file"));
525: jButtonModifyFile.setText(I18n.getString(
526: "localeResourceFilesDialog.buttonModifyFile",
527: " Modify file"));
528: // End autogenerated code ----------------------
529:
530: jTable1.getColumnModel().getColumn(0).setHeaderValue(
531: I18n.getString(
532: "localeResourceFilesDialog.tablecolumn.locale",
533: "Locale"));
534: jTable1
535: .getColumnModel()
536: .getColumn(1)
537: .setHeaderValue(
538: I18n
539: .getString(
540: "localeResourceFilesDialog.tablecolumn.fileName",
541: "File name"));
542: this .setTitle(I18n.getString("localeResourceFilesDialog.title",
543: "Locale resource bundles"));
544: }
545: }
|