Skip to content

Commit d04d8de

Browse files
authored
Merge pull request #5082 from rcordovano/text-trans-api-mods-for-4.12.0
Text trans api mods for 4.12.0
2 parents 982d2f2 + 755743e commit d04d8de

14 files changed

+114
-55
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OptionsCategory_Name_Machine_Translation=Machine Translation
2+
OptionsCategory_Keywords_Machine_Translation_Settings=Machine Translation Settings
3+
TranslationContentPanel.ocrLabel.text=OCR:
4+
TranslationOptionsPanelController.moduleErr=Module Error
5+
TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete.
6+
TranslationContentPanel.showLabel.text=Show:
7+
TranslationOptionsPanel.translationServiceLabel.text=Text translator:
8+
TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
OptionsCategory_Name_Machine_Translation=Machine Translation
2+
OptionsCategory_Keywords_Machine_Translation_Settings=Machine Translation Settings
3+
TranslationContentPanel.ocrLabel.text=OCR:
4+
TranslationOptionsPanel.noTextTranslators.text=No text translators exist, translation is disabled.
5+
TranslationOptionsPanel.noTextTranslatorSelected.text=No text translator selected, translation is disabled.
6+
TranslationOptionsPanel.textTranslatorsUnavailable.text=Unable to get selected text translator, translation is disabled.
7+
TranslationOptionsPanel.translationDisabled.text=Translation disabled
8+
TranslationOptionsPanelController.moduleErr=Module Error
9+
TranslationOptionsPanelController.moduleErr.msg=A module caused an error listening to TranslationSettingsPanelController updates. See log to determine which module. Some data could be incomplete.
10+
TranslationContentPanel.showLabel.text=Show:
11+
TranslationOptionsPanel.translationServiceLabel.text=Text translator:
12+
TranslationOptionsPanel.translationOptionsDescription.text=Configure a 3rd party text translation service to enable text and file name translation.

Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslationService.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Optional;
2424
import org.openide.util.Lookup;
2525
import org.sleuthkit.autopsy.core.UserPreferences;
26+
import javax.annotation.concurrent.GuardedBy;
2627

2728
/**
2829
* Performs a lookup for a TextTranslator service provider and if present, will
@@ -33,6 +34,7 @@ public final class TextTranslationService {
3334
private final static TextTranslationService tts = new TextTranslationService();
3435

3536
private final Collection<? extends TextTranslator> translators;
37+
@GuardedBy("this")
3638
private Optional<TextTranslator> selectedTranslator;
3739

3840
private TextTranslationService() {
@@ -50,7 +52,7 @@ public static TextTranslationService getInstance() {
5052
* Update the translator currently in use to match the one saved to the user
5153
* preferences
5254
*/
53-
public void updateSelectedTranslator() {
55+
synchronized void updateSelectedTranslator() {
5456
String translatorName = UserPreferences.getTextTranslatorName();
5557
for (TextTranslator translator : translators) {
5658
if (translator.getName().equals(translatorName)) {
@@ -75,7 +77,7 @@ public void updateSelectedTranslator() {
7577
* when specific translation
7678
* implementations fail
7779
*/
78-
public String translate(String input) throws NoServiceProviderException, TranslationException {
80+
public synchronized String translate(String input) throws NoServiceProviderException, TranslationException {
7981
if (hasProvider()) {
8082
return selectedTranslator.get().translate(input);
8183
}
@@ -92,7 +94,7 @@ public String translate(String input) throws NoServiceProviderException, Transla
9294
*
9395
* @throws NoServiceProviderException
9496
*/
95-
public TextTranslator getTranslatorByName(String translatorName) throws NoServiceProviderException {
97+
TextTranslator getTranslatorByName(String translatorName) throws NoServiceProviderException {
9698
for (TextTranslator translator : translators) {
9799
if (translator.getName().equals(translatorName)) {
98100
return translator;
@@ -107,7 +109,7 @@ public TextTranslator getTranslatorByName(String translatorName) throws NoServic
107109
*
108110
* @return an unmodifiable collection of TextTranslators
109111
*/
110-
public Collection<? extends TextTranslator> getTranslators() {
112+
Collection<? extends TextTranslator> getTranslators() {
111113
return Collections.unmodifiableCollection(translators);
112114
}
113115

@@ -117,16 +119,16 @@ public Collection<? extends TextTranslator> getTranslators() {
117119
*
118120
* @return
119121
*/
120-
public boolean hasProvider() {
122+
public synchronized boolean hasProvider() {
121123
return selectedTranslator.isPresent();
122124
}
123-
125+
124126
/**
125-
* Returns the hard limit for translation request sizes.
126-
*
127-
* @return
127+
* Gets the maximum number of characters allowed in a translation request.
128+
*
129+
* @return The maximum character count.
128130
*/
129-
public int getMaxPayloadSize() {
130-
return selectedTranslator.get().getMaxPayloadSize();
131+
public synchronized int getMaxTextChars() {
132+
return selectedTranslator.get().getMaxTextChars();
131133
}
132134
}

Core/src/org/sleuthkit/autopsy/texttranslation/TextTranslator.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.sleuthkit.autopsy.texttranslation;
2020

21-
import java.awt.Component;
21+
import javax.swing.JPanel;
2222

2323
/**
2424
* Interface for creating text translators. Implementing classes will be picked
@@ -45,22 +45,24 @@ public interface TextTranslator {
4545
String getName();
4646

4747
/**
48-
* Get the component to display on the settings options panel when this
48+
* Get the JPanel to display on the settings options panel when this
4949
* TextTranslator is selected
5050
*
51-
* @return the component which displays the settings options
51+
* @return the panel which displays the settings options
5252
*/
53-
Component getComponent();
53+
JPanel getSettingsPanel();
5454

5555
/**
56-
* Save the settings as they have been modified in the component.
56+
* Saves the current state of the settings in the settings panel.
57+
*
58+
* @throws TranslationConfigException
5759
*/
58-
void saveSettings();
60+
void saveSettings() throws TranslationConfigException;
5961

6062
/**
61-
* Returns the hard limit for translation request sizes.
63+
* Gets the maximum number of characters allowed in a translation request.
6264
*
63-
* @return
65+
* @return The maximum character count.
6466
*/
65-
int getMaxPayloadSize();
67+
int getMaxTextChars();
6668
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Autopsy Forensic Browser
3+
*
4+
* Copyright 2019 Basis Technology Corp.
5+
* Contact: carrier <at> sleuthkit <dot> org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.sleuthkit.autopsy.texttranslation;
20+
21+
/**
22+
* Instances of this exception class are thrown when there is an error
23+
* configuring text translation.
24+
*/
25+
public class TranslationConfigException extends Exception {
26+
27+
private static final long serialVersionUID = 1L;
28+
29+
/**
30+
* Constructs a new exception with the specified message.
31+
*
32+
* @param message The message.
33+
*/
34+
public TranslationConfigException(String message) {
35+
super(message);
36+
}
37+
38+
/**
39+
* Constructs a new exception with the specified message and cause.
40+
*
41+
* @param message The message.
42+
* @param cause The cause.
43+
*/
44+
public TranslationConfigException(String message, Throwable cause) {
45+
super(message, cause);
46+
}
47+
48+
}

Core/src/org/sleuthkit/autopsy/texttranslation/TranslationException.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
* Provides a system exception for Text Translation errors
2323
*/
2424
public class TranslationException extends Exception {
25-
26-
/**
27-
* Constructs a new exception with null as its message.
28-
*/
29-
public TranslationException() {
30-
super();
31-
}
25+
26+
private static final long serialVersionUID = 1L;
3227

3328
/**
3429
* Constructs a new exception with the specified message.

Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.form renamed to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.form

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<Component class="javax.swing.JLabel" name="translationServiceLabel">
6262
<Properties>
6363
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
64-
<ResourceString bundle="org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties" key="TranslationOptionsPanel.translationServiceLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
64+
<ResourceString bundle="org/sleuthkit/autopsy/texttranslation/Bundle.properties" key="TranslationOptionsPanel.translationServiceLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
6565
</Property>
6666
</Properties>
6767
</Component>
@@ -72,7 +72,7 @@
7272
<Component class="javax.swing.JLabel" name="translationOptionsDescription">
7373
<Properties>
7474
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
75-
<ResourceString bundle="org/sleuthkit/autopsy/texttranslation/ui/Bundle.properties" key="TranslationOptionsPanel.translationOptionsDescription.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
75+
<ResourceString bundle="org/sleuthkit/autopsy/texttranslation/Bundle.properties" key="TranslationOptionsPanel.translationOptionsDescription.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
7676
</Property>
7777
</Properties>
7878
</Component>

Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanel.java renamed to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanel.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.sleuthkit.autopsy.texttranslation.ui;
19+
package org.sleuthkit.autopsy.texttranslation;
2020

2121
import java.awt.BorderLayout;
2222
import java.awt.Color;
@@ -27,14 +27,12 @@
2727
import javax.swing.JLabel;
2828
import org.openide.util.NbBundle.Messages;
2929
import org.sleuthkit.autopsy.core.UserPreferences;
30-
import org.sleuthkit.autopsy.texttranslation.NoServiceProviderException;
31-
import org.sleuthkit.autopsy.texttranslation.TextTranslationService;
3230
import org.sleuthkit.autopsy.coreutils.Logger;
3331

3432
/**
3533
* Options panel to display translation options
3634
*/
37-
public class TranslationOptionsPanel extends javax.swing.JPanel {
35+
final class TranslationOptionsPanel extends javax.swing.JPanel {
3836

3937
private final static Logger logger = Logger.getLogger(TranslationOptionsPanel.class.getName());
4038
private static final long serialVersionUID = 1L;
@@ -45,7 +43,7 @@ public class TranslationOptionsPanel extends javax.swing.JPanel {
4543
* Creates new form TranslationOptionsPanel
4644
*/
4745
@Messages({"TranslationOptionsPanel.translationDisabled.text=Translation disabled"})
48-
public TranslationOptionsPanel(TranslationOptionsPanelController theController) {
46+
TranslationOptionsPanel(TranslationOptionsPanelController theController) {
4947
initComponents();
5048
controller = theController;
5149
translatorComboBox.addItem(Bundle.TranslationOptionsPanel_translationDisabled_text());
@@ -78,7 +76,7 @@ private void loadSelectedPanelSettings() {
7876
translationServicePanel.removeAll();
7977
if (translatorComboBox.getSelectedItem() != null && !translatorComboBox.getSelectedItem().toString().equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
8078
try {
81-
Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getComponent();
79+
Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getSettingsPanel();
8280
panel.addPropertyChangeListener(new PropertyChangeListener() {
8381
@Override
8482
public void propertyChange(PropertyChangeEvent evt) {
@@ -126,7 +124,7 @@ void store() {
126124
if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
127125
try {
128126
TextTranslationService.getInstance().getTranslatorByName(currentSelection).saveSettings();
129-
} catch (NoServiceProviderException ex) {
127+
} catch (NoServiceProviderException | TranslationConfigException ex) {
130128
logger.log(Level.WARNING, "Unable to save settings for TextTranslator named: " + currentSelection, ex);
131129
}
132130
}
@@ -172,7 +170,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
172170
.addGap(10, 10, 10)
173171
.addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
174172
.addGap(0, 0, Short.MAX_VALUE))
175-
.addComponent(translationOptionsDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE))
173+
.addComponent(translationOptionsDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 462, Short.MAX_VALUE))
176174
.addContainerGap())
177175
);
178176
layout.setVerticalGroup(

Core/src/org/sleuthkit/autopsy/texttranslation/ui/TranslationOptionsPanelController.java renamed to Core/src/org/sleuthkit/autopsy/texttranslation/TranslationOptionsPanelController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
package org.sleuthkit.autopsy.texttranslation.ui;
19+
package org.sleuthkit.autopsy.texttranslation;
2020

2121
import java.beans.PropertyChangeListener;
2222
import java.beans.PropertyChangeSupport;

Core/src/org/sleuthkit/autopsy/texttranslation/translators/BingTranslator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
import com.squareup.okhttp.Request;
2727
import com.squareup.okhttp.RequestBody;
2828
import com.squareup.okhttp.Response;
29-
import java.awt.Component;
3029
import java.io.IOException;
30+
import javax.swing.JPanel;
3131
import org.openide.util.NbBundle.Messages;
3232
import org.openide.util.lookup.ServiceProvider;
3333
import org.sleuthkit.autopsy.texttranslation.TextTranslator;
34+
import org.sleuthkit.autopsy.texttranslation.TranslationConfigException;
3435
import org.sleuthkit.autopsy.texttranslation.TranslationException;
3536

3637
/**
@@ -132,12 +133,12 @@ public String getName() {
132133
}
133134

134135
@Override
135-
public Component getComponent() {
136+
public JPanel getSettingsPanel() {
136137
return settingsPanel;
137138
}
138139

139140
@Override
140-
public void saveSettings() {
141+
public void saveSettings() throws TranslationConfigException {
141142
settings.setAuthenticationKey(settingsPanel.getAuthenticationKey());
142143
settings.setTargetLanguageCode(settingsPanel.getTargetLanguageCode());
143144
settings.saveSettings();
@@ -173,7 +174,7 @@ private String parseJSONResponse(String json_text) throws TranslationException {
173174
}
174175

175176
@Override
176-
public int getMaxPayloadSize() {
177+
public int getMaxTextChars() {
177178
return MAX_STRING_LENGTH;
178179
}
179180
}

0 commit comments

Comments
 (0)