Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
GoogleTranslatorSettingsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2019-2020 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.translators;
20 
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;
29 import java.io.File;
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;
45 
49 public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
50 
51  private static final Logger logger = Logger.getLogger(GoogleTranslatorSettingsPanel.class.getName());
52  private static final String JSON_EXTENSION = "json";
53  private static final String DEFUALT_TEST_STRING = "traducción exitoso"; //spanish which should translate to something along the lines of "successful translation"
54  private static final long serialVersionUID = 1L;
55  private final ItemListener listener = new ComboBoxSelectionListener();
56  private String targetLanguageCode = "";
57 
61  public GoogleTranslatorSettingsPanel(String credentialsPath, String languageCode) {
63  targetLanguageCode = languageCode;
64  credentialsPathField.setText(credentialsPath);
66 
67  instructionsTextArea.addHyperlinkListener(new HyperlinkListener() {
68  @Override
69  public void hyperlinkUpdate(HyperlinkEvent e) {
70  if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
71  // Try to display the URL in the user's browswer.
72  if(Desktop.isDesktopSupported()) {
73  try {
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);
77  }
78  }
79 
80  }
81  }
82  });
83  }
84 
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.",})
95  private Translate getTemporaryTranslationService() {
96  //This method also has the side effect of more or less validating the JSON file which was selected as it is necessary to get the list of target languages
97  try {
98  InputStream credentialStream;
99  try {
100  credentialStream = new FileInputStream(credentialsPathField.getText());
101  } catch (FileNotFoundException ignored) {
102  warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_fileNotFound());
103  return null;
104  }
105  Credentials creds;
106  try {
107  creds = ServiceAccountCredentials.fromStream(credentialStream);
108  } catch (IOException ignored) {
109  warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unableToMakeCredentials());
110  return null;
111  }
112  if (creds == null) {
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");
115  return null;
116  } else {
117  TranslateOptions.Builder builder = TranslateOptions.newBuilder();
118  builder.setCredentials(creds);
119  builder.setTargetLanguage(targetLanguageCode); //localize the list to the currently selected target language
120  warningLabel.setText(""); //clear any previous warning text
121  return builder.build().getService();
122  }
123  } catch (Throwable throwable) {
124  //Catching throwables because some of this Google Translate code throws throwables
125  warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_unknownFailureGetting());
126  logger.log(Level.WARNING, "Throwable caught while getting list of supported languages", throwable);
127  return null;
128  }
129  }
130 
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."})
137  targetLanguageComboBox.removeItemListener(listener);
138  try {
139  if (!StringUtils.isBlank(credentialsPathField.getText())) {
140  List<Language> listSupportedLanguages;
141  Translate tempService = getTemporaryTranslationService();
142  if (tempService != null) {
143  listSupportedLanguages = tempService.listSupportedLanguages();
144  } else {
145  listSupportedLanguages = new ArrayList<>();
146  }
147  targetLanguageComboBox.removeAllItems();
148  if (!listSupportedLanguages.isEmpty()) {
149  listSupportedLanguages.forEach((lang) -> {
150  targetLanguageComboBox.addItem(new LanguageWrapper(lang));
151  });
152  selectLanguageByCode(targetLanguageCode);
153  targetLanguageComboBox.addItemListener(listener);
154  enableControls(true);
155 
156  } else {
157  enableControls(false);
158  }
159  } else {
160  warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_noFileSelected());
161  enableControls(false);
162  }
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);
166  enableControls(false);
167  }
168  }
169 
176  private void enableControls(boolean enabled) {
177  targetLanguageComboBox.setEnabled(enabled);
178  testButton.setEnabled(enabled);
179  testResultValueLabel.setEnabled(enabled);
180  testUntranslatedTextField.setEnabled(enabled);
181  untranslatedLabel.setEnabled(enabled);
182  resultLabel.setEnabled(enabled);
183  }
184 
191  private void selectLanguageByCode(String code) {
192  for (int i = 0; i < targetLanguageComboBox.getModel().getSize(); i++) {
193  if (targetLanguageComboBox.getItemAt(i).getLanguageCode().equals(code)) {
194  targetLanguageComboBox.setSelectedIndex(i);
195  return;
196  }
197  }
198  }
199 
205  @SuppressWarnings("unchecked")
206  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
207  private void initComponents() {
208  java.awt.GridBagConstraints gridBagConstraints;
209 
210  javax.swing.JLabel credentialsLabel = new javax.swing.JLabel();
211  credentialsPathField = new javax.swing.JTextField();
212  browseButton = new javax.swing.JButton();
213  targetLanguageComboBox = new javax.swing.JComboBox<>();
214  javax.swing.JLabel targetLanguageLabel = new javax.swing.JLabel();
215  warningLabel = new javax.swing.JLabel();
216  testResultValueLabel = new javax.swing.JLabel();
217  resultLabel = new javax.swing.JLabel();
218  untranslatedLabel = new javax.swing.JLabel();
219  testUntranslatedTextField = new javax.swing.JTextField();
220  testButton = new javax.swing.JButton();
221  instructionsScrollPane = new javax.swing.JScrollPane();
222  instructionsTextArea = new javax.swing.JTextPane();
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));
224 
225  setLayout(new java.awt.GridBagLayout());
226 
227  org.openide.awt.Mnemonics.setLocalizedText(credentialsLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.credentialsLabel.text")); // NOI18N
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);
237 
238  credentialsPathField.setEditable(false);
239  credentialsPathField.setMaximumSize(new java.awt.Dimension(700, 22));
240  credentialsPathField.setPreferredSize(new java.awt.Dimension(100, 22));
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);
248  add(credentialsPathField, gridBagConstraints);
249 
250  org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.browseButton.text")); // NOI18N
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) {
255  }
256  });
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);
262  add(browseButton, gridBagConstraints);
263 
264  targetLanguageComboBox.setEnabled(false);
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);
272  add(targetLanguageComboBox, gridBagConstraints);
273 
274  org.openide.awt.Mnemonics.setLocalizedText(targetLanguageLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.targetLanguageLabel.text")); // NOI18N
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);
283 
284  warningLabel.setForeground(new java.awt.Color(255, 0, 0));
285  org.openide.awt.Mnemonics.setLocalizedText(warningLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.warningLabel.text")); // NOI18N
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);
294  add(warningLabel, gridBagConstraints);
295 
296  org.openide.awt.Mnemonics.setLocalizedText(testResultValueLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.testResultValueLabel.text")); // NOI18N
297  testResultValueLabel.setMaximumSize(new java.awt.Dimension(600, 22));
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);
305  add(testResultValueLabel, gridBagConstraints);
306 
307  org.openide.awt.Mnemonics.setLocalizedText(resultLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.resultLabel.text")); // NOI18N
308  resultLabel.setEnabled(false);
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);
314  add(resultLabel, gridBagConstraints);
315 
316  org.openide.awt.Mnemonics.setLocalizedText(untranslatedLabel, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.untranslatedLabel.text")); // NOI18N
317  untranslatedLabel.setEnabled(false);
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);
324  add(untranslatedLabel, gridBagConstraints);
325 
326  testUntranslatedTextField.setText(DEFUALT_TEST_STRING);
327  testUntranslatedTextField.setEnabled(false);
328  testUntranslatedTextField.setMinimumSize(new java.awt.Dimension(160, 22));
329  testUntranslatedTextField.setPreferredSize(new java.awt.Dimension(160, 22));
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);
337  add(testUntranslatedTextField, gridBagConstraints);
338 
339  org.openide.awt.Mnemonics.setLocalizedText(testButton, org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.testButton.text")); // NOI18N
340  testButton.setEnabled(false);
341  testButton.addActionListener(new java.awt.event.ActionListener() {
342  public void actionPerformed(java.awt.event.ActionEvent evt) {
344  }
345  });
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);
352  add(testButton, gridBagConstraints);
353 
354  instructionsScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
355  instructionsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
356 
357  instructionsTextArea.setEditable(false);
358  instructionsTextArea.setBackground(new java.awt.Color(240, 240, 240));
359  instructionsTextArea.setContentType("text/html"); // NOI18N
360  instructionsTextArea.setText(org.openide.util.NbBundle.getMessage(GoogleTranslatorSettingsPanel.class, "GoogleTranslatorSettingsPanel.instructionsTextArea.text")); // NOI18N
361  instructionsTextArea.setMaximumSize(new java.awt.Dimension(1000, 200));
362  instructionsTextArea.setPreferredSize(new java.awt.Dimension(164, 78));
364 
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);
374  add(instructionsScrollPane, gridBagConstraints);
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);
382  }// </editor-fold>//GEN-END:initComponents
383 
384  @Messages({"GoogleTranslatorSettingsPanel.json.description=JSON Files",
385  "GoogleTranslatorSettingsPanel.fileChooser.confirmButton=Select"})
386  private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
387  JFileChooser fileChooser = new JFileChooser();
388  fileChooser.setDragEnabled(false);
389  //if they previously had a path set, start navigation there
390  if (!StringUtils.isBlank(credentialsPathField.getText())) {
391  fileChooser.setCurrentDirectory(new File(credentialsPathField.getText()).getParentFile());
392  }
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) {
398  credentialsPathField.setText(fileChooser.getSelectedFile().getPath());
400  testResultValueLabel.setText("");
401  firePropertyChange("SettingChanged", true, false);
402  }
403  }//GEN-LAST:event_browseButtonActionPerformed
404 
405  @Messages({"GoogleTranslatorSettingsPanel.errorMessage.translationFailure=Translation failure with specified credentials"})
406  private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed
407  testResultValueLabel.setText("");
408  Translate tempTranslate = getTemporaryTranslationService();
409  if (tempTranslate != null) {
410  try {
411  Translation translation = tempTranslate.translate(testUntranslatedTextField.getText());
412  testResultValueLabel.setText(translation.getTranslatedText());
413  warningLabel.setText("");
414  } catch (Exception ex) {
415  warningLabel.setText(Bundle.GoogleTranslatorSettingsPanel_errorMessage_translationFailure());
416  logger.log(Level.WARNING, Bundle.GoogleTranslatorSettingsPanel_errorMessage_translationFailure(), ex);
417  }
418  }
419  }//GEN-LAST:event_testButtonActionPerformed
420 
421  // Variables declaration - do not modify//GEN-BEGIN:variables
422  private javax.swing.JButton browseButton;
423  private javax.swing.JTextField credentialsPathField;
424  private javax.swing.JScrollPane instructionsScrollPane;
425  private javax.swing.JTextPane instructionsTextArea;
426  private javax.swing.JLabel resultLabel;
428  private javax.swing.JButton testButton;
429  private javax.swing.JLabel testResultValueLabel;
430  private javax.swing.JTextField testUntranslatedTextField;
431  private javax.swing.JLabel untranslatedLabel;
432  private javax.swing.JLabel warningLabel;
433  // End of variables declaration//GEN-END:variables
434 
441  String getTargetLanguageCode() {
442  return targetLanguageCode;
443  }
444 
451  String getCredentialsPath() {
452  return credentialsPathField.getText();
453  }
454 
459  private class ComboBoxSelectionListener implements ItemListener {
460 
461  @Override
462  public void itemStateChanged(java.awt.event.ItemEvent evt) {
463  String selectedCode = ((LanguageWrapper) targetLanguageComboBox.getSelectedItem()).getLanguageCode();
464  if (!StringUtils.isBlank(selectedCode) && !selectedCode.equals(targetLanguageCode)) {
465  targetLanguageCode = selectedCode;
467  testResultValueLabel.setText("");
468  firePropertyChange("SettingChanged", true, false);
469  }
470  }
471  }
472 }
javax.swing.JComboBox< org.sleuthkit.autopsy.texttranslation.translators.LanguageWrapper > targetLanguageComboBox
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.