Autopsy  4.14.0
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  }
115 
119  void store() {
120  //The current text translator name is saved to user preferences
121  UserPreferences.setTextTranslatorName(currentSelection);
122  //The TextTranslationService updates the TextTranslator in use from user preferences
123  TextTranslationService.getInstance().updateSelectedTranslator();
124  if (currentSelection != null && !currentSelection.equals(Bundle.TranslationOptionsPanel_translationDisabled_text())) {
125  try {
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);
129  }
130  }
131 
132  }
133 
139  @SuppressWarnings("unchecked")
140  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
141  private void initComponents() {
142 
143  translatorComboBox = new javax.swing.JComboBox<>();
144  translationServiceLabel = new javax.swing.JLabel();
145  translationServicePanel = new javax.swing.JPanel();
146  translationOptionsDescription = new javax.swing.JLabel();
147 
148  translatorComboBox.addActionListener(new java.awt.event.ActionListener() {
149  public void actionPerformed(java.awt.event.ActionEvent evt) {
150  translatorComboBoxActionPerformed(evt);
151  }
152  });
153 
154  org.openide.awt.Mnemonics.setLocalizedText(translationServiceLabel, org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class, "TranslationOptionsPanel.translationServiceLabel.text")); // NOI18N
155 
156  translationServicePanel.setLayout(new java.awt.BorderLayout());
157 
158  org.openide.awt.Mnemonics.setLocalizedText(translationOptionsDescription, org.openide.util.NbBundle.getMessage(TranslationOptionsPanel.class, "TranslationOptionsPanel.translationOptionsDescription.text")); // NOI18N
159 
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()
165  .addContainerGap()
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)
170  .addGap(10, 10, 10)
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))
174  .addContainerGap())
175  );
176  layout.setVerticalGroup(
177  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178  .addGroup(layout.createSequentialGroup()
179  .addContainerGap()
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)
187  .addContainerGap())
188  );
189  }// </editor-fold>//GEN-END:initComponents
190 
191  private void translatorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_translatorComboBoxActionPerformed
192  updatePanel();
193  }//GEN-LAST:event_translatorComboBoxActionPerformed
194 
195 
196  // Variables declaration - do not modify//GEN-BEGIN:variables
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;
201  // End of variables declaration//GEN-END:variables
202 
203 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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.