Autopsy  4.19.0
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-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.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> content = new HashSet<>();
55 
56  AddTagAction(String menuText) {
57  super(menuText);
58  }
59 
60  @Override
61  public JMenuItem getPopupPresenter() {
62  content.clear();
63  return new TagMenu();
64  }
65 
72  Collection<Content> getContentToTag() {
73  return Collections.unmodifiableCollection(content);
74  }
75 
85  public JMenuItem getMenuForContent(Collection<? extends Content> contentToTag) {
86  content.clear();
87  content.addAll(contentToTag);
88  return new TagMenu();
89  }
90 
97  @Override
98  @SuppressWarnings("NoopMethodInAbstractClass")
99  public void actionPerformed(ActionEvent event) {
100  }
101 
106  abstract protected String getActionDisplayName();
107 
112  abstract protected void addTag(TagName tagName, String comment);
113 
119  // @@@ This user interface has some significant usability issues and needs
120  // to be reworked.
121  private final class TagMenu extends JMenu {
122 
123  private static final long serialVersionUID = 1L;
124 
125  TagMenu() {
126  super(getActionDisplayName());
127 
128  // Get the current set of tag names.
129  Map<String, TagName> tagNamesMap = null;
130  List<String> standardTagNames = TagsManager.getStandardTagNames();
131  Map<String, JMenu> tagSetMenuMap = new HashMap<>();
132  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
133  try {
135  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
136 
137  // Create a menu item for each of the existing and visible tags.
138  // Selecting one of these menu items adds a tag with the associated tag name.
139  if (!tagNamesMap.isEmpty()) {
140  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
141  TagName tagName = entry.getValue();
142  TagSet tagSet = tagsManager.getTagSet(tagName);
143 
144  // Show custom tags before predefined tags in the menu
145  if (tagSet != null) {
146  JMenu menu = tagSetMenuMap.get(tagSet.getName());
147  if (menu == null) {
148  menu = createSubmenuForTagSet(tagSet);
149  tagSetMenuMap.put(tagSet.getName(), menu);
150  }
151  } else if (standardTagNames.contains(tagName.getDisplayName())) {
152  standardTagMenuitems.add(createMenutItem(tagName));
153  } else {
154  add(createMenutItem(tagName));
155  }
156  }
157  }
158  } catch (TskCoreException | NoCurrentCaseException ex) {
159  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
160  }
161 
162  if (getItemCount() > 0) {
163  addSeparator();
164  }
165 
166  standardTagMenuitems.forEach((menuItem) -> {
167  add(menuItem);
168  });
169 
170  tagSetMenuMap.values().forEach((menu) -> {
171  add(menu);
172  });
173 
174  addSeparator();
175 
176  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
177  // a dialog that can be used to create or select a tag name with an
178  // optional comment and adds a tag with the resulting name.
179  JMenuItem tagAndCommentItem = new JMenuItem(
180  NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
181  tagAndCommentItem.addActionListener((ActionEvent e) -> {
183  if (null != tagNameAndComment) {
184  addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
185  }
186  });
187  add(tagAndCommentItem);
188 
189  // Create a "New Tag..." menu item.
190  // Selecting this item initiates a dialog that can be used to create
191  // or select a tag name and adds a tag with the resulting name.
192  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
193  newTagMenuItem.addActionListener((ActionEvent e) -> {
194  TagName tagName = GetTagNameDialog.doDialog();
195  if (null != tagName) {
196  addTag(tagName, NO_COMMENT);
197  }
198  });
199  add(newTagMenuItem);
200 
201  }
202 
210  private JMenu createSubmenuForTagSet(TagSet tagSet) {
211  JMenu menu = new JMenu(tagSet.getName());
212  List<TagName> tagNameList = tagSet.getTagNames();
213 
214  for (TagName tagName : tagNameList) {
215  menu.add(createMenutItem(tagName));
216  }
217 
218  return menu;
219  }
220 
228  private JMenuItem createMenutItem(TagName tagName) {
229  String tagDisplayName = tagName.getDisplayName();
230  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
231  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
232 
233  if (tagDisplayName.equals(TagsManager.getBookmarkTagDisplayName())) {
234  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
235  }
236 
237  tagNameItem.addActionListener((ActionEvent e) -> {
238  addTag(tagName, NO_COMMENT);
239  });
240 
241  return tagNameItem;
242  }
243  }
244 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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