Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ViewContextAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.directorytree;
20 
21 import java.awt.EventQueue;
22 import java.awt.event.ActionEvent;
23 import java.beans.PropertyVetoException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.concurrent.ExecutionException;
28 import java.util.logging.Level;
30 import javax.swing.AbstractAction;
31 import javax.swing.SwingWorker;
32 import org.openide.nodes.AbstractNode;
33 import org.openide.explorer.ExplorerManager;
34 import org.openide.explorer.view.TreeView;
35 import org.openide.nodes.Children;
36 import org.openide.nodes.Node;
48 
59 public class ViewContextAction extends AbstractAction {
60 
61  private Content content;
62  private static final Logger logger = Logger.getLogger(ViewContextAction.class.getName());
63 
64  public ViewContextAction(String title, BlackboardArtifactNode node) {
65  super(title);
66  this.content = node.getLookup().lookup(Content.class);
67 
68  }
69 
71  super(title);
72  this.content = node.getLookup().lookup(Content.class);
73  }
74 
75  public ViewContextAction(String title, Content content) {
76  super(title);
77  this.content = content;
78  }
79 
80  @Override
81  public void actionPerformed(ActionEvent e) {
82  EventQueue.invokeLater(new Runnable() {
83  @Override
84  public void run() {
85  // create a list of Content objects starting with content's
86  // Image and ends with content
88  List<Content> hierarchy = content.accept(vtor);
89  Collections.reverse(hierarchy);
90 
91  Node generated = new DirectoryTreeFilterNode(new AbstractNode(new RootContentChildren(hierarchy)), true);
92  Children genChilds = generated.getChildren();
93 
95  TreeView dirTreeView = dirTree.getTree();
96  ExplorerManager dirTreeExplorerManager = dirTree.getExplorerManager();
97  Node dirTreeRootNode = dirTreeExplorerManager.getRootContext();
98  Children dirChilds = dirTreeRootNode.getChildren();
99  Children currentChildren = dirChilds.findChild(DataSourcesNode.NAME).getChildren();
100 
101  Node dirExplored = null;
102 
103  // Find the parent node of the content in the directory tree
104  for (int i = 0; i < genChilds.getNodesCount() - 1; i++) {
105  Node currentGeneratedNode = genChilds.getNodeAt(i);
106  for (int j = 0; j < currentChildren.getNodesCount(); j++) {
107  Node currentDirectoryTreeNode = currentChildren.getNodeAt(j);
108  if (currentGeneratedNode.getDisplayName().equals(currentDirectoryTreeNode.getDisplayName())) {
109  dirExplored = currentDirectoryTreeNode;
110  dirTreeView.expandNode(dirExplored);
111  currentChildren = currentDirectoryTreeNode.getChildren();
112  break;
113  }
114  }
115  }
116 
117  // Set the parent node of the content as the selection in the
118  // directory tree
119  try {
120  if (dirExplored != null) {
121  dirTreeView.expandNode(dirExplored);
122  dirTreeExplorerManager.setExploredContextAndSelection(dirExplored, new Node[]{dirExplored});
123  }
124  } catch (PropertyVetoException ex) {
125  logger.log(Level.WARNING, "Couldn't set selected node", ex); //NON-NLS
126  }
127 
128 
129  EventQueue.invokeLater(new Runnable() {
130  @Override
131  public void run() {
132  DataResultTopComponent dataResultTC = dirTree.getDirectoryListing();
133  Node currentRootNodeOfDataResultTC = dataResultTC.getRootNode();
134  Node contentNode = content.accept(new RootContentChildren.CreateSleuthkitNodeVisitor());
135  new SelectionWorker(dataResultTC, contentNode.getName(), currentRootNodeOfDataResultTC).execute();
136  }
137  });
138  }
139  });
140  }
141 
147  private class SelectionWorker extends SwingWorker<Node[], Integer> {
148 
149  DataResultTopComponent dataResultTC;
150  String nameOfNodeToSelect;
151  Node originalRootNodeOfDataResultTC;
152 
153  SelectionWorker(DataResultTopComponent dataResult, String nameToSelect, Node originalRoot) {
154  this.dataResultTC = dataResult;
155  this.nameOfNodeToSelect = nameToSelect;
156  this.originalRootNodeOfDataResultTC = originalRoot;
157  }
158 
159  @Override
160  protected Node[] doInBackground() throws Exception {
161  // Calls to Children::getNodes(true) block until all child Nodes have
162  // been created, regardless of whether they are created lazily.
163  // This means that this call will return the actual child Nodes
164  // and will *NEVER* return a proxy wait Node. This is done on the
165  // background thread to ensure we are not hanging the ui as it could
166  // be a lengthy operation.
167  return originalRootNodeOfDataResultTC.getChildren().getNodes(true);
168  }
169 
170  @Override
171  protected void done() {
172  Node[] nodesDisplayedInDataResultViewer;
173  try {
174  nodesDisplayedInDataResultViewer = get();
175  } catch (InterruptedException | ExecutionException ex) {
176  logger.log(Level.WARNING, "Failed to get nodes in selection worker.", ex); //NON-NLS
177  return;
178  }
179  // catch and ignore if we were cancelled
180  catch (java.util.concurrent.CancellationException ex ) {
181  return;
182  }
183 
184  // It is possible the user selected a different Node to be displayed
185  // in the DataResultViewer while the child Nodes were being generated.
186  // In that case, we don't want to set the selection because it the
187  // nodes returned from get() won't be in the DataResultTopComponent's
188  // ExplorerManager. If we did call setSelectedNodes, it would clear
189  // the current selection, which is not good.
190  if (dataResultTC.getRootNode().equals(originalRootNodeOfDataResultTC) == false) {
191  return;
192  }
193 
194  // Find the correct node to select from the nodes that are displayed
195  // in the data result viewer and set it as the selection of the
196  // DataResultTopComponent.
197  for (Node node : nodesDisplayedInDataResultViewer) {
198  if (nameOfNodeToSelect.equals(node.getName())) {
199  dataResultTC.requestActive();
200  dataResultTC.setSelectedNodes(new Node[]{node});
201  DirectoryTreeTopComponent.getDefault().fireViewerComplete();
202  break;
203  }
204  }
205  }
206 
207  }
208 
218  private class ReverseHierarchyVisitor extends ContentVisitor.Default<List<Content>> {
219 
220  List<Content> ret = new ArrayList<Content>();
221 
222  private List<Content> visitParentButDontAddMe(Content content) {
223  Content parent = null;
224  try {
225  parent = content.getParent();
226  } catch (TskCoreException ex) {
227  logger.log(Level.WARNING, "Couldn't get parent of Content object: " + content); //NON-NLS
228  }
229  return parent == null ? ret : parent.accept(this);
230  }
231 
232  @Override
233  protected List<Content> defaultVisit(Content content) {
234  ret.add(content);
235  Content parent = null;
236  try {
237  parent = content.getParent();
238  } catch (TskCoreException ex) {
239  logger.log(Level.WARNING, "Couldn't get parent of Content object: " + content); //NON-NLS
240  }
241  return parent == null ? ret : parent.accept(this);
242  }
243 
244  @Override
245  public List<Content> visit(FileSystem fs) {
246  return visitParentButDontAddMe(fs);
247  }
248 
249  @Override
250  public List<Content> visit(VolumeSystem vs) {
251  return visitParentButDontAddMe(vs);
252  }
253  }
254 }
ViewContextAction(String title, BlackboardArtifactNode node)
ViewContextAction(String title, AbstractFsContentNode<?extends AbstractFile > node)
public< T > T accept(ContentVisitor< T > v)
static Logger getLogger(String name)
Definition: Logger.java:131

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.