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

Copyright © 2012-2022 Basis Technology. Generated on: Mon Dec 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.