Autopsy  4.18.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-2019 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;
44 import org.sleuthkit.datamodel.BlackboardArtifact;
45 import org.sleuthkit.datamodel.BlackboardArtifactTag;
46 import org.sleuthkit.datamodel.TagName;
47 import org.sleuthkit.datamodel.TskCoreException;
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 
95  public JMenuItem getMenuForArtifacts(Collection<BlackboardArtifact> selectedArtifacts) {
96  return new TagMenu(selectedArtifacts);
97  }
98 
99  @Override
100  public void actionPerformed(ActionEvent event) {
101  }
102 
103  protected String getActionDisplayName() {
104  return MENU_TEXT;
105  }
106 
107  @NbBundle.Messages({"# {0} - artifactID",
108  "DeleteFileBlackboardArtifactTagAction.deleteTag.alert=Unable to untag artifact {0}."})
109  protected void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId) {
110  new SwingWorker<Void, Void>() {
111 
112  @Override
113  protected Void doInBackground() throws Exception {
114  TagsManager tagsManager;
115  try {
117  } catch (NoCurrentCaseException ex) {
118  logger.log(Level.SEVERE, "Error untagging artifact. No open case found.", ex); //NON-NLS
119  Platform.runLater(()
120  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
121  );
122  return null;
123  }
124 
125  try {
126  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), artifactTag.getContent().getName()}); //NON-NLS
127  tagsManager.deleteBlackboardArtifactTag(artifactTag);
128  } catch (TskCoreException tskCoreException) {
129  logger.log(Level.SEVERE, "Error untagging artifact", tskCoreException); //NON-NLS
130  Platform.runLater(()
131  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
132  );
133  }
134  return null;
135  }
136 
137  @Override
138  protected void done() {
139  super.done();
140  try {
141  get();
142  } catch (InterruptedException | ExecutionException ex) {
143  logger.log(Level.SEVERE, "Unexpected exception while untagging artifact", ex); //NON-NLS
144  }
145  }
146  }.execute();
147  }
148 
154  @NbBundle.Messages({"# {0} - artifactID",
155  "DeleteFileBlackboardArtifactTagAction.deleteTags.alert=Unable to untag artifact {0}."})
156  private final class TagMenu extends JMenu {
157 
158  private static final long serialVersionUID = 1L;
159 
160  TagMenu() {
161  this(new HashSet<>(Utilities.actionsGlobalContext().lookupAll(BlackboardArtifact.class)));
162  }
163 
164  TagMenu(Collection<BlackboardArtifact> selectedBlackboardArtifactsList) {
165  super(getActionDisplayName());
166  if (!selectedBlackboardArtifactsList.isEmpty()) {
167  BlackboardArtifact artifact
168  = selectedBlackboardArtifactsList.iterator().next();
169 
170  Map<String, TagName> tagNamesMap = null;
171  List<String> standardTagNames = TagsManager.getStandardTagNames();
172  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
173  try {
174  // Get the current set of tag names.
176 
177  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
178  } catch (TskCoreException | NoCurrentCaseException ex) {
179  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
180  }
181 
182  // Each tag name in the current set of tags gets its own menu item in
183  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
184  // a tag with the associated tag name.
185  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
186  try {
187  List<BlackboardArtifactTag> existingTagsList
188  = Case.getCurrentCaseThrows().getServices().getTagsManager()
189  .getBlackboardArtifactTagsByArtifact(artifact);
190 
191  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
192  String tagDisplayName = entry.getKey();
193 
194  TagName tagName = entry.getValue();
195  for (BlackboardArtifactTag artifactTag : existingTagsList) {
196  if (tagDisplayName.equals(artifactTag.getName().getDisplayName())) {
197  JMenuItem tagNameItem = new JMenuItem(TagUtils.getDecoratedTagDisplayName(tagName));
198  tagNameItem.addActionListener((ActionEvent e) -> {
199  deleteTag(tagName, artifactTag, artifact.getArtifactID());
200  });
201  // Show custom tags before predefined tags in the menu
202  if (standardTagNames.contains(tagDisplayName)) {
203  standardTagMenuitems.add(tagNameItem);
204  } else {
205  add(tagNameItem);
206  }
207  }
208  }
209  }
210  } catch (TskCoreException | NoCurrentCaseException ex) {
211  Logger.getLogger(TagMenu.class.getName())
212  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
213  }
214  }
215 
216  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty()) {
217  addSeparator();
218  }
219  standardTagMenuitems.forEach((menuItem) -> {
220  add(menuItem);
221  });
222  if (getItemCount() == 0) {
223  setEnabled(false);
224  }
225  }
226  }
227  }
228 
229 }
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
JMenuItem getMenuForArtifacts(Collection< BlackboardArtifact > selectedArtifacts)

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.