Autopsy  4.10.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
GetTagNameDialog.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.Window;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.KeyEvent;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.logging.Level;
29 import javax.swing.AbstractAction;
30 import javax.swing.ActionMap;
31 import javax.swing.InputMap;
32 import javax.swing.JComponent;
33 import javax.swing.JDialog;
34 import javax.swing.JOptionPane;
35 import javax.swing.KeyStroke;
36 import javax.swing.table.AbstractTableModel;
37 import org.openide.util.ImageUtilities;
38 import org.openide.util.NbBundle;
39 import org.openide.util.NbBundle.Messages;
40 import org.openide.windows.WindowManager;
45 import org.sleuthkit.datamodel.TagName;
46 import org.sleuthkit.datamodel.TskCoreException;
47 import org.sleuthkit.datamodel.TskData;
48 
52 @Messages({"GetTagNameDialog.descriptionLabel.text=Description:",
53  "GetTagNameDialog.notableCheckbox.text=Tag indicates item is notable."})
54 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
55 public class GetTagNameDialog extends JDialog {
56 
57  private static final long serialVersionUID = 1L;
58  private static final String TAG_ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
59  private final Map<String, TagName> tagNamesMap = new TreeMap<>();
60  private TagName tagName = null;
61 
70  public static TagName doDialog() {
71  return doDialog(WindowManager.getDefault().getMainWindow());
72  }
73 
84  public static TagName doDialog(Window owner) {
85  GetTagNameDialog dialog = new GetTagNameDialog(owner);
86  dialog.display();
87  return dialog.tagName;
88  }
89 
90  private GetTagNameDialog(Window owner) {
91  super(owner,
92  NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.createTag"),
93  ModalityType.APPLICATION_MODAL);
94  }
95 
96  private void display() {
97  setIconImage(ImageUtilities.loadImage(TAG_ICON_PATH));
98  initComponents();
99 
100  // Set up the dialog to close when Esc is pressed.
101  String cancelName = NbBundle.getMessage(this.getClass(), "GetTagNameDialog.cancelName");
102  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
103  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
104  ActionMap actionMap = getRootPane().getActionMap();
105  actionMap.put(cancelName, new AbstractAction() {
106  private static final long serialVersionUID = 1L;
107 
108  @Override
109  public void actionPerformed(ActionEvent e) {
110  cancelButtonActionPerformed(e);
111  }
112  });
113 
114  // Get the current set of tag names and hash them for a speedy lookup in
115  // case the user chooses an existing tag name from the tag names table.
116  try {
118  tagNamesMap.putAll(tagsManager.getDisplayNamesToTagNamesMap());
119  } catch (TskCoreException | NoCurrentCaseException ex) {
120  Logger.getLogger(GetTagNameDialog.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
121  }
122 
123  // Populate the tag names table.
124  tagsTable.setModel(new TagsTableModel(new ArrayList<>(tagNamesMap.keySet())));
125  tagsTable.setTableHeader(null);
126  tagsTable.setCellSelectionEnabled(false);
127  tagsTable.setFocusable(false);
128  tagsTable.setRowHeight(tagsTable.getRowHeight() + 5);
129 
130  // Center and show the dialog box.
131  this.setLocationRelativeTo(this.getOwner());
132  setVisible(true);
133  }
134 
135  private class TagsTableModel extends AbstractTableModel {
136 
137  private static final long serialVersionUID = 1L;
138  private final ArrayList<String> tagDisplayNames = new ArrayList<>();
139 
140  TagsTableModel(List<String> tagDisplayNames) {
141  for (String tagDisplayName : tagDisplayNames) {
142  this.tagDisplayNames.add(tagDisplayName);
143  }
144  }
145 
146  @Override
147  public int getRowCount() {
148  return tagDisplayNames.size();
149  }
150 
151  @Override
152  public boolean isCellEditable(int rowIndex, int columnIndex) {
153  return false;
154  }
155 
156  @Override
157  public int getColumnCount() {
158  return 1;
159  }
160 
161  @Override
162  public String getValueAt(int rowIndex, int columnIndex) {
163  return tagDisplayNames.get(rowIndex);
164  }
165  }
166 
172  @SuppressWarnings("unchecked")
173  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
174  private void initComponents() {
175 
176  cancelButton = new javax.swing.JButton();
177  okButton = new javax.swing.JButton();
178  jScrollPane1 = new javax.swing.JScrollPane();
179  tagsTable = new javax.swing.JTable();
180  preexistingLabel = new javax.swing.JLabel();
181  newTagPanel = new javax.swing.JPanel();
182  tagNameLabel = new javax.swing.JLabel();
183  tagNameField = new javax.swing.JTextField();
184  descriptionLabel = new javax.swing.JLabel();
185  descriptionScrollPane = new javax.swing.JScrollPane();
186  descriptionTextArea = new javax.swing.JTextArea();
187  notableCheckbox = new javax.swing.JCheckBox();
188 
189  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
190  addKeyListener(new java.awt.event.KeyAdapter() {
191  public void keyReleased(java.awt.event.KeyEvent evt) {
192  formKeyReleased(evt);
193  }
194  });
195 
196  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.cancelButton.text")); // NOI18N
197  cancelButton.addActionListener(new java.awt.event.ActionListener() {
198  public void actionPerformed(java.awt.event.ActionEvent evt) {
199  cancelButtonActionPerformed(evt);
200  }
201  });
202 
203  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.okButton.text")); // NOI18N
204  okButton.addActionListener(new java.awt.event.ActionListener() {
205  public void actionPerformed(java.awt.event.ActionEvent evt) {
206  okButtonActionPerformed(evt);
207  }
208  });
209 
210  jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
211 
212  tagsTable.setBackground(new java.awt.Color(240, 240, 240));
213  tagsTable.setModel(new javax.swing.table.DefaultTableModel(
214  new Object [][] {
215 
216  },
217  new String [] {
218 
219  }
220  ));
221  tagsTable.setOpaque(false);
222  tagsTable.setShowHorizontalLines(false);
223  tagsTable.setShowVerticalLines(false);
224  tagsTable.setTableHeader(null);
225  jScrollPane1.setViewportView(tagsTable);
226 
227  org.openide.awt.Mnemonics.setLocalizedText(preexistingLabel, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.preexistingLabel.text")); // NOI18N
228 
229  newTagPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.newTagPanel.border.title"))); // NOI18N
230 
231  org.openide.awt.Mnemonics.setLocalizedText(tagNameLabel, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.tagNameLabel.text")); // NOI18N
232 
233  tagNameField.setText(org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.tagNameField.text")); // NOI18N
234  tagNameField.addKeyListener(new java.awt.event.KeyAdapter() {
235  public void keyReleased(java.awt.event.KeyEvent evt) {
236  tagNameFieldKeyReleased(evt);
237  }
238  });
239 
240  org.openide.awt.Mnemonics.setLocalizedText(descriptionLabel, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.descriptionLabel.text")); // NOI18N
241 
242  descriptionTextArea.setColumns(20);
243  descriptionTextArea.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
244  descriptionTextArea.setRows(3);
245  descriptionScrollPane.setViewportView(descriptionTextArea);
246 
247  org.openide.awt.Mnemonics.setLocalizedText(notableCheckbox, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.notableCheckbox.text")); // NOI18N
248 
249  javax.swing.GroupLayout newTagPanelLayout = new javax.swing.GroupLayout(newTagPanel);
250  newTagPanel.setLayout(newTagPanelLayout);
251  newTagPanelLayout.setHorizontalGroup(
252  newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
253  .addGroup(newTagPanelLayout.createSequentialGroup()
254  .addContainerGap()
255  .addGroup(newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
256  .addComponent(descriptionScrollPane, javax.swing.GroupLayout.Alignment.TRAILING)
257  .addComponent(tagNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
258  .addGroup(newTagPanelLayout.createSequentialGroup()
259  .addGroup(newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
260  .addComponent(notableCheckbox)
261  .addComponent(descriptionLabel)
262  .addComponent(tagNameLabel))
263  .addGap(0, 0, Short.MAX_VALUE)))
264  .addContainerGap())
265  );
266  newTagPanelLayout.setVerticalGroup(
267  newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
268  .addGroup(newTagPanelLayout.createSequentialGroup()
269  .addGap(6, 6, 6)
270  .addComponent(tagNameLabel)
271  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
272  .addComponent(tagNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
273  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
274  .addComponent(descriptionLabel)
275  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
276  .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
277  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
278  .addComponent(notableCheckbox)
279  .addContainerGap(31, Short.MAX_VALUE))
280  );
281 
282  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
283  getContentPane().setLayout(layout);
284  layout.setHorizontalGroup(
285  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
286  .addGroup(layout.createSequentialGroup()
287  .addContainerGap()
288  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
289  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
290  .addComponent(preexistingLabel))
291  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
292  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addGroup(layout.createSequentialGroup()
294  .addGap(0, 233, Short.MAX_VALUE)
295  .addComponent(okButton)
296  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
297  .addComponent(cancelButton))
298  .addComponent(newTagPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
299  .addContainerGap())
300  );
301  layout.setVerticalGroup(
302  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
303  .addGroup(layout.createSequentialGroup()
304  .addContainerGap()
305  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
306  .addGroup(layout.createSequentialGroup()
307  .addComponent(preexistingLabel)
308  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
309  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
310  .addComponent(newTagPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
311  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
312  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
313  .addComponent(cancelButton)
314  .addComponent(okButton))
315  .addContainerGap())
316  );
317 
318  pack();
319  }// </editor-fold>//GEN-END:initComponents
320 
321  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
322  tagName = null;
323  dispose();
324  }//GEN-LAST:event_cancelButtonActionPerformed
325 
326  @NbBundle.Messages({"GetTagNameDialog.tagNameAlreadyExists.message=Tag name must be unique. A tag with this name already exists.",
327  "GetTagNameDialog.tagNameAlreadyExists.title=Duplicate Tag Name",
328  "GetTagNameDialog.tagDescriptionIllegalCharacters.message=Tag descriptions may not contain commas (,) or semicolons (;)",
329  "GetTagNameDialog.tagDescriptionIllegalCharacters.title=Invalid character in tag description"})
330  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
331  String tagDisplayName = tagNameField.getText();
332  String userTagDescription = descriptionTextArea.getText();
333  TskData.FileKnown status = notableCheckbox.isSelected() ? TskData.FileKnown.BAD : TskData.FileKnown.UNKNOWN;
334  if (tagDisplayName.isEmpty()) {
335  JOptionPane.showMessageDialog(this,
336  NbBundle.getMessage(this.getClass(),
337  "GetTagNameDialog.mustSupplyTtagName.msg"),
338  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagNameErr"),
339  JOptionPane.ERROR_MESSAGE);
340  } else if (TagsManager.containsIllegalCharacters(tagDisplayName)) {
341  JOptionPane.showMessageDialog(this,
342  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.illegalChars.msg"),
343  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.illegalCharsErr"),
344  JOptionPane.ERROR_MESSAGE);
345  } else if (userTagDescription.contains(",")
346  || userTagDescription.contains(";")) {
347  JOptionPane.showMessageDialog(this,
348  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagDescriptionIllegalCharacters.message"),
349  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagDescriptionIllegalCharacters.title"),
350  JOptionPane.ERROR_MESSAGE);
351  } else {
352  tagName = tagNamesMap.get(tagDisplayName);
353 
354  if (tagName == null) {
355  try {
356  tagName = Case.getCurrentCaseThrows().getServices().getTagsManager().addTagName(tagDisplayName, userTagDescription, TagName.HTML_COLOR.NONE, status);
357  dispose();
358  } catch (TskCoreException | NoCurrentCaseException ex) {
359  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
360  JOptionPane.showMessageDialog(this,
361  NbBundle.getMessage(this.getClass(),
362  "GetTagNameDialog.unableToAddTagNameToCase.msg",
363  tagDisplayName),
364  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.taggingErr"),
365  JOptionPane.ERROR_MESSAGE);
366  tagName = null;
368  try {
370  } catch (TskCoreException | NoCurrentCaseException ex1) {
371  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
372  JOptionPane.showMessageDialog(this,
373  NbBundle.getMessage(this.getClass(),
374  "GetTagNameDialog.tagNameExistsTskCore.msg",
375  tagDisplayName),
376  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.dupTagErr"),
377  JOptionPane.ERROR_MESSAGE);
378  tagName = null;
379  }
380  }
381  } else {
382  JOptionPane.showMessageDialog(this,
383  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagNameAlreadyExists.message"),
384  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagNameAlreadyExists.title"),
385  JOptionPane.INFORMATION_MESSAGE);
386  }
387  }
388  }//GEN-LAST:event_okButtonActionPerformed
389 
390  private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
391  if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
392  okButtonActionPerformed(null);
393  }
394  }//GEN-LAST:event_formKeyReleased
395 
396  private void tagNameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tagNameFieldKeyReleased
397  if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
398  okButtonActionPerformed(null);
399  }
400  }//GEN-LAST:event_tagNameFieldKeyReleased
401 
402  // Variables declaration - do not modify//GEN-BEGIN:variables
403  private javax.swing.JButton cancelButton;
404  private javax.swing.JLabel descriptionLabel;
405  private javax.swing.JScrollPane descriptionScrollPane;
406  private javax.swing.JTextArea descriptionTextArea;
407  private javax.swing.JScrollPane jScrollPane1;
408  private javax.swing.JPanel newTagPanel;
409  private javax.swing.JCheckBox notableCheckbox;
410  private javax.swing.JButton okButton;
411  private javax.swing.JLabel preexistingLabel;
412  private javax.swing.JTextField tagNameField;
413  private javax.swing.JLabel tagNameLabel;
414  private javax.swing.JTable tagsTable;
415  // End of variables declaration//GEN-END:variables
416 
417 }
void tagNameFieldKeyReleased(java.awt.event.KeyEvent evt)
synchronized TagName addTagName(String displayName)
void okButtonActionPerformed(java.awt.event.ActionEvent evt)
static boolean containsIllegalCharacters(String tagDisplayName)
void formKeyReleased(java.awt.event.KeyEvent evt)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.