mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-13 00:16:16 +00:00
check for interupts/cancelation in ImageUtils methods/tasks; set cancelation text on progress bars.
This commit is contained in:
parent
0d8855d8b4
commit
4938ad4487
@ -32,10 +32,10 @@ import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.swing.SortOrder;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.SwingWorker;
|
||||
import javax.swing.Timer;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -213,13 +213,11 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Node that wraps around original node and adds the bitmap icon
|
||||
* representing the picture
|
||||
*/
|
||||
private class ThumbnailViewNode extends FilterNode {
|
||||
class ThumbnailViewNode extends FilterNode {
|
||||
|
||||
private Logger logger = Logger.getLogger(ThumbnailViewNode.class.getName());
|
||||
|
||||
@ -282,19 +280,28 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
|
||||
|
||||
private final Content content;
|
||||
private final ProgressHandle progressHandle;
|
||||
private volatile boolean started = false;
|
||||
private final String progressText;
|
||||
|
||||
ThumbnailLoadTask(Content content) {
|
||||
this.content = content;
|
||||
final String progressText = Bundle.ThumbnailViewNode_progressHandle_text(content.getName());
|
||||
progressText = Bundle.ThumbnailViewNode_progressHandle_text(content.getName());
|
||||
progressHandle = ProgressHandle.createHandle(progressText);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Image doInBackground() throws Exception {
|
||||
synchronized (progressHandle) {
|
||||
progressHandle.start();
|
||||
started = true;
|
||||
}
|
||||
return ImageUtils.getThumbnail(content, iconSize);
|
||||
}
|
||||
|
||||
private void cancel() {
|
||||
SwingUtilities.invokeLater(() -> progressHandle.setDisplayName(progressText + " (Cancelling)"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
super.done();
|
||||
@ -302,11 +309,18 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
|
||||
thumbCache = new SoftReference<>(super.get());
|
||||
fireIconChange();
|
||||
} catch (CancellationException ex) {
|
||||
//do nothing, it was cancelled
|
||||
//Task was cancelled, do nothing
|
||||
} catch (InterruptedException | ExecutionException ex) {
|
||||
if (ex.getCause() instanceof CancellationException) {
|
||||
} else {
|
||||
logger.log(Level.SEVERE, "Error getting thumbnail icon for " + content.getName(), ex); //NON-NLS
|
||||
}
|
||||
} finally {
|
||||
synchronized (progressHandle) {
|
||||
if (started) {
|
||||
progressHandle.finish();
|
||||
}
|
||||
}
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
timer = null;
|
||||
@ -314,24 +328,30 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
|
||||
thumbTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private final ExecutorService executor = Executors.newFixedThreadPool(4,
|
||||
new ThreadFactoryBuilder().setNameFormat("Thumbnail-Loader-%d").build());
|
||||
|
||||
private final List<Future<?>> futures = new ArrayList<>();
|
||||
private final List<ThumbnailViewNode.ThumbnailLoadTask> tasks = new ArrayList<>();
|
||||
|
||||
synchronized void cancelLoadingThumbnails() {
|
||||
futures.forEach(future -> future.cancel(true));
|
||||
futures.clear();
|
||||
tasks.forEach(ThumbnailViewNode.ThumbnailLoadTask::cancel);
|
||||
tasks.clear();
|
||||
executor.shutdownNow();
|
||||
}
|
||||
|
||||
private synchronized ThumbnailViewNode.ThumbnailLoadTask loadThumbnail(ThumbnailViewNode node, Content content) {
|
||||
if (executor.isShutdown() == false) {
|
||||
ThumbnailViewNode.ThumbnailLoadTask task = node.new ThumbnailLoadTask(content);
|
||||
futures.add(task);
|
||||
tasks.add(task);
|
||||
executor.submit(task);
|
||||
return task;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,6 +40,7 @@ import java.util.List;
|
||||
import static java.util.Objects.nonNull;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
@ -164,8 +165,8 @@ public class ImageUtils {
|
||||
/**
|
||||
* Thread/Executor that saves generated thumbnails to disk in the background
|
||||
*/
|
||||
private static final Executor imageSaver
|
||||
= Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
|
||||
private static final Executor imageSaver =
|
||||
Executors.newSingleThreadExecutor(new BasicThreadFactory.Builder()
|
||||
.namingPattern("thumbnail-saver-%d").build()); //NON-NLS
|
||||
|
||||
public static List<String> getSupportedImageExtensions() {
|
||||
@ -241,8 +242,9 @@ public class ImageUtils {
|
||||
*
|
||||
* @param file the AbstractFile to test
|
||||
* @param mimeTypePrefix a MIME 'top-level type name' such as "image/",
|
||||
* including the "/". In addition to the list of supported MIME types, any
|
||||
* type that starts with this prefix will be regarded as supported
|
||||
* including the "/". In addition to the list of
|
||||
* supported MIME types, any type that starts with
|
||||
* this prefix will be regarded as supported
|
||||
* @param supportedMimeTypes a collection of mimetypes that are supported
|
||||
* @param supportedExtension a collection of extensions that are supported
|
||||
*
|
||||
@ -310,8 +312,14 @@ public class ImageUtils {
|
||||
* to rescale easily, but we lose animations.
|
||||
*/
|
||||
try (BufferedInputStream bufferedReadContentStream = getBufferedReadContentStream(file);) {
|
||||
if (Thread.interrupted()) {
|
||||
return DEFAULT_THUMBNAIL;
|
||||
}
|
||||
final BufferedImage image = ImageIO.read(bufferedReadContentStream);
|
||||
if (image != null) {
|
||||
if (Thread.interrupted()) {
|
||||
return DEFAULT_THUMBNAIL;
|
||||
}
|
||||
return ScalrWrapper.resizeHighQuality(image, iconSize, iconSize);
|
||||
}
|
||||
} catch (IOException iOException) {
|
||||
@ -321,6 +329,9 @@ public class ImageUtils {
|
||||
}
|
||||
|
||||
Task<javafx.scene.image.Image> thumbnailTask = newGetThumbnailTask(file, iconSize, true);
|
||||
if (Thread.interrupted()) {
|
||||
return DEFAULT_THUMBNAIL;
|
||||
}
|
||||
thumbnailTask.run();
|
||||
try {
|
||||
return SwingFXUtils.fromFXImage(thumbnailTask.get(), null);
|
||||
@ -367,8 +378,8 @@ public class ImageUtils {
|
||||
* @param fileID the fileID to get the cached thumbnail location for
|
||||
*
|
||||
* @return A File object representing the location of the cached thumbnail.
|
||||
* This file may not actually exist(yet). Returns null if there was any
|
||||
* problem getting the file, such as no case was open.
|
||||
* This file may not actually exist(yet). Returns null if there was
|
||||
* any problem getting the file, such as no case was open.
|
||||
*/
|
||||
private static File getCachedThumbnailLocation(long fileID) {
|
||||
return cacheFileMap.computeIfAbsent(fileID, id -> {
|
||||
@ -553,8 +564,9 @@ public class ImageUtils {
|
||||
* file.
|
||||
*
|
||||
* @param file the file to extract the data from
|
||||
* @param errorTemplate a message template used to log errors. Should take
|
||||
* one parameter: the file's unique path or name.
|
||||
* @param errorTemplate a message template used to log errors. Should
|
||||
* take one parameter: the file's unique path or
|
||||
* name.
|
||||
* @param propertyExtractor an implementation of {@link PropertyExtractor}
|
||||
* used to retrieve the specific property.
|
||||
*
|
||||
@ -695,7 +707,9 @@ public class ImageUtils {
|
||||
throw new IIOException(msg);
|
||||
}
|
||||
updateProgress(-1, 1);
|
||||
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
//resize, or if that fails, crop it
|
||||
try {
|
||||
thumbnail = ScalrWrapper.resizeFast(bufferedImage, iconSize);
|
||||
@ -709,6 +723,9 @@ public class ImageUtils {
|
||||
final int cropHeight = Math.min(iconSize, height);
|
||||
final int cropWidth = Math.min(iconSize, width);
|
||||
try {
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
thumbnail = ScalrWrapper.cropImage(bufferedImage, cropWidth, cropHeight);
|
||||
} catch (Exception cropException) {
|
||||
LOGGER.log(Level.WARNING, "Could not crop {0}: " + cropException.toString(), ImageUtils.getContentPathSafe(file)); //NON-NLS
|
||||
@ -720,17 +737,15 @@ public class ImageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
updateProgress(-1, 1);
|
||||
|
||||
//if we got a valid thumbnail save it
|
||||
if ((cacheFile != null) && thumbnail != null && DEFAULT_THUMBNAIL != thumbnail) {
|
||||
saveThumbnail(thumbnail);
|
||||
}
|
||||
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
return SwingFXUtils.toFXImage(thumbnail, null);
|
||||
}
|
||||
|
||||
@ -806,6 +821,9 @@ public class ImageUtils {
|
||||
}
|
||||
|
||||
protected javafx.scene.image.Image readImage() throws IOException {
|
||||
if (isCancelled()) {
|
||||
return null;
|
||||
}
|
||||
if (ImageUtils.isGIF(file)) {
|
||||
//use JavaFX to directly read GIF to preserve potential animation
|
||||
javafx.scene.image.Image image = new javafx.scene.image.Image(getBufferedReadContentStream(file));
|
||||
@ -865,6 +883,14 @@ public class ImageUtils {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
if (Thread.interrupted()) {
|
||||
this.cancel(true);
|
||||
}
|
||||
return super.isCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void succeeded() {
|
||||
super.succeeded();
|
||||
|
Loading…
x
Reference in New Issue
Block a user