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

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