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

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