Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
AddTaggedHashesToHashDbConfigPanel.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.report.taggedhashes;
20 
21 import java.awt.Component;
22 import java.awt.event.MouseAdapter;
23 import java.awt.event.MouseEvent;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.logging.Level;
29 import javax.swing.JCheckBox;
30 import javax.swing.JLabel;
31 import javax.swing.JList;
32 import javax.swing.JOptionPane;
33 import javax.swing.ListCellRenderer;
34 import javax.swing.ListModel;
35 import javax.swing.event.ListDataListener;
42 import org.sleuthkit.datamodel.TagName;
43 import org.sleuthkit.datamodel.TskCoreException;
44 
49 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
50 class AddTaggedHashesToHashDbConfigPanel extends javax.swing.JPanel {
51 
52  private static final long serialVersionUID = 1L;
53  private List<TagName> tagNames;
54  private final Map<String, Boolean> tagNameSelections = new LinkedHashMap<>();
55  private final TagNamesListModel tagsNamesListModel = new TagNamesListModel();
56  private final TagsNamesListCellRenderer tagsNamesRenderer = new TagsNamesListCellRenderer();
57  private HashDb selectedHashSet = null;
58 
59  AddTaggedHashesToHashDbConfigPanel() {
60  initComponents();
61  customizeComponents();
62  }
63 
64  private void customizeComponents() {
65  populateTagNameComponents();
66  populateHashSetComponents();
67  }
68 
69  private void populateTagNameComponents() {
70  // Get the tag names in use for the current case.
71  try {
72  tagNames = Case.getCurrentCaseThrows().getServices().getTagsManager().getTagNamesInUse();
73  } catch (TskCoreException ex) {
74  Logger.getLogger(AddTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex);
75  JOptionPane.showMessageDialog(this, "Error getting tag names for case.", "Tag Names Not Found", JOptionPane.ERROR_MESSAGE);
76  } catch (NoCurrentCaseException ex) {
77  Logger.getLogger(AddTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex);
78  JOptionPane.showMessageDialog(this, "Error getting tag names for case.", "Exception while getting open case.", JOptionPane.ERROR_MESSAGE);
79  }
80 
81  // Mark the tag names as unselected. Note that tagNameSelections is a
82  // LinkedHashMap so that order is preserved and the tagNames and tagNameSelections
83  // containers are "parallel" containers.
84  for (TagName tagName : tagNames) {
85  tagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
86  }
87 
88  // Set up the tag names JList component to be a collection of check boxes
89  // for selecting tag names. The mouse click listener updates tagNameSelections
90  // to reflect user choices.
91  tagNamesListBox.setModel(tagsNamesListModel);
92  tagNamesListBox.setCellRenderer(tagsNamesRenderer);
93  tagNamesListBox.setVisibleRowCount(-1);
94  tagNamesListBox.addMouseListener(new MouseAdapter() {
95  @Override
96  public void mousePressed(MouseEvent evt) {
97  JList<?> list = (JList) evt.getSource();
98  int index = list.locationToIndex(evt.getPoint());
99  if (index > -1) {
100  String value = tagsNamesListModel.getElementAt(index);
101  tagNameSelections.put(value, !tagNameSelections.get(value));
102  list.repaint();
103  }
104  }
105  });
106  }
107 
108  private void populateHashSetComponents() {
109  // Clear the components because this method is called both during construction
110  // and when the user changes the hash set configuration.
111  hashSetsComboBox.removeAllItems();
112 
113  // Get the updateable hash databases and add their hash set names to the
114  // JComboBox component.
115  List<HashDb> updateableHashSets = HashDbManager.getInstance().getUpdateableHashSets();
116  if (!updateableHashSets.isEmpty()) {
117  for (HashDb hashDb : updateableHashSets) {
118  hashSetsComboBox.addItem(hashDb);
119  }
120  hashSetsComboBox.setEnabled(true);
121  } else {
122  hashSetsComboBox.setEnabled(false);
123  }
124  }
125 
131  List<TagName> getSelectedTagNames() {
132  List<TagName> selectedTagNames = new ArrayList<>();
133  for (TagName tagName : tagNames) {
134  if (tagNameSelections.get(tagName.getDisplayName())) {
135  selectedTagNames.add(tagName);
136  }
137  }
138  return selectedTagNames;
139  }
140 
146  HashDb getSelectedHashDatabase() {
147  return selectedHashSet;
148  }
149 
150  // This class is a list model for the tag names JList component.
151  private class TagNamesListModel implements ListModel<String> {
152 
153  @Override
154  public int getSize() {
155  return tagNames.size();
156  }
157 
158  @Override
159  public String getElementAt(int index) {
160  return tagNames.get(index).getDisplayName();
161  }
162 
163  @Override
164  public void addListDataListener(ListDataListener l) {
165  }
166 
167  @Override
168  public void removeListDataListener(ListDataListener l) {
169  }
170  }
171 
172  // This class renders the items in the tag names JList component as JCheckbox components.
173  private class TagsNamesListCellRenderer extends JCheckBox implements ListCellRenderer<String> {
174  private static final long serialVersionUID = 1L;
175 
176  @Override
177  public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
178  if (value != null) {
179  setEnabled(list.isEnabled());
180  setSelected(tagNameSelections.get(value));
181  setFont(list.getFont());
182  setBackground(list.getBackground());
183  setForeground(list.getForeground());
184  setText(value);
185  return this;
186  }
187  return new JLabel();
188  }
189  }
190 
196  @SuppressWarnings("unchecked")
197  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
198  private void initComponents() {
199 
200  jScrollPane1 = new javax.swing.JScrollPane();
201  tagNamesListBox = new javax.swing.JList<>();
202  selectAllButton = new javax.swing.JButton();
203  deselectAllButton = new javax.swing.JButton();
204  jLabel1 = new javax.swing.JLabel();
205  hashSetsComboBox = new javax.swing.JComboBox<>();
206  configureHashDatabasesButton = new javax.swing.JButton();
207  jLabel2 = new javax.swing.JLabel();
208 
209  jScrollPane1.setViewportView(tagNamesListBox);
210 
211  org.openide.awt.Mnemonics.setLocalizedText(selectAllButton, org.openide.util.NbBundle.getMessage(AddTaggedHashesToHashDbConfigPanel.class, "AddTaggedHashesToHashDbConfigPanel.selectAllButton.text")); // NOI18N
212  selectAllButton.addActionListener(new java.awt.event.ActionListener() {
213  public void actionPerformed(java.awt.event.ActionEvent evt) {
214  selectAllButtonActionPerformed(evt);
215  }
216  });
217 
218  org.openide.awt.Mnemonics.setLocalizedText(deselectAllButton, org.openide.util.NbBundle.getMessage(AddTaggedHashesToHashDbConfigPanel.class, "AddTaggedHashesToHashDbConfigPanel.deselectAllButton.text")); // NOI18N
219  deselectAllButton.addActionListener(new java.awt.event.ActionListener() {
220  public void actionPerformed(java.awt.event.ActionEvent evt) {
221  deselectAllButtonActionPerformed(evt);
222  }
223  });
224 
225  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(AddTaggedHashesToHashDbConfigPanel.class, "AddTaggedHashesToHashDbConfigPanel.jLabel1.text")); // NOI18N
226 
227  hashSetsComboBox.addActionListener(new java.awt.event.ActionListener() {
228  public void actionPerformed(java.awt.event.ActionEvent evt) {
229  hashSetsComboBoxActionPerformed(evt);
230  }
231  });
232 
233  org.openide.awt.Mnemonics.setLocalizedText(configureHashDatabasesButton, org.openide.util.NbBundle.getMessage(AddTaggedHashesToHashDbConfigPanel.class, "AddTaggedHashesToHashDbConfigPanel.configureHashDatabasesButton.text")); // NOI18N
234  configureHashDatabasesButton.addActionListener(new java.awt.event.ActionListener() {
235  public void actionPerformed(java.awt.event.ActionEvent evt) {
236  configureHashDatabasesButtonActionPerformed(evt);
237  }
238  });
239 
240  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(AddTaggedHashesToHashDbConfigPanel.class, "AddTaggedHashesToHashDbConfigPanel.jLabel2.text")); // NOI18N
241 
242  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
243  this.setLayout(layout);
244  layout.setHorizontalGroup(
245  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
246  .addGroup(layout.createSequentialGroup()
247  .addContainerGap()
248  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
249  .addComponent(jLabel2)
250  .addComponent(jLabel1)
251  .addGroup(layout.createSequentialGroup()
252  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
253  .addComponent(jScrollPane1)
254  .addGroup(layout.createSequentialGroup()
255  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
256  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
257  .addComponent(configureHashDatabasesButton)))
258  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
259  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
260  .addComponent(deselectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
261  .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
262  .addContainerGap())
263  );
264 
265  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deselectAllButton, selectAllButton});
266 
267  layout.setVerticalGroup(
268  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
269  .addGroup(layout.createSequentialGroup()
270  .addContainerGap()
271  .addComponent(jLabel1)
272  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
273  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
274  .addGroup(layout.createSequentialGroup()
275  .addComponent(selectAllButton)
276  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
277  .addComponent(deselectAllButton))
278  .addComponent(jScrollPane1))
279  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
280  .addComponent(jLabel2)
281  .addGap(4, 4, 4)
282  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
283  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
284  .addComponent(configureHashDatabasesButton))
285  .addContainerGap())
286  );
287  }// </editor-fold>//GEN-END:initComponents
288 
289  private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
290  for (TagName tagName : tagNames) {
291  tagNameSelections.put(tagName.getDisplayName(), Boolean.TRUE);
292  }
293  tagNamesListBox.repaint();
294  }//GEN-LAST:event_selectAllButtonActionPerformed
295 
296  private void hashSetsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hashSetsComboBoxActionPerformed
297  selectedHashSet = (HashDb)hashSetsComboBox.getSelectedItem();
298  }//GEN-LAST:event_hashSetsComboBoxActionPerformed
299 
300  private void deselectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
301  for (TagName tagName : tagNames) {
302  tagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
303  }
304  tagNamesListBox.repaint();
305  }//GEN-LAST:event_deselectAllButtonActionPerformed
306 
307  private void configureHashDatabasesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureHashDatabasesButtonActionPerformed
308  HashLookupSettingsPanel configPanel = new HashLookupSettingsPanel();
309  configPanel.load();
310  if (JOptionPane.showConfirmDialog(this, configPanel, "Hash Set Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
311  configPanel.store();
312  populateHashSetComponents();
313  } else {
314  configPanel.cancel();
315  populateHashSetComponents();
316  }
317  }//GEN-LAST:event_configureHashDatabasesButtonActionPerformed
318 
319  // Variables declaration - do not modify//GEN-BEGIN:variables
320  private javax.swing.JButton configureHashDatabasesButton;
321  private javax.swing.JButton deselectAllButton;
322  private javax.swing.JComboBox<HashDb> hashSetsComboBox;
323  private javax.swing.JLabel jLabel1;
324  private javax.swing.JLabel jLabel2;
325  private javax.swing.JScrollPane jScrollPane1;
326  private javax.swing.JButton selectAllButton;
327  private javax.swing.JList<String> tagNamesListBox;
328  // End of variables declaration//GEN-END:variables
329 }
Component getListCellRendererComponent(JList<?extends String > list, String value, int index, boolean isSelected, boolean cellHasFocus)

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