19 package org.sleuthkit.autopsy.texttranslation.translators;
21 import com.google.auth.Credentials;
22 import com.google.auth.oauth2.ServiceAccountCredentials;
23 import com.google.cloud.translate.Language;
24 import com.google.cloud.translate.Translate;
25 import com.google.cloud.translate.TranslateOptions;
26 import com.google.cloud.translate.Translation;
27 import java.awt.Desktop;
28 import java.awt.event.ItemListener;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.net.URISyntaxException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.logging.Level;
39 import javax.swing.JFileChooser;
40 import javax.swing.event.HyperlinkEvent;
41 import javax.swing.event.HyperlinkListener;
42 import javax.swing.filechooser.FileNameExtensionFilter;
43 import org.apache.commons.lang3.StringUtils;
44 import org.openide.util.NbBundle.Messages;
63 targetLanguageCode = languageCode;
69 public void hyperlinkUpdate(HyperlinkEvent e) {
70 if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
72 if(Desktop.isDesktopSupported()) {
74 Desktop.getDesktop().browse(e.getURL().toURI());
75 }
catch (IOException | URISyntaxException ex) {
76 logger.log(Level.WARNING,
"Failed to display URL in external viewer", ex);
91 @Messages({
"GoogleTranslatorSettingsPanel.errorMessage.fileNotFound=Credentials file not found, please set the location to be a valid JSON credentials file.",
92 "GoogleTranslatorSettingsPanel.errorMessage.unableToReadCredentials=Unable to read credentials from credentials file, please set the location to be a valid JSON credentials file.",
93 "GoogleTranslatorSettingsPanel.errorMessage.unableToMakeCredentials=Unable to construct credentials object from credentials file, please set the location to be a valid JSON credentials file.",
94 "GoogleTranslatorSettingsPanel.errorMessage.unknownFailureGetting=Failure getting list of supported languages with current credentials file.",})
98 InputStream credentialStream;
101 }
catch (FileNotFoundException ignored) {
102 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_fileNotFound());
107 creds = ServiceAccountCredentials.fromStream(credentialStream);
108 }
catch (IOException ignored) {
109 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unableToMakeCredentials());
113 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unableToReadCredentials());
114 logger.log(Level.WARNING,
"Credentials were not successfully made, no translations will be available from the GoogleTranslator");
117 TranslateOptions.Builder builder = TranslateOptions.newBuilder();
118 builder.setCredentials(creds);
119 builder.setTargetLanguage(targetLanguageCode);
121 return builder.build().getService();
123 }
catch (Throwable throwable) {
125 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unknownFailureGetting());
126 logger.log(Level.WARNING,
"Throwable caught while getting list of supported languages", throwable);
134 @Messages({
"GoogleTranslatorSettingsPanel.errorMessage.noFileSelected=A JSON file must be selected to provide your credentials for Google Translate.",
135 "GoogleTranslatorSettingsPanel.errorMessage.unknownFailurePopulating=Failure populating list of supported languages with current credentials file."})
140 List<Language> listSupportedLanguages;
142 if (tempService != null) {
143 listSupportedLanguages = tempService.listSupportedLanguages();
145 listSupportedLanguages =
new ArrayList<>();
148 if (!listSupportedLanguages.isEmpty()) {
149 listSupportedLanguages.forEach((lang) -> {
160 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_noFileSelected());
163 }
catch (Throwable throwable) {
164 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unknownFailurePopulating());
165 logger.log(Level.WARNING,
"Throwable caught while populating list of supported languages", throwable);
205 @SuppressWarnings(
"unchecked")
208 java.awt.GridBagConstraints gridBagConstraints;
210 javax.swing.JLabel credentialsLabel =
new javax.swing.JLabel();
214 javax.swing.JLabel targetLanguageLabel =
new javax.swing.JLabel();
223 javax.swing.Box.Filler filler1 =
new javax.swing.Box.Filler(
new java.awt.Dimension(0, 0),
new java.awt.Dimension(0, 0),
new java.awt.Dimension(32767, 0));
225 setLayout(
new java.awt.GridBagLayout());
227 org.openide.awt.Mnemonics.setLocalizedText(credentialsLabel,
org.openide.util.NbBundle.getMessage(
GoogleTranslatorSettingsPanel.class,
"GoogleTranslatorSettingsPanel.credentialsLabel.text"));
228 credentialsLabel.setMaximumSize(
new java.awt.Dimension(200, 16));
229 gridBagConstraints =
new java.awt.GridBagConstraints();
230 gridBagConstraints.gridx = 0;
231 gridBagConstraints.gridy = 1;
232 gridBagConstraints.gridwidth = 3;
233 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
234 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
235 gridBagConstraints.insets =
new java.awt.Insets(17, 12, 0, 0);
236 add(credentialsLabel, gridBagConstraints);
241 gridBagConstraints =
new java.awt.GridBagConstraints();
242 gridBagConstraints.gridx = 3;
243 gridBagConstraints.gridy = 1;
244 gridBagConstraints.gridwidth = 6;
245 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
246 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
247 gridBagConstraints.insets =
new java.awt.Insets(14, 7, 0, 0);
251 browseButton.setMaximumSize(
new java.awt.Dimension(100, 25));
252 browseButton.addActionListener(
new java.awt.event.ActionListener() {
253 public void actionPerformed(java.awt.event.ActionEvent evt) {
257 gridBagConstraints =
new java.awt.GridBagConstraints();
258 gridBagConstraints.gridx = 9;
259 gridBagConstraints.gridy = 1;
260 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
261 gridBagConstraints.insets =
new java.awt.Insets(13, 7, 0, 0);
265 gridBagConstraints =
new java.awt.GridBagConstraints();
266 gridBagConstraints.gridx = 3;
267 gridBagConstraints.gridy = 2;
268 gridBagConstraints.gridwidth = 4;
269 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
270 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
271 gridBagConstraints.insets =
new java.awt.Insets(13, 7, 0, 0);
274 org.openide.awt.Mnemonics.setLocalizedText(targetLanguageLabel,
org.openide.util.NbBundle.getMessage(
GoogleTranslatorSettingsPanel.class,
"GoogleTranslatorSettingsPanel.targetLanguageLabel.text"));
275 gridBagConstraints =
new java.awt.GridBagConstraints();
276 gridBagConstraints.gridx = 0;
277 gridBagConstraints.gridy = 2;
278 gridBagConstraints.gridwidth = 3;
279 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
280 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
281 gridBagConstraints.insets =
new java.awt.Insets(16, 12, 0, 0);
282 add(targetLanguageLabel, gridBagConstraints);
284 warningLabel.setForeground(
new java.awt.Color(255, 0, 0));
286 gridBagConstraints =
new java.awt.GridBagConstraints();
287 gridBagConstraints.gridx = 0;
288 gridBagConstraints.gridy = 4;
289 gridBagConstraints.gridwidth = 10;
290 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
291 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
292 gridBagConstraints.weightx = 1.0;
293 gridBagConstraints.insets =
new java.awt.Insets(13, 12, 6, 0);
298 gridBagConstraints =
new java.awt.GridBagConstraints();
299 gridBagConstraints.gridx = 7;
300 gridBagConstraints.gridy = 3;
301 gridBagConstraints.gridwidth = 3;
302 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
303 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
304 gridBagConstraints.insets =
new java.awt.Insets(0, 7, 0, 0);
309 gridBagConstraints =
new java.awt.GridBagConstraints();
310 gridBagConstraints.gridx = 6;
311 gridBagConstraints.gridy = 3;
312 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
313 gridBagConstraints.insets =
new java.awt.Insets(10, 5, 0, 0);
318 gridBagConstraints =
new java.awt.GridBagConstraints();
319 gridBagConstraints.gridx = 3;
320 gridBagConstraints.gridy = 3;
321 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
322 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
323 gridBagConstraints.insets =
new java.awt.Insets(10, 7, 0, 0);
330 gridBagConstraints =
new java.awt.GridBagConstraints();
331 gridBagConstraints.gridx = 4;
332 gridBagConstraints.gridy = 3;
333 gridBagConstraints.gridwidth = 2;
334 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
335 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
336 gridBagConstraints.insets =
new java.awt.Insets(8, 5, 0, 0);
341 testButton.addActionListener(
new java.awt.event.ActionListener() {
342 public void actionPerformed(java.awt.event.ActionEvent evt) {
346 gridBagConstraints =
new java.awt.GridBagConstraints();
347 gridBagConstraints.gridx = 0;
348 gridBagConstraints.gridy = 3;
349 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
350 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
351 gridBagConstraints.insets =
new java.awt.Insets(6, 12, 0, 0);
355 instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
365 gridBagConstraints =
new java.awt.GridBagConstraints();
366 gridBagConstraints.gridx = 0;
367 gridBagConstraints.gridy = 0;
368 gridBagConstraints.gridwidth = 10;
369 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
370 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
371 gridBagConstraints.weightx = 1.0;
372 gridBagConstraints.weighty = 1.0;
373 gridBagConstraints.insets =
new java.awt.Insets(13, 12, 0, 0);
375 gridBagConstraints =
new java.awt.GridBagConstraints();
376 gridBagConstraints.gridx = 10;
377 gridBagConstraints.gridy = 0;
378 gridBagConstraints.gridheight = 5;
379 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
380 gridBagConstraints.weightx = 0.6;
381 add(filler1, gridBagConstraints);
384 @Messages({
"GoogleTranslatorSettingsPanel.json.description=JSON Files",
385 "GoogleTranslatorSettingsPanel.fileChooser.confirmButton=Select"})
387 JFileChooser fileChooser =
new JFileChooser();
388 fileChooser.setDragEnabled(
false);
393 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
394 fileChooser.setMultiSelectionEnabled(
false);
395 fileChooser.setFileFilter(
new FileNameExtensionFilter(Bundle.GoogleTranslatorSettingsPanel_json_description(),
JSON_EXTENSION));
396 int dialogResult = fileChooser.showDialog(
this, Bundle.GoogleTranslatorSettingsPanel_fileChooser_confirmButton());
397 if (dialogResult == JFileChooser.APPROVE_OPTION) {
401 firePropertyChange(
"SettingChanged",
true,
false);
405 @Messages({
"GoogleTranslatorSettingsPanel.errorMessage.translationFailure=Translation failure with specified credentials"})
409 if (tempTranslate != null) {
414 }
catch (Exception ex) {
415 warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_translationFailure());
416 logger.log(Level.WARNING, Bundle.GoogleTranslatorSettingsPanel_errorMessage_translationFailure(), ex);
441 String getTargetLanguageCode() {
451 String getCredentialsPath() {
464 if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
465 targetLanguageCode = selectedCode;
468 firePropertyChange(
"SettingChanged",
true,
false);
String targetLanguageCode
void populateTargetLanguageComboBox()
javax.swing.JLabel testResultValueLabel
javax.swing.JTextField testUntranslatedTextField
javax.swing.JButton browseButton
final ItemListener listener
void browseButtonActionPerformed(java.awt.event.ActionEvent evt)
Translate getTemporaryTranslationService()
GoogleTranslatorSettingsPanel(String credentialsPath, String languageCode)
javax.swing.JScrollPane instructionsScrollPane
javax.swing.JButton testButton
javax.swing.JComboBox< org.sleuthkit.autopsy.texttranslation.translators.LanguageWrapper > targetLanguageComboBox
void selectLanguageByCode(String code)
void enableControls(boolean enabled)
javax.swing.JTextPane instructionsTextArea
static final long serialVersionUID
javax.swing.JLabel warningLabel
static final String DEFUALT_TEST_STRING
static final Logger logger
javax.swing.JTextField credentialsPathField
javax.swing.JLabel untranslatedLabel
void testButtonActionPerformed(java.awt.event.ActionEvent evt)
synchronized static Logger getLogger(String name)
void itemStateChanged(java.awt.event.ItemEvent evt)
javax.swing.JLabel resultLabel
static final String JSON_EXTENSION