Autopsy  4.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  EventQueue.invokeLater(new Runnable() {
129  @Override
130  public void run() {
131  DataResultTopComponent dataResultTC = dirTree.getDirectoryListing();
132  Node currentRootNodeOfDataResultTC = dataResultTC.getRootNode();
133  Node contentNode = content.accept(new RootContentChildren.CreateSleuthkitNodeVisitor());
134  new SelectionWorker(dataResultTC, contentNode.getName(), currentRootNodeOfDataResultTC).execute();
135  }
136  });
137  }
138  });
139  }
140 
146  private class SelectionWorker extends SwingWorker<Node[], Integer> {
147 
148  DataResultTopComponent dataResultTC;
149  String nameOfNodeToSelect;
150  Node originalRootNodeOfDataResultTC;
151 
152  SelectionWorker(DataResultTopComponent dataResult, String nameToSelect, Node originalRoot) {
153  this.dataResultTC = dataResult;
154  this.nameOfNodeToSelect = nameToSelect;
155  this.originalRootNodeOfDataResultTC = originalRoot;
156  }
157 
158  @Override
159  protected Node[] doInBackground() throws Exception {
160  // Calls to Children::getNodes(true) block until all child Nodes have
161  // been created, regardless of whether they are created lazily.
162  // This means that this call will return the actual child Nodes
163  // and will *NEVER* return a proxy wait Node. This is done on the
164  // background thread to ensure we are not hanging the ui as it could
165  // be a lengthy operation.
166  return originalRootNodeOfDataResultTC.getChildren().getNodes(true);
167  }
168 
169  @Override
170  protected void done() {
171  Node[] nodesDisplayedInDataResultViewer;
172  try {
173  nodesDisplayedInDataResultViewer = get();
174  } catch (InterruptedException | ExecutionException ex) {
175  logger.log(Level.WARNING, "Failed to get nodes in selection worker.", ex); //NON-NLS
176  return;
177  } // catch and ignore if we were cancelled
178  catch (java.util.concurrent.CancellationException ex) {
179  return;
180  }
181 
182  // It is possible the user selected a different Node to be displayed
183  // in the DataResultViewer while the child Nodes were being generated.
184  // In that case, we don't want to set the selection because it the
185  // nodes returned from get() won't be in the DataResultTopComponent's
186  // ExplorerManager. If we did call setSelectedNodes, it would clear
187  // the current selection, which is not good.
188  if (dataResultTC.getRootNode().equals(originalRootNodeOfDataResultTC) == false) {
189  return;
190  }
191 
192  // Find the correct node to select from the nodes that are displayed
193  // in the data result viewer and set it as the selection of the
194  // DataResultTopComponent.
195  for (Node node : nodesDisplayedInDataResultViewer) {
196  if (nameOfNodeToSelect.equals(node.getName())) {
197  dataResultTC.requestActive();
198  dataResultTC.setSelectedNodes(new Node[]{node});
199  DirectoryTreeTopComponent.getDefault().fireViewerComplete();
200  break;
201  }
202  }
203  }
204 
205  }
206 
216  private class ReverseHierarchyVisitor extends ContentVisitor.Default<List<Content>> {
217 
218  List<Content> ret = new ArrayList<Content>();
219 
220  private List<Content> visitParentButDontAddMe(Content content) {
221  Content parent = null;
222  try {
223  parent = content.getParent();
224  } catch (TskCoreException ex) {
225  logger.log(Level.WARNING, "Couldn't get parent of Content object: " + content); //NON-NLS
226  }
227  return parent == null ? ret : parent.accept(this);
228  }
229 
230  @Override
231  protected List<Content> defaultVisit(Content content) {
232  ret.add(content);
233  Content parent = null;
234  try {
235  parent = content.getParent();
236  } catch (TskCoreException ex) {
237  logger.log(Level.WARNING, "Couldn't get parent of Content object: " + content); //NON-NLS
238  }
239  return parent == null ? ret : parent.accept(this);
240  }
241 
242  @Override
243  public List<Content> visit(FileSystem fs) {
244  return visitParentButDontAddMe(fs);
245  }
246 
247  @Override
248  public List<Content> visit(VolumeSystem vs) {
249  return visitParentButDontAddMe(vs);
250  }
251  }
252 }
ViewContextAction(String title, BlackboardArtifactNode node)
ViewContextAction(String title, AbstractFsContentNode<?extends AbstractFile > node)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
public< T > T accept(ContentVisitor< T > v)

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.