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();
121 UserPreferences.setTextTranslatorName(currentSelection);
123 TextTranslationService.getInstance().updateSelectedTranslator();
124 if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
126 TextTranslationService.getInstance().getTranslatorByName(currentSelection).saveSettings();
127 }
catch (NoServiceProviderException | TranslationConfigException ex) {
128 logger.log(Level.WARNING,
"Unable to save settings for TextTranslator named: " + currentSelection, ex);
139 @SuppressWarnings(
"unchecked")
141 private
void initComponents() {
143 translatorComboBox =
new javax.swing.JComboBox<>();
144 translationServiceLabel =
new javax.swing.JLabel();
145 translationServicePanel =
new javax.swing.JPanel();
146 translationOptionsDescription =
new javax.swing.JLabel();
148 translatorComboBox.addActionListener(
new java.awt.event.ActionListener() {
149 public void actionPerformed(java.awt.event.ActionEvent evt) {
150 translatorComboBoxActionPerformed(evt);
154 org.openide.awt.Mnemonics.setLocalizedText(translationServiceLabel,
org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class,
"TranslationOptionsPanel.translationServiceLabel.text"));
156 translationServicePanel.setLayout(
new java.awt.BorderLayout());
158 org.openide.awt.Mnemonics.setLocalizedText(translationOptionsDescription,
org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class,
"TranslationOptionsPanel.translationOptionsDescription.text"));
160 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
161 this.setLayout(layout);
162 layout.setHorizontalGroup(
163 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
164 .addGroup(layout.createSequentialGroup()
166 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
167 .addComponent(translationServicePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
168 .addGroup(layout.createSequentialGroup()
169 .addComponent(translationServiceLabel)
171 .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
172 .addGap(0, 0, Short.MAX_VALUE))
173 .addComponent(translationOptionsDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 462, Short.MAX_VALUE))
176 layout.setVerticalGroup(
177 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178 .addGroup(layout.createSequentialGroup()
180 .addComponent(translationOptionsDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
181 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
182 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
183 .addComponent(translatorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
184 .addComponent(translationServiceLabel))
185 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
186 .addComponent(translationServicePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
191 private void translatorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
197 private javax.swing.JLabel translationOptionsDescription;
198 private javax.swing.JLabel translationServiceLabel;
199 private javax.swing.JPanel translationServicePanel;
200 private javax.swing.JComboBox<String> translatorComboBox;
synchronized static Logger getLogger(String name)