Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
TranslationContentPanel.java
Go to the documentation of this file.
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.ui;
20 
21 import com.google.common.base.Strings;
22 import com.google.common.collect.ImmutableBiMap;
23 import java.awt.ComponentOrientation;
24 import java.awt.Font;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.util.ArrayList;
28 import java.util.List;
29 import javax.swing.JComboBox;
30 import org.openide.util.NbBundle;
31 import org.openide.util.NbBundle.Messages;
33 
38 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
39 public class TranslationContentPanel extends javax.swing.JPanel {
40 
41  private static final ImmutableBiMap<String, String> LANGUAGE_NAMES;
42  private static final long serialVersionUID = 1L;
43  private int lastSelectedIndex = 0;
44 
46 
51  initComponents();
52 
53  DISPLAY_PANE = new AutoWrappingJTextPane();
54  DISPLAY_PANE.setEditable(false);
55  textScrollPane.setViewportView(DISPLAY_PANE);
56  reset();
57  }
58 
59  final void removeDisplayTextOptions(String option) {
60  displayTextComboBox.removeItem(option);
61  }
62 
63  public void display(String text, ComponentOrientation direction, int font) {
64  DISPLAY_PANE.setComponentOrientation(direction);
65  DISPLAY_PANE.setText(text);
66  DISPLAY_PANE.setFont(new Font(DISPLAY_PANE.getFont().getName(),
67  font, DISPLAY_PANE.getFont().getSize()));
68  DISPLAY_PANE.setCaretPosition(0);
69  }
70 
71  void addDisplayTextActionListener(ActionListener listener) {
72  displayTextComboBox.addActionListener(listener);
73  }
74 
75  final void addOcrDropDownActionListener(ActionListener listener) {
76  ocrDropdown.addActionListener(listener);
77  }
78 
79  String getDisplayDropDownSelection() {
80  return (String) displayTextComboBox.getSelectedItem();
81  }
82 
83  String getSelectedOcrLanguagePack() {
84  String selection = (String) ocrDropdown.getSelectedItem();
85  return Strings.nullToEmpty(LANGUAGE_NAMES.inverse().get(selection));
86  }
87 
93  private void removeAllActionListeners(JComboBox<String> dropDown) {
94  for (ActionListener listener : dropDown.getActionListeners()) {
95  dropDown.removeActionListener(listener);
96  }
97  }
98 
99  void setWarningLabelMsg(String msg) {
100  warningLabel.setText(msg);
101  }
102 
103  @NbBundle.Messages({"TranslationContentPanel.autoDetectOCR=Autodetect language"})
104  final void reset() {
105  display("", ComponentOrientation.LEFT_TO_RIGHT, Font.PLAIN);
106 
107  removeAllActionListeners(displayTextComboBox);
108  displayTextComboBox.removeAllItems();
109  displayTextComboBox.addItem(DisplayDropdownOptions.TRANSLATED_TEXT.toString());
110  displayTextComboBox.addItem(DisplayDropdownOptions.ORIGINAL_TEXT.toString());
111 
112  enableOCRSelection(false);
113  removeAllActionListeners(ocrDropdown);
114  ocrDropdown.removeAllItems();
115  }
116 
117  final void enableOCRSelection(boolean enabled) {
118  ocrLabel.setEnabled(enabled);
119  ocrDropdown.setEnabled(enabled);
120  }
121 
127  void addLanguagePackNames(List<String> languagePackAbbreviations) {
128  //Put the default at the top of the list.
129  ocrDropdown.addItem(Bundle.TranslationContentPanel_autoDetectOCR());
130 
131  //Create a shallow copy of the input list for sorting
132  List<String> localCopy = new ArrayList<>(languagePackAbbreviations);
133 
134  localCopy.stream().sorted().forEach((abbrev) -> {
135  if (LANGUAGE_NAMES.get(abbrev) != null) {
136  ocrDropdown.addItem(LANGUAGE_NAMES.get(abbrev));
137  }
138  });
139 
140  ocrDropdown.setSelectedIndex(lastSelectedIndex);
141  addOcrDropDownActionListener((ActionEvent e) -> {
142  lastSelectedIndex = ocrDropdown.getSelectedIndex();
143  });
144  }
145 
146  private static ImmutableBiMap<String, String> createLanguageBiMap() {
147  return ImmutableBiMap.<String, String>builder()
148  .put("afr", "Afrikaans")
149  .put("amh", "Amharic")
150  .put("ara", "Arabic")
151  .put("asm", "Assamese")
152  .put("aze", "Azerbaijani")
153  .put("aze_cyrl", "Azerbaijani - Cyrillic")
154  .put("bel", "Belarusian")
155  .put("ben", "Bengali")
156  .put("bod", "Tibetan")
157  .put("bos", "Bosnian")
158  .put("bul", "Bulgarian")
159  .put("cat", "Catalan; Valencian")
160  .put("ceb", "Cebuano")
161  .put("ces", "Czech")
162  .put("chi_sim", "Chinese - Simplified")
163  .put("chi_tra", "Chinese - Traditional")
164  .put("chr", "Cherokee")
165  .put("cym", "Welsh")
166  .put("dan", "Danish")
167  .put("deu", "German")
168  .put("dzo", "Dzongkha")
169  .put("ell", "Greek, Modern (1453-)")
170  .put("eng", "English")
171  .put("enm", "English, Middle (1100-1500)")
172  .put("epo", "Esperanto")
173  .put("est", "Estonian")
174  .put("eus", "Basque")
175  .put("fas", "Persian")
176  .put("fin", "Finnish")
177  .put("fra", "French")
178  .put("frk", "Frankish")
179  .put("frm", "French, Middle (ca. 1400-1600)")
180  .put("gle", "Irish")
181  .put("glg", "Galician")
182  .put("grc", "Greek, Ancient (-1453)")
183  .put("guj", "Gujarati")
184  .put("hat", "Haitian; Haitian Creole")
185  .put("heb", "Hebrew")
186  .put("hin", "Hindi")
187  .put("hrv", "Croatian")
188  .put("hun", "Hungarian")
189  .put("iku", "Inuktitut")
190  .put("ind", "Indonesian")
191  .put("isl", "Icelandic")
192  .put("ita", "Italian")
193  .put("ita_old", "Italian - Old")
194  .put("jav", "Javanese")
195  .put("jpn", "Japanese")
196  .put("kan", "Kannada")
197  .put("kat", "Georgian")
198  .put("kat_old", "Georgian - Old")
199  .put("kaz", "Kazakh")
200  .put("khm", "Central Khmer")
201  .put("kir", "Kirghiz; Kyrgyz")
202  .put("kor", "Korean")
203  .put("kur", "Kurdish")
204  .put("lao", "Lao")
205  .put("lat", "Latin")
206  .put("lav", "Latvian")
207  .put("lit", "Lithuanian")
208  .put("mal", "Malayalam")
209  .put("mar", "Marathi")
210  .put("mkd", "Macedonian")
211  .put("mlt", "Maltese")
212  .put("msa", "Malay")
213  .put("mya", "Burmese")
214  .put("nep", "Nepali")
215  .put("nld", "Dutch; Flemish")
216  .put("nor", "Norwegian")
217  .put("ori", "Oriya")
218  .put("pan", "Panjabi; Punjabi")
219  .put("pol", "Polish")
220  .put("por", "Portuguese")
221  .put("pus", "Pushto; Pashto")
222  .put("ron", "Romanian; Moldavian; Moldovan")
223  .put("rus", "Russian")
224  .put("san", "Sanskrit")
225  .put("sin", "Sinhala; Sinhalese")
226  .put("slk", "Slovak")
227  .put("slv", "Slovenian")
228  .put("spa", "Spanish; Castilian")
229  .put("spa_old", "Spanish; Castilian - Old")
230  .put("sqi", "Albanian")
231  .put("srp", "Serbian")
232  .put("srp_latn", "Serbian - Latin")
233  .put("swa", "Swahili")
234  .put("swe", "Swedish")
235  .put("syr", "Syriac")
236  .put("tam", "Tamil")
237  .put("tel", "Telugu")
238  .put("tgk", "Tajik")
239  .put("tgl", "Tagalog")
240  .put("tha", "Thai")
241  .put("tir", "Tigrinya")
242  .put("tur", "Turkish")
243  .put("uig", "Uighur; Uyghur")
244  .put("ukr", "Ukrainian")
245  .put("urd", "Urdu")
246  .put("uzb", "Uzbek")
247  .put("uzb_cyrl", "Uzbek - Cyrillic")
248  .put("vie", "Vietnamese")
249  .put("yid", "Yiddish")
250  .build();
251  }
252 
253  static {
254  //https://github.com/tesseract-ocr/tesseract/wiki/Data-Files
255  LANGUAGE_NAMES = createLanguageBiMap();
256  }
257 
261  @Messages({"TranslatedContentPanel.comboBoxOption.originalText=Original Text (Up to 25KB)",
262  "TranslatedContentPanel.comboBoxOption.translatedText=Translated Text"})
263  static enum DisplayDropdownOptions {
264  ORIGINAL_TEXT(Bundle.TranslatedContentPanel_comboBoxOption_originalText()),
265  TRANSLATED_TEXT(Bundle.TranslatedContentPanel_comboBoxOption_translatedText());
266 
267  private final String displayString;
268 
269  DisplayDropdownOptions(String displayString) {
270  this.displayString = displayString;
271  }
272 
273  @Override
274  public String toString() {
275  return displayString;
276  }
277  }
278 
284  @SuppressWarnings("unchecked")
285  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
286  private void initComponents() {
287  java.awt.GridBagConstraints gridBagConstraints;
288 
289  jPanel1 = new javax.swing.JPanel();
290  displayTextComboBox = new javax.swing.JComboBox<>();
291  ocrDropdown = new javax.swing.JComboBox<>();
292  ocrLabel = new javax.swing.JLabel();
293  warningLabel = new javax.swing.JLabel();
294  showLabel = new javax.swing.JLabel();
295  textScrollPane = new javax.swing.JScrollPane();
296 
297  setMaximumSize(new java.awt.Dimension(2000, 2000));
298  setMinimumSize(new java.awt.Dimension(2, 2));
299  setName(""); // NOI18N
300  setPreferredSize(new java.awt.Dimension(100, 58));
301  setVerifyInputWhenFocusTarget(false);
302  setLayout(new java.awt.BorderLayout());
303 
304  jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
305  jPanel1.setMaximumSize(new java.awt.Dimension(182, 24));
306  jPanel1.setPreferredSize(new java.awt.Dimension(182, 24));
307  jPanel1.setLayout(new java.awt.GridBagLayout());
308 
309  displayTextComboBox.setMinimumSize(new java.awt.Dimension(43, 20));
310  displayTextComboBox.setPreferredSize(new java.awt.Dimension(43, 20));
311  gridBagConstraints = new java.awt.GridBagConstraints();
312  gridBagConstraints.gridx = 3;
313  gridBagConstraints.gridy = 0;
314  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
315  gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
316  gridBagConstraints.weightx = 0.1;
317  gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
318  jPanel1.add(displayTextComboBox, gridBagConstraints);
319 
320  ocrDropdown.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "N/A" }));
321  ocrDropdown.setEnabled(false);
322  ocrDropdown.setName(""); // NOI18N
323  gridBagConstraints = new java.awt.GridBagConstraints();
324  gridBagConstraints.gridx = 5;
325  gridBagConstraints.gridy = 0;
326  gridBagConstraints.gridwidth = 2;
327  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
328  gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
329  gridBagConstraints.weightx = 0.1;
330  gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
331  jPanel1.add(ocrDropdown, gridBagConstraints);
332 
333  org.openide.awt.Mnemonics.setLocalizedText(ocrLabel, org.openide.util.NbBundle.getMessage(TranslationContentPanel.class, "TranslationContentPanel.ocrLabel.text")); // NOI18N
334  ocrLabel.setEnabled(false);
335  gridBagConstraints = new java.awt.GridBagConstraints();
336  gridBagConstraints.gridx = 4;
337  gridBagConstraints.gridy = 0;
338  gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
339  gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
340  jPanel1.add(ocrLabel, gridBagConstraints);
341 
342  warningLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/images/warning16.png"))); // NOI18N
343  gridBagConstraints = new java.awt.GridBagConstraints();
344  gridBagConstraints.gridx = 0;
345  gridBagConstraints.gridy = 0;
346  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
347  gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
348  gridBagConstraints.weightx = 0.25;
349  jPanel1.add(warningLabel, gridBagConstraints);
350 
351  org.openide.awt.Mnemonics.setLocalizedText(showLabel, org.openide.util.NbBundle.getMessage(TranslationContentPanel.class, "TranslationContentPanel.showLabel.text")); // NOI18N
352  gridBagConstraints = new java.awt.GridBagConstraints();
353  gridBagConstraints.gridx = 2;
354  gridBagConstraints.gridy = 0;
355  jPanel1.add(showLabel, gridBagConstraints);
356 
357  add(jPanel1, java.awt.BorderLayout.NORTH);
358 
359  textScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
360  textScrollPane.setMaximumSize(new java.awt.Dimension(20000, 20000));
361  textScrollPane.setPreferredSize(new java.awt.Dimension(640, 250));
362  add(textScrollPane, java.awt.BorderLayout.CENTER);
363  }// </editor-fold>//GEN-END:initComponents
364 
365  // Variables declaration - do not modify//GEN-BEGIN:variables
366  private javax.swing.JComboBox<String> displayTextComboBox;
367  private javax.swing.JPanel jPanel1;
368  private javax.swing.JComboBox<String> ocrDropdown;
369  private javax.swing.JLabel ocrLabel;
370  private javax.swing.JLabel showLabel;
371  private javax.swing.JScrollPane textScrollPane;
372  private javax.swing.JLabel warningLabel;
373  // End of variables declaration//GEN-END:variables
374 }
void display(String text, ComponentOrientation direction, int font)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.