19 package org.sleuthkit.autopsy.actions;
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;
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;
52 "ReplaceTagAction.replaceTag=Replace Selected Tag(s) With"
54 abstract class ReplaceTagAction<T extends Tag> extends AbstractAction implements Presenter.Popup {
56 private static final long serialVersionUID = 1L;
57 protected static final String MENU_TEXT = NbBundle.getMessage(ReplaceTagAction.class,
58 "ReplaceTagAction.replaceTag");
60 ReplaceTagAction(String menuText) {
71 @SuppressWarnings(
"NoopMethodInAbstractClass")
72 public
void actionPerformed(ActionEvent event) {
75 protected String getActionDisplayName() {
87 abstract protected void replaceTag(T oldTag, TagName newTagName, String comment);
94 abstract Collection<? extends T> getTagsToReplace();
97 public JMenuItem getPopupPresenter() {
98 return new ReplaceTagMenu();
107 private static final long serialVersionUID = 1L;
110 super(getActionDisplayName());
112 final Collection<? extends T> selectedTags = getTagsToReplace();
115 Map<String, TagName> tagNamesMap = null;
117 Map<String, JMenu> tagSetMenuMap =
new HashMap<>();
118 List<JMenuItem> standardTagMenuitems =
new ArrayList<>();
121 Set<String> existingTagNames =
new HashSet<>();
126 if (!selectedTags.isEmpty()) {
127 T firstTag = selectedTags.iterator().next();
128 existingTagNames.add(firstTag.getName().getDisplayName());
131 if (!tagNamesMap.isEmpty()) {
132 for (Map.Entry<String,
TagName> entry : tagNamesMap.entrySet()) {
133 TagName tagName = entry.getValue();
137 if (tagSet != null) {
138 JMenu menu = tagSetMenuMap.get(tagSet.
getName());
140 menu = createSubmenuForTagSet(tagSet, existingTagNames, selectedTags);
141 tagSetMenuMap.put(tagSet.
getName(), menu);
144 standardTagMenuitems.add(createMenutItem(tagName, existingTagNames, selectedTags));
146 add(createMenutItem(tagName, existingTagNames, selectedTags));
150 JMenuItem empty =
new JMenuItem(NbBundle.getMessage(
this.getClass(),
"AddTagAction.noTags"));
151 empty.setEnabled(
false);
160 if (this.getItemCount() > 0) {
163 standardTagMenuitems.forEach((menuItem) -> {
167 tagSetMenuMap.values().forEach((menuItem) -> {
172 JMenuItem newTagMenuItem =
new JMenuItem(NbBundle.getMessage(
this.getClass(),
"AddTagAction.newTag"));
173 newTagMenuItem.addActionListener((ActionEvent event) -> {
175 if (null != newTagName) {
176 selectedTags.forEach((oldtag) -> {
177 replaceTag(oldtag, newTagName, oldtag.getComment());
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());
194 add(tagAndCommentItem);
207 private JMenu createSubmenuForTagSet(
TagSet tagSet, Set<String> tagNamesToDisable, Collection<? extends T> selectedTags) {
208 JMenu menu =
new JMenu(tagSet.
getName());
211 for (
TagName tagName : tagNameList) {
212 menu.add(createMenutItem(tagName, tagNamesToDisable, selectedTags));
227 private JMenuItem createMenutItem(TagName tagName, Set<String> tagNamesToDisable, Collection<? extends T> selectedTags) {
228 String tagDisplayName = tagName.getDisplayName();
229 String notableString = tagName.getTagType() == TskData.TagType.BAD ? TagsManager.getNotableTagLabel() :
"";
230 JMenuItem tagNameItem =
new JMenuItem(tagDisplayName + notableString);
232 if (tagDisplayName.equals(TagsManager.getBookmarkTagDisplayName())) {
233 tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
236 tagNameItem.addActionListener((ActionEvent e) -> {
237 selectedTags.forEach((oldtag) -> {
238 replaceTag(oldtag, tagName, oldtag.getComment());
242 tagNameItem.setEnabled(!tagNamesToDisable.contains(tagDisplayName));
TagsManager getTagsManager()
List< TagName > getTagNames()
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()
static TagName doDialog()