Autopsy  4.15.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReplaceTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018-2020 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.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeMap;
30 import java.util.logging.Level;
31 import javax.swing.AbstractAction;
32 import javax.swing.JMenu;
33 import javax.swing.JMenuItem;
34 import org.openide.util.NbBundle;
35 import org.openide.util.actions.Presenter;
40 import org.sleuthkit.datamodel.Tag;
41 import org.sleuthkit.datamodel.TagName;
42 import org.sleuthkit.datamodel.TagSet;
43 import org.sleuthkit.datamodel.TskCoreException;
44 import org.sleuthkit.datamodel.TskData;
45 
51 @NbBundle.Messages({
52  "ReplaceTagAction.replaceTag=Replace Selected Tag(s) With"
53 })
54 abstract class ReplaceTagAction<T extends Tag> extends AbstractAction implements Presenter.Popup {
55 
56  private static final long serialVersionUID = 1L;
57  protected static final String MENU_TEXT = NbBundle.getMessage(ReplaceTagAction.class,
58  "ReplaceTagAction.replaceTag");
59 
60  ReplaceTagAction(String menuText) {
61  super(menuText);
62  }
63 
70  @Override
71  @SuppressWarnings("NoopMethodInAbstractClass")
72  public void actionPerformed(ActionEvent event) {
73  }
74 
75  protected String getActionDisplayName() {
76  return MENU_TEXT;
77  }
78 
87  abstract protected void replaceTag(T oldTag, TagName newTagName, String comment);
88 
94  abstract Collection<? extends T> getTagsToReplace();
95 
96  @Override
97  public JMenuItem getPopupPresenter() {
98  return new ReplaceTagMenu();
99  }
100 
105  private final class ReplaceTagMenu extends JMenu {
106 
107  private static final long serialVersionUID = 1L;
108 
109  ReplaceTagMenu() {
110  super(getActionDisplayName());
111 
112  final Collection<? extends T> selectedTags = getTagsToReplace();
113 
114  // Get the current set of tag names.
115  Map<String, TagName> tagNamesMap = null;
116  List<String> standardTagNames = TagsManager.getStandardTagNames();
117  Map<String, JMenu> tagSetMenuMap = new HashMap<>();
118  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
119  // Ideally we should'nt allow user to pick a replacement tag that's already been applied to an item
120  // In the very least we don't allow them to pick the same tag as the one they are trying to replace
121  Set<String> existingTagNames = new HashSet<>();
122  try {
124  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
125 
126  if (!selectedTags.isEmpty()) {
127  T firstTag = selectedTags.iterator().next();
128  existingTagNames.add(firstTag.getName().getDisplayName());
129  }
130 
131  if (!tagNamesMap.isEmpty()) {
132  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
133  TagName tagName = entry.getValue();
134  TagSet tagSet = tagsManager.getTagSet(tagName);
135 
136  // Show custom tags before predefined tags in the menu
137  if (tagSet != null) {
138  JMenu menu = tagSetMenuMap.get(tagSet.getName());
139  if (menu == null) {
140  menu = createSubmenuForTagSet(tagSet, existingTagNames, selectedTags);
141  tagSetMenuMap.put(tagSet.getName(), menu);
142  }
143  } else if (standardTagNames.contains(tagName.getDisplayName())) {
144  standardTagMenuitems.add(createMenutItem(tagName, existingTagNames, selectedTags));
145  } else {
146  add(createMenutItem(tagName, existingTagNames, selectedTags));
147  }
148  }
149  } else {
150  JMenuItem empty = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.noTags"));
151  empty.setEnabled(false);
152  add(empty);
153  }
154 
155  } catch (TskCoreException | NoCurrentCaseException ex) {
156  Logger.getLogger(ReplaceTagMenu.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
157  }
158 
159  //
160  if (this.getItemCount() > 0) {
161  addSeparator();
162  }
163  standardTagMenuitems.forEach((menuItem) -> {
164  add(menuItem);
165  });
166 
167  tagSetMenuMap.values().forEach((menuItem) -> {
168  add(menuItem);
169  });
170 
171  addSeparator();
172  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
173  newTagMenuItem.addActionListener((ActionEvent event) -> {
174  TagName newTagName = GetTagNameDialog.doDialog();
175  if (null != newTagName) {
176  selectedTags.forEach((oldtag) -> {
177  replaceTag(oldtag, newTagName, oldtag.getComment());
178  });
179  }
180  });
181  add(newTagMenuItem);
182  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
183  // a dialog that can be used to create or select a tag name with an
184  // optional comment and adds a tag with the resulting name.
185  JMenuItem tagAndCommentItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
186  tagAndCommentItem.addActionListener((ActionEvent event) -> {
188  if (null != tagNameAndComment) {
189  selectedTags.forEach((oldtag) -> {
190  replaceTag(oldtag, tagNameAndComment.getTagName(), tagNameAndComment.getComment());
191  });
192  }
193  });
194  add(tagAndCommentItem);
195  }
196  }
197 
207  private JMenu createSubmenuForTagSet(TagSet tagSet, Set<String> tagNamesToDisable, Collection<? extends T> selectedTags) {
208  JMenu menu = new JMenu(tagSet.getName());
209  List<TagName> tagNameList = tagSet.getTagNames();
210 
211  for (TagName tagName : tagNameList) {
212  menu.add(createMenutItem(tagName, tagNamesToDisable, selectedTags));
213  }
214 
215  return menu;
216  }
217 
227  private JMenuItem createMenutItem(TagName tagName, Set<String> tagNamesToDisable, Collection<? extends T> selectedTags) {
228  String tagDisplayName = tagName.getDisplayName();
229  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
230  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
231 
232  if (tagDisplayName.equals(TagsManager.getBookmarkTagDisplayName())) {
233  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
234  }
235 
236  tagNameItem.addActionListener((ActionEvent e) -> {
237  selectedTags.forEach((oldtag) -> {
238  replaceTag(oldtag, tagName, oldtag.getComment());
239  });
240  });
241 
242  tagNameItem.setEnabled(!tagNamesToDisable.contains(tagDisplayName));
243 
244  return tagNameItem;
245  }
246 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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