19 package org.sleuthkit.autopsy.texttranslation.ui;
21 import java.awt.ComponentOrientation;
23 import java.io.IOException;
24 import java.util.concurrent.CancellationException;
25 import java.util.concurrent.ExecutionException;
26 import java.util.logging.Level;
27 import javax.swing.SwingUtilities;
28 import javax.swing.SwingWorker;
29 import org.openide.util.NbBundle;
39 public abstract class TranslateTextTask extends SwingWorker<TranslateTextTask.TranslateResult, Void> {
91 this.contentDescriptor = fileDescriptor;
101 protected abstract String
retrieveText() throws IOException, InterruptedException, IllegalStateException;
109 protected abstract
void onTextDisplay(String text, ComponentOrientation orientation,
int font);
130 protected void onErrorDisplay(String text, ComponentOrientation orientation,
int font) {
136 "TranslatedContentViewer.translatingText=Translating text, please wait...",
137 "TranslatedContentViewer.fileHasNoText=File has no text.",
138 "TranslatedContentViewer.noServiceProvider=The machine translation software was not found.",
139 "# {0} - exception message",
"TranslatedContentViewer.translationException=An error occurred while translating the text ({0})."
143 if (this.isCancelled()) {
144 throw new InterruptedException();
150 }
catch (IOException | IllegalStateException ex) {
154 if (this.isCancelled()) {
155 throw new InterruptedException();
158 if (fileText == null || fileText.isEmpty()) {
162 if (!this.translateText) {
176 SwingUtilities.invokeLater(() -> {
177 onProgressDisplay(Bundle.TranslatedContentViewer_translatingText(), ComponentOrientation.LEFT_TO_RIGHT, Font.ITALIC);
181 String translation =
translate(fileText);
182 if (this.isCancelled()) {
183 throw new InterruptedException();
186 if (translation == null || translation.isEmpty()) {
193 logger.log(Level.WARNING,
"Error translating text for file " +
this.contentDescriptor, ex);
196 logger.log(Level.WARNING,
"Error translating text for file " +
this.contentDescriptor, ex);
197 return TranslateResult.
error(Bundle.TranslatedContentViewer_translationException(ex.getMessage()));
206 if (this.isCancelled()) {
207 throw new InterruptedException();
211 String result = executionResult.
getResult();
212 int len = result.length();
213 int maxOrientChars = Math.min(len, 1024);
214 String orientDetectSubstring = result.substring(0, maxOrientChars);
220 }
catch (InterruptedException | CancellationException ignored) {
222 }
catch (ExecutionException ex) {
223 logger.log(Level.WARNING,
"Error occurred during background task execution for file " +
this.contentDescriptor, ex);
224 onErrorDisplay(Bundle.TranslatedContentViewer_translationException(ex.getMessage()), ComponentOrientation.LEFT_TO_RIGHT, Font.ITALIC);
236 "TranslatedContentViewer.emptyTranslation=The machine translation software did not return any text."
240 return translatorInstance.
translate(input);
static TranslateResult success(String content)
static TranslateResult error(String message)
abstract void onTextDisplay(String text, ComponentOrientation orientation, int font)
synchronized String translate(String input)
final String errorMessage
String translate(String input)
static TextTranslationService getInstance()
TranslateResult translateRetrievedText(String fileText)
TranslateResult(String result, String errorMessage, boolean successful)
TranslateTextTask(boolean translateText, String fileDescriptor)
final String contentDescriptor
static final Logger logger
void onErrorDisplay(String text, ComponentOrientation orientation, int font)
synchronized static Logger getLogger(String name)
abstract String retrieveText()
static ComponentOrientation getTextDirection(String text)
TranslateResult doInBackground()
void onProgressDisplay(String text, ComponentOrientation orientation, int font)
final boolean translateText