Autopsy  4.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-2016 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;
35 
40 abstract class AddTagAction extends AbstractAction implements Presenter.Popup {
41 
42  private static final long serialVersionUID = 1L;
43  private static final String NO_COMMENT = "";
44 
45  AddTagAction(String menuText) {
46  super(menuText);
47  }
48 
49  @Override
50  public JMenuItem getPopupPresenter() {
51  return new TagMenu();
52  }
53 
60  @Override
61  @SuppressWarnings("NoopMethodInAbstractClass")
62  public void actionPerformed(ActionEvent event) {
63  }
64 
69  abstract protected String getActionDisplayName();
70 
75  abstract protected void addTag(TagName tagName, String comment);
76 
82  // @@@ This user interface has some significant usability issues and needs
83  // to be reworked.
84  private class TagMenu extends JMenu {
85 
86  private static final long serialVersionUID = 1L;
87 
88  TagMenu() {
89  super(getActionDisplayName());
90 
91  // Get the current set of tag names.
93  Map<String, TagName> tagNamesMap = null;
94  try {
95  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
96  } catch (TskCoreException ex) {
97  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
98  }
99 
100  // Create a "Quick Tag" sub-menu.
101  JMenu quickTagMenu = new JMenu(NbBundle.getMessage(this.getClass(), "AddTagAction.quickTag"));
102  add(quickTagMenu);
103 
104  // Each tag name in the current set of tags gets its own menu item in
105  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
106  // a tag with the associated tag name.
107  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
108  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
109  String tagDisplayName = entry.getKey();
110  JMenuItem tagNameItem = new JMenuItem(tagDisplayName);
111  // for the bookmark tag name only, added shortcut label
112  if (tagDisplayName.equals(NbBundle.getMessage(AddTagAction.class, "AddBookmarkTagAction.bookmark.text"))) {
113  tagNameItem.setAccelerator(AddBookmarkTagAction.BOOKMARK_SHORTCUT);
114  }
115 
116  tagNameItem.addActionListener((ActionEvent e) -> {
117  getAndAddTag(entry.getKey(), entry.getValue(), NO_COMMENT);
118  });
119  quickTagMenu.add(tagNameItem);
120  }
121  } else {
122  JMenuItem empty = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.noTags"));
123  empty.setEnabled(false);
124  quickTagMenu.add(empty);
125  }
126 
127  quickTagMenu.addSeparator();
128 
129  // The "Quick Tag" menu also gets an "Choose Tag..." menu item.
130  // Selecting this item initiates a dialog that can be used to create
131  // or select a tag name and adds a tag with the resulting name.
132  JMenuItem newTagMenuItem = new JMenuItem(NbBundle.getMessage(this.getClass(), "AddTagAction.newTag"));
133  newTagMenuItem.addActionListener((ActionEvent e) -> {
134  TagName tagName = GetTagNameDialog.doDialog();
135  if (null != tagName) {
136  addTag(tagName, NO_COMMENT);
137  }
138  });
139  quickTagMenu.add(newTagMenuItem);
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 
167  private void getAndAddTag(String tagDisplayName, TagName tagName, String comment) {
168  if (tagName == null) {
169  try {
170  tagName = Case.getCurrentCase().getServices().getTagsManager().addTagName(tagDisplayName);
172  try {
173  tagName = Case.getCurrentCase().getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
174  } catch (TskCoreException ex1) {
175  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " already exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
176  }
177  } catch (TskCoreException ex) {
178  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
179  }
180  }
181  addTag(tagName, comment);
182  }
183  }
184 }
synchronized TagName addTagName(String displayName)
synchronized Map< String, TagName > getDisplayNamesToTagNamesMap()
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
void getAndAddTag(String tagDisplayName, TagName tagName, String comment)

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.