Autopsy  4.1
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.openide.nodes.FilterNode;
32 import org.openide.nodes.Node;
33 import org.openide.util.NbBundle;
37 
42 class ThumbnailViewNode extends FilterNode {
43 
44  static private final Image waitingIcon = Toolkit.getDefaultToolkit().createImage(ThumbnailViewNode.class.getResource("/org/sleuthkit/autopsy/images/working_spinner.gif"));
45 
46  private SoftReference<Image> iconCache = null;
47  private int iconSize = ImageUtils.ICON_SIZE_MEDIUM;
48 
49  private SwingWorker<Image, Object> swingWorker;
50  private Timer timer;
51 
55  ThumbnailViewNode(Node arg, int iconSize) {
56  super(arg, Children.LEAF);
57  this.iconSize = iconSize;
58  }
59 
60  @Override
61  public String getDisplayName() {
62  return StringUtils.abbreviate(super.getDisplayName(), 18);
63  }
64 
65  @Override
66  @NbBundle.Messages({"# {0} - file name",
67  "ThumbnailViewNode.progressHandle.text=Generating thumbnail for {0}"})
68  public Image getIcon(int type) {
69  Image icon = null;
70 
71  if (iconCache != null) {
72  icon = iconCache.get();
73  }
74 
75  if (icon != null) {
76  return icon;
77  } else {
78  final Content content = this.getLookup().lookup(Content.class);
79  if (content == null) {
80  return ImageUtils.getDefaultThumbnail();
81  }
82  if (swingWorker == null || swingWorker.isDone()) {
83  swingWorker = new SwingWorker<Image, Object>() {
84  final private ProgressHandle progressHandle = ProgressHandle.createHandle(Bundle.ThumbnailViewNode_progressHandle_text(content.getName()));
85 
86  @Override
87  protected Image doInBackground() throws Exception {
88  progressHandle.start();
89  return ImageUtils.getThumbnail(content, iconSize);
90  }
91 
92  @Override
93  protected void done() {
94  super.done();
95  try {
96  iconCache = new SoftReference<>(super.get());
97  fireIconChange();
98  } catch (InterruptedException | ExecutionException ex) {
99  Logger.getLogger(ThumbnailViewNode.class.getName()).log(Level.SEVERE, "Error getting thumbnail icon for " + content.getName(), ex); //NON-NLS
100  } finally {
101  progressHandle.finish();
102  if (timer != null) {
103  timer.stop();
104  timer = null;
105 
106  }
107  swingWorker = null;
108  }
109  }
110  };
111  swingWorker.execute();
112  }
113  if (timer == null) {
114  timer = new Timer(100, (ActionEvent e) -> {
115  fireIconChange();
116  });
117  timer.start();
118  }
119  return waitingIcon;
120  }
121  }
122 
123  public void setIconSize(int iconSize) {
124  this.iconSize = iconSize;
125  iconCache = null;
126  swingWorker = null;
127  }
128 }

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