Autopsy  4.4
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-2017 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.logging.Level;
29 import javax.swing.AbstractAction;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.explorer.ExplorerManager;
32 import org.openide.explorer.view.TreeView;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.Node;
35 import org.openide.util.NbBundle.Messages;
44 import org.sleuthkit.datamodel.AbstractFile;
45 import org.sleuthkit.datamodel.Content;
46 import org.sleuthkit.datamodel.ContentVisitor;
47 import org.sleuthkit.datamodel.FileSystem;
48 import org.sleuthkit.datamodel.TskCoreException;
49 import org.sleuthkit.datamodel.TskData;
50 import org.sleuthkit.datamodel.VolumeSystem;
51 
58 public final class ViewContextAction extends AbstractAction {
59 
60  private static final long serialVersionUID = 1L;
61  private static final Logger logger = Logger.getLogger(ViewContextAction.class.getName());
62  private final Content content;
63 
73  public ViewContextAction(String displayName, BlackboardArtifactNode artifactNode) {
74  super(displayName);
75  this.content = artifactNode.getLookup().lookup(Content.class);
76  if (content instanceof AbstractFile) {
77  AbstractFile file = (AbstractFile) content;
78  if ((TskData.FileKnown.KNOWN == file.getKnown() && UserPreferences.hideKnownFilesInDataSourcesTree())
79  || (TskData.TSK_DB_FILES_TYPE_ENUM.SLACK == file.getType() && UserPreferences.hideSlackFilesInDataSourcesTree())) {
80  this.setEnabled(false);
81  }
82  }
83  }
84 
95  public ViewContextAction(String displayName, AbstractFsContentNode<? extends AbstractFile> fileSystemContentNode) {
96  super(displayName);
97  this.content = fileSystemContentNode.getLookup().lookup(Content.class);
98  }
99 
109  public ViewContextAction(String displayName, Content content) {
110  super(displayName);
111  this.content = content;
112  }
113 
122  @Override
123  @Messages({
124  "ViewContextAction.errorMessage.cannotFindDirectory=Failed to locate directory.",
125  "ViewContextAction.errorMessage.cannotSelectDirectory=Failed to select directory in tree.",
126  })
127  public void actionPerformed(ActionEvent event) {
128  EventQueue.invokeLater(() -> {
129  /*
130  * Get the "Data Sources" node from the tree view.
131  */
133  ExplorerManager treeViewExplorerMgr = treeViewTopComponent.getExplorerManager();
134  Node parentTreeViewNode = treeViewExplorerMgr.getRootContext().getChildren().findChild(DataSourcesNode.NAME);
135 
136  /*
137  * Get the parent content for the content to be selected in the
138  * results view. If the parent content is null, then the specified
139  * content is a data source, and the parent tree view node is the
140  * "Data Sources" node. Otherwise, the tree view needs to be
141  * searched to find the parent treeview node.
142  */
143  Content parentContent = null;
144  try {
145  parentContent = content.getParent();
146  } catch (TskCoreException ex) {
147  MessageNotifyUtil.Message.error(Bundle.ViewContextAction_errorMessage_cannotFindDirectory());
148  logger.log(Level.SEVERE, String.format("Could not get parent of Content object: %s", content), ex); //NON-NLS
149  return;
150  }
151  if (null != parentContent) {
152  /*
153  * Get an ordered list of the ancestors of the specified
154  * content, starting with its data source.
155  *
156  */
157  AncestorVisitor ancestorVisitor = new AncestorVisitor();
158  List<Content> contentBranch = parentContent.accept(ancestorVisitor);
159  Collections.reverse(contentBranch);
160 
173  Node dummyRootNode = new DirectoryTreeFilterNode(new AbstractNode(new RootContentChildren(contentBranch)), true);
174  Children ancestorChildren = dummyRootNode.getChildren();
175 
176  /*
177  * Search the tree for the parent node. Note that this algorithm
178  * simply discards "extra" ancestor nodes not shown in the tree,
179  * such as the root directory of the file system for file system
180  * content.
181  */
182  Children treeNodeChildren = parentTreeViewNode.getChildren();
183  for (int i = 0; i < ancestorChildren.getNodesCount(); i++) {
184  Node ancestorNode = ancestorChildren.getNodeAt(i);
185  for (int j = 0; j < treeNodeChildren.getNodesCount(); j++) {
186  Node treeNode = treeNodeChildren.getNodeAt(j);
187  if (ancestorNode.getDisplayName().equals(treeNode.getDisplayName())) {
188  parentTreeViewNode = treeNode;
189  treeNodeChildren = treeNode.getChildren();
190  break;
191  }
192  }
193  }
194  }
195 
196  /*
197  * Set the child selection info of the parent tree node, then select
198  * the parent node in the tree view. The results view will retrieve
199  * this selection info and use it to complete this action when the
200  * tree view top component responds to the selection of the parent
201  * node by pushing it into the results view top component.
202  */
203  DisplayableItemNode undecoratedParentNode = (DisplayableItemNode) ((DirectoryTreeFilterNode) parentTreeViewNode).getOriginal();
204  undecoratedParentNode.setChildNodeSelectionInfo(new ContentNodeSelectionInfo(content));
205  TreeView treeView = treeViewTopComponent.getTree();
206  treeView.expandNode(parentTreeViewNode);
207  try {
208  treeViewExplorerMgr.setExploredContextAndSelection(parentTreeViewNode, new Node[]{parentTreeViewNode});
209  } catch (PropertyVetoException ex) {
210  MessageNotifyUtil.Message.error(Bundle.ViewContextAction_errorMessage_cannotSelectDirectory());
211  logger.log(Level.SEVERE, "Failed to select the parent node in the tree view", ex); //NON-NLS
212  }
213  });
214  }
215 
221  private static class AncestorVisitor extends ContentVisitor.Default<List<Content>> {
222 
223  List<Content> lineage = new ArrayList<>();
224 
225  @Override
226  protected List<Content> defaultVisit(Content content) {
227  lineage.add(content);
228  Content parent = null;
229  try {
230  parent = content.getParent();
231  } catch (TskCoreException ex) {
232  logger.log(Level.SEVERE, String.format("Could not get parent of Content object: %s", content), ex); //NON-NLS
233  }
234  return parent == null ? lineage : parent.accept(this);
235  }
236 
237  @Override
238  public List<Content> visit(VolumeSystem volumeSystem) {
239  /*
240  * Volume systems are not shown in the tree view. This is not
241  * strictly necesssary given the algorithm above, but it is a simple
242  * optimization.
243  */
244  return skipToParent(volumeSystem);
245  }
246 
247  @Override
248  public List<Content> visit(FileSystem fileSystem) {
249  /*
250  * File systems are not shown in the tree view. This is not strictly
251  * necesssary given the algorithm above, but it is a simple
252  * optimization.
253  */
254  return skipToParent(fileSystem);
255  }
256 
257  private List<Content> skipToParent(Content content) {
258  Content parent = null;
259  try {
260  parent = content.getParent();
261  } catch (TskCoreException ex) {
262  logger.log(Level.SEVERE, String.format("Could not get parent of Content object: %s", content), ex); //NON-NLS
263  }
264  return parent == null ? lineage : parent.accept(this);
265  }
266  }
267 
268 }
ViewContextAction(String displayName, Content content)
ViewContextAction(String displayName, BlackboardArtifactNode artifactNode)
ViewContextAction(String displayName, AbstractFsContentNode<?extends AbstractFile > fileSystemContentNode)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
void setChildNodeSelectionInfo(NodeSelectionInfo selectedChildNodeInfo)
static synchronized DirectoryTreeTopComponent findInstance()

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