move thread pool to DataResultViewerThumbnail so it can cancel all tasks easily in setNode

This commit is contained in:
millmanorama 2017-06-06 15:27:13 +02:00
parent 1bb84edc23
commit 115b0a99ce
4 changed files with 67 additions and 38 deletions

View File

@ -21,10 +21,18 @@ package org.sleuthkit.autopsy.corecomponents;
import java.awt.Color; import java.awt.Color;
import java.awt.Cursor; import java.awt.Cursor;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.awt.Image;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
@ -68,6 +76,7 @@ final class DataResultViewerThumbnail extends AbstractDataResultViewer {
private int curPageImages; private int curPageImages;
private int iconSize = ImageUtils.ICON_SIZE_MEDIUM; private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
private final PageUpdater pageUpdater = new PageUpdater(); private final PageUpdater pageUpdater = new PageUpdater();
private final ThumbnailLoader thumbLoader = new ThumbnailLoader();
/** /**
* Constructs a thumbnail viewer for the results view, with paging support, * Constructs a thumbnail viewer for the results view, with paging support,
@ -312,6 +321,7 @@ final class DataResultViewerThumbnail extends AbstractDataResultViewer {
@Override @Override
public void setNode(Node givenNode) { public void setNode(Node givenNode) {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
thumbLoader.cancellAll();
try { try {
if (givenNode != null) { if (givenNode != null) {
/* /*
@ -319,7 +329,8 @@ final class DataResultViewerThumbnail extends AbstractDataResultViewer {
* produce ThumbnailPageNodes with ThumbnailViewNode children * produce ThumbnailPageNodes with ThumbnailViewNode children
* from the child nodes of the given node. * from the child nodes of the given node.
*/ */
ThumbnailViewChildren childNode = new ThumbnailViewChildren(givenNode, iconSize); ThumbnailViewChildren childNode = new ThumbnailViewChildren(givenNode, thumbLoader);
childNode.setIconSize(iconSize);
final Node root = new AbstractNode(childNode); final Node root = new AbstractNode(childNode);
pageUpdater.setRoot(root); pageUpdater.setRoot(root);
root.addNodeListener(pageUpdater); root.addNodeListener(pageUpdater);
@ -433,8 +444,8 @@ final class DataResultViewerThumbnail extends AbstractDataResultViewer {
try { try {
get(); get();
} catch (InterruptedException | ExecutionException ex) { } catch (InterruptedException | ExecutionException ex) {
NotifyDescriptor d NotifyDescriptor d =
= new NotifyDescriptor.Message( new NotifyDescriptor.Message(
NbBundle.getMessage(this.getClass(), "DataResultViewerThumbnail.switchPage.done.errMsg", NbBundle.getMessage(this.getClass(), "DataResultViewerThumbnail.switchPage.done.errMsg",
ex.getMessage()), ex.getMessage()),
NotifyDescriptor.ERROR_MESSAGE); NotifyDescriptor.ERROR_MESSAGE);
@ -585,4 +596,21 @@ final class DataResultViewerThumbnail extends AbstractDataResultViewer {
} }
} }
} }
static class ThumbnailLoader {
private final ExecutorService executor = Executors.newFixedThreadPool(4);
private final List<Future<?>> futures = new ArrayList<>();
synchronized void cancellAll() {
futures.forEach(future -> future.cancel(true));
futures.clear();
}
synchronized void load(ThumbnailViewNode.ThumbnailLoadTask swingWorker) {
futures.add(swingWorker);
executor.submit(swingWorker);
}
}
} }

View File

@ -25,6 +25,7 @@ import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children; import org.openide.nodes.Children;
import org.openide.nodes.Node; import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups; import org.openide.util.lookup.Lookups;
import org.sleuthkit.autopsy.corecomponents.DataResultViewerThumbnail.ThumbnailLoader;
import org.sleuthkit.autopsy.coreutils.ImageUtils; import org.sleuthkit.autopsy.coreutils.ImageUtils;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.Content;
@ -48,15 +49,16 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
private int totalPages = 0; private int totalPages = 0;
private int iconSize = ImageUtils.ICON_SIZE_MEDIUM; private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
private static final Logger logger = Logger.getLogger(ThumbnailViewChildren.class.getName()); private static final Logger logger = Logger.getLogger(ThumbnailViewChildren.class.getName());
private final ThumbnailLoader thumbLoader;
/** /**
* the constructor * the constructor
*/ */
ThumbnailViewChildren(Node arg, int iconSize) { ThumbnailViewChildren(Node arg, ThumbnailLoader thumbLoader) {
super(true); //support lazy loading super(true); //support lazy loading
this.parent = arg; this.parent = arg;
this.iconSize = iconSize; this.thumbLoader = thumbLoader;
} }
@Override @Override
@ -188,17 +190,19 @@ class ThumbnailViewChildren extends Children.Keys<Integer> {
protected void removeNotify() { protected void removeNotify() {
super.removeNotify(); super.removeNotify();
setKeys(new ArrayList<Node>()); setKeys(new ArrayList<>());
} }
@Override @Override
protected Node[] createNodes(Node wrapped) { protected Node[] createNodes(Node wrapped) {
if (wrapped != null) { if (wrapped != null) {
final ThumbnailViewNode thumb = new ThumbnailViewNode(wrapped, iconSize); final ThumbnailViewNode thumb = new ThumbnailViewNode(wrapped, thumbLoader);
thumb.setIconSize(iconSize);
return new Node[]{thumb}; return new Node[]{thumb};
} else { } else {
return new Node[]{}; return new Node[]{};
} }
} }
} }
} }

View File

@ -24,8 +24,6 @@ import java.awt.event.ActionEvent;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.Timer; import javax.swing.Timer;
@ -34,6 +32,7 @@ import org.netbeans.api.progress.ProgressHandle;
import org.openide.nodes.FilterNode; import org.openide.nodes.FilterNode;
import org.openide.nodes.Node; import org.openide.nodes.Node;
import org.openide.util.NbBundle; import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.corecomponents.DataResultViewerThumbnail.ThumbnailLoader;
import org.sleuthkit.autopsy.coreutils.ImageUtils; import org.sleuthkit.autopsy.coreutils.ImageUtils;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.Content;
@ -44,22 +43,23 @@ import org.sleuthkit.datamodel.Content;
*/ */
class ThumbnailViewNode extends FilterNode { class ThumbnailViewNode extends FilterNode {
private Logger logger = Logger.getLogger(ThumbnailViewNode.class.getName());
static private final Image waitingIcon = Toolkit.getDefaultToolkit().createImage(ThumbnailViewNode.class.getResource("/org/sleuthkit/autopsy/images/working_spinner.gif")); static private final Image waitingIcon = Toolkit.getDefaultToolkit().createImage(ThumbnailViewNode.class.getResource("/org/sleuthkit/autopsy/images/working_spinner.gif"));
private SoftReference<Image> iconCache = null; private SoftReference<Image> thumbCache = null;
private int iconSize = ImageUtils.ICON_SIZE_MEDIUM; private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
private final static Executor executor = Executors.newFixedThreadPool(1); private ThumbnailLoadTask thumbTask;
private SwingWorker<Image, Object> swingWorker;
private Timer timer; private Timer timer;
private final ThumbnailLoader thumbLoader;
/** /**
* the constructor * the constructor
*/ */
ThumbnailViewNode(Node arg, int iconSize) { ThumbnailViewNode(Node arg, ThumbnailLoader thumbLoader) {
super(arg, Children.LEAF); super(arg, Children.LEAF);
this.iconSize = iconSize; this.thumbLoader = thumbLoader;
} }
@Override @Override
@ -70,50 +70,48 @@ private final static Executor executor = Executors.newFixedThreadPool(1);
@Override @Override
@NbBundle.Messages({"# {0} - file name", @NbBundle.Messages({"# {0} - file name",
"ThumbnailViewNode.progressHandle.text=Generating thumbnail for {0}"}) "ThumbnailViewNode.progressHandle.text=Generating thumbnail for {0}"})
public Image getIcon(int type) { synchronized public Image getIcon(int type) {
Image icon = null; Image thumbnail = null;
if (iconCache != null) { if (thumbCache != null) {
icon = iconCache.get(); thumbnail = thumbCache.get();
} }
if (icon != null) { if (thumbnail != null) {
return icon; return thumbnail;
} else { } else {
final Content content = this.getLookup().lookup(Content.class); final Content content = this.getLookup().lookup(Content.class);
if (content == null) { if (content == null) {
return ImageUtils.getDefaultThumbnail(); return ImageUtils.getDefaultThumbnail();
} }
if (swingWorker == null || swingWorker.isDone()) { if (thumbTask == null || thumbTask.isDone()) {
swingWorker = new ThumbnailLoadingWorker(content); thumbTask = new ThumbnailLoadTask(content);
executor.execute(swingWorker); thumbLoader.load(thumbTask);
// swingWorker.execute();
} }
if (timer == null) { if (timer == null) {
timer = new Timer(100, (ActionEvent e) -> { timer = new Timer(1, actionEvent -> fireIconChange());
fireIconChange();
});
timer.start(); timer.start();
} }
return waitingIcon; return waitingIcon;
} }
} }
public void setIconSize(int iconSize) { synchronized public void setIconSize(int iconSize) {
this.iconSize = iconSize; this.iconSize = iconSize;
iconCache = null; thumbCache = null;
swingWorker = null; thumbTask = null;
} }
private class ThumbnailLoadingWorker extends SwingWorker<Image, Object> { class ThumbnailLoadTask extends SwingWorker<Image, Object> {
private final Content content; private final Content content;
private final ProgressHandle progressHandle; private final ProgressHandle progressHandle;
ThumbnailLoadingWorker(Content content) { ThumbnailLoadTask(Content content) {
this.content = content; this.content = content;
final String progressText = Bundle.ThumbnailViewNode_progressHandle_text(content.getName()); final String progressText = Bundle.ThumbnailViewNode_progressHandle_text(content.getName());
progressHandle = ProgressHandle.createHandle(progressText, this::cancel); progressHandle = ProgressHandle.createHandle(progressText);
} }
private boolean cancel() { private boolean cancel() {
@ -130,12 +128,12 @@ private final static Executor executor = Executors.newFixedThreadPool(1);
protected void done() { protected void done() {
super.done(); super.done();
try { try {
iconCache = new SoftReference<>(super.get()); thumbCache = new SoftReference<>(super.get());
fireIconChange(); fireIconChange();
} catch (CancellationException ex) { } catch (CancellationException ex) {
//do nothing, it was cancelled //do nothing, it was cancelled
} catch (InterruptedException | ExecutionException ex) { } catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(ThumbnailViewNode.class.getName()).log(Level.SEVERE, "Error getting thumbnail icon for " + content.getName(), ex); //NON-NLS logger.log(Level.SEVERE, "Error getting thumbnail icon for " + content.getName(), ex); //NON-NLS
} finally { } finally {
progressHandle.finish(); progressHandle.finish();
if (timer != null) { if (timer != null) {
@ -143,7 +141,7 @@ private final static Executor executor = Executors.newFixedThreadPool(1);
timer = null; timer = null;
} }
swingWorker = null; thumbTask = null;
} }
} }
} }

View File

@ -1007,5 +1007,4 @@ public class ImageUtils {
return getCachedThumbnailFile(content, iconSize); return getCachedThumbnailFile(content, iconSize);
} }
} }