001: package com.xoetrope.carousel.langed;
002:
003: import com.google.api.translate.Translate;
004: import java.util.Date;
005: import javax.swing.JOptionPane;
006: import javax.swing.SwingUtilities;
007: import net.xoetrope.editor.XEditorUtilities;
008:
009: /**
010: * <p>Title: LanguageEditor</p>
011: * <p>Description: Language Resource Translation Utility</p>
012: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
013: * the GNU Public License (GPL), please see license.txt for more details. If
014: * you make commercial use of this software you must purchase a commercial
015: * license from Xoetrope.</p>
016: * <p> $Revision: 1.5 $</p>
017: * @author Luan O'Carroll
018: */
019: public class StringAdder extends Thread {
020: private EdLanguage sourceLang;
021: private EdLanguage targetLang;
022: private LangEdPanel langEdPanel;
023: private ProgressDialog progressDlg;
024: private MachineTranslationDialog mtDlg = null;
025:
026: StringAdder(LangEdPanel lep, EdLanguage src, EdLanguage target) {
027: sourceLang = src;
028: targetLang = target;
029: langEdPanel = lep;
030: progressDlg = new ProgressDialog(
031: "Machine translation progress", true);
032: }
033:
034: public void showMTDialog() {
035: mtDlg = new MachineTranslationDialog("Machine translation",
036: true);
037: mtDlg.pack();
038: XEditorUtilities.centreDialog(mtDlg);
039: mtDlg.setVisible(true);
040: }
041:
042: public void run() {
043: // do the following on the gui thread
044: SwingUtilities.invokeLater(new Runnable() {
045: public void run() {
046: XEditorUtilities.centreDialog(progressDlg);
047:
048: // if (( progressDlg == null ) || !progressDlg.isValid() )
049: // progressDlg = new ProgressDialog( "Machine translation progress", true );
050: progressDlg.setVisible(true);
051: }
052: });
053:
054: int numItems = sourceLang.getSize();
055: int srcIdx, srcId, targetIdx;
056:
057: try {
058: long startTime = new Date().getTime();
059: int numEmptyStrings = 0;
060:
061: // if ( mtDlg != null ) {
062: for (int iItem = 0; iItem < numItems; iItem++) {
063: srcId = sourceLang.getStringId(iItem);
064: targetIdx = targetLang.getStringIndex(srcId);
065: if (targetIdx == -1)
066: numEmptyStrings++;
067: else {
068: String targetStr = targetLang.getString(targetIdx);
069: if (targetStr.length() == 0)
070: numEmptyStrings++;
071: }
072: }
073: progressDlg.setProgressMaxValue(numEmptyStrings);
074: // }
075:
076: String sourceLangCode = sourceLang.getLangCode()
077: .toLowerCase();
078: String targetLangCode = targetLang.getLangCode()
079: .toLowerCase();
080:
081: for (int iItem = 0; iItem < numItems; iItem++) {
082: srcId = sourceLang.getStringId(iItem);
083: targetIdx = targetLang.getStringIndex(srcId);
084: String targetStr = "";
085: if (targetIdx != -1)
086: targetStr = targetLang.getString(targetIdx);
087: int targetStrLen = targetStr.length();
088: if ((targetIdx == -1) || (targetStrLen == 0)) {
089: String sourceStr = sourceLang.getString(srcId);
090: String translatedStr = sourceStr;
091:
092: // if (( mtDlg != null ) && ( sourceStr.length() > 1 )) {
093: // if ( mtDlg.isOk() && progressDlg.isOk())
094: // translatedStr = AutoTranslate.translate( mtDlg.getUrl(),
095: // mtDlg.getDirectionPrefix(), mtDlg.getDirection(),
096: // mtDlg.getSrcPrefix(), sourceStr,
097: // mtDlg.getStartOfResultTag(),
098: // mtDlg.getEndOfResultTag());
099: // else
100: // break;
101: // }
102:
103: try {
104: translatedStr = Translate.translate(sourceStr,
105: sourceLangCode, targetLangCode);
106: } catch (Exception ex) {
107: }
108:
109: if (targetIdx != -1)
110: targetLang.setString(srcId, sourceStr,
111: translatedStr);
112: else
113: targetIdx = targetLang.addString(srcId,
114: sourceStr, translatedStr);
115:
116: progressDlg.incrementProgress();
117:
118: if ((new Date().getTime() - startTime) > 5000) {
119: langEdPanel.stringAdderComplete();
120: startTime = new Date().getTime();
121: }
122: } else if (targetStrLen == 0)
123: progressDlg.incrementProgress();
124: }
125: } catch (Exception ex) {
126: progressDlg.dispose();
127: if (mtDlg != null)
128: JOptionPane
129: .showMessageDialog(
130: mtDlg,
131: "An error occured while adding the missing string.\r\nPlease check the connection and the parameters.",
132: "Machine translation error",
133: JOptionPane.OK_OPTION
134: | JOptionPane.ERROR_MESSAGE);
135: }
136:
137: // if (( mtDlg != null ) && mtDlg.isOk()) {
138: // mtDlg.setVisible( false );
139: // mtDlg.dispose();
140: // }
141: progressDlg.incrementProgress();
142: progressDlg.finished();
143:
144: langEdPanel.stringAdderComplete();
145: }
146: }
|