Autopsy  4.9.1
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 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.Set;
28 import java.util.TreeMap;
29 import java.util.logging.Level;
30 import javax.swing.AbstractAction;
31 import javax.swing.JMenu;
32 import javax.swing.JMenuItem;
33 import org.openide.util.NbBundle;
34 import org.openide.util.actions.Presenter;
39 import org.sleuthkit.datamodel.Tag;
40 import org.sleuthkit.datamodel.TagName;
41 import org.sleuthkit.datamodel.TskCoreException;
42 import org.sleuthkit.datamodel.TskData;
43 
49 @NbBundle.Messages({
50  "ReplaceTagAction.replaceTag=Replace Selected Tag(s) With"
51 })
52 abstract class ReplaceTagAction<T extends Tag> extends AbstractAction implements Presenter.Popup {
53 
54  private static final long serialVersionUID = 1L;
55  protected static final String MENU_TEXT = NbBundle.getMessage(ReplaceTagAction.class,
56  "ReplaceTagAction.replaceTag");
57 
58  ReplaceTagAction(String menuText) {
59  super(menuText);
60  }
61 
68  @Override
69  @SuppressWarnings("NoopMethodInAbstractClass")
70  public void actionPerformed(ActionEvent event) {
71  }
72 
73  protected String getActionDisplayName() {
74  return MENU_TEXT;
75  }
76 
84  abstract protected void replaceTag(T oldTag, TagName newTagName, String comment);
85 
91  abstract Collection<? extends T> getTagsToReplace();
92 
93 
94  @Override
95  public JMenuItem getPopupPresenter() {
96  return new ReplaceTagMenu();
97  }
98 
103  private final class ReplaceTagMenu extends JMenu {
104 
105  private static final long serialVersionUID = 1L;
106 
107  ReplaceTagMenu() {
108  super(getActionDisplayName());
109 
110  final Collection<? extends T> selectedTags = getTagsToReplace();
111 
112  // Get the current set of tag names.
113  Map<String, TagName> tagNamesMap = null;
114  List<String> standardTagNames = TagsManager.getStandardTagNames();
115  try {
117  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
118  } catch (TskCoreException | NoCurrentCaseException ex) {
119  Logger.getLogger(ReplaceTagMenu.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
120  }
121 
122  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
123  // Ideally we should'nt allow user to pick a replacement tag that's already been applied to an item
124  // In the very least we don't allow them to pick the same tag as the one they are trying to replace
125  Set<String> existingTagNames = new HashSet<>();
126  if (!selectedTags.isEmpty()) {
127  T firstTag = selectedTags.iterator().next();
128  existingTagNames.add(firstTag.getName().getDisplayName());
129  }
130 
131  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
132  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
133  String tagDisplayName = entry.getKey();
134  String notableString = entry.getValue().getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
135  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
136  // for the bookmark tag name only, added shortcut label
137  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
138  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
139  }
140 
141  // Add action to replace the tag
142  tagNameItem.addActionListener((ActionEvent event) -> {
143  selectedTags.forEach((oldtag) -> {
144  replaceTag(oldtag, entry.getValue(), oldtag.getComment());
145  });
146  });
147 
148  // Don't allow replacing a tag with same tag.
149  if (existingTagNames.contains(tagDisplayName)) {
150  tagNameItem.setEnabled(false);
151  }
152 
153 
154  // Show custom tags before predefined tags in the menu
155  if (standardTagNames.contains(tagDisplayName)) {
156  standardTagMenuitems.add(tagNameItem);
157  } else {
158  add(tagNameItem);
159  }
160  }
161  } else {
162  JMenuItem empty = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.noTags"));
163  empty.setEnabled(false);
164  add(empty);
165  }
166 
167  //
168  if (this.getItemCount() > 0) {
169  addSeparator();
170  }
171  standardTagMenuitems.forEach((menuItem) -> {
172  add(menuItem);
173  });
174 
175  addSeparator();
176  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
177  newTagMenuItem.addActionListener((ActionEvent event) -> {
178  TagName newTagName = GetTagNameDialog.doDialog();
179  if (null != newTagName) {
180  selectedTags.forEach((oldtag) -> {
181  replaceTag(oldtag, newTagName, oldtag.getComment());
182  });
183  }
184  });
185  add(newTagMenuItem);
186  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
187  // a dialog that can be used to create or select a tag name with an
188  // optional comment and adds a tag with the resulting name.
189  JMenuItem tagAndCommentItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
190  tagAndCommentItem.addActionListener((ActionEvent event) -> {
192  if (null != tagNameAndComment) {
193  selectedTags.forEach((oldtag) -> {
194  replaceTag(oldtag, tagNameAndComment.getTagName(), tagNameAndComment.getComment());
195  });
196  }
197  });
198  add(tagAndCommentItem);
199  }
200  }
201 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.