Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2021 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.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
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.Content;
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 
50 abstract class AddTagAction extends AbstractAction implements Presenter.Popup {
51 
52  private static final long serialVersionUID = 1L;
53  private static final String NO_COMMENT = "";
54  private final Collection<Content> contentObjsToTag;
55 
62  AddTagAction(String menuText) {
63  super(menuText);
64  contentObjsToTag = new HashSet<>();
65  }
66 
67  @Override
68  public JMenuItem getPopupPresenter() {
69  contentObjsToTag.clear();
70  return new TagMenu();
71  }
72 
79  Collection<Content> getContentToTag() {
80  return Collections.unmodifiableCollection(contentObjsToTag);
81  }
82 
92  public JMenuItem getMenuForContent(Collection<? extends Content> contentToTag) {
93  contentObjsToTag.clear();
94  contentObjsToTag.addAll(contentToTag);
95  return new TagMenu();
96  }
97 
104  @Override
105  @SuppressWarnings("NoopMethodInAbstractClass")
106  public void actionPerformed(ActionEvent event) {
107  }
108 
113  abstract protected String getActionDisplayName();
114 
119  abstract protected void addTag(TagName tagName, String comment);
120 
121  @Override
122  public Object clone() throws CloneNotSupportedException {
123  return super.clone();
124  }
125 
131  // @@@ This user interface has some significant usability issues and needs
132  // to be reworked.
133  private final class TagMenu extends JMenu {
134 
135  private static final long serialVersionUID = 1L;
136 
137  TagMenu() {
138  super(getActionDisplayName());
139 
140  // Get the current set of tag names.
141  Map<String, TagName> tagNamesMap;
142  List<String> standardTagNames = TagsManager.getStandardTagNames();
143  Map<String, JMenu> tagSetMenuMap = new HashMap<>();
144  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
145  try {
147  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
148 
149  // Create a menu item for each of the existing and visible tags.
150  // Selecting one of these menu items adds a tag with the associated tag name.
151  if (!tagNamesMap.isEmpty()) {
152  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
153  TagName tagName = entry.getValue();
154  TagSet tagSet = tagsManager.getTagSet(tagName);
155 
156  // Show custom tags before predefined tags in the menu
157  if (tagSet != null) {
158  JMenu menu = tagSetMenuMap.get(tagSet.getName());
159  if (menu == null) {
160  menu = createSubmenuForTagSet(tagSet);
161  tagSetMenuMap.put(tagSet.getName(), menu);
162  }
163  } else if (standardTagNames.contains(tagName.getDisplayName())) {
164  standardTagMenuitems.add(createMenutItem(tagName));
165  } else {
166  add(createMenutItem(tagName));
167  }
168  }
169  }
170  } catch (TskCoreException | NoCurrentCaseException ex) {
171  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
172  }
173 
174  if (getItemCount() > 0) {
175  addSeparator();
176  }
177 
178  standardTagMenuitems.forEach((menuItem) -> {
179  add(menuItem);
180  });
181 
182  tagSetMenuMap.values().forEach((menu) -> {
183  add(menu);
184  });
185 
186  addSeparator();
187 
188  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
189  // a dialog that can be used to create or select a tag name with an
190  // optional comment and adds a tag with the resulting name.
191  JMenuItem tagAndCommentItem = new JMenuItem(
192  NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
193  tagAndCommentItem.addActionListener((ActionEvent e) -> {
195  if (null != tagNameAndComment) {
196  addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
197  }
198  });
199  add(tagAndCommentItem);
200 
201  // Create a "New Tag..." menu item.
202  // Selecting this item initiates a dialog that can be used to create
203  // or select a tag name and adds a tag with the resulting name.
204  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
205  newTagMenuItem.addActionListener((ActionEvent e) -> {
206  TagName tagName = GetTagNameDialog.doDialog();
207  if (null != tagName) {
208  addTag(tagName, NO_COMMENT);
209  }
210  });
211  add(newTagMenuItem);
212 
213  }
214 
222  private JMenu createSubmenuForTagSet(TagSet tagSet) {
223  JMenu menu = new JMenu(tagSet.getName());
224  List<TagName> tagNameList = tagSet.getTagNames();
225 
226  for (TagName tagName : tagNameList) {
227  menu.add(createMenutItem(tagName));
228  }
229 
230  return menu;
231  }
232 
240  private JMenuItem createMenutItem(TagName tagName) {
241  String tagDisplayName = tagName.getDisplayName();
242  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
243  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
244 
245  if (tagDisplayName.equals(TagsManager.getBookmarkTagDisplayName())) {
246  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
247  }
248 
249  tagNameItem.addActionListener((ActionEvent e) -> {
250  addTag(tagName, NO_COMMENT);
251  });
252 
253  return tagNameItem;
254  }
255 
256  }
257 
258 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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