Autopsy  4.8.0
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-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.Component;
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.logging.Level;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.TreeMap;
30 import javax.swing.AbstractAction;
31 import javax.swing.ActionMap;
32 import javax.swing.DefaultListCellRenderer;
33 import javax.swing.InputMap;
34 import javax.swing.JComponent;
35 import javax.swing.JDialog;
36 import javax.swing.JList;
37 import javax.swing.KeyStroke;
38 import org.openide.util.NbBundle;
39 import org.openide.windows.WindowManager;
44 import org.sleuthkit.datamodel.TagName;
45 import org.sleuthkit.datamodel.TskCoreException;
46 import org.sleuthkit.datamodel.TskData;
47 
51 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
52 public class GetTagNameAndCommentDialog extends JDialog {
53 
54  private static final long serialVersionUID = 1L;
55  private final List<TagName> tagNamesList = new ArrayList<>();
56  private final List<TagName> standardTagNamesList = new ArrayList<>();
57  private TagNameAndComment tagNameAndComment = null;
58 
59  public static class TagNameAndComment {
60 
61  private final TagName tagName;
62  private final String comment;
63 
64  private TagNameAndComment(TagName tagName, String comment) {
65  this.tagName = tagName;
66  this.comment = comment;
67  }
68 
69  public TagName getTagName() {
70  return tagName;
71  }
72 
73  public String getComment() {
74  return comment;
75  }
76  }
77 
87  public static TagNameAndComment doDialog() {
88  return doDialog(WindowManager.getDefault().getMainWindow());
89  }
90 
103  public static TagNameAndComment doDialog(Window owner) {
105  dialog.display();
106  return dialog.getTagNameAndComment();
107  }
108 
115  return tagNameAndComment;
116  }
117 
118  private GetTagNameAndCommentDialog(Window owner) {
119  super(owner,
120  NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.selectTag"),
121  ModalityType.APPLICATION_MODAL);
122  }
123 
124  private void display() {
125  initComponents();
126  tagCombo.setRenderer(new DefaultListCellRenderer() {
127  private static final long serialVersionUID = 1L;
128 
129  @Override
130  public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
131  String status = ((TagName) value).getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
132  String newValue = ((TagName) value).getDisplayName() + status;
133  return super.getListCellRendererComponent(list, newValue, index, isSelected, cellHasFocus);
134  }
135  });
136  // Set up the dialog to close when Esc is pressed.
137  String cancelName = NbBundle.getMessage(this.getClass(), "GetTagNameAndCommentDialog.cancelName");
138  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
139 
140  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
141  ActionMap actionMap = getRootPane().getActionMap();
142 
143  actionMap.put(cancelName, new AbstractAction() {
144  private static final long serialVersionUID = 1L;
145 
146  @Override
147  public void actionPerformed(ActionEvent e) {
148  dispose();
149  }
150  }
151  );
152 
153  // Populate the combo box with the available tag names and save the
154  // tag name DTOs to be enable to return the one the user selects.
155  // Tag name DTOs may be null (user tag names that have not been used do
156  // not exist in the database).
157  try {
159  List<String> standardTagNames = TagsManager.getStandardTagNames();
160  Map<String, TagName> tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
161 
162  tagNamesMap.entrySet().stream().map((entry) -> entry.getValue()).forEachOrdered((tagName) -> {
163  if (standardTagNames.contains(tagName.getDisplayName())) {
164  standardTagNamesList.add(tagName);
165  } else {
166  tagNamesList.add(tagName);
167  }
168  });
169 
170  } catch (TskCoreException | NoCurrentCaseException ex) {
172  .getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
173  }
174  tagNamesList.forEach((tag) -> {
175  tagCombo.addItem(tag);
176  });
177 
178  standardTagNamesList.forEach((tag) -> {
179  tagCombo.addItem(tag);
180  });
181 
182  // Center and show the dialog box.
183  this.setLocationRelativeTo(this.getOwner());
184  setVisible(true);
185  }
186 
192  @SuppressWarnings("unchecked")
193  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
194  private void initComponents() {
195 
196  okButton = new javax.swing.JButton();
197  cancelButton = new javax.swing.JButton();
198  tagCombo = new javax.swing.JComboBox<TagName>();
199  tagLabel = new javax.swing.JLabel();
200  commentLabel = new javax.swing.JLabel();
201  newTagButton = new javax.swing.JButton();
202  jScrollPane1 = new javax.swing.JScrollPane();
203  commentText = new javax.swing.JTextArea();
204 
205  addWindowListener(new java.awt.event.WindowAdapter() {
206  public void windowClosing(java.awt.event.WindowEvent evt) {
207  closeDialog(evt);
208  }
209  });
210 
211  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.okButton.text")); // NOI18N
212  okButton.addActionListener(new java.awt.event.ActionListener() {
213  public void actionPerformed(java.awt.event.ActionEvent evt) {
214  okButtonActionPerformed(evt);
215  }
216  });
217 
218  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.cancelButton.text")); // NOI18N
219  cancelButton.addActionListener(new java.awt.event.ActionListener() {
220  public void actionPerformed(java.awt.event.ActionEvent evt) {
221  cancelButtonActionPerformed(evt);
222  }
223  });
224 
225  tagCombo.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagCombo.toolTipText")); // NOI18N
226 
227  org.openide.awt.Mnemonics.setLocalizedText(tagLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagLabel.text")); // NOI18N
228 
229  org.openide.awt.Mnemonics.setLocalizedText(commentLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentLabel.text")); // NOI18N
230 
231  org.openide.awt.Mnemonics.setLocalizedText(newTagButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.newTagButton.text")); // NOI18N
232  newTagButton.addActionListener(new java.awt.event.ActionListener() {
233  public void actionPerformed(java.awt.event.ActionEvent evt) {
234  newTagButtonActionPerformed(evt);
235  }
236  });
237 
238  commentText.setColumns(20);
239  commentText.setRows(5);
240  commentText.setText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.text")); // NOI18N
241  commentText.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.toolTipText")); // NOI18N
242  jScrollPane1.setViewportView(commentText);
243 
244  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
245  getContentPane().setLayout(layout);
246  layout.setHorizontalGroup(
247  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
248  .addGroup(layout.createSequentialGroup()
249  .addContainerGap()
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
252  .addComponent(newTagButton)
253  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
254  .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
255  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
256  .addComponent(cancelButton))
257  .addGroup(layout.createSequentialGroup()
258  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
259  .addComponent(commentLabel)
260  .addComponent(tagLabel))
261  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
262  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
263  .addComponent(tagCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
264  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE))))
265  .addContainerGap())
266  );
267 
268  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
269 
270  layout.setVerticalGroup(
271  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
272  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
273  .addGap(24, 24, 24)
274  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
275  .addComponent(tagCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
276  .addComponent(tagLabel))
277  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
278  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
279  .addComponent(commentLabel)
280  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
281  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
282  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
283  .addComponent(cancelButton)
284  .addComponent(okButton)
285  .addComponent(newTagButton))
286  .addContainerGap())
287  );
288 
289  getRootPane().setDefaultButton(okButton);
290 
291  pack();
292  }// </editor-fold>//GEN-END:initComponents
293 
294  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
295  TagName tagNameFromCombo = (TagName) tagCombo.getSelectedItem();
296  tagNameAndComment = new TagNameAndComment(tagNameFromCombo, commentText.getText());
297  dispose();
298  }//GEN-LAST:event_okButtonActionPerformed
299 
300  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
301  tagNameAndComment = null;
302  dispose();
303  }//GEN-LAST:event_cancelButtonActionPerformed
304 
305  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
306  tagNameAndComment = null;
307  dispose();
308  }//GEN-LAST:event_closeDialog
309 
310  private void newTagButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTagButtonActionPerformed
311  TagName newTagName = GetTagNameDialog.doDialog(this);
312  if (newTagName != null) {
313  tagNamesList.add(newTagName);
314  tagCombo.addItem(newTagName);
315  tagCombo.setSelectedItem(newTagName);
316  }
317  }//GEN-LAST:event_newTagButtonActionPerformed
318 
319  // Variables declaration - do not modify//GEN-BEGIN:variables
320  private javax.swing.JButton cancelButton;
321  private javax.swing.JLabel commentLabel;
322  private javax.swing.JTextArea commentText;
323  private javax.swing.JScrollPane jScrollPane1;
324  private javax.swing.JButton newTagButton;
325  private javax.swing.JButton okButton;
326  private javax.swing.JComboBox<TagName> tagCombo;
327  private javax.swing.JLabel tagLabel;
328  // End of variables declaration//GEN-END:variables
329 
330 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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