Autopsy  4.13.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 2011-2019 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.HashSet;
26 import java.util.List;
27 import java.util.Map;
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.Content;
40 import org.sleuthkit.datamodel.TagName;
41 import org.sleuthkit.datamodel.TskCoreException;
42 import org.sleuthkit.datamodel.TskData;
43 
48 abstract class AddTagAction extends AbstractAction implements Presenter.Popup {
49 
50  private static final long serialVersionUID = 1L;
51  private static final String NO_COMMENT = "";
52  private final Collection<Content> content = new HashSet<>();
53 
54  AddTagAction(String menuText) {
55  super(menuText);
56  }
57 
58  @Override
59  public JMenuItem getPopupPresenter() {
60  content.clear();
61  return new TagMenu();
62  }
63 
70  Collection<Content> getContentToTag() {
71  return Collections.unmodifiableCollection(content);
72  }
73 
83  public JMenuItem getMenuForContent(Collection<? extends Content> contentToTag) {
84  content.clear();
85  content.addAll(contentToTag);
86  return new TagMenu();
87  }
88 
95  @Override
96  @SuppressWarnings("NoopMethodInAbstractClass")
97  public void actionPerformed(ActionEvent event) {
98  }
99 
104  abstract protected String getActionDisplayName();
105 
110  abstract protected void addTag(TagName tagName, String comment);
111 
117  // @@@ This user interface has some significant usability issues and needs
118  // to be reworked.
119  private final class TagMenu extends JMenu {
120 
121  private static final long serialVersionUID = 1L;
122 
123  TagMenu() {
124  super(getActionDisplayName());
125 
126  // Get the current set of tag names.
127  Map<String, TagName> tagNamesMap = null;
128  List<String> standardTagNames = TagsManager.getStandardTagNames();
129  try {
131  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
132  } catch (TskCoreException | NoCurrentCaseException ex) {
133  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
134  }
135 
136  // Create a menu item for each of the existing and visible tags.
137  // Selecting one of these menu items adds a tag with the associated tag name.
138  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
139  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
140  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
141  String tagDisplayName = entry.getKey();
142  String notableString = entry.getValue().getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
143  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
144  // for the bookmark tag name only, added shortcut label
145  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
146  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
147  }
148 
149  tagNameItem.addActionListener((ActionEvent e) -> {
150  getAndAddTag(entry.getKey(), entry.getValue(), NO_COMMENT);
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  }
161 
162  if (getItemCount() > 0) {
163  addSeparator();
164  }
165 
166  standardTagMenuitems.forEach((menuItem) -> {
167  add(menuItem);
168  });
169 
170  addSeparator();
171 
172  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
173  // a dialog that can be used to create or select a tag name with an
174  // optional comment and adds a tag with the resulting name.
175  JMenuItem tagAndCommentItem = new JMenuItem(
176  NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
177  tagAndCommentItem.addActionListener((ActionEvent e) -> {
179  if (null != tagNameAndComment) {
180  addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
181  }
182  });
183  add(tagAndCommentItem);
184 
185  // Create a "New Tag..." menu item.
186  // Selecting this item initiates a dialog that can be used to create
187  // or select a tag name and adds a tag with the resulting name.
188  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
189  newTagMenuItem.addActionListener((ActionEvent e) -> {
190  TagName tagName = GetTagNameDialog.doDialog();
191  if (null != tagName) {
192  addTag(tagName, NO_COMMENT);
193  }
194  });
195  add(newTagMenuItem);
196 
197  }
198 
211  private void getAndAddTag(String tagDisplayName, TagName tagName, String comment) {
212  Case openCase;
213  try {
214  openCase = Case.getCurrentCaseThrows();
215  } catch (NoCurrentCaseException ex) {
216  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex); // NON-NLS
217  return;
218  }
219 
220  if (tagName == null) {
221  try {
222  tagName = openCase.getServices().getTagsManager().addTagName(tagDisplayName);
224  try {
225  tagName = openCase.getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
226  } catch (TskCoreException ex1) {
227  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " already exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
228  }
229  } catch (TskCoreException ex) {
230  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
231  }
232  }
233  addTag(tagName, comment);
234  }
235  }
236 }
synchronized TagName addTagName(String displayName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
void getAndAddTag(String tagDisplayName, TagName tagName, String comment)

Copyright © 2012-2019 Basis Technology. Generated on: Tue Jan 7 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.