Autopsy  4.10.0
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-2018 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.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.concurrent.ExecutionException;
29 import java.util.logging.Level;
30 import javafx.application.Platform;
31 import javafx.scene.control.Alert;
32 import javax.swing.AbstractAction;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.SwingWorker;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.util.actions.Presenter;
43 import org.sleuthkit.datamodel.BlackboardArtifact;
44 import org.sleuthkit.datamodel.BlackboardArtifactTag;
45 import org.sleuthkit.datamodel.TagName;
46 import org.sleuthkit.datamodel.TskCoreException;
47 import org.sleuthkit.datamodel.TskData;
48 
53 @NbBundle.Messages({
54  "DeleteFileBlackboardArtifactTagAction.deleteTag=Remove Result Tag"
55 })
56 public class DeleteFileBlackboardArtifactTagAction extends AbstractAction implements Presenter.Popup {
57 
58  private static final Logger logger = Logger.getLogger(DeleteFileBlackboardArtifactTagAction.class.getName());
59 
60  private static final long serialVersionUID = 1L;
61  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileBlackboardArtifactTagAction.class,
62  "DeleteFileBlackboardArtifactTagAction.deleteTag");
63 
64  // This class is a singleton to support multi-selection of nodes, since
65  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
66  // node in the array returns a reference to the same action object from Node.getActions(boolean).
68 
69  public static synchronized DeleteFileBlackboardArtifactTagAction getInstance() {
70  if (null == instance) {
72  }
73  return instance;
74  }
75 
77  super(MENU_TEXT);
78  }
79 
80  @Override
81  public JMenuItem getPopupPresenter() {
82  return new TagMenu();
83  }
84 
85  @Override
86  public void actionPerformed(ActionEvent event) {
87  }
88 
89  protected String getActionDisplayName() {
90  return MENU_TEXT;
91  }
92 
93  @NbBundle.Messages({"# {0} - artifactID",
94  "DeleteFileBlackboardArtifactTagAction.deleteTag.alert=Unable to untag artifact {0}."})
95  protected void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId) {
96  new SwingWorker<Void, Void>() {
97 
98  @Override
99  protected Void doInBackground() throws Exception {
100  TagsManager tagsManager;
101  try {
103  } catch (NoCurrentCaseException ex) {
104  logger.log(Level.SEVERE, "Error untagging artifact. No open case found.", ex); //NON-NLS
105  Platform.runLater(()
106  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
107  );
108  return null;
109  }
110 
111  try {
112  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), artifactTag.getContent().getName()}); //NON-NLS
113  tagsManager.deleteBlackboardArtifactTag(artifactTag);
114  } catch (TskCoreException tskCoreException) {
115  logger.log(Level.SEVERE, "Error untagging artifact", tskCoreException); //NON-NLS
116  Platform.runLater(()
117  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
118  );
119  }
120  return null;
121  }
122 
123  @Override
124  protected void done() {
125  super.done();
126  try {
127  get();
128  } catch (InterruptedException | ExecutionException ex) {
129  logger.log(Level.SEVERE, "Unexpected exception while untagging artifact", ex); //NON-NLS
130  }
131  }
132  }.execute();
133  }
134 
140  @NbBundle.Messages({"# {0} - artifactID",
141  "DeleteFileBlackboardArtifactTagAction.deleteTags.alert=Unable to untag artifact {0}."})
142  private final class TagMenu extends JMenu {
143 
144  private static final long serialVersionUID = 1L;
145 
146  TagMenu() {
147  super(getActionDisplayName());
148 
149  final Collection<BlackboardArtifact> selectedBlackboardArtifactsList
150  = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class));
151 
152  if (!selectedBlackboardArtifactsList.isEmpty()) {
153  BlackboardArtifact artifact
154  = selectedBlackboardArtifactsList.iterator().next();
155 
156  Map<String, TagName> tagNamesMap = null;
157  List<String> standardTagNames = TagsManager.getStandardTagNames();
158  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
159  try {
160  // Get the current set of tag names.
162 
163  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
164  } catch (TskCoreException | NoCurrentCaseException ex) {
165  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
166  }
167 
168  // Each tag name in the current set of tags gets its own menu item in
169  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
170  // a tag with the associated tag name.
171  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
172  try {
173  List<BlackboardArtifactTag> existingTagsList
176 
177  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
178  String tagDisplayName = entry.getKey();
179 
180  TagName tagName = entry.getValue();
181  for (BlackboardArtifactTag artifactTag : existingTagsList) {
182  if (tagDisplayName.equals(artifactTag.getName().getDisplayName())) {
183  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
184  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
185  tagNameItem.addActionListener((ActionEvent e) -> {
186  deleteTag(tagName, artifactTag, artifact.getArtifactID());
187  });
188  // Show custom tags before predefined tags in the menu
189  if (standardTagNames.contains(tagDisplayName)) {
190  standardTagMenuitems.add(tagNameItem);
191  } else {
192  add(tagNameItem);
193  }
194  }
195  }
196  }
197  } catch (TskCoreException | NoCurrentCaseException ex) {
198  Logger.getLogger(TagMenu.class.getName())
199  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
200  }
201  }
202 
203  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty() ){
204  addSeparator();
205  }
206  standardTagMenuitems.forEach((menuItem) -> {
207  add(menuItem);
208  });
209  if (getItemCount() == 0) {
210  setEnabled(false);
211  }
212  }
213  }
214  }
215 
216 }
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:124
List< BlackboardArtifactTag > getBlackboardArtifactTagsByArtifact(BlackboardArtifact artifact)

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.