From 88972c660aa96a1f22965785708394d93b385ae0 Mon Sep 17 00:00:00 2001 From: NicolasGrosjean Date: Thu, 22 Sep 2022 10:45:32 +0200 Subject: [PATCH] Add option to call automatically DeepL #49 --- AppInfo.txt | 3 +++ src/config/ConfigStorage.java | 5 +++++ src/config/WorkingSession.java | 20 +++++++++++++++++--- src/gui/DetailsButton.java | 9 ++++++--- src/gui/DetailsDialog.java | 8 ++++++-- src/gui/TranslatorDialog.java | 10 +++++++--- src/gui/Window.java | 2 ++ src/gui/WorkingSessionDialog.java | 22 ++++++++++++++++------ src/versionning/OnlineVersionChecker.java | 2 +- 9 files changed, 63 insertions(+), 18 deletions(-) diff --git a/AppInfo.txt b/AppInfo.txt index 29c997d..cd0227a 100644 --- a/AppInfo.txt +++ b/AppInfo.txt @@ -1,3 +1,6 @@ +AppVersion=3.4 +- Add option and button to call free DeepL API (#47) + AppVersion=3.3 - Fix crash when translating CK3 with more than 2 languages (#45) diff --git a/src/config/ConfigStorage.java b/src/config/ConfigStorage.java index 819d573..587eca1 100644 --- a/src/config/ConfigStorage.java +++ b/src/config/ConfigStorage.java @@ -25,6 +25,7 @@ public class ConfigStorage { private static final String wsDirectoryAttribute = "directory"; private static final String wsSourceLanguageAttribute = "source"; private static final String wsDestinationLanguageAttribute = "destination"; + private static final String wsAutomaticDeepLAttribute = "automaticDeepL"; private static final String wsAutomaticGoogleAttribute = "automaticGoogle"; private static final String wsAcceptAllCopies = "acceptAllCopies"; @@ -59,12 +60,14 @@ public ConfigStorage(String configurationFile) { boolean modified = false; while (it.hasNext()) { Element wsElem = it.next(); + String automaticDeepLTranslate = wsElem.getAttributeValue(wsAutomaticDeepLAttribute); String automaticGoogleTranslate = wsElem.getAttributeValue(wsAutomaticGoogleAttribute); String acceptAllCopies = wsElem.getAttributeValue(wsAcceptAllCopies); WorkingSession ws = new WorkingSession(wsElem.getAttributeValue(wsNameAttribute), wsElem.getAttributeValue(wsDirectoryAttribute), WorkingSession.getLanguage(wsElem.getAttributeValue(wsSourceLanguageAttribute)), WorkingSession.getLanguage(wsElem.getAttributeValue(wsDestinationLanguageAttribute)), + (automaticDeepLTranslate != null) ? Boolean.valueOf(automaticDeepLTranslate) : false, (automaticGoogleTranslate != null) ? Boolean.valueOf(automaticGoogleTranslate) : false, (acceptAllCopies != null) ? Boolean.valueOf(acceptAllCopies) : false); if (!ws.getErrorMessage().equals("")) { @@ -104,6 +107,8 @@ private Element workingSessionToElement(WorkingSession ws) { workingSessionElem.setAttribute(new Attribute(wsSourceLanguageAttribute, ws.getSourceLanguage().getCode())); workingSessionElem .setAttribute(new Attribute(wsDestinationLanguageAttribute, ws.getDestinationLanguage().getCode())); + workingSessionElem + .setAttribute(new Attribute(wsAutomaticDeepLAttribute, Boolean.toString(ws.isAutomaticDeepLCall()))); workingSessionElem .setAttribute(new Attribute(wsAutomaticGoogleAttribute, Boolean.toString(ws.isAutomaticGoogleCall()))); workingSessionElem diff --git a/src/config/WorkingSession.java b/src/config/WorkingSession.java index c1bc6e3..e75ec89 100644 --- a/src/config/WorkingSession.java +++ b/src/config/WorkingSession.java @@ -36,6 +36,11 @@ public class WorkingSession { */ private boolean automaticGoogleCall; + /** + * Call automatically DeepL translate when the destination text is empty + */ + private boolean automaticDeepLCall; + /** * Do not consider copy text as a missing translation */ @@ -58,7 +63,7 @@ public class WorkingSession { * @param destinationLanguage */ public WorkingSession(String name, String directory, Language sourceLanguage, - Language destinationLanguage, boolean automaticGoogleCall, + Language destinationLanguage, boolean automaticDeepLCall, boolean automaticGoogleCall, boolean acceptAllCopies) { if (!isAvailableLanguagesInitialized()) { throw new IllegalArgumentException("The list of available languages was not initialized!"); @@ -83,6 +88,7 @@ public WorkingSession(String name, String directory, Language sourceLanguage, RuntimeException e) { errorMessage.append(e.getMessage() + System.lineSeparator()); } + setAutomaticDeepLCall(automaticDeepLCall); setAutomaticGoogleCall(automaticGoogleCall); setAcceptAllCopies(acceptAllCopies); @@ -90,8 +96,8 @@ public WorkingSession(String name, String directory, Language sourceLanguage, } public WorkingSession(String name, String directory, Language sourceLanguage, - boolean automaticGoogleCall, boolean acceptAllCopies) { - this(name, directory, sourceLanguage, new Language(), automaticGoogleCall, acceptAllCopies); + boolean automaticDeepLCall, boolean automaticGoogleCall, boolean acceptAllCopies) { + this(name, directory, sourceLanguage, new Language(), automaticDeepLCall, automaticGoogleCall, acceptAllCopies); } public String getName() { @@ -141,6 +147,14 @@ public void setDestinationLanguage(Language destinationLanguage) { } } + public boolean isAutomaticDeepLCall() { + return automaticDeepLCall; + } + + public void setAutomaticDeepLCall(boolean automaticDeepLCall) { + this.automaticDeepLCall = automaticDeepLCall; + } + public boolean isAutomaticGoogleCall() { return automaticGoogleCall; } diff --git a/src/gui/DetailsButton.java b/src/gui/DetailsButton.java index b2f5a74..d4317b1 100644 --- a/src/gui/DetailsButton.java +++ b/src/gui/DetailsButton.java @@ -19,12 +19,12 @@ public class DetailsButton extends DefaultCellEditor { public DetailsButton(JCheckBox checkBox, Language sourceLanguage, boolean hasDestinationLanguage, Language destinationLanguage, - boolean automaticGoogleCall, Window window) { + boolean automaticDeepLCall, boolean automaticGoogleCall, Window window) { super(checkBox); button = new JButton(); button.setOpaque(true); bListener = new ButtonListener(sourceLanguage, hasDestinationLanguage, - destinationLanguage, automaticGoogleCall, window); + destinationLanguage, automaticDeepLCall, automaticGoogleCall, window); button.addActionListener(bListener); } @@ -53,16 +53,19 @@ class ButtonListener implements ActionListener { private Language sourceLanguage; private boolean hasDestinationLanguage; private Language destinationLanguage; + private boolean automaticDeepLCall; private boolean automaticGoogleCall; private Window window; ButtonListener(Language sourceLanguage, boolean hasDestinationLanguage, Language destinationLanguage, + boolean automaticDeepLCall, boolean automaticGoogleCall, Window window) { this.sourceLanguage = sourceLanguage; this.hasDestinationLanguage = hasDestinationLanguage; this.destinationLanguage = destinationLanguage; + this.automaticDeepLCall = automaticDeepLCall; this.automaticGoogleCall = automaticGoogleCall; this.window = window; } @@ -77,7 +80,7 @@ public void actionPerformed(ActionEvent event) { TranslatorParsedFile f = (TranslatorParsedFile)table.getValueAt(row, column-3); new DetailsDialog(null, f.getName(), true, f, sourceLanguage, hasDestinationLanguage, destinationLanguage, - automaticGoogleCall, window); + automaticDeepLCall, automaticGoogleCall, window); } } } diff --git a/src/gui/DetailsDialog.java b/src/gui/DetailsDialog.java index 699b69d..8d07e95 100644 --- a/src/gui/DetailsDialog.java +++ b/src/gui/DetailsDialog.java @@ -20,6 +20,7 @@ public DetailsDialog(JFrame parent, String title, boolean modal, Language sourceLanguage, boolean hasDestinationLanguage, Language destinationLanguage, + boolean automaticDeepLCall, boolean automaticGoogleCall, Window window) { // Create the JDialog @@ -53,7 +54,7 @@ public DetailsDialog(JFrame parent, String title, boolean modal, if (hasDestinationLanguage) { JButton translateBtn = new JButton("Translate..."); translateBtn.addActionListener(new TranslateListener(title, file, sourceLanguage, - destinationLanguage, automaticGoogleCall, window)); + destinationLanguage, automaticDeepLCall, automaticGoogleCall, window)); getContentPane().add(translateBtn, BorderLayout.SOUTH); } setVisible(true); @@ -76,6 +77,7 @@ class TranslateListener implements ActionListener { private ITranslator file; private Language sourceLanguage; private Language destinationLanguage; + private boolean automaticDeepLCall; private boolean automaticGoogleCall; private Window window; @@ -83,12 +85,14 @@ class TranslateListener implements ActionListener { public TranslateListener(String fileName, ITranslator file, Language sourceLanguage, Language destinationLanguage, + boolean automaticDeepLCall, boolean automaticGoogleCall, Window window) { this.fileName = fileName; this.file = file; this.sourceLanguage = sourceLanguage; this.destinationLanguage = destinationLanguage; + this.automaticDeepLCall = automaticDeepLCall; this.automaticGoogleCall = automaticGoogleCall; this.window = window; } @@ -100,7 +104,7 @@ public void actionPerformed(ActionEvent event) { // Open a new one new TranslatorDialog(null, fileName, true, file, sourceLanguage, destinationLanguage, - automaticGoogleCall); + automaticDeepLCall, automaticGoogleCall); // Refresh working session window.refreshWorkingSession(); diff --git a/src/gui/TranslatorDialog.java b/src/gui/TranslatorDialog.java index 7b76dfa..02f410a 100644 --- a/src/gui/TranslatorDialog.java +++ b/src/gui/TranslatorDialog.java @@ -59,6 +59,7 @@ public class TranslatorDialog extends JDialog { private GoogleTranslate google; private DeeplTranslate deepL; private String destinationLanguageCode; + private boolean automaticDeepLCall; private boolean automaticGoogleCall; private String oldSourceText; private String oldDestinationText; @@ -74,13 +75,15 @@ public class TranslatorDialog extends JDialog { public TranslatorDialog(JFrame parent, String fileName, boolean modal, ITranslator file, Language sourceLanguage, - Language destinationLanguage, boolean automaticGoogleCall) { + Language destinationLanguage, + boolean automaticDeepLCall, boolean automaticGoogleCall) { // Create the JDialog super(parent, "", modal); setSize(1150, 620); setLocationRelativeTo(null); this.fileName = fileName; + this.automaticDeepLCall = automaticDeepLCall; this.automaticGoogleCall = automaticGoogleCall; google = new GoogleTranslate(sourceLanguage.getLocale().toString(), destinationLanguage.getLocale().toString()); @@ -358,8 +361,9 @@ private void updateTextAreaAndTitle() { setTitle(fileName + " - " + entry.getId() + " (line " + entry.getDestLineNumber() + ")"); oldSourceText = entry.getSource(); oldDestinationText = entry.getDestination(); - // TODO Call DeepL before - if (automaticGoogleCall && destTextPane.getText().equals("")) { + if (automaticDeepLCall && destTextPane.getText().equals("")) { + callTranslateAPI(deepL); + } else if (automaticGoogleCall && destTextPane.getText().equals("")) { callTranslateAPI(google); } } else { diff --git a/src/gui/Window.java b/src/gui/Window.java index fc49053..49812e5 100644 --- a/src/gui/Window.java +++ b/src/gui/Window.java @@ -234,6 +234,7 @@ public String getToolTipText(MouseEvent e) { table.getColumn(columnTitles[4]).setCellEditor(new DetailsButton(new JCheckBox(), ws.getSourceLanguage(), !ws.getDestinationLanguage().isNone(), ws.getDestinationLanguage(), + ws.isAutomaticDeepLCall(), ws.isAutomaticGoogleCall(), this)); table.getColumn(columnTitles[4]).setPreferredWidth(50); @@ -455,6 +456,7 @@ public void actionPerformed(ActionEvent e) { ws.isAcceptAllCopies()); new TranslatorDialog(null, f.getName(), true, file, ws.getSourceLanguage(), ws.getDestinationLanguage(), + ws.isAutomaticDeepLCall(), ws.isAutomaticGoogleCall()); // Refresh working session diff --git a/src/gui/WorkingSessionDialog.java b/src/gui/WorkingSessionDialog.java index d6c9e8e..2b580d1 100644 --- a/src/gui/WorkingSessionDialog.java +++ b/src/gui/WorkingSessionDialog.java @@ -33,6 +33,7 @@ public class WorkingSessionDialog extends JDialog { private JTextField localisationDirectoryTF; private JComboBox sourceLanguageSourceComboBox; private JComboBox destinationLanguageComboBox; + private JCheckBox automaticDeepLCall; private JCheckBox automaticGoogleCall; private JCheckBox acceptAllCopiesCB; private JPanel container; @@ -59,10 +60,11 @@ public class WorkingSessionDialog extends JDialog { */ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String name, String directory, Language sourceLanguage, Language destinationLanguage, - boolean automaticGoogleTranslate, boolean acceptAllCopies) { + boolean automaticDeepLTranslate, boolean automaticGoogleTranslate, + boolean acceptAllCopies) { // Create the JDialog super(parent, title, modal); - setSize(500, 450); + setSize(500, 520); setLocationRelativeTo(null); setResizable(false); @@ -86,6 +88,9 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n destinationLanguageComboBox = new JComboBox(avalaibleDestinationLanguages); destinationLanguageComboBox.setSelectedItem(destinationLanguage); + automaticDeepLCall = new JCheckBox("Call automatically DeepL when destination text is empty"); + automaticDeepLCall.setSelected(automaticDeepLTranslate); + automaticGoogleCall = new JCheckBox("Call automatically Google Translate when destination text is empty"); automaticGoogleCall.setSelected(automaticGoogleTranslate); @@ -113,6 +118,9 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n languagePanel.add(destinationLanguageLabel); languagePanel.add(destinationLanguageComboBox); languagePanel.setBorder(BorderFactory.createTitledBorder("Translation")); + JPanel deepLPanel = new JPanel(); + deepLPanel.setBorder(BorderFactory.createTitledBorder("DeepL")); + deepLPanel.add(automaticDeepLCall); JPanel googlePanel = new JPanel(); googlePanel.setBorder(BorderFactory.createTitledBorder("Google translate")); googlePanel.add(automaticGoogleCall); @@ -125,10 +133,11 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n // Add to the container container = new JPanel(); - container.setLayout(new GridLayout(6, 1, 5, 5)); + container.setLayout(new GridLayout(7, 1, 5, 5)); container.add(wsNamePanel); container.add(localisationDirPanel); container.add(languagePanel); + container.add(deepLPanel); container.add(googlePanel); container.add(copyPanel); container.add(validCancelPanel); @@ -143,14 +152,15 @@ public WorkingSessionDialog(JFrame parent, String title, boolean modal, String n } public WorkingSessionDialog(JFrame parent, String title, boolean modal) { - this(parent, title, modal, "", "", new Language(), new Language(), false, false); + this(parent, title, modal, "", "", new Language(), new Language(), false, false, false); } public WorkingSessionDialog(JFrame parent, String title, boolean modal, WorkingSession ws) { this(parent, title, modal, ws.getName(), ws.getDirectory(), ws.getSourceLanguage(), ws.getDestinationLanguage(), - ws.isAutomaticGoogleCall(), ws.isAcceptAllCopies()); + ws.isAutomaticDeepLCall(), ws.isAutomaticGoogleCall(), + ws.isAcceptAllCopies()); } public WorkingSession getWorkingSession() { @@ -165,7 +175,7 @@ public WorkingSession getWorkingSession() { return new WorkingSession(wsName.getText(), localisationDirectoryTF.getText(), sourceLanguageSourceComboBox.getItemAt(sourceLanguageSourceComboBox.getSelectedIndex()), destinationLanguageComboBox.getItemAt(destinationLanguageComboBox.getSelectedIndex()), - automaticGoogleCall.isSelected(), acceptAllCopiesCB.isSelected()); + automaticDeepLCall.isSelected(), automaticGoogleCall.isSelected(), acceptAllCopiesCB.isSelected()); } class FileExplorer implements ActionListener { diff --git a/src/versionning/OnlineVersionChecker.java b/src/versionning/OnlineVersionChecker.java index 24b7c7e..732c91c 100644 --- a/src/versionning/OnlineVersionChecker.java +++ b/src/versionning/OnlineVersionChecker.java @@ -16,7 +16,7 @@ */ public class OnlineVersionChecker { private static String URL_APP_INFO_TXT = "https://raw.githubusercontent.com/NicolasGrosjean/Translate_helper/master/AppInfo.txt"; - private static String VERSION = "3.3"; + private static String VERSION = "3.4"; private int lastestOnlineMajorVersionNumber; private int lastestOnlineMinorVersionNumber;