Autopsy  4.6.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.logging.Level;
26 import java.util.HashSet;
27 import java.util.Set;
28 import javax.swing.AbstractAction;
29 import javax.swing.ActionMap;
30 import javax.swing.DefaultListCellRenderer;
31 import javax.swing.InputMap;
32 import javax.swing.JComponent;
33 import javax.swing.JDialog;
34 import javax.swing.JList;
35 import javax.swing.KeyStroke;
36 import org.openide.util.NbBundle;
37 import org.openide.windows.WindowManager;
42 import org.sleuthkit.datamodel.TagName;
43 import org.sleuthkit.datamodel.TskCoreException;
44 import org.sleuthkit.datamodel.TskData;
45 
46 public class GetTagNameAndCommentDialog extends JDialog {
47 
48  private static final long serialVersionUID = 1L;
49  private final Set<TagName> tagNamesSet = new HashSet<>();
51 
52  public static class TagNameAndComment {
53 
54  private final TagName tagName;
55  private final String comment;
56 
57  private TagNameAndComment(TagName tagName, String comment) {
58  this.tagName = tagName;
59  this.comment = comment;
60  }
61 
62  public TagName getTagName() {
63  return tagName;
64  }
65 
66  public String getComment() {
67  return comment;
68  }
69  }
70 
80  public static TagNameAndComment doDialog() {
81  return doDialog(WindowManager.getDefault().getMainWindow());
82  }
83 
96  public static TagNameAndComment doDialog(Window owner) {
98  dialog.display();
99  return dialog.tagNameAndComment;
100  }
101 
102  private GetTagNameAndCommentDialog(Window owner) {
103  super(owner,
104  NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.createTag"),
105  ModalityType.APPLICATION_MODAL);
106  }
107 
108 
109  private void display() {
110  initComponents();
111  tagCombo.setRenderer(new DefaultListCellRenderer() {
112  private static final long serialVersionUID = 1L;
113  @Override
114  public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
115  String status = ((TagName) value).getKnownStatus() == TskData.FileKnown.BAD ?TagsManager.getNotableTagLabel() : "";
116  String newValue = ((TagName) value).getDisplayName() + status;
117  return super.getListCellRendererComponent(list, newValue, index, isSelected, cellHasFocus);
118  }
119  });
120  // Set up the dialog to close when Esc is pressed.
121  String cancelName = NbBundle.getMessage(this.getClass(), "GetTagNameAndCommentDialog.cancelName");
122  InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
123 
124  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
125  ActionMap actionMap = getRootPane().getActionMap();
126 
127  actionMap.put(cancelName, new AbstractAction() {
128  private static final long serialVersionUID = 1L;
129 
130  @Override
131  public void actionPerformed(ActionEvent e) {
132  dispose();
133  }
134  }
135  );
136 
137  // Populate the combo box with the available tag names and save the
138  // tag name DTOs to be enable to return the one the user selects.
139  // Tag name DTOs may be null (user tag names that have not been used do
140  // not exist in the database).
141  try {
143  tagNamesSet.addAll(tagsManager.getAllTagNames());
144 
145  } catch (TskCoreException | NoCurrentCaseException ex) {
147  .getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
148  }
149  for (TagName tag : tagNamesSet) {
150 
151  tagCombo.addItem(tag);
152  }
153 
154  // Center and show the dialog box.
155  this.setLocationRelativeTo(this.getOwner());
156  setVisible(true);
157  }
158 
164  @SuppressWarnings("unchecked")
165  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
166  private void initComponents() {
167 
168  okButton = new javax.swing.JButton();
169  cancelButton = new javax.swing.JButton();
170  tagCombo = new javax.swing.JComboBox<TagName>();
171  tagLabel = new javax.swing.JLabel();
172  commentLabel = new javax.swing.JLabel();
173  commentText = new javax.swing.JTextField();
174  newTagButton = new javax.swing.JButton();
175 
176  addWindowListener(new java.awt.event.WindowAdapter() {
177  public void windowClosing(java.awt.event.WindowEvent evt) {
178  closeDialog(evt);
179  }
180  });
181 
182  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.okButton.text")); // NOI18N
183  okButton.addActionListener(new java.awt.event.ActionListener() {
184  public void actionPerformed(java.awt.event.ActionEvent evt) {
186  }
187  });
188 
189  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.cancelButton.text")); // NOI18N
190  cancelButton.addActionListener(new java.awt.event.ActionListener() {
191  public void actionPerformed(java.awt.event.ActionEvent evt) {
193  }
194  });
195 
196  tagCombo.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagCombo.toolTipText")); // NOI18N
197 
198  org.openide.awt.Mnemonics.setLocalizedText(tagLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.tagLabel.text")); // NOI18N
199 
200  org.openide.awt.Mnemonics.setLocalizedText(commentLabel, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentLabel.text")); // NOI18N
201 
202  commentText.setText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.text")); // NOI18N
203  commentText.setToolTipText(org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.commentText.toolTipText")); // NOI18N
204 
205  org.openide.awt.Mnemonics.setLocalizedText(newTagButton, org.openide.util.NbBundle.getMessage(GetTagNameAndCommentDialog.class, "GetTagNameAndCommentDialog.newTagButton.text")); // NOI18N
206  newTagButton.addActionListener(new java.awt.event.ActionListener() {
207  public void actionPerformed(java.awt.event.ActionEvent evt) {
209  }
210  });
211 
212  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
213  getContentPane().setLayout(layout);
214  layout.setHorizontalGroup(
215  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
216  .addGroup(layout.createSequentialGroup()
217  .addContainerGap()
218  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
219  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
220  .addComponent(newTagButton)
221  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 165, Short.MAX_VALUE)
222  .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
223  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
224  .addComponent(cancelButton))
225  .addGroup(layout.createSequentialGroup()
226  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
227  .addComponent(commentLabel)
228  .addComponent(tagLabel))
229  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
230  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
231  .addComponent(commentText)
232  .addComponent(tagCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
233  .addContainerGap())
234  );
235 
236  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
237 
238  layout.setVerticalGroup(
239  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
240  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
241  .addGap(24, 24, 24)
242  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
243  .addComponent(tagCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
244  .addComponent(tagLabel))
245  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
247  .addComponent(commentLabel)
248  .addComponent(commentText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
249  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
250  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
251  .addComponent(cancelButton)
252  .addComponent(okButton)
253  .addComponent(newTagButton))
254  .addContainerGap())
255  );
256 
257  getRootPane().setDefaultButton(okButton);
258 
259  pack();
260  }// </editor-fold>//GEN-END:initComponents
261 
262  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
263  TagName tagNameFromCombo = (TagName) tagCombo.getSelectedItem();
264  tagNameAndComment = new TagNameAndComment(tagNameFromCombo, commentText.getText());
265  dispose();
266  }//GEN-LAST:event_okButtonActionPerformed
267 
268  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
269  tagNameAndComment = null;
270  dispose();
271  }//GEN-LAST:event_cancelButtonActionPerformed
272 
273  private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
274  tagNameAndComment = null;
275  dispose();
276  }//GEN-LAST:event_closeDialog
277 
278  private void newTagButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newTagButtonActionPerformed
279  TagName newTagName = GetTagNameDialog.doDialog(this);
280  if (newTagName != null) {
281  tagNamesSet.add(newTagName);
282  tagCombo.addItem(newTagName);
283  tagCombo.setSelectedItem(newTagName);
284  }
285  }//GEN-LAST:event_newTagButtonActionPerformed
286 
287  // Variables declaration - do not modify//GEN-BEGIN:variables
288  private javax.swing.JButton cancelButton;
289  private javax.swing.JLabel commentLabel;
290  private javax.swing.JTextField commentText;
291  private javax.swing.JButton newTagButton;
292  private javax.swing.JButton okButton;
293  private javax.swing.JComboBox<TagName> tagCombo;
294  private javax.swing.JLabel tagLabel;
295  // End of variables declaration//GEN-END:variables
296 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.