Autopsy  4.7.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 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 
83  abstract protected void replaceTag(T oldTag, TagName newTagName);
84 
90  abstract Collection<? extends T> getTagsToReplace();
91 
92 
93  @Override
94  public JMenuItem getPopupPresenter() {
95  return new ReplaceTagMenu();
96  }
97 
102  private final class ReplaceTagMenu extends JMenu {
103 
104  private static final long serialVersionUID = 1L;
105 
106  ReplaceTagMenu() {
107  super(getActionDisplayName());
108 
109  final Collection<? extends T> selectedTags = getTagsToReplace();
110 
111  // Get the current set of tag names.
112  Map<String, TagName> tagNamesMap = null;
113  List<String> standardTagNames = TagsManager.getStandardTagNames();
114  try {
116  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
117  } catch (TskCoreException | NoCurrentCaseException ex) {
118  Logger.getLogger(ReplaceTagMenu.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
119  }
120 
121  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
122  // Ideally we should'nt allow user to pick a replacement tag that's already been applied to an item
123  // In the very least we don't allow them to pick the same tag as the one they are trying to replace
124  Set<String> existingTagNames = new HashSet<>();
125  if (!selectedTags.isEmpty()) {
126  T firstTag = selectedTags.iterator().next();
127  existingTagNames.add(firstTag.getName().getDisplayName());
128  }
129 
130  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
131  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
132  String tagDisplayName = entry.getKey();
133  String notableString = entry.getValue().getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
134  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
135  // for the bookmark tag name only, added shortcut label
136  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
137  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
138  }
139 
140  // Add action to replace the tag
141  tagNameItem.addActionListener((ActionEvent event) -> {
142  selectedTags.forEach((oldtag) -> {
143  replaceTag(oldtag, entry.getValue());
144  });
145  });
146 
147  // Don't allow replacing a tag with same tag.
148  if (existingTagNames.contains(tagDisplayName)) {
149  tagNameItem.setEnabled(false);
150  }
151 
152 
153  // Show custom tags before predefined tags in the menu
154  if (standardTagNames.contains(tagDisplayName)) {
155  standardTagMenuitems.add(tagNameItem);
156  } else {
157  add(tagNameItem);
158  }
159  }
160  } else {
161  JMenuItem empty = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.noTags"));
162  empty.setEnabled(false);
163  add(empty);
164  }
165 
166  //
167  if (this.getItemCount() > 0) {
168  addSeparator();
169  }
170  standardTagMenuitems.forEach((menuItem) -> {
171  add(menuItem);
172  });
173 
174  addSeparator();
175  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
176  newTagMenuItem.addActionListener((ActionEvent event) -> {
177  TagName newTagName = GetTagNameDialog.doDialog();
178  if (null != newTagName) {
179  selectedTags.forEach((oldtag) -> {
180  replaceTag(oldtag, newTagName);
181  });
182  }
183  });
184  add(newTagMenuItem);
185 
186  }
187  }
188 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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