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

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