Autopsy  4.4.1
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-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.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.windows.WindowManager;
43 import org.sleuthkit.datamodel.TagName;
44 import org.sleuthkit.datamodel.TskCoreException;
45 
46 public class GetTagNameDialog extends JDialog {
47 
48  private static final long serialVersionUID = 1L;
49  private static final String TAG_ICON_PATH = "org/sleuthkit/autopsy/images/tag-folder-blue-icon-16.png"; //NON-NLS
50  private final Map<String, TagName> tagNamesMap = new TreeMap<>();
51  private TagName tagName = null;
52 
61  public static TagName doDialog() {
62  return doDialog(WindowManager.getDefault().getMainWindow());
63  }
64 
75  public static TagName doDialog(Window owner) {
76  GetTagNameDialog dialog = new GetTagNameDialog(owner);
77  dialog.display();
78  return dialog.tagName;
79  }
80 
81  private GetTagNameDialog(Window owner) {
82  super(owner,
83  NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.createTag"),
84  ModalityType.APPLICATION_MODAL);
85  }
86 
87  private void display() {
88  setIconImage(ImageUtilities.loadImage(TAG_ICON_PATH));
90 
91  // Set up the dialog to close when Esc is pressed.
92  String cancelName = NbBundle.getMessage(this.getClass(), "GetTagNameDialog.cancelName");
93  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
94  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
95  ActionMap actionMap = getRootPane().getActionMap();
96  actionMap.put(cancelName, new AbstractAction() {
97  private static final long serialVersionUID = 1L;
98 
99  @Override
100  public void actionPerformed(ActionEvent e) {
102  }
103  });
104 
105  // Get the current set of tag names and hash them for a speedy lookup in
106  // case the user chooses an existing tag name from the tag names table.
108  try {
109  tagNamesMap.putAll(tagsManager.getDisplayNamesToTagNamesMap());
110  } catch (TskCoreException ex) {
111  Logger.getLogger(GetTagNameDialog.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
112  }
113 
114  // Populate the tag names table.
115  tagsTable.setModel(new TagsTableModel(new ArrayList<>(tagNamesMap.keySet())));
116  tagsTable.setTableHeader(null);
117  tagsTable.setCellSelectionEnabled(false);
118  tagsTable.setFocusable(false);
119  tagsTable.setRowHeight(tagsTable.getRowHeight() + 5);
120 
121  // Center and show the dialog box.
122  this.setLocationRelativeTo(this.getOwner());
123  setVisible(true);
124  }
125 
126  private class TagsTableModel extends AbstractTableModel {
127 
128  private static final long serialVersionUID = 1L;
129  private final ArrayList<String> tagDisplayNames = new ArrayList<>();
130 
131  TagsTableModel(List<String> tagDisplayNames) {
132  for (String tagDisplayName : tagDisplayNames) {
133  this.tagDisplayNames.add(tagDisplayName);
134  }
135  }
136 
137  @Override
138  public int getRowCount() {
139  return tagDisplayNames.size();
140  }
141 
142  @Override
143  public boolean isCellEditable(int rowIndex, int columnIndex) {
144  return false;
145  }
146 
147  @Override
148  public int getColumnCount() {
149  return 1;
150  }
151 
152  @Override
153  public String getValueAt(int rowIndex, int columnIndex) {
154  return tagDisplayNames.get(rowIndex);
155  }
156  }
157 
163  @SuppressWarnings("unchecked")
164  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
165  private void initComponents() {
166 
167  cancelButton = new javax.swing.JButton();
168  okButton = new javax.swing.JButton();
169  jScrollPane1 = new javax.swing.JScrollPane();
170  tagsTable = new javax.swing.JTable();
171  preexistingLabel = new javax.swing.JLabel();
172  newTagPanel = new javax.swing.JPanel();
173  tagNameLabel = new javax.swing.JLabel();
174  tagNameField = new javax.swing.JTextField();
175 
176  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
177  addKeyListener(new java.awt.event.KeyAdapter() {
178  public void keyReleased(java.awt.event.KeyEvent evt) {
179  formKeyReleased(evt);
180  }
181  });
182 
183  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.cancelButton.text")); // NOI18N
184  cancelButton.addActionListener(new java.awt.event.ActionListener() {
185  public void actionPerformed(java.awt.event.ActionEvent evt) {
187  }
188  });
189 
190  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.okButton.text")); // NOI18N
191  okButton.addActionListener(new java.awt.event.ActionListener() {
192  public void actionPerformed(java.awt.event.ActionEvent evt) {
194  }
195  });
196 
197  jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
198 
199  tagsTable.setBackground(new java.awt.Color(240, 240, 240));
200  tagsTable.setModel(new javax.swing.table.DefaultTableModel(
201  new Object [][] {
202 
203  },
204  new String [] {
205 
206  }
207  ));
208  tagsTable.setShowHorizontalLines(false);
209  tagsTable.setShowVerticalLines(false);
210  tagsTable.setTableHeader(null);
211  jScrollPane1.setViewportView(tagsTable);
212 
213  org.openide.awt.Mnemonics.setLocalizedText(preexistingLabel, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.preexistingLabel.text")); // NOI18N
214 
215  newTagPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.newTagPanel.border.title"))); // NOI18N
216 
217  org.openide.awt.Mnemonics.setLocalizedText(tagNameLabel, org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.tagNameLabel.text")); // NOI18N
218 
219  tagNameField.setText(org.openide.util.NbBundle.getMessage(GetTagNameDialog.class, "GetTagNameDialog.tagNameField.text")); // NOI18N
220  tagNameField.addKeyListener(new java.awt.event.KeyAdapter() {
221  public void keyReleased(java.awt.event.KeyEvent evt) {
223  }
224  });
225 
226  javax.swing.GroupLayout newTagPanelLayout = new javax.swing.GroupLayout(newTagPanel);
227  newTagPanel.setLayout(newTagPanelLayout);
228  newTagPanelLayout.setHorizontalGroup(
229  newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
230  .addGroup(newTagPanelLayout.createSequentialGroup()
231  .addContainerGap()
232  .addComponent(tagNameLabel)
233  .addGap(36, 36, 36)
234  .addComponent(tagNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
235  .addContainerGap())
236  );
237  newTagPanelLayout.setVerticalGroup(
238  newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239  .addGroup(newTagPanelLayout.createSequentialGroup()
240  .addContainerGap()
241  .addGroup(newTagPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
242  .addComponent(tagNameLabel)
243  .addComponent(tagNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
244  .addContainerGap(164, Short.MAX_VALUE))
245  );
246 
247  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
248  getContentPane().setLayout(layout);
249  layout.setHorizontalGroup(
250  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251  .addGroup(layout.createSequentialGroup()
252  .addContainerGap()
253  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
254  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
255  .addComponent(preexistingLabel))
256  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
257  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
258  .addGroup(layout.createSequentialGroup()
259  .addGap(0, 233, Short.MAX_VALUE)
260  .addComponent(okButton)
261  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
262  .addComponent(cancelButton))
263  .addComponent(newTagPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
264  .addContainerGap())
265  );
266  layout.setVerticalGroup(
267  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
268  .addGroup(layout.createSequentialGroup()
269  .addContainerGap()
270  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
271  .addGroup(layout.createSequentialGroup()
272  .addComponent(preexistingLabel)
273  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
274  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
275  .addComponent(newTagPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
276  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
277  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
278  .addComponent(cancelButton)
279  .addComponent(okButton))
280  .addContainerGap())
281  );
282 
283  pack();
284  }// </editor-fold>//GEN-END:initComponents
285 
286  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
287  tagName = null;
288  dispose();
289  }//GEN-LAST:event_cancelButtonActionPerformed
290 
291  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
292  String tagDisplayName = tagNameField.getText();
293  if (tagDisplayName.isEmpty()) {
294  JOptionPane.showMessageDialog(null,
295  NbBundle.getMessage(this.getClass(),
296  "GetTagNameDialog.mustSupplyTtagName.msg"),
297  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.tagNameErr"),
298  JOptionPane.ERROR_MESSAGE);
299  } else if (TagsManager.containsIllegalCharacters(tagDisplayName)) {
300  JOptionPane.showMessageDialog(null,
301  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.illegalChars.msg"),
302  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.illegalCharsErr"),
303  JOptionPane.ERROR_MESSAGE);
304  } else {
305  tagName = tagNamesMap.get(tagDisplayName);
306  if (tagName == null) {
307  try {
308  tagName = Case.getCurrentCase().getServices().getTagsManager().addTagName(tagDisplayName);
309  dispose();
310  } catch (TskCoreException ex) {
311  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, "Error adding " + tagDisplayName + " tag name", ex); //NON-NLS
312  JOptionPane.showMessageDialog(null,
313  NbBundle.getMessage(this.getClass(),
314  "GetTagNameDialog.unableToAddTagNameToCase.msg",
315  tagDisplayName),
316  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.taggingErr"),
317  JOptionPane.ERROR_MESSAGE);
318  tagName = null;
320  try {
321  tagName = Case.getCurrentCase().getServices().getTagsManager().getDisplayNamesToTagNamesMap().get(tagDisplayName);
322  } catch (TskCoreException ex1) {
323  Logger.getLogger(AddTagAction.class.getName()).log(Level.SEVERE, tagDisplayName + " exists in database but an error occurred in retrieving it.", ex1); //NON-NLS
324  JOptionPane.showMessageDialog(null,
325  NbBundle.getMessage(this.getClass(),
326  "GetTagNameDialog.tagNameExistsTskCore.msg",
327  tagDisplayName),
328  NbBundle.getMessage(this.getClass(), "GetTagNameDialog.dupTagErr"),
329  JOptionPane.ERROR_MESSAGE);
330  tagName = null;
331  }
332  }
333  } else {
334  dispose();
335  }
336  }
337  }//GEN-LAST:event_okButtonActionPerformed
338 
339  private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
340  if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
342  }
343  }//GEN-LAST:event_formKeyReleased
344 
345  private void tagNameFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tagNameFieldKeyReleased
346  if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
348  }
349  }//GEN-LAST:event_tagNameFieldKeyReleased
350 
351  // Variables declaration - do not modify//GEN-BEGIN:variables
352  private javax.swing.JButton cancelButton;
353  private javax.swing.JScrollPane jScrollPane1;
354  private javax.swing.JPanel newTagPanel;
355  private javax.swing.JButton okButton;
356  private javax.swing.JLabel preexistingLabel;
357  private javax.swing.JTextField tagNameField;
358  private javax.swing.JLabel tagNameLabel;
359  private javax.swing.JTable tagsTable;
360  // End of variables declaration//GEN-END:variables
361 
362 }
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:161
void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)

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