Autopsy  4.6.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-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.Map;
23 import java.util.TreeMap;
24 import java.util.logging.Level;
25 import javax.swing.AbstractAction;
26 import javax.swing.JMenu;
27 import javax.swing.JMenuItem;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.Presenter;
34 import org.sleuthkit.datamodel.TagName;
35 import org.sleuthkit.datamodel.TskCoreException;
36 import org.sleuthkit.datamodel.TskData;
37 
42 abstract class AddTagAction extends AbstractAction implements Presenter.Popup {
43 
44  private static final long serialVersionUID = 1L;
45  private static final String NO_COMMENT = "";
46 
47  AddTagAction(String menuText) {
48  super(menuText);
49  }
50 
51  @Override
52  public JMenuItem getPopupPresenter() {
53  return new TagMenu();
54  }
55 
62  @Override
63  @SuppressWarnings("NoopMethodInAbstractClass")
64  public void actionPerformed(ActionEvent event) {
65  }
66 
71  abstract protected String getActionDisplayName();
72 
77  abstract protected void addTag(TagName tagName, String comment);
78 
84  // @@@ This user interface has some significant usability issues and needs
85  // to be reworked.
86  private class TagMenu extends JMenu {
87 
88  private static final long serialVersionUID = 1L;
89 
90  TagMenu() {
91  super(getActionDisplayName());
92 
93  // Get the current set of tag names.
94  Map<String, TagName> tagNamesMap = null;
95  try {
97  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
98  } catch (TskCoreException | NoCurrentCaseException ex) {
99  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
100  }
101 
102  // Create a "Quick Tag" sub-menu.
103  JMenu quickTagMenu = new JMenu(NbBundle.getMessage(this.getClass(), "AddTagAction.quickTag"));
104  add(quickTagMenu);
105 
106  // Each tag name in the current set of tags gets its own menu item in
107  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
108  // a tag with the associated tag name.
109  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
110  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
111  String tagDisplayName = entry.getKey();
112  String notableString = entry.getValue().getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
113  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
114  // for the bookmark tag name only, added shortcut label
115  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
116  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
117  }
118 
119  tagNameItem.addActionListener((ActionEvent e) -> {
120  getAndAddTag(entry.getKey(), entry.getValue(), NO_COMMENT);
121  });
122  quickTagMenu.add(tagNameItem);
123  }
124  } else {
125  JMenuItem empty = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.noTags"));
126  empty.setEnabled(false);
127  quickTagMenu.add(empty);
128  }
129 
130  quickTagMenu.addSeparator();
131 
132  // The "Quick Tag" menu also gets an "Choose Tag..." menu item.
133  // Selecting this item initiates a dialog that can be used to create
134  // or select a tag name and adds a tag with the resulting name.
135  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
136  newTagMenuItem.addActionListener((ActionEvent e) -> {
137  TagName tagName = GetTagNameDialog.doDialog();
138  if (null != tagName) {
139  addTag(tagName, NO_COMMENT);
140  }
141  });
142  quickTagMenu.add(newTagMenuItem);
143 
144  // Create a "Choose Tag and Comment..." menu item. Selecting this item initiates
145  // a dialog that can be used to create or select a tag name with an
146  // optional comment and adds a tag with the resulting name.
147  JMenuItem tagAndCommentItem = new JMenuItem(
148  NbBundle.getMessage(this.getClass(), "AddTagAction.tagAndComment"));
149  tagAndCommentItem.addActionListener((ActionEvent e) -> {
151  if (null != tagNameAndComment) {
152  addTag(tagNameAndComment.getTagName(), tagNameAndComment.getComment());
153  }
154  });
155  add(tagAndCommentItem);
156  }
157 
170  private void getAndAddTag(String tagDisplayName, TagName tagName, String comment) {
171  Case openCase;
172  try {
173  openCase = Case.getOpenCase();
174  } catch (NoCurrentCaseException ex) {
175  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex); // NON-NLS
176  return;
177  }
178 
179  if (tagName == null) {
180  try {
181  tagName = openCase.getServices().getTagsManager().addTagName(tagDisplayName);
183  try {
184  tagName = openCase.getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
185  } catch (TskCoreException ex1) {
186  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " already exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
187  }
188  } catch (TskCoreException ex) {
189  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
190  }
191  }
192  addTag(tagName, comment);
193  }
194  }
195 }
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-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.