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: * TranslationStatusDetailDialog.java
028: *
029: * Created on March 27, 2006, 8:06 PM
030: *
031: */
032:
033: package it.businesslogic.ireport.plugin.locale;
034:
035: import it.businesslogic.ireport.gui.MainFrame;
036: import it.businesslogic.ireport.plugin.locale.ResourceKey;
037: import it.businesslogic.ireport.util.Misc;
038: import java.awt.Cursor;
039: import java.io.File;
040: import java.io.FileOutputStream;
041: import java.io.OutputStreamWriter;
042: import java.io.PrintWriter;
043: import java.util.Iterator;
044: import java.util.List;
045: import java.util.Properties;
046: import javax.swing.DefaultListSelectionModel;
047: import javax.swing.JFileChooser;
048: import javax.swing.JOptionPane;
049: import javax.swing.SwingUtilities;
050: import javax.swing.event.ListSelectionEvent;
051: import javax.swing.event.ListSelectionListener;
052: import javax.swing.table.DefaultTableModel;
053: import javax.swing.table.TableColumn;
054: import javax.swing.table.TableColumnModel;
055: import it.businesslogic.ireport.util.I18n;
056:
057: /**
058: *
059: * @author gtoffoli
060: */
061: public class TranslationStatusDetailDialog extends javax.swing.JDialog
062: implements JasperBabylonRunnerListener {
063:
064: private java.util.Properties properties = null;
065: private String fileName = "";
066:
067: /** Creates new form TranslationStatusDialog */
068: public TranslationStatusDetailDialog(java.awt.Frame parent,
069: boolean modal) {
070: super (parent, modal);
071: initAll();
072: }
073:
074: /**
075: */
076: public TranslationStatusDetailDialog(java.awt.Dialog parent,
077: boolean modal) {
078: super (parent, modal);
079: initAll();
080: }
081:
082: private void initAll() {
083: initComponents();
084:
085: TableColumnModel tcm = jTableLanguages.getColumnModel();
086: TableColumn tc = tcm.getColumn(0);
087: tc.setCellRenderer(new MissingResourceCellRenderer());
088:
089: DefaultListSelectionModel dsm = new DefaultListSelectionModel();
090: dsm.setSelectionMode(dsm.SINGLE_SELECTION);
091: dsm.addListSelectionListener(new ListSelectionListener() {
092: public void valueChanged(ListSelectionEvent e) {
093: jButtonDetails.setEnabled(jTableLanguages
094: .getSelectedRow() >= 0);
095: }
096: });
097: jTableLanguages.setSelectionModel(dsm);
098:
099: applyI18n();
100: pack();
101: Misc.centerFrame(this );
102:
103: javax.swing.KeyStroke escape = javax.swing.KeyStroke
104: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
105: false);
106: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
107: public void actionPerformed(java.awt.event.ActionEvent e) {
108: jButtonOkActionPerformed(e);
109: }
110: };
111:
112: getRootPane().getInputMap(
113: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
114: escape, "ESCAPE");
115: getRootPane().getActionMap().put("ESCAPE", escapeAction);
116:
117: //to make the default button ...
118: this .getRootPane().setDefaultButton(this .jButtonOk);
119: }
120:
121: /*
122: * languageFile (es. ireport_it.properties)
123: */
124: public void setLanguage(String language,
125: Properties defaultTranslations) {
126: DefaultTableModel dtm = (DefaultTableModel) jTableLanguages
127: .getModel();
128: dtm.setRowCount(0);
129: properties = new Properties();
130: this .fileName = language;
131: try {
132: properties.load(MainFrame.getMainInstance()
133: .getReportClassLoader().getResourceAsStream(
134: "it/businesslogic/ireport/locale/"
135: + language));
136: } catch (Exception ex) {
137: }
138:
139: Iterator namesIterator = defaultTranslations.keySet()
140: .iterator();
141: String[] names = new String[defaultTranslations.keySet().size()];
142: for (int i = 0; i < names.length; ++i) {
143: names[i] = (String) namesIterator.next();
144:
145: }
146: java.util.Arrays.sort(names);
147:
148: double translated = 0;
149: for (int i = 0; i < names.length; ++i) {
150: String name = "" + names[i];
151: ResourceKey rk = new ResourceKey(name);
152: if (properties.getProperty(name) == null) {
153: rk.setMissing(true);
154: }
155: dtm.addRow(new Object[] {
156: rk,
157: ((!rk.isMissing()) ? properties.getProperty(name)
158: : ""),
159: defaultTranslations.getProperty(name) });
160:
161: }
162:
163: jTableLanguages.updateUI();
164: }
165:
166: /** This method is called from within the constructor to
167: * initialize the form
168: * WARNING: Do NOT modify this code. The content of this method is
169: * always regenerated by the Form Editor.
170: */
171: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
172: private void initComponents() {
173: java.awt.GridBagConstraints gridBagConstraints;
174:
175: jPanel2 = new javax.swing.JPanel();
176: jButtonImportFromJB = new javax.swing.JButton();
177: jButtonExportToJB = new javax.swing.JButton();
178: jScrollPane1 = new javax.swing.JScrollPane();
179: jTableLanguages = new javax.swing.JTable();
180: jPanel1 = new javax.swing.JPanel();
181: jButtonSave = new javax.swing.JButton();
182: jButtonDetails = new javax.swing.JButton();
183: jButtonOk = new javax.swing.JButton();
184:
185: getContentPane().setLayout(new java.awt.GridBagLayout());
186:
187: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
188: jPanel2.setLayout(new java.awt.GridBagLayout());
189:
190: jButtonImportFromJB.setText("Import from JasperBabylon");
191: jButtonImportFromJB
192: .setActionCommand("Import from JasperBabylon");
193: jButtonImportFromJB
194: .addActionListener(new java.awt.event.ActionListener() {
195: public void actionPerformed(
196: java.awt.event.ActionEvent evt) {
197: jButtonExportToJBjButtonDetailsActionPerformed2(evt);
198: }
199: });
200:
201: gridBagConstraints = new java.awt.GridBagConstraints();
202: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
203: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
204: jPanel2.add(jButtonImportFromJB, gridBagConstraints);
205:
206: jButtonExportToJB.setText("Export to JasperBabylon");
207: jButtonExportToJB.setActionCommand("Export to JasperBabylon");
208: jButtonExportToJB
209: .addActionListener(new java.awt.event.ActionListener() {
210: public void actionPerformed(
211: java.awt.event.ActionEvent evt) {
212: jButtonDetailsActionPerformed2(evt);
213: }
214: });
215:
216: gridBagConstraints = new java.awt.GridBagConstraints();
217: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
218: gridBagConstraints.weightx = 1.0;
219: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
220: jPanel2.add(jButtonExportToJB, gridBagConstraints);
221:
222: gridBagConstraints = new java.awt.GridBagConstraints();
223: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
224: gridBagConstraints.weightx = 1.0;
225: getContentPane().add(jPanel2, gridBagConstraints);
226:
227: jTableLanguages
228: .setModel(new javax.swing.table.DefaultTableModel(
229: new Object[][] {
230:
231: }, new String[] { "Key", "Translation",
232: "Default" }) {
233: Class[] types = new Class[] {
234: java.lang.String.class,
235: java.lang.String.class,
236: java.lang.String.class };
237: boolean[] canEdit = new boolean[] { false, true,
238: false };
239:
240: public Class getColumnClass(int columnIndex) {
241: return types[columnIndex];
242: }
243:
244: public boolean isCellEditable(int rowIndex,
245: int columnIndex) {
246: return canEdit[columnIndex];
247: }
248: });
249: jTableLanguages.setGridColor(new java.awt.Color(204, 204, 204));
250: jTableLanguages
251: .addMouseListener(new java.awt.event.MouseAdapter() {
252: public void mouseClicked(
253: java.awt.event.MouseEvent evt) {
254: jTableLanguagesMouseClicked(evt);
255: }
256: });
257:
258: jScrollPane1.setViewportView(jTableLanguages);
259:
260: gridBagConstraints = new java.awt.GridBagConstraints();
261: gridBagConstraints.gridx = 0;
262: gridBagConstraints.gridwidth = 2;
263: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
264: gridBagConstraints.weightx = 1.0;
265: gridBagConstraints.weighty = 1.0;
266: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
267: getContentPane().add(jScrollPane1, gridBagConstraints);
268:
269: jPanel1.setLayout(new java.awt.GridBagLayout());
270:
271: jButtonSave.setText("Save");
272: jButtonSave
273: .addActionListener(new java.awt.event.ActionListener() {
274: public void actionPerformed(
275: java.awt.event.ActionEvent evt) {
276: jButtonDetailsActionPerformed1(evt);
277: }
278: });
279:
280: gridBagConstraints = new java.awt.GridBagConstraints();
281: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
282: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
283: jPanel1.add(jButtonSave, gridBagConstraints);
284:
285: jButtonDetails.setLabel("Export to file");
286: jButtonDetails
287: .addActionListener(new java.awt.event.ActionListener() {
288: public void actionPerformed(
289: java.awt.event.ActionEvent evt) {
290: jButtonDetailsActionPerformed(evt);
291: }
292: });
293:
294: gridBagConstraints = new java.awt.GridBagConstraints();
295: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
296: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
297: jPanel1.add(jButtonDetails, gridBagConstraints);
298:
299: jButtonOk.setText("Close");
300: jButtonOk
301: .addActionListener(new java.awt.event.ActionListener() {
302: public void actionPerformed(
303: java.awt.event.ActionEvent evt) {
304: jButtonOkActionPerformed(evt);
305: }
306: });
307:
308: gridBagConstraints = new java.awt.GridBagConstraints();
309: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
310: gridBagConstraints.weightx = 1.0;
311: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
312: jPanel1.add(jButtonOk, gridBagConstraints);
313:
314: gridBagConstraints = new java.awt.GridBagConstraints();
315: gridBagConstraints.gridx = 0;
316: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
317: gridBagConstraints.weightx = 1.0;
318: getContentPane().add(jPanel1, gridBagConstraints);
319:
320: pack();
321: }// </editor-fold>//GEN-END:initComponents
322:
323: private void jButtonExportToJBjButtonDetailsActionPerformed2(
324: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportToJBjButtonDetailsActionPerformed2
325:
326: setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
327: JasperBabylonRunner jbr = new JasperBabylonRunner();
328: jbr.setRunnerListener(this );
329: String code = this .getFileName();
330: if (code.startsWith("Ireport_")) {
331: code = code.substring(8);
332: }
333: if (code.endsWith(".properties")) {
334: code = code.substring(0, code.length() - 11);
335: }
336: jbr.getLocale(code);
337:
338: }//GEN-LAST:event_jButtonExportToJBjButtonDetailsActionPerformed2
339:
340: private void jButtonDetailsActionPerformed2(
341: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed2
342:
343: if (JOptionPane
344: .showConfirmDialog(this ,
345: "Are you sure you want send your translation on JasperBabylon?") == JOptionPane.YES_OPTION) {
346: java.util.Properties p = new java.util.Properties();
347: for (int i = 0; i < jTableLanguages.getRowCount(); ++i) {
348: ResourceKey rk = (ResourceKey) jTableLanguages
349: .getValueAt(i, 0);
350: String value = (String) jTableLanguages
351: .getValueAt(i, 1);
352: if (value == null || value.length() == 0)
353: continue;
354: p.setProperty(rk.getKey(), value);
355: }
356:
357: setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
358: JasperBabylonRunner jbr = new JasperBabylonRunner();
359: jbr.setRunnerListener(this );
360: String code = this .getFileName();
361: if (code.startsWith("Ireport_")) {
362: code = code.substring(8);
363: }
364: if (code.endsWith(".properties")) {
365: code = code.substring(0, code.length() - 11);
366: }
367: jbr.putLocale(code, p);
368: }
369:
370: }//GEN-LAST:event_jButtonDetailsActionPerformed2
371:
372: private void jButtonDetailsActionPerformed1(
373: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed1
374:
375: java.util.Properties p = new java.util.Properties();
376: for (int i = 0; i < jTableLanguages.getRowCount(); ++i) {
377: ResourceKey rk = (ResourceKey) jTableLanguages.getValueAt(
378: i, 0);
379: String value = (String) jTableLanguages.getValueAt(i, 1);
380: if (value == null || value.length() == 0)
381: continue;
382: p.setProperty(rk.getKey(), value);
383: }
384: Misc.saveTemporaryLocale(p, this .getFileName());
385: if (this .getParent() != null
386: && this .getParent() instanceof TranslationStatusDialog) {
387: ((TranslationStatusDialog) this .getParent()).updateStatus();
388: }
389: jButtonOkActionPerformed(null);
390:
391: }//GEN-LAST:event_jButtonDetailsActionPerformed1
392:
393: private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
394:
395: this .setVisible(false);
396: this .dispose();
397: }//GEN-LAST:event_jButtonOkActionPerformed
398:
399: private void jButtonDetailsActionPerformed(
400: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed
401: // jfilechooser...
402: try {
403: JFileChooser jfc = new JFileChooser();
404: jfc.setSelectedFile(new File(getFileName()));
405: if (jfc.showSaveDialog(this ) == JFileChooser.APPROVE_OPTION) {
406:
407: FileOutputStream fos = new FileOutputStream(jfc
408: .getSelectedFile());
409: this .getProperties().store(fos, "");
410: PrintWriter pw = new PrintWriter(
411: new OutputStreamWriter(fos));
412: pw
413: .println("\n\n# ---------- Missing translations -------------");
414:
415: for (int i = 0; i < jTableLanguages.getRowCount(); ++i) {
416: ResourceKey rk = (ResourceKey) jTableLanguages
417: .getValueAt(i, 0);
418: if (rk.isMissing()) {
419: pw.println("#" + rk + "="
420: + jTableLanguages.getValueAt(i, 2));
421: }
422: }
423:
424: pw.close();
425: }
426: } catch (Exception ex) {
427: ex.printStackTrace();
428: }
429:
430: }//GEN-LAST:event_jButtonDetailsActionPerformed
431:
432: private void jTableLanguagesMouseClicked(
433: java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableLanguagesMouseClicked
434:
435: }//GEN-LAST:event_jTableLanguagesMouseClicked
436:
437: /**
438: * @param args the command line arguments
439: */
440: public static void main(String args[]) {
441: java.awt.EventQueue.invokeLater(new Runnable() {
442: public void run() {
443: new TranslationStatusDialog(new javax.swing.JFrame(),
444: true).setVisible(true);
445: }
446: });
447: }
448:
449: public java.util.Properties getProperties() {
450: return properties;
451: }
452:
453: public void setProperties(java.util.Properties properties) {
454: this .properties = properties;
455: }
456:
457: public String getFileName() {
458: return fileName;
459: }
460:
461: public void setFileName(String fileName) {
462: this .fileName = fileName;
463: }
464:
465: // Variables declaration - do not modify//GEN-BEGIN:variables
466: private javax.swing.JButton jButtonDetails;
467: private javax.swing.JButton jButtonExportToJB;
468: private javax.swing.JButton jButtonImportFromJB;
469: private javax.swing.JButton jButtonOk;
470: private javax.swing.JButton jButtonSave;
471: private javax.swing.JPanel jPanel1;
472: private javax.swing.JPanel jPanel2;
473: private javax.swing.JScrollPane jScrollPane1;
474: private javax.swing.JTable jTableLanguages;
475:
476: // End of variables declaration//GEN-END:variables
477:
478: public void applyI18n() {
479: // Start autogenerated code ----------------------
480: jButtonDetails.setText(I18n
481: .getString(
482: "translationStatusDetailDialog.buttonDetails",
483: "Export"));
484: jButtonOk.setText(I18n.getString(
485: "translationStatusDetailDialog.buttonOk", "Close"));
486: // End autogenerated code ----------------------
487:
488: jTableLanguages
489: .getColumnModel()
490: .getColumn(0)
491: .setHeaderValue(
492: I18n
493: .getString(
494: "translationStatusDetailDialog.tablecolumn.key",
495: "Key"));
496: jTableLanguages
497: .getColumnModel()
498: .getColumn(1)
499: .setHeaderValue(
500: I18n
501: .getString(
502: "translationStatusDetailDialog.tablecolumn.translation",
503: "Translation"));
504: jTableLanguages
505: .getColumnModel()
506: .getColumn(2)
507: .setHeaderValue(
508: I18n
509: .getString(
510: "translationStatusDetailDialog.tablecolumn.default",
511: "Default"));
512:
513: jButtonExportToJB.setText(I18n.getString(
514: "translationStatusDetailDialog.buttonExportToJB",
515: "Export to JasperBabylon"));
516: jButtonImportFromJB.setText(I18n.getString(
517: "translationStatusDetailDialog.buttonImportFromJB",
518: "Import from JasperBabylon"));
519: jButtonSave.setText(I18n.getString(
520: "translationStatusDetailDialog.buttonSave", "Save"));
521: }
522:
523: public void listLocalesComplete(List list) {
524: }
525:
526: public void getLocaleComplete(Properties props) {
527:
528: for (int i = 0; i < jTableLanguages.getRowCount(); ++i) {
529: ResourceKey rk = (ResourceKey) jTableLanguages.getValueAt(
530: i, 0);
531: if (props.getProperty(rk.getKey()) != null) {
532: rk.setMissing(false);
533: jTableLanguages.setValueAt(props.getProperty(rk
534: .getKey()), i, 1);
535: }
536: }
537:
538: try {
539: SwingUtilities.invokeAndWait(new Runnable() {
540: public void run() {
541: setCursor(Cursor.getDefaultCursor());
542: jTableLanguages.updateUI();
543: }
544: });
545: } catch (Exception ex) {
546: ex.printStackTrace();
547: }
548: }
549:
550: public void putLocaleComplete(String returnMessage) {
551:
552: try {
553: final String msg = returnMessage;
554: SwingUtilities.invokeAndWait(new Runnable() {
555: public void run() {
556: setCursor(Cursor.getDefaultCursor());
557: JOptionPane.showMessageDialog(
558: TranslationStatusDetailDialog.this , msg);
559: }
560: });
561: } catch (Exception ex) {
562: }
563: }
564:
565: public void operationError(int operation, String message) {
566: try {
567: final String msg = message;
568: SwingUtilities.invokeAndWait(new Runnable() {
569: public void run() {
570: setCursor(Cursor.getDefaultCursor());
571: JOptionPane.showMessageDialog(
572: TranslationStatusDetailDialog.this , msg);
573: }
574: });
575: } catch (Exception ex) {
576: }
577: }
578: }
|