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