Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ThumbnailViewNode.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-16 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.corecomponents;
20 
21 import java.awt.Image;
22 import java.awt.Toolkit;
23 import java.awt.event.ActionEvent;
24 import java.lang.ref.SoftReference;
25 import java.util.concurrent.ExecutionException;
26 import java.util.logging.Level;
27 import javax.swing.SwingWorker;
28 import javax.swing.Timer;
29 import org.apache.commons.lang3.StringUtils;
30 import org.netbeans.api.progress.ProgressHandle;
31 import org.netbeans.api.progress.ProgressHandleFactory;
32 import org.openide.nodes.FilterNode;
33 import org.openide.nodes.Node;
34 import org.openide.util.NbBundle;
37 import org.sleuthkit.datamodel.Content;
38 
43 class ThumbnailViewNode extends FilterNode {
44 
45  static private final Image waitingIcon = Toolkit.getDefaultToolkit().createImage(ThumbnailViewNode.class.getResource("/org/sleuthkit/autopsy/images/working_spinner.gif"));
46 
47  private SoftReference<Image> iconCache = null;
48  private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
49 
50  private SwingWorker<Image, Object> swingWorker;
51  private Timer timer;
52 
56  ThumbnailViewNode(Node arg, int iconSize) {
57  super(arg, Children.LEAF);
58  this.iconSize = iconSize;
59  }
60 
61  @Override
62  public String getDisplayName() {
63  return StringUtils.abbreviate(super.getDisplayName(), 18);
64  }
65 
66  @Override
67  @NbBundle.Messages({"# {0} - file name",
68  "ThumbnailViewNode.progressHandle.text=Generating thumbnail for {0}"})
69  public Image getIcon(int type) {
70  Image icon = null;
71 
72  if (iconCache != null) {
73  icon = iconCache.get();
74  }
75 
76  if (icon != null) {
77  return icon;
78  } else {
79  final Content content = this.getLookup().lookup(Content.class);
80  if (content == null) {
81  return ImageUtils.getDefaultThumbnail();
82  }
83  if (swingWorker == null || swingWorker.isDone()) {
84  swingWorker = new SwingWorker<Image, Object>() {
85  final private ProgressHandle progressHandle = ProgressHandleFactory.createHandle(Bundle.ThumbnailViewNode_progressHandle_text(content.getName()));
86 
87  @Override
88  protected Image doInBackground() throws Exception {
89  progressHandle.start();
90  return ImageUtils.getThumbnail(content, iconSize);
91  }
92 
93  @Override
94  protected void done() {
95  super.done();
96  try {
97  iconCache = new SoftReference<>(super.get());
98  fireIconChange();
99  } catch (InterruptedException | ExecutionException ex) {
100  Logger.getLogger(ThumbnailViewNode.class.getName()).log(Level.SEVERE, "Error getting thumbnail icon for " + content.getName(), ex); //NON-NLS
101  } finally {
102  progressHandle.finish();
103  if (timer != null) {
104  timer.stop();
105  timer = null;
106 
107  }
108  swingWorker = null;
109  }
110  }
111  };
112  swingWorker.execute();
113  }
114  if (timer == null) {
115  timer = new Timer(100, (ActionEvent e) -> {
116  fireIconChange();
117  });
118  timer.start();
119  }
120  return waitingIcon;
121  }
122  }
123 
124  public void setIconSize(int iconSize) {
125  this.iconSize = iconSize;
126  iconCache = null;
127  swingWorker = null;
128  }
129 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.