Autopsy  4.6.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.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;
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 import org.sleuthkit.datamodel.TskData;
47 
52 @NbBundle.Messages({
53  "DeleteFileBlackboardArtifactTagAction.deleteTag=Remove Result Tag"
54 })
55 public class DeleteFileBlackboardArtifactTagAction extends AbstractAction implements Presenter.Popup {
56 
57  private static final Logger logger = Logger.getLogger(DeleteFileBlackboardArtifactTagAction.class.getName());
58 
59  private static final long serialVersionUID = 1L;
60  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileBlackboardArtifactTagAction.class,
61  "DeleteFileBlackboardArtifactTagAction.deleteTag");
62 
63  // This class is a singleton to support multi-selection of nodes, since
64  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
65  // node in the array returns a reference to the same action object from Node.getActions(boolean).
67 
68  public static synchronized DeleteFileBlackboardArtifactTagAction getInstance() {
69  if (null == instance) {
71  }
72  return instance;
73  }
74 
76  super(MENU_TEXT);
77  }
78 
79  @Override
80  public JMenuItem getPopupPresenter() {
81  return new TagMenu();
82  }
83 
84  @Override
85  public void actionPerformed(ActionEvent event) {
86  }
87 
88  protected String getActionDisplayName() {
89  return MENU_TEXT;
90  }
91 
92  @NbBundle.Messages({"# {0} - artifactID",
93  "DeleteFileBlackboardArtifactTagAction.deleteTag.alert=Unable to untag artifact {0}."})
94  protected void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId) {
95  new SwingWorker<Void, Void>() {
96 
97  @Override
98  protected Void doInBackground() throws Exception {
99  TagsManager tagsManager;
100  try {
101  tagsManager = Case.getOpenCase().getServices().getTagsManager();
102  } catch (NoCurrentCaseException ex) {
103  logger.log(Level.SEVERE, "Error untagging artifact. No open case found.", ex); //NON-NLS
104  Platform.runLater(()
105  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
106  );
107  return null;
108  }
109 
110  try {
111  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), artifactTag.getContent().getName()}); //NON-NLS
112  tagsManager.deleteBlackboardArtifactTag(artifactTag);
113  } catch (TskCoreException tskCoreException) {
114  logger.log(Level.SEVERE, "Error untagging artifact", tskCoreException); //NON-NLS
115  Platform.runLater(()
116  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
117  );
118  }
119  return null;
120  }
121 
122  @Override
123  protected void done() {
124  super.done();
125  try {
126  get();
127  } catch (InterruptedException | ExecutionException ex) {
128  logger.log(Level.SEVERE, "Unexpected exception while untagging artifact", ex); //NON-NLS
129  }
130  }
131  }.execute();
132  }
133 
139  @NbBundle.Messages({"# {0} - artifactID",
140  "DeleteFileBlackboardArtifactTagAction.deleteTags.alert=Unable to untag artifact {0}."})
141  private class TagMenu extends JMenu {
142 
143  private static final long serialVersionUID = 1L;
144 
145  TagMenu() {
146  super(getActionDisplayName());
147 
148  final Collection<BlackboardArtifact> selectedBlackboardArtifactsList
149  = new HashSet<>(Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class));
150 
151  if (!selectedBlackboardArtifactsList.isEmpty()) {
152  BlackboardArtifact artifact
153  = selectedBlackboardArtifactsList.iterator().next();
154 
155  Map<String, TagName> tagNamesMap = null;
156  try {
157  // Get the current set of tag names.
159 
160  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
161  } catch (TskCoreException | NoCurrentCaseException ex) {
162  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
163  }
164 
165  // Each tag name in the current set of tags gets its own menu item in
166  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
167  // a tag with the associated tag name.
168  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
169  try {
170  List<BlackboardArtifactTag> existingTagsList
173 
174  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
175  String tagDisplayName = entry.getKey();
176 
177  TagName tagName = entry.getValue();
178  for (BlackboardArtifactTag artifactTag : existingTagsList) {
179  if (tagDisplayName.equals(artifactTag.getName().getDisplayName())) {
180  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
181  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
182  tagNameItem.addActionListener((ActionEvent e) -> {
183  deleteTag(tagName, artifactTag, artifact.getArtifactID());
184  });
185  add(tagNameItem);
186  }
187  }
188  }
189  } catch (TskCoreException | NoCurrentCaseException ex) {
190  Logger.getLogger(TagMenu.class.getName())
191  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
192  }
193  }
194 
195  if (getItemCount() == 0) {
196  setEnabled(false);
197  }
198  }
199  }
200  }
201 
202 }
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-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.