Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
GetTagNameAndCommentDialog.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.Window;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.KeyEvent;
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.ActionMap;
29 import javax.swing.InputMap;
30 import javax.swing.JComponent;
31 import javax.swing.JDialog;
32 import javax.swing.KeyStroke;
33 import org.openide.util.NbBundle;
34 import org.openide.windows.WindowManager;
38 import org.sleuthkit.datamodel.TagName;
39 import org.sleuthkit.datamodel.TskCoreException;
40 
41 public class GetTagNameAndCommentDialog extends JDialog {
42 
43  private static final long serialVersionUID = 1L;
44  private static final String NO_TAG_NAMES_MESSAGE = NbBundle.getMessage(GetTagNameAndCommentDialog.class,
45  "GetTagNameAndCommentDialog.noTags");
46  private final Map<String, TagName> tagNamesMap = new TreeMap<>();
48 
49  public static class TagNameAndComment {
50 
51  private final TagName tagName;
52  private final String comment;
53 
54  private TagNameAndComment(TagName tagName, String comment) {
55  this.tagName = tagName;
56  this.comment = comment;
57  }
58 
59  public TagName getTagName() {
60  return tagName;
61  }
62 
63  public String getComment() {
64  return comment;
65  }
66  }
67 
77  public static TagNameAndComment doDialog() {
78  return doDialog(WindowManager.getDefault().getMainWindow());
79  }
80 
93  public static TagNameAndComment doDialog(Window owner) {
95  dialog.display();
96  return dialog.tagNameAndComment;
97  }
98 
99  private GetTagNameAndCommentDialog(Window owner) {
100  super(owner,
101  NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.createTag"),
102  ModalityType.APPLICATION_MODAL);
103  }
104 
105  private void display() {
106  initComponents();
107 
108  // Set up the dialog to close when Esc is pressed.
109  String cancelName = NbBundle.getMessage(this.getClass(), "GetTagNameAndCommentDialog.cancelName");
110  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
111  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
112  ActionMap actionMap = getRootPane().getActionMap();
113  actionMap.put(cancelName, new AbstractAction() {
114  private static final long serialVersionUID = 1L;
115  @Override
116  public void actionPerformed(ActionEvent e) {
117  dispose();
118  }
119  });
120 
121  // Populate the combo box with the available tag names and save the
122  // tag name DTOs to be enable to return the one the user selects.
123  // Tag name DTOs may be null (user tag names that have not been used do
124  // not exist in the database).
126  try {
127  tagNamesMap.putAll(tagsManager.getDisplayNamesToTagNamesMap());
128  } catch (TskCoreException ex) {
129  Logger.getLogger(GetTagNameAndCommentDialog.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
130  }
131  if (null != tagNamesMap && tagNamesMap.isEmpty()) {
132  tagCombo.addItem(NO_TAG_NAMES_MESSAGE);
133  } else {
134  for (String tagDisplayName : tagNamesMap.keySet()) {
135  tagCombo.addItem(tagDisplayName);
136  }
137  }
138 
139  // Center and show the dialog box.
140  this.setLocationRelativeTo(this.getOwner());
141  setVisible(true);
142  }
143 
149  @SuppressWarnings("unchecked")
150  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
151  private void initComponents() {
152 
153  okButton = new javax.swing.JButton();
154  cancelButton = new javax.swing.JButton();
155  tagCombo = new javax.swing.JComboBox<String>();
156  tagLabel = new javax.swing.JLabel();
157  commentLabel = new javax.swing.JLabel();
158  commentText = new javax.swing.JTextField();
159  newTagButton = new javax.swing.JButton();
160 
161  addWindowListener(new java.awt.event.WindowAdapter() {
162  public void windowClosing(java.awt.event.WindowEvent evt) {
163  closeDialog(evt);
164  }
165  });
166 
167  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.okButton.text")); // NOI18N
168  okButton.addActionListener(new java.awt.event.ActionListener() {
169  public void actionPerformed(java.awt.event.ActionEvent evt) {
171  }
172  });
173 
174  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.cancelButton.text")); // NOI18N
175  cancelButton.addActionListener(new java.awt.event.ActionListener() {
176  public void actionPerformed(java.awt.event.ActionEvent evt) {
178  }
179  });
180 
181  tagCombo.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagCombo.toolTipText")); // NOI18N
182 
183  org.openide.awt.Mnemonics.setLocalizedText(tagLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagLabel.text")); // NOI18N
184 
185  org.openide.awt.Mnemonics.setLocalizedText(commentLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentLabel.text")); // NOI18N
186 
187  commentText.setText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.text")); // NOI18N
188  commentText.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.toolTipText")); // NOI18N
189 
190  org.openide.awt.Mnemonics.setLocalizedText(newTagButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.newTagButton.text")); // NOI18N
191  newTagButton.addActionListener(new java.awt.event.ActionListener() {
192  public void actionPerformed(java.awt.event.ActionEvent evt) {
194  }
195  });
196 
197  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
198  getContentPane().setLayout(layout);
199  layout.setHorizontalGroup(
200  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
201  .addGroup(layout.createSequentialGroup()
202  .addContainerGap()
203  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
204  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
205  .addComponent(newTagButton)
206  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
207  .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
208  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
209  .addComponent(cancelButton))
210  .addGroup(layout.createSequentialGroup()
211  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
212  .addComponent(commentLabel)
213  .addComponent(tagLabel))
214  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
215  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
216  .addComponent(commentText)
217  .addComponent(tagCombo, 0, 214, Short.MAX_VALUE))
218  .addGap(0, 0, Short.MAX_VALUE)))
219  .addContainerGap())
220  );
221 
222  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
223 
224  layout.setVerticalGroup(
225  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
226  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
227  .addGap(24, 24, 24)
228  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
229  .addComponent(tagCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
230  .addComponent(tagLabel))
231  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
232  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
233  .addComponent(commentLabel)
234  .addComponent(commentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
235  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
236  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
237  .addComponent(cancelButton)
238  .addComponent(okButton)
239  .addComponent(newTagButton))
240  .addContainerGap())
241  );
242 
243  getRootPane().setDefaultButton(okButton);
244 
245  pack();
246  }// </editor-fold>//GEN-END:initComponents
247 
248  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
249  String tagDisplayName = (String) tagCombo.getSelectedItem();
250  TagName tagNameFromCombo = tagNamesMap.get(tagDisplayName);
251  if (tagNameFromCombo == null) {
252  try {
253  tagNameFromCombo = Case.getCurrentCase().getServices().getTagsManager().addTagName(tagDisplayName);
255  try {
256  tagNameFromCombo = Case.getCurrentCase().getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
257  } catch (TskCoreException ex1) {
258  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " already exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
259  }
260  } catch (TskCoreException ex) {
261  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
262  }
263  }
264  tagNameAndComment = new TagNameAndComment(tagNameFromCombo, commentText.getText());
265  dispose();
266  }//GEN-LAST:event_okButtonActionPerformed
267 
268  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
269  tagNameAndComment = null;
270  dispose();
271  }//GEN-LAST:event_cancelButtonActionPerformed
272 
273  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
274  tagNameAndComment = null;
275  dispose();
276  }//GEN-LAST:event_closeDialog
277 
278  private void newTagButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTagButtonActionPerformed
279  TagName newTagName = GetTagNameDialog.doDialog(this);
280  if (newTagName != null) {
281  tagNamesMap.put(newTagName.getDisplayName(), newTagName);
282  tagCombo.addItem(newTagName.getDisplayName());
283  tagCombo.setSelectedItem(newTagName.getDisplayName());
284  }
285  }//GEN-LAST:event_newTagButtonActionPerformed
286 
287  // Variables declaration - do not modify//GEN-BEGIN:variables
288  private javax.swing.JButton cancelButton;
289  private javax.swing.JLabel commentLabel;
290  private javax.swing.JTextField commentText;
291  private javax.swing.JButton newTagButton;
292  private javax.swing.JButton okButton;
293  private javax.swing.JComboBox<String> tagCombo;
294  private javax.swing.JLabel tagLabel;
295  // End of variables declaration//GEN-END:variables
296 }
synchronized TagName addTagName(String displayName)
synchronized Map< String, TagName > getDisplayNamesToTagNamesMap()
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

Copyright © 2012-2016 Basis Technology. Generated on: Tue Oct 25 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.