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: * TranslationStatusDialog.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.IReportConnection;
036: import it.businesslogic.ireport.gui.MainFrame;
037: import it.businesslogic.ireport.util.LocaleAdapter;
038: import it.businesslogic.ireport.util.Misc;
039: import java.io.File;
040: import java.io.FileOutputStream;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Locale;
044: import java.util.Properties;
045: import javax.swing.DefaultListSelectionModel;
046: import javax.swing.JOptionPane;
047: import javax.swing.event.ListSelectionEvent;
048: import javax.swing.event.ListSelectionListener;
049: import javax.swing.table.DefaultTableModel;
050: import javax.swing.table.TableColumn;
051: import javax.swing.table.TableColumnModel;
052: import it.businesslogic.ireport.util.I18n;
053: import java.sql.PreparedStatement;
054: import java.util.Enumeration;
055:
056: /**
057: *
058: * @author gtoffoli
059: */
060: public class TranslationStatusDialog extends javax.swing.JDialog {
061:
062: /** Creates new form TranslationStatusDialog */
063: public TranslationStatusDialog(java.awt.Frame parent, boolean modal) {
064: super (parent, modal);
065: initComponents();
066:
067: TableColumnModel tcm = jTableLanguages.getColumnModel();
068: TableColumn tc = tcm.getColumn(1);
069: tc.setCellRenderer(new ProgressBarCellRenderer());
070:
071: DefaultListSelectionModel dsm = new DefaultListSelectionModel();
072: dsm.setSelectionMode(dsm.SINGLE_SELECTION);
073: dsm.addListSelectionListener(new ListSelectionListener() {
074: public void valueChanged(ListSelectionEvent e) {
075: jButtonDetails.setEnabled(jTableLanguages
076: .getSelectedRow() >= 0);
077: }
078: });
079: jTableLanguages.setSelectionModel(dsm);
080:
081: applyI18n();
082:
083: updateStatus();
084:
085: this .jButtonInsertDb.setVisible(false);
086:
087: pack();
088: Misc.centerFrame(this );
089:
090: javax.swing.KeyStroke escape = javax.swing.KeyStroke
091: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
092: false);
093: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
094: public void actionPerformed(java.awt.event.ActionEvent e) {
095: jButtonOkActionPerformed(e);
096: }
097: };
098:
099: getRootPane().getInputMap(
100: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
101: escape, "ESCAPE");
102: getRootPane().getActionMap().put("ESCAPE", escapeAction);
103:
104: //to make the default button ...
105: this .getRootPane().setDefaultButton(this .jButtonOk);
106: }
107:
108: public void updateStatus() {
109:
110: List listOfLanguages = it.businesslogic.ireport.util.I18n
111: .getListOfAvailLanguages();
112:
113: DefaultTableModel dtm = (DefaultTableModel) jTableLanguages
114: .getModel();
115: dtm.setRowCount(0);
116:
117: //dtm.addRow(new Object[]{"Default",null,null});
118:
119: Properties props_default = new Properties();
120: try {
121: props_default
122: .load(MainFrame
123: .getMainInstance()
124: .getReportClassLoader()
125: .getResourceAsStream(
126: "it/businesslogic/ireport/locale/Ireport.properties"));
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: }
130:
131: jLabelLocalizableString.setText(I18n.getFormattedString(
132: "translationStatusDialog.localizableStrings",
133: "Localizable strings: {0}", new Object[] { new Integer(
134: props_default.size() - 1) }));
135:
136: for (int i = 0; i < listOfLanguages.size(); ++i) {
137: Locale lang = (Locale) listOfLanguages.get(i);
138: LocaleAdapter adapter = new LocaleAdapter(lang);
139: String language = lang.getLanguage();
140: String country = lang.getCountry();
141: String variant = lang.getVariant();
142:
143: String language_name = (language.length() > 0) ? "_"
144: + language : "";
145: language_name += (country.length() > 0) ? "_" + country
146: : "";
147: language_name += (variant.length() > 0) ? "_" + variant
148: : "";
149:
150: language_name = "Ireport" + language_name + ".properties";
151:
152: int percentage_language = getLanguageCompletation(
153: props_default, language_name);
154: /*
155: if( lang.getCountry().equals(selectedCountry) &&
156: lang.getLanguage().equals(selectedLanguage) &&
157: lang.getVariant().equals(selectedVariant) ) {
158: def_language = "" + adapter;
159: }
160: */
161:
162: dtm.addRow(new Object[] { adapter,
163: new Integer(percentage_language), language_name });
164: }
165:
166: }
167:
168: public int getLanguageCompletation(Properties defaultTranslations,
169: String language) {
170: Properties props2 = new Properties();
171: try {
172: props2.load(MainFrame.getMainInstance()
173: .getReportClassLoader().getResourceAsStream(
174: "it/businesslogic/ireport/locale/"
175: + language));
176: } catch (Exception ex) {
177: }
178:
179: double size = 0;
180: Iterator namesIterator = defaultTranslations.keySet()
181: .iterator();
182: size = defaultTranslations.size();
183: double translated = 0;
184: while (namesIterator.hasNext()) {
185: String name = "" + namesIterator.next();
186: if (name.equals("defaultFont")) {
187: size--;
188: continue;
189: }
190: if (props2.getProperty(name) != null) {
191: translated++;
192: }
193: }
194: if (translated == 0)
195: return 0;
196: return (int) ((translated / size) * 100);
197: }
198:
199: /** This method is called from within the constructor to
200: * initialize the form
201: * WARNING: Do NOT modify this code. The content of this method is
202: * always regenerated by the Form Editor.
203: */
204: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
205: private void initComponents() {
206: java.awt.GridBagConstraints gridBagConstraints;
207:
208: jLabelLocalizableString = new javax.swing.JLabel();
209: jScrollPane1 = new javax.swing.JScrollPane();
210: jTableLanguages = new javax.swing.JTable();
211: jPanel1 = new javax.swing.JPanel();
212: jButtonDetails = new javax.swing.JButton();
213: jButtonInsertDb = new javax.swing.JButton();
214: jButtonExportToJB = new javax.swing.JButton();
215: jButtonOk = new javax.swing.JButton();
216:
217: getContentPane().setLayout(new java.awt.GridBagLayout());
218:
219: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
220: jLabelLocalizableString.setText("Localizable strings:");
221: gridBagConstraints = new java.awt.GridBagConstraints();
222: gridBagConstraints.gridwidth = 2;
223: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
224: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
225: getContentPane().add(jLabelLocalizableString,
226: gridBagConstraints);
227:
228: jTableLanguages
229: .setModel(new javax.swing.table.DefaultTableModel(
230: new Object[][] {
231:
232: }, new String[] { "Title 1", "Title 2",
233: "Title 3" }) {
234: boolean[] canEdit = new boolean[] { false, false,
235: false };
236:
237: public boolean isCellEditable(int rowIndex,
238: int columnIndex) {
239: return canEdit[columnIndex];
240: }
241: });
242: jTableLanguages.setGridColor(new java.awt.Color(204, 204, 204));
243: jTableLanguages.setShowVerticalLines(false);
244: jTableLanguages
245: .addMouseListener(new java.awt.event.MouseAdapter() {
246: public void mouseClicked(
247: java.awt.event.MouseEvent evt) {
248: jTableLanguagesMouseClicked(evt);
249: }
250: });
251:
252: jScrollPane1.setViewportView(jTableLanguages);
253:
254: gridBagConstraints = new java.awt.GridBagConstraints();
255: gridBagConstraints.gridx = 0;
256: gridBagConstraints.gridwidth = 2;
257: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
258: gridBagConstraints.weightx = 1.0;
259: gridBagConstraints.weighty = 1.0;
260: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
261: getContentPane().add(jScrollPane1, gridBagConstraints);
262:
263: jPanel1.setLayout(new java.awt.GridBagLayout());
264:
265: jButtonDetails.setText("Details...");
266: jButtonDetails.setEnabled(false);
267: jButtonDetails
268: .addActionListener(new java.awt.event.ActionListener() {
269: public void actionPerformed(
270: java.awt.event.ActionEvent evt) {
271: jButtonDetailsActionPerformed(evt);
272: }
273: });
274:
275: gridBagConstraints = new java.awt.GridBagConstraints();
276: gridBagConstraints.gridx = 0;
277: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
278: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
279: jPanel1.add(jButtonDetails, gridBagConstraints);
280:
281: jButtonInsertDb.setText("InsertDB");
282: jButtonInsertDb
283: .addActionListener(new java.awt.event.ActionListener() {
284: public void actionPerformed(
285: java.awt.event.ActionEvent evt) {
286: jButtonInsertDbActionPerformed(evt);
287: }
288: });
289:
290: jPanel1.add(jButtonInsertDb, new java.awt.GridBagConstraints());
291:
292: jButtonExportToJB.setText("Import from JasperBabylon");
293: jButtonExportToJB
294: .addActionListener(new java.awt.event.ActionListener() {
295: public void actionPerformed(
296: java.awt.event.ActionEvent evt) {
297: jButtonExportToJBjButtonDetailsActionPerformed2(evt);
298: }
299: });
300:
301: gridBagConstraints = new java.awt.GridBagConstraints();
302: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
303: gridBagConstraints.weightx = 1.0;
304: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
305: jPanel1.add(jButtonExportToJB, gridBagConstraints);
306:
307: jButtonOk.setText("Close");
308: jButtonOk
309: .addActionListener(new java.awt.event.ActionListener() {
310: public void actionPerformed(
311: java.awt.event.ActionEvent evt) {
312: jButtonOkActionPerformed(evt);
313: }
314: });
315:
316: gridBagConstraints = new java.awt.GridBagConstraints();
317: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
318: gridBagConstraints.weightx = 1.0;
319: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
320: jPanel1.add(jButtonOk, gridBagConstraints);
321:
322: gridBagConstraints = new java.awt.GridBagConstraints();
323: gridBagConstraints.gridx = 0;
324: gridBagConstraints.gridwidth = 2;
325: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
326: gridBagConstraints.weightx = 1.0;
327: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
328: getContentPane().add(jPanel1, gridBagConstraints);
329:
330: pack();
331: }// </editor-fold>//GEN-END:initComponents
332:
333: private void jButtonExportToJBjButtonDetailsActionPerformed2(
334: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportToJBjButtonDetailsActionPerformed2
335:
336: QueryLocalesDialog qld = new QueryLocalesDialog(this , true);
337: qld.setVisible(true);
338: if (qld.getDialogResult() == JOptionPane.OK_OPTION) {
339: Misc.saveTemporaryLocale(qld.getLocaleProperties(),
340: "Ireport_" + qld.getLocale_code() + ".properties");
341: this .updateStatus();
342: }
343:
344: }//GEN-LAST:event_jButtonExportToJBjButtonDetailsActionPerformed2
345:
346: private void jButtonInsertDbActionPerformed(
347: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInsertDbActionPerformed
348:
349: IReportConnection c = (IReportConnection) MainFrame
350: .getMainInstance().getProperties().get(
351: "DefaultConnection");
352: java.sql.Connection conn = null;
353: PreparedStatement stmt = null;
354: try {
355: if (c == null
356: || !(c instanceof it.businesslogic.ireport.connection.JDBCConnection)) {
357: javax.swing.JOptionPane.showMessageDialog(this ,
358: "Not a JDBC connection", "",
359: javax.swing.JOptionPane.ERROR_MESSAGE);
360: return;
361: }
362:
363: conn = c.getConnection();
364: conn.createStatement().execute(
365: "delete from translations where app_id=1");
366:
367: stmt = conn
368: .prepareStatement("insert into translations (app_id, language_id, s_key, s_value) values(1,?,?,?);");
369:
370: // 1. Add default keys...
371: Properties props_default = new Properties();
372: try {
373: props_default
374: .load(MainFrame
375: .getMainInstance()
376: .getReportClassLoader()
377: .getResourceAsStream(
378: "it/businesslogic/ireport/locale/Ireport.properties"));
379: } catch (Exception ex) {
380: ex.printStackTrace();
381: }
382: Enumeration enum_keys = props_default.keys();
383: while (enum_keys.hasMoreElements()) {
384: String key = ("" + enum_keys.nextElement()).trim();
385: String val = props_default.getProperty(key);
386:
387: stmt.setString(1, "");
388: stmt.setString(2, key);
389: stmt.setString(3, val);
390: stmt.executeUpdate();
391: }
392:
393: List listOfLanguages = it.businesslogic.ireport.util.I18n
394: .getListOfAvailLanguages();
395:
396: System.out.println("Languages: " + listOfLanguages.size());
397: for (int k = 0; k < listOfLanguages.size(); ++k) {
398: Locale lang = (Locale) listOfLanguages.get(k);
399: LocaleAdapter adapter = new LocaleAdapter(lang);
400: String language = lang.getLanguage();
401: String country = lang.getCountry();
402: String variant = lang.getVariant();
403:
404: String language_id = language;
405: language_id += (country.length() > 0) ? "_" + country
406: : "";
407: language_id += (variant.length() > 0) ? "_" + variant
408: : "";
409:
410: String language_name = "Ireport"
411: + ((language.length() > 0) ? "_" : "")
412: + language_id + ".properties";
413:
414: System.out.println("Working on " + language_name);
415:
416: // 2. Add keys for each langauge
417: Properties props2 = new Properties();
418: try {
419: props2.load(MainFrame.getMainInstance()
420: .getReportClassLoader()
421: .getResourceAsStream(
422: "it/businesslogic/ireport/locale/"
423: + language_name));
424: } catch (Exception ex) {
425: ex.printStackTrace();
426: }
427:
428: enum_keys = props2.keys();
429: while (enum_keys.hasMoreElements()) {
430: String key = ("" + enum_keys.nextElement()).trim();
431: String val = props2.getProperty(key);
432: if (props_default.containsKey(key)) {
433: stmt.setString(1, language_id);
434: stmt.setString(2, key);
435: stmt.setString(3, val);
436: stmt.executeUpdate();
437: }
438: }
439: }
440:
441: } catch (Exception ex) {
442: ex.printStackTrace();
443: } finally {
444: if (conn != null)
445: try {
446: conn.close();
447: } catch (Exception ex) {
448: }
449: }
450:
451: }//GEN-LAST:event_jButtonInsertDbActionPerformed
452:
453: private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
454:
455: this .setVisible(false);
456: this .dispose();
457:
458: }//GEN-LAST:event_jButtonOkActionPerformed
459:
460: private void jTableLanguagesMouseClicked(
461: java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableLanguagesMouseClicked
462: if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1) {
463: jButtonDetailsActionPerformed(null);
464: }
465:
466: }//GEN-LAST:event_jTableLanguagesMouseClicked
467:
468: private void jButtonDetailsActionPerformed(
469: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDetailsActionPerformed
470:
471: if (jTableLanguages.getSelectedRow() < 0)
472: return;
473: int row = jTableLanguages.getSelectedRow();
474:
475: LocaleAdapter adapter = (LocaleAdapter) jTableLanguages
476: .getValueAt(jTableLanguages.getSelectedRow(), 0);
477:
478: TranslationStatusDetailDialog tsdd = new TranslationStatusDetailDialog(
479: this , true);
480:
481: Properties props_default = new Properties();
482: try {
483: props_default
484: .load(MainFrame
485: .getMainInstance()
486: .getReportClassLoader()
487: .getResourceAsStream(
488: "it/businesslogic/ireport/locale/Ireport.properties"));
489: } catch (Exception ex) {
490: ex.printStackTrace();
491: }
492:
493: if (props_default != null) {
494: tsdd.setLanguage(""
495: + jTableLanguages.getValueAt(jTableLanguages
496: .getSelectedRow(), 2), props_default);
497: tsdd.setVisible(true);
498: }
499:
500: }//GEN-LAST:event_jButtonDetailsActionPerformed
501:
502: /**
503: * @param args the command line arguments
504: */
505: public static void main(String args[]) {
506: java.awt.EventQueue.invokeLater(new Runnable() {
507: public void run() {
508: new TranslationStatusDialog(new javax.swing.JFrame(),
509: true).setVisible(true);
510: }
511: });
512: }
513:
514: // Variables declaration - do not modify//GEN-BEGIN:variables
515: private javax.swing.JButton jButtonDetails;
516: private javax.swing.JButton jButtonExportToJB;
517: private javax.swing.JButton jButtonInsertDb;
518: private javax.swing.JButton jButtonOk;
519: private javax.swing.JLabel jLabelLocalizableString;
520: private javax.swing.JPanel jPanel1;
521: private javax.swing.JScrollPane jScrollPane1;
522: private javax.swing.JTable jTableLanguages;
523:
524: // End of variables declaration//GEN-END:variables
525:
526: public void applyI18n() {
527: // Start autogenerated code ----------------------
528: jButtonDetails.setText(I18n.getString(
529: "translationStatusDialog.buttonDetails", "Details..."));
530: jButtonOk.setText(I18n.getString(
531: "translationStatusDialog.buttonOk", "Close"));
532: jLabelLocalizableString.setText(I18n.getFormattedString(
533: "translationStatusDialog.buttonOk", "Close",
534: new Object[] { new Integer(0) }));
535: // End autogenerated code ----------------------
536:
537: jTableLanguages.getColumnModel().getColumn(0).setHeaderValue(
538: I18n.getString(
539: "translationStatusDialog.table.Language",
540: "Language"));
541: jTableLanguages.getColumnModel().getColumn(1).setHeaderValue(
542: I18n.getString("translationStatusDialog.table.Status",
543: "Status"));
544: jTableLanguages.getColumnModel().getColumn(2).setHeaderValue(
545: I18n.getString("translationStatusDialog.table.File",
546: "File name"));
547:
548: jButtonExportToJB.setText(I18n.getString(
549: "translationStatusDialog.buttonExportToJB",
550: "Import from JasperBabylon"));
551: }
552: }
|