Autopsy  4.19.3
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.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 java.util.stream.Collectors;
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;
45 import org.sleuthkit.datamodel.BlackboardArtifact;
46 import org.sleuthkit.datamodel.BlackboardArtifactTag;
47 import org.sleuthkit.datamodel.TagName;
48 import org.sleuthkit.datamodel.TskCoreException;
49 
54 @NbBundle.Messages({
55  "DeleteFileBlackboardArtifactTagAction.deleteTag=Remove Result Tag"
56 })
57 public class DeleteFileBlackboardArtifactTagAction extends AbstractAction implements Presenter.Popup {
58 
59  private static final Logger logger = Logger.getLogger(DeleteFileBlackboardArtifactTagAction.class.getName());
60 
61  private static final long serialVersionUID = 1L;
62  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileBlackboardArtifactTagAction.class,
63  "DeleteFileBlackboardArtifactTagAction.deleteTag");
64 
65  // This class is a singleton to support multi-selection of nodes, since
66  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
67  // node in the array returns a reference to the same action object from Node.getActions(boolean).
69 
70  public static synchronized DeleteFileBlackboardArtifactTagAction getInstance() {
71  if (null == instance) {
73  }
74  return instance;
75  }
76 
78  super(MENU_TEXT);
79  }
80 
81  @Override
82  public JMenuItem getPopupPresenter() {
83  return new TagMenu();
84  }
85 
96  public JMenuItem getMenuForArtifacts(Collection<BlackboardArtifact> selectedArtifacts) {
97  return new TagMenu(selectedArtifacts);
98  }
99 
100  @Override
101  public void actionPerformed(ActionEvent event) {
102  }
103 
104  protected String getActionDisplayName() {
105  return MENU_TEXT;
106  }
107 
108  @NbBundle.Messages({"# {0} - artifactID",
109  "DeleteFileBlackboardArtifactTagAction.deleteTag.alert=Unable to untag artifact {0}."})
110  protected void deleteTag(TagName tagName, BlackboardArtifactTag artifactTag, long artifactId) {
111  new SwingWorker<Void, Void>() {
112 
113  @Override
114  protected Void doInBackground() throws Exception {
115  TagsManager tagsManager;
116  try {
118  } catch (NoCurrentCaseException ex) {
119  logger.log(Level.SEVERE, "Error untagging artifact. No open case found.", ex); //NON-NLS
120  Platform.runLater(()
121  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
122  );
123  return null;
124  }
125 
126  try {
127  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), artifactTag.getContent().getName()}); //NON-NLS
128  tagsManager.deleteBlackboardArtifactTag(artifactTag);
129  } catch (TskCoreException tskCoreException) {
130  logger.log(Level.SEVERE, "Error untagging artifact", tskCoreException); //NON-NLS
131  Platform.runLater(()
132  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileBlackboardArtifactTagAction_deleteTag_alert(artifactId)).show()
133  );
134  }
135  return null;
136  }
137 
138  @Override
139  protected void done() {
140  super.done();
141  try {
142  get();
143  } catch (InterruptedException | ExecutionException ex) {
144  logger.log(Level.SEVERE, "Unexpected exception while untagging artifact", ex); //NON-NLS
145  }
146  }
147  }.execute();
148  }
149 
155  @NbBundle.Messages({"# {0} - artifactID",
156  "DeleteFileBlackboardArtifactTagAction.deleteTags.alert=Unable to untag artifact {0}."})
157  private final class TagMenu extends JMenu {
158 
159  private static final long serialVersionUID = 1L;
160 
161  TagMenu() {
162  this(Utilities.actionsGlobalContext()
163  .lookupAll(BlackboardArtifactItem.class)
164  .stream()
165  .map((bai) -> (BlackboardArtifact) bai.getTskContent())
166  .collect(Collectors.toSet()));
167  }
168 
169  TagMenu(Collection<BlackboardArtifact> selectedBlackboardArtifactsList) {
170  super(getActionDisplayName());
171  if (!selectedBlackboardArtifactsList.isEmpty()) {
172  BlackboardArtifact artifact
173  = selectedBlackboardArtifactsList.iterator().next();
174 
175  Map<String, TagName> tagNamesMap = null;
176  List<String> standardTagNames = TagsManager.getStandardTagNames();
177  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
178  try {
179  // Get the current set of tag names.
181 
182  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
183  } catch (TskCoreException | NoCurrentCaseException ex) {
184  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
185  }
186 
187  // Each tag name in the current set of tags gets its own menu item in
188  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
189  // a tag with the associated tag name.
190  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
191  try {
192  List<BlackboardArtifactTag> existingTagsList
193  = Case.getCurrentCaseThrows().getServices().getTagsManager()
194  .getBlackboardArtifactTagsByArtifact(artifact);
195 
196  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
197  String tagDisplayName = entry.getKey();
198 
199  TagName tagName = entry.getValue();
200  for (BlackboardArtifactTag artifactTag : existingTagsList) {
201  if (tagDisplayName.equals(artifactTag.getName().getDisplayName())) {
202  JMenuItem tagNameItem = new JMenuItem(TagUtils.getDecoratedTagDisplayName(tagName));
203  tagNameItem.addActionListener((ActionEvent e) -> {
204  deleteTag(tagName, artifactTag, artifact.getArtifactID());
205  });
206  // Show custom tags before predefined tags in the menu
207  if (standardTagNames.contains(tagDisplayName)) {
208  standardTagMenuitems.add(tagNameItem);
209  } else {
210  add(tagNameItem);
211  }
212  }
213  }
214  }
215  } catch (TskCoreException | NoCurrentCaseException ex) {
216  Logger.getLogger(TagMenu.class.getName())
217  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
218  }
219  }
220 
221  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty()) {
222  addSeparator();
223  }
224  standardTagMenuitems.forEach((menuItem) -> {
225  add(menuItem);
226  });
227  if (getItemCount() == 0) {
228  setEnabled(false);
229  }
230  }
231  }
232  }
233 
234 }
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-2022 Basis Technology. Generated on: Thu Dec 8 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.