Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DeleteFileBlackboardArtifactTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.actions;
20 
21 import java.awt.event.ActionEvent;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.concurrent.ExecutionException;
28 import java.util.logging.Level;
29 import javafx.application.Platform;
30 import javafx.scene.control.Alert;
31 import javax.swing.AbstractAction;
32 import javax.swing.JMenu;
33 import javax.swing.JMenuItem;
34 import javax.swing.SwingWorker;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Utilities;
37 import org.openide.util.actions.Presenter;
41 import org.sleuthkit.datamodel.AbstractFile;
42 import org.sleuthkit.datamodel.BlackboardArtifact;
43 import org.sleuthkit.datamodel.BlackboardArtifactTag;
44 import org.sleuthkit.datamodel.TagName;
45 import org.sleuthkit.datamodel.TskCoreException;
46 
51 @NbBundle.Messages({
52  "DeleteFileBlackboardArtifactTagAction.deleteTag=Remove Result Tag"
53 })
54 public class DeleteFileBlackboardArtifactTagAction extends AbstractAction implements Presenter.Popup {
55 
56  private static final Logger LOGGER = Logger.getLogger(DeleteFileBlackboardArtifactTagAction.class.getName());
57 
58  private static final long serialVersionUID = 1L;
59  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileBlackboardArtifactTagAction.class,
60  "DeleteFileBlackboardArtifactTagAction.deleteTag");
61 
62  // This class is a singleton to support multi-selection of nodes, since
63  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
64  // node in the array returns a reference to the same action object from Node.getActions(boolean).
66 
67  public static synchronized DeleteFileBlackboardArtifactTagAction getInstance() {
68  if (null == instance) {
70  }
71  return instance;
72  }
73 
75  super(MENU_TEXT);
76  }
77 
78  @Override
79  public JMenuItem getPopupPresenter() {
80  return new TagMenu();
81  }
82 
83  @Override
84  public void actionPerformed(ActionEvent event) {
85  }
86 
87  protected String getActionDisplayName() {
88  return MENU_TEXT;
89  }
90 
91  @NbBundle.Messages({"# {0} - artifactID",
92  "DeleteFileBlackboardArtifactTagAction.deleteTag.alert=Unable to untag artifact {0}."})
93  protected void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId) {
94  new SwingWorker<Void, Void>() {
95 
96  @Override
97  protected Void doInBackground() throws Exception {
99 
100  // Pull the from the global context to avoid unnecessary calls
101  // to the database.
102  final Collection<AbstractFile> selectedFilesList =
103  new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
104  AbstractFile file = selectedFilesList.iterator().next();
105 
106  try {
107  LOGGER.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), file.getName()}); //NON-NLS
108  tagsManager.deleteBlackboardArtifactTag(artifactTag);
109  } catch (TskCoreException tskCoreException) {
110  LOGGER.log(Level.SEVERE, "Error untagging artifact", tskCoreException); //NON-NLS
111  Platform.runLater(() ->
112  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
113  );
114  }
115  return null;
116  }
117 
118  @Override
119  protected void done() {
120  super.done();
121  try {
122  get();
123  } catch (InterruptedException | ExecutionException ex) {
124  LOGGER.log(Level.SEVERE, "Unexpected exception while untagging artifact", ex); //NON-NLS
125  }
126  }
127  }.execute();
128  }
129 
135  @NbBundle.Messages({"# {0} - artifactID",
136  "DeleteFileBlackboardArtifactTagAction.deleteTags.alert=Unable to untag artifact {0}."})
137  private class TagMenu extends JMenu {
138 
139  private static final long serialVersionUID = 1L;
140 
141  TagMenu() {
142  super(getActionDisplayName());
143 
144  final Collection<BlackboardArtifact> selectedBlackboardArtifactsList =
145  new HashSet<>(Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class));
146 
147  if(!selectedBlackboardArtifactsList.isEmpty()) {
148  BlackboardArtifact artifact =
149  selectedBlackboardArtifactsList.iterator().next();
150 
151  // Get the current set of tag names.
153 
154  Map<String, TagName> tagNamesMap = null;
155  try {
156  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
157  } catch (TskCoreException ex) {
158  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
159  }
160 
161  // Each tag name in the current set of tags gets its own menu item in
162  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
163  // a tag with the associated tag name.
164  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
165  try {
166  List<BlackboardArtifactTag> existingTagsList =
169 
170  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
171  String tagDisplayName = entry.getKey();
172 
173  TagName tagName = entry.getValue();
174  for(BlackboardArtifactTag artifactTag : existingTagsList) {
175  if(tagDisplayName.equals(artifactTag.getName().getDisplayName())) {
176  JMenuItem tagNameItem = new JMenuItem(tagDisplayName);
177  tagNameItem.addActionListener((ActionEvent e) -> {
178  deleteTag(tagName, artifactTag, artifact.getArtifactID());
179  });
180  add(tagNameItem);
181  }
182  }
183  }
184  } catch (TskCoreException ex) {
185  Logger.getLogger(TagMenu.class.getName())
186  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
187  }
188  }
189 
190  if(getItemCount() == 0) {
191  setEnabled(false);
192  }
193  }
194  }
195  }
196 
197 }
static synchronized DeleteFileBlackboardArtifactTagAction getInstance()
void deleteBlackboardArtifactTag(BlackboardArtifactTag tag)
void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)

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