Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
TranslationOptionsPanel.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;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.logging.Level;
27 import javax.swing.JLabel;
28 import org.openide.util.NbBundle.Messages;
31 
35 final class TranslationOptionsPanel extends javax.swing.JPanel {
36 
37  private final static Logger logger = Logger.getLogger(TranslationOptionsPanel.class.getName());
38  private static final long serialVersionUID = 1L;
39  private final TranslationOptionsPanelController controller;
40  private String currentSelection = "";
41 
45  @Messages({"TranslationOptionsPanel.translationDisabled.text=Translation disabled"})
46  TranslationOptionsPanel(TranslationOptionsPanelController theController) {
47  initComponents();
48  controller = theController;
49  translatorComboBox.addItem(Bundle.TranslationOptionsPanel_translationDisabled_text());
50  TextTranslationService.getInstance().getTranslators().forEach((translator) -> {
51  translatorComboBox.addItem(translator.getName());
52  });
53  translatorComboBox.setEnabled(translatorComboBox.getItemCount() > 1);
54  load();
55  }
56 
61  private void updatePanel() {
62  if (!currentSelection.equals(translatorComboBox.getSelectedItem().toString())) {
63  currentSelection = translatorComboBox.getSelectedItem().toString();
64  loadSelectedPanelSettings();
65  controller.changed();
66  }
67  }
68 
72  @Messages({"TranslationOptionsPanel.textTranslatorsUnavailable.text=Unable to get selected text translator, translation is disabled.",
73  "TranslationOptionsPanel.noTextTranslatorSelected.text=No text translator selected, translation is disabled.",
74  "TranslationOptionsPanel.noTextTranslators.text=No text translators exist, translation is disabled."})
75  private void loadSelectedPanelSettings() {
76  translationServicePanel.removeAll();
77  if (translatorComboBox.getSelectedItem() != null && !translatorComboBox.getSelectedItem().toString().equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
78  try {
79  Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getSettingsPanel();
80  panel.addPropertyChangeListener(new PropertyChangeListener() {
81  @Override
82  public void propertyChange(PropertyChangeEvent evt) {
83  controller.changed();
84  }
85  });
86  translationServicePanel.add(panel, BorderLayout.PAGE_START);
87  } catch (NoServiceProviderException ex) {
88  logger.log(Level.WARNING, "Unable to get TextExtractor named: " + translatorComboBox.getSelectedItem().toString(), ex);
89  JLabel label = new JLabel(Bundle.TranslationOptionsPanel_textTranslatorsUnavailable_text());
90  label.setForeground(Color.RED);
91  translationServicePanel.add(label, BorderLayout.PAGE_START);
92  }
93  } else {
94  if (translatorComboBox.getItemCount() < 2) {
95  translationServicePanel.add(new JLabel(Bundle.TranslationOptionsPanel_noTextTranslators_text()), BorderLayout.PAGE_START);
96  } else {
97  translationServicePanel.add(new JLabel(Bundle.TranslationOptionsPanel_noTextTranslatorSelected_text()), BorderLayout.PAGE_START);
98  }
99  }
100  revalidate();
101  repaint();
102  }
103 
107  final void load() {
108  currentSelection = UserPreferences.getTextTranslatorName();
109  if (currentSelection == null) {
110  currentSelection = Bundle.TranslationOptionsPanel_translationDisabled_text();
111  }
112  translatorComboBox.setSelectedItem(currentSelection);
113  loadSelectedPanelSettings();
114  enableOcrCheckBox.setSelected(UserPreferences.getUseOcrInTranslation());
115  }
116 
120  void store() {
121  //The current text translator name is saved to user preferences
122  UserPreferences.setTextTranslatorName(currentSelection);
123  //The TextTranslationService updates the TextTranslator in use from user preferences
124  TextTranslationService.getInstance().updateSelectedTranslator();
125  if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
126  try {
127  TextTranslationService.getInstance().getTranslatorByName(currentSelection).saveSettings();
128  } catch (NoServiceProviderException | TranslationConfigException ex) {
129  logger.log(Level.WARNING, "Unable to save settings for TextTranslator named: " + currentSelection, ex);
130  }
131  }
132  // Save whether OCR is enabled in the content viewer
133  UserPreferences.setUseOcrInTranslation(enableOcrCheckBox.isSelected());
134 
135  }
136 
142  @SuppressWarnings("unchecked")
143  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
144  private void initComponents() {
145 
146  translatorComboBox = new javax.swing.JComboBox<>();
147  translationServiceLabel = new javax.swing.JLabel();
148  translationServicePanel = new javax.swing.JPanel();
149  translationOptionsDescription = new javax.swing.JLabel();
150  jSeparator1 = new javax.swing.JSeparator();
151  enableOcrCheckBox = new javax.swing.JCheckBox();
152 
153  translatorComboBox.addActionListener(new java.awt.event.ActionListener() {
154  public void actionPerformed(java.awt.event.ActionEvent evt) {
155  translatorComboBoxActionPerformed(evt);
156  }
157  });
158 
159  org.openide.awt.Mnemonics.setLocalizedText(translationServiceLabel, org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class, "TranslationOptionsPanel.translationServiceLabel.text")); // NOI18N
160 
161  translationServicePanel.setLayout(new java.awt.BorderLayout());
162 
163  org.openide.awt.Mnemonics.setLocalizedText(translationOptionsDescription, org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class, "TranslationOptionsPanel.translationOptionsDescription.text")); // NOI18N
164 
165  org.openide.awt.Mnemonics.setLocalizedText(enableOcrCheckBox, org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class, "TranslationOptionsPanel.enableOcrCheckBox.text")); // NOI18N
166  enableOcrCheckBox.addActionListener(new java.awt.event.ActionListener() {
167  public void actionPerformed(java.awt.event.ActionEvent evt) {
168  enableOcrCheckBoxActionPerformed(evt);
169  }
170  });
171 
172  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
173  this.setLayout(layout);
174  layout.setHorizontalGroup(
175  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
176  .addComponent(jSeparator1)
177  .addGroup(layout.createSequentialGroup()
178  .addContainerGap()
179  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
180  .addComponent(translationServicePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
181  .addComponent(translationOptionsDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
182  .addGroup(layout.createSequentialGroup()
183  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
184  .addGroup(layout.createSequentialGroup()
185  .addComponent(translationServiceLabel)
186  .addGap(10, 10, 10)
187  .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
188  .addComponent(enableOcrCheckBox))
189  .addGap(0, 0, Short.MAX_VALUE)))
190  .addContainerGap())
191  );
192  layout.setVerticalGroup(
193  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
194  .addGroup(layout.createSequentialGroup()
195  .addContainerGap()
196  .addComponent(translationOptionsDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
197  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
198  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
199  .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
200  .addComponent(translationServiceLabel))
201  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202  .addComponent(translationServicePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
203  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
204  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
205  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
206  .addComponent(enableOcrCheckBox)
207  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
208  );
209  }// </editor-fold>//GEN-END:initComponents
210 
211  private void translatorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_translatorComboBoxActionPerformed
212  updatePanel();
213  }//GEN-LAST:event_translatorComboBoxActionPerformed
214 
215  private void enableOcrCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_enableOcrCheckBoxActionPerformed
216  controller.changed();
217  }//GEN-LAST:event_enableOcrCheckBoxActionPerformed
218 
219 
220  // Variables declaration - do not modify//GEN-BEGIN:variables
221  private javax.swing.JCheckBox enableOcrCheckBox;
222  private javax.swing.JSeparator jSeparator1;
223  private javax.swing.JLabel translationOptionsDescription;
224  private javax.swing.JLabel translationServiceLabel;
225  private javax.swing.JPanel translationServicePanel;
226  private javax.swing.JComboBox<String> translatorComboBox;
227  // End of variables declaration//GEN-END:variables
228 
229 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.