19package org.sleuthkit.autopsy.corecomponents;
21import com.google.common.collect.Lists;
22import com.google.common.util.concurrent.ThreadFactoryBuilder;
24import java.awt.Toolkit;
25import java.awt.image.BufferedImage;
26import java.io.IOException;
27import java.lang.ref.SoftReference;
28import java.lang.reflect.InvocationTargetException;
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.Comparator;
33import java.util.concurrent.Callable;
34import java.util.concurrent.CancellationException;
35import java.util.concurrent.ExecutionException;
36import java.util.concurrent.ExecutorService;
37import java.util.concurrent.Executors;
38import java.util.concurrent.FutureTask;
39import java.util.logging.Level;
40import java.util.stream.Collectors;
41import java.util.stream.IntStream;
42import java.util.stream.Stream;
43import javax.imageio.ImageIO;
44import javax.swing.SwingUtilities;
45import javax.swing.Timer;
46import org.apache.commons.lang3.StringUtils;
47import org.netbeans.api.progress.ProgressHandle;
48import org.netbeans.api.progress.ProgressHandleFactory;
49import org.openide.nodes.AbstractNode;
50import org.openide.nodes.Children;
51import org.openide.nodes.FilterNode;
52import org.openide.nodes.Node;
53import org.openide.util.NbBundle;
54import org.openide.util.lookup.Lookups;
55import org.sleuthkit.autopsy.corecomponents.ResultViewerPersistence.SortCriterion;
56import static org.sleuthkit.autopsy.corecomponents.ResultViewerPersistence.loadSortCriteria;
57import org.sleuthkit.autopsy.coreutils.ImageUtils;
58import org.sleuthkit.autopsy.coreutils.Logger;
59import org.sleuthkit.datamodel.AbstractFile;
60import org.sleuthkit.datamodel.Content;
71class ThumbnailViewChildren
extends Children.Keys<Integer> {
73 private static final Logger logger = Logger.getLogger(ThumbnailViewChildren.class.getName());
75 @NbBundle.Messages(
"ThumbnailViewChildren.progress.cancelling=(Cancelling)")
76 private static final String CANCELLING_POSTIX = Bundle.ThumbnailViewChildren_progress_cancelling();
78 private static Image waitingIcon;
80 static final int IMAGES_PER_PAGE = 200;
82 private final ExecutorService executor = Executors.newFixedThreadPool(3,
83 new ThreadFactoryBuilder().setNameFormat(
"Thumbnail-Loader-%d").build());
86 private final Node parent;
87 private final List<List<Node>> pages =
new ArrayList<>();
88 private int thumbSize;
94 private static Image getWaitingIcon() {
95 if (waitingIcon ==
null) {
96 String imgPath =
"/org/sleuthkit/autopsy/images/working_spinner.gif";
99 }
catch (IOException ex) {
100 logger.log(Level.WARNING,
"There was an error loading image: " + imgPath, ex);
113 ThumbnailViewChildren(Node parent,
int thumbSize) {
116 this.parent = parent;
117 this.thumbSize = thumbSize;
121 protected void addNotify() {
130 final List<Node> suppContent
131 = Stream.of(parent.getChildren().getNodes())
132 .filter(ThumbnailViewChildren::isSupported)
133 .sorted(getComparator())
134 .collect(Collectors.toList());
136 if (suppContent.isEmpty()) {
142 pages.addAll(Lists.partition(suppContent, IMAGES_PER_PAGE));
145 setKeys(IntStream.range(0, pages.size()).boxed().collect(Collectors.toList()));
155 private synchronized Comparator<Node> getComparator() {
156 Comparator<Node> comp = (node1, node2) -> 0;
158 if (!(parent instanceof TableFilterNode)) {
161 List<SortCriterion> sortCriteria = loadSortCriteria((TableFilterNode) parent);
170 return sortCriteria.stream()
171 .map(this::getCriterionComparator)
172 .collect(Collectors.reducing(Comparator::thenComparing))
186 private Comparator<Node> getCriterionComparator(SortCriterion criterion) {
187 @SuppressWarnings(
"unchecked")
188 Comparator<Node> c = Comparator.comparing(node -> getPropertyValue(node, criterion.getProperty()),
189 Comparator.nullsFirst(Comparator.naturalOrder()));
190 switch (criterion.getSortOrder()) {
208 @SuppressWarnings(
"rawtypes")
209 private Comparable getPropertyValue(Node node, Node.Property<?> prop) {
210 for (Node.PropertySet ps : node.getPropertySets()) {
211 for (Node.Property<?> p : ps.getProperties()) {
212 if (p.equals(prop)) {
214 if (p.getValue() instanceof Comparable) {
215 return (Comparable) p.getValue();
218 return p.getValue().toString();
221 } catch (IllegalAccessException | InvocationTargetException ex) {
222 logger.log(Level.WARNING,
"Error getting value for thumbnail children", ex);
231 protected void removeNotify() {
232 super.removeNotify();
237 protected Node[] createNodes(Integer pageNum) {
238 return new Node[]{
new ThumbnailPageNode(pageNum, pages.get(pageNum))};
242 private static boolean isSupported(Node node) {
244 Content content = node.getLookup().lookup(AbstractFile.class);
245 if (content !=
null) {
252 public void setThumbsSize(
int thumbSize) {
253 this.thumbSize = thumbSize;
254 for (Node page : getNodes()) {
255 for (Node node : page.getChildren().getNodes()) {
256 ((ThumbnailViewNode) node).setThumbSize(thumbSize);
261 synchronized void cancelLoadingThumbnails() {
262 tasks.forEach(task -> task.cancel(Boolean.TRUE));
263 executor.shutdownNow();
267 private synchronized ThumbnailViewNode.ThumbnailLoadTask loadThumbnail(ThumbnailViewNode node) {
268 if (executor.isShutdown() ==
false) {
269 ThumbnailViewNode.ThumbnailLoadTask task = node.new ThumbnailLoadTask();
271 executor.submit(task);
300 super(wrappedNode, FilterNode.Children.LEAF);
302 this.content = this.getLookup().lookup(AbstractFile.class);
307 return StringUtils.abbreviate(super.getDisplayName(), 18);
311 @NbBundle.Messages({
"# {0} - file name",
312 "ThumbnailViewNode.progressHandle.text=Generating thumbnail for {0}"})
320 if (thumbnail !=
null) {
334 return getWaitingIcon();
337 synchronized void setThumbSize(
int iconSize) {
338 this.thumbSize = iconSize;
340 if (thumbTask !=
null) {
347 private class ThumbnailLoadTask
extends FutureTask<Image> {
353 ThumbnailLoadTask() {
354 super(
new Callable<Image>() {
355 public Image call() {
368 synchronized public boolean cancel(
boolean mayInterrupt) {
371 return super.cancel(mayInterrupt);
380 synchronized protected void done() {
382 SwingUtilities.invokeLater(() -> {
394 }
catch (CancellationException ex) {
396 }
catch (InterruptedException | ExecutionException ex) {
397 if (
false == (ex.getCause() instanceof CancellationException)) {
398 logger.log(Level.SEVERE,
"Error getting thumbnail icon for " +
content.getName(), ex);
415 setName(Integer.toString(pageNum + 1));
416 int from = 1 + (pageNum * IMAGES_PER_PAGE);
418 setDisplayName(from +
"-" + to);
420 this.setIconBaseWithExtension(
"org/sleuthkit/autopsy/images/Folder-icon.png");
429 private class ThumbnailPageNodeChildren
extends Children.Keys<Node> {
436 ThumbnailPageNodeChildren(List<Node>
keyNodes) {
449 super.removeNotify();
450 setKeys(Collections.emptyList());
453 int getChildCount() {
454 return keyNodes.size();
459 if (wrapped !=
null) {
461 return new Node[]{thumb};
Node[] createNodes(Node wrapped)
ThumbnailPageNode(Integer pageNum, List< Node > childNodes)
final ProgressHandle progressHandle
synchronized boolean isCancelled()
synchronized boolean cancel(boolean mayInterrupt)
final String progressText
ThumbnailLoadTask thumbTask
SoftReference< Image > thumbCache
synchronized Image getIcon(int type)
ThumbnailViewNode(Node wrappedNode, int thumbSize)
static Image getDefaultThumbnail()
static boolean thumbnailSupported(Content content)
static BufferedImage getThumbnail(Content content, int iconSize)
synchronized static Logger getLogger(String name)