Autopsy  4.9.1
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-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.List;
24 import java.util.Map;
25 import java.util.TreeMap;
26 import java.util.logging.Level;
27 import javax.swing.AbstractAction;
28 import javax.swing.JMenu;
29 import javax.swing.JMenuItem;
30 import org.openide.util.NbBundle;
31 import org.openide.util.actions.Presenter;
36 import org.sleuthkit.datamodel.TagName;
37 import org.sleuthkit.datamodel.TskCoreException;
38 import org.sleuthkit.datamodel.TskData;
39 
44 abstract class AddTagAction extends AbstractAction implements Presenter.Popup {
45 
46  private static final long serialVersionUID = 1L;
47  private static final String NO_COMMENT = "";
48 
49  AddTagAction(String menuText) {
50  super(menuText);
51  }
52 
53  @Override
54  public JMenuItem getPopupPresenter() {
55  return new TagMenu();
56  }
57 
64  @Override
65  @SuppressWarnings("NoopMethodInAbstractClass")
66  public void actionPerformed(ActionEvent event) {
67  }
68 
73  abstract protected String getActionDisplayName();
74 
79  abstract protected void addTag(TagName tagName, String comment);
80 
86  // @@@ This user interface has some significant usability issues and needs
87  // to be reworked.
88  private final class TagMenu extends JMenu {
89 
90  private static final long serialVersionUID = 1L;
91 
92  TagMenu() {
93  super(getActionDisplayName());
94 
95  // Get the current set of tag names.
96  Map<String, TagName> tagNamesMap = null;
97  List<String> standardTagNames = TagsManager.getStandardTagNames();
98  try {
100  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
101  } catch (TskCoreException | NoCurrentCaseException ex) {
102  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
103  }
104 
105  // Create a menu item for each of the existing and visible tags.
106  // Selecting one of these menu items adds a tag with the associated tag name.
107  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
108  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
109  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
110  String tagDisplayName = entry.getKey();
111  String notableString = entry.getValue().getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
112  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
113  // for the bookmark tag name only, added shortcut label
114  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
115  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
116  }
117 
118  tagNameItem.addActionListener((ActionEvent e) -> {
119  getAndAddTag(entry.getKey(), entry.getValue(), NO_COMMENT);
120  });
121 
122  // Show custom tags before predefined tags in the menu
123  if (standardTagNames.contains(tagDisplayName)) {
124  standardTagMenuitems.add(tagNameItem);
125  } else {
126  add(tagNameItem);
127  }
128  }
129  }
130 
131  if (getItemCount() > 0) {
132  addSeparator();
133  }
134 
135  standardTagMenuitems.forEach((menuItem) -> {
136  add(menuItem);
137  });
138 
139  addSeparator();
140 
141  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
142  // a dialog that can be used to create or select a tag name with an
143  // optional comment and adds a tag with the resulting name.
144  JMenuItem tagAndCommentItem = new JMenuItem(
145  NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
146  tagAndCommentItem.addActionListener((ActionEvent e) -> {
148  if (null != tagNameAndComment) {
149  addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
150  }
151  });
152  add(tagAndCommentItem);
153 
154  // Create a "New Tag..." menu item.
155  // Selecting this item initiates a dialog that can be used to create
156  // or select a tag name and adds a tag with the resulting name.
157  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
158  newTagMenuItem.addActionListener((ActionEvent e) -> {
159  TagName tagName = GetTagNameDialog.doDialog();
160  if (null != tagName) {
161  addTag(tagName, NO_COMMENT);
162  }
163  });
164  add(newTagMenuItem);
165 
166  }
167 
180  private void getAndAddTag(String tagDisplayName, TagName tagName, String comment) {
181  Case openCase;
182  try {
183  openCase = Case.getCurrentCaseThrows();
184  } catch (NoCurrentCaseException ex) {
185  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex); // NON-NLS
186  return;
187  }
188 
189  if (tagName == null) {
190  try {
191  tagName = openCase.getServices().getTagsManager().addTagName(tagDisplayName);
193  try {
194  tagName = openCase.getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
195  } catch (TskCoreException ex1) {
196  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " already exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
197  }
198  } catch (TskCoreException ex) {
199  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
200  }
201  }
202  addTag(tagName, comment);
203  }
204  }
205 }
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-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.