19 package org.sleuthkit.autopsy.texttranslation;
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;
35 final class TranslationOptionsPanel
extends javax.swing.JPanel {
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 =
"";
45 @Messages({
"TranslationOptionsPanel.translationDisabled.text=Translation disabled"})
46 TranslationOptionsPanel(TranslationOptionsPanelController theController) {
48 controller = theController;
49 translatorComboBox.addItem(Bundle.TranslationOptionsPanel_translationDisabled_text());
50 TextTranslationService.getInstance().getTranslators().forEach((translator) -> {
51 translatorComboBox.addItem(translator.getName());
53 translatorComboBox.setEnabled(translatorComboBox.getItemCount() > 1);
61 private void updatePanel() {
62 if (!currentSelection.equals(translatorComboBox.getSelectedItem().toString())) {
63 currentSelection = translatorComboBox.getSelectedItem().toString();
64 loadSelectedPanelSettings();
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())) {
79 Component panel = TextTranslationService.getInstance().getTranslatorByName(translatorComboBox.getSelectedItem().toString()).getSettingsPanel();
80 panel.addPropertyChangeListener(
new PropertyChangeListener() {
82 public void propertyChange(PropertyChangeEvent evt) {
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);
94 if (translatorComboBox.getItemCount() < 2) {
95 translationServicePanel.add(
new JLabel(Bundle.TranslationOptionsPanel_noTextTranslators_text()), BorderLayout.PAGE_START);
97 translationServicePanel.add(
new JLabel(Bundle.TranslationOptionsPanel_noTextTranslatorSelected_text()), BorderLayout.PAGE_START);
108 currentSelection = UserPreferences.getTextTranslatorName();
109 if (currentSelection == null) {
110 currentSelection = Bundle.TranslationOptionsPanel_translationDisabled_text();
112 translatorComboBox.setSelectedItem(currentSelection);
113 loadSelectedPanelSettings();
114 enableOcrCheckBox.setSelected(UserPreferences.getUseOcrInTranslation());
122 UserPreferences.setTextTranslatorName(currentSelection);
124 TextTranslationService.getInstance().updateSelectedTranslator();
125 if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
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);
133 UserPreferences.setUseOcrInTranslation(enableOcrCheckBox.isSelected());
142 @SuppressWarnings(
"unchecked")
144 private
void initComponents() {
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();
153 translatorComboBox.addActionListener(
new java.awt.event.ActionListener() {
154 public void actionPerformed(java.awt.event.ActionEvent evt) {
155 translatorComboBoxActionPerformed(evt);
159 org.openide.awt.Mnemonics.setLocalizedText(translationServiceLabel,
org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class,
"TranslationOptionsPanel.translationServiceLabel.text"));
161 translationServicePanel.setLayout(
new java.awt.BorderLayout());
163 org.openide.awt.Mnemonics.setLocalizedText(translationOptionsDescription,
org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class,
"TranslationOptionsPanel.translationOptionsDescription.text"));
165 org.openide.awt.Mnemonics.setLocalizedText(enableOcrCheckBox,
org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class,
"TranslationOptionsPanel.enableOcrCheckBox.text"));
166 enableOcrCheckBox.addActionListener(
new java.awt.event.ActionListener() {
167 public void actionPerformed(java.awt.event.ActionEvent evt) {
168 enableOcrCheckBoxActionPerformed(evt);
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()
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)
187 .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
188 .addComponent(enableOcrCheckBox))
189 .addGap(0, 0, Short.MAX_VALUE)))
192 layout.setVerticalGroup(
193 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
194 .addGroup(layout.createSequentialGroup()
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))
211 private void translatorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
215 private void enableOcrCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
216 controller.changed();
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;
synchronized static Logger getLogger(String name)