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

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