Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SaveTaggedHashesToHashDbConfigPanel.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.modules.taggedhashes;
20 
21 import java.awt.Component;
22 import java.awt.event.ItemEvent;
23 import java.awt.event.ItemListener;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.logging.Level;
32 import javax.swing.JCheckBox;
33 import javax.swing.JLabel;
34 import javax.swing.JList;
35 import javax.swing.JOptionPane;
36 import javax.swing.ListCellRenderer;
37 import javax.swing.ListModel;
38 import javax.swing.event.ListDataListener;
45 import org.sleuthkit.datamodel.TagName;
46 import org.sleuthkit.datamodel.TskCoreException;
47 
52 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
53 class SaveTaggedHashesToHashDbConfigPanel extends javax.swing.JPanel {
54 
55  private static final long serialVersionUID = 1L;
56  private List<TagName> tagNames;
57  private Map<String, Boolean> tagNameSelections = new LinkedHashMap<>();
58  private TagNamesListModel tagsNamesListModel;
59  private TagsNamesListCellRenderer tagsNamesRenderer;
60  private String selectedHashSetName;
61  private List<HashDb> updateableHashSets = new ArrayList<>();
62 
63  SaveTaggedHashesToHashDbConfigPanel() {
64  initComponents();
65  customizeComponents();
66 
67  // Set up the tag names JList component to be a collection of check boxes
68  // for selecting tag names. The mouse click listener updates tagNameSelections
69  // to reflect user choices.
70  //tagNamesListBox.setModel(tagsNamesListModel);
71  //tagNamesListBox.setCellRenderer(tagsNamesRenderer);
72  //tagNamesListBox.setVisibleRowCount(-1);
73  tagNamesListBox.addMouseListener(new MouseAdapter() {
74  @Override
75  public void mousePressed(MouseEvent evt) {
76  if (jAllTagsCheckBox.isSelected()) {
77  return;
78  }
79  JList<?> list = (JList) evt.getSource();
80  int index = list.locationToIndex(evt.getPoint());
81  if (index > -1) {
82  String value = tagsNamesListModel.getElementAt(index);
83  tagNameSelections.put(value, !tagNameSelections.get(value));
84  list.repaint();
85  }
86  }
87  });
88 
89  this.jAllTagsCheckBox.addItemListener(new ItemListener() {
90  @Override
91  public void itemStateChanged(ItemEvent e) {
92  tagNamesListBox.setEnabled(!jAllTagsCheckBox.isSelected());
93  selectAllButton.setEnabled(!jAllTagsCheckBox.isSelected());
94  deselectAllButton.setEnabled(!jAllTagsCheckBox.isSelected());
95  selectAllTags(jAllTagsCheckBox.isSelected());
96  }
97  });
98  }
99 
100  HashesReportModuleSettings getConfiguration() {
101  return new HashesReportModuleSettings(jAllTagsCheckBox.isSelected(), selectedHashSetName);
102  }
103 
104  void setConfiguration(HashesReportModuleSettings settings) {
105  // Need to reset tags. User may have opened a different case or
106  // there may not be a case open any more (Command Line wizard).
107  customizeComponents();
108 
109  // update tag selection
110  jAllTagsCheckBox.setSelected(settings.isExportAllTags());
111  if (settings.isExportAllTags()) {
112  selectAllTags(true);
113  }
114 
115  // update hash database selection
116  if (settings.getHashDbName() != null) {
117  populateHashSetComponents();
118  hashSetsComboBox.setSelectedItem(settings.getHashDbName());
119  }
120  }
121 
122  private void customizeComponents() {
123  populateTagNameComponents();
124 
125  tagsNamesListModel = new TagNamesListModel();
126  tagsNamesRenderer = new TagsNamesListCellRenderer();
127  tagNamesListBox.setModel(tagsNamesListModel);
128  tagNamesListBox.setCellRenderer(tagsNamesRenderer);
129  tagNamesListBox.setVisibleRowCount(-1);
130 
131  populateHashSetComponents();
132  }
133 
134  private void populateTagNameComponents() {
135  // Get the tag names in use for the current case.
136  tagNames = new ArrayList<>();
137  Map<String, Boolean> updatedTagNameSelections = new LinkedHashMap<>();
138  try {
139  // There may not be a case open when configuring report modules for Command Line execution
140  tagNames = Case.getCurrentCaseThrows().getServices().getTagsManager().getTagNamesInUse();
141  } catch (TskCoreException ex) {
142  Logger.getLogger(SaveTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex);
143  } catch (NoCurrentCaseException ex) {
144  // There may not be a case open when configuring report modules for Command Line execution
145  if (Case.isCaseOpen()) {
146  Logger.getLogger(SaveTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex);
147  }
148  }
149 
150  // Preserve the previous selections. Note that tagNameSelections is a
151  // LinkedHashMap so that order is preserved and the tagNames and tagNameSelections
152  // containers are "parallel" containers.
153  for (TagName tagName : tagNames) {
154  if (tagNameSelections.get(tagName.getDisplayName()) != null && Objects.equals(tagNameSelections.get(tagName.getDisplayName()), Boolean.TRUE)) {
155  updatedTagNameSelections.put(tagName.getDisplayName(), Boolean.TRUE);
156  } else {
157  updatedTagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
158  }
159  }
160  tagNameSelections = updatedTagNameSelections;
161  }
162 
163  private void populateHashSetComponents() {
164  // Clear the components because this method is called both during construction
165  // and when the user changes the hash set configuration.
166  hashSetsComboBox.removeAllItems();
167  selectedHashSetName = "";
168 
169  // Get the updateable hash databases and add their hash set names to the
170  // JComboBox component.
171  updateableHashSets = HashDbManager.getInstance().getUpdateableHashSets();
172  if (!updateableHashSets.isEmpty()) {
173  for (HashDb hashDb : updateableHashSets) {
174  hashSetsComboBox.addItem(hashDb.getHashSetName());
175  }
176  hashSetsComboBox.setEnabled(true);
177  } else {
178  hashSetsComboBox.setEnabled(false);
179  }
180  }
181 
187  List<TagName> getSelectedTagNames() {
188  List<TagName> selectedTagNames = new ArrayList<>();
189  for (TagName tagName : tagNames) {
190  if (tagNameSelections.get(tagName.getDisplayName())) {
191  selectedTagNames.add(tagName);
192  }
193  }
194  return selectedTagNames;
195  }
196 
202  HashDb getSelectedHashDatabase() {
203  for (HashDb hashDb : updateableHashSets) {
204  if (hashDb.getHashSetName().equals(selectedHashSetName)) {
205  return hashDb;
206  }
207  }
208  return null;
209  }
210 
211  // This class is a list model for the tag names JList component.
212  private class TagNamesListModel implements ListModel<String> {
213 
214  @Override
215  public int getSize() {
216  return tagNames.size();
217  }
218 
219  @Override
220  public String getElementAt(int index) {
221  return tagNames.get(index).getDisplayName();
222  }
223 
224  @Override
225  public void addListDataListener(ListDataListener l) {
226  }
227 
228  @Override
229  public void removeListDataListener(ListDataListener l) {
230  }
231  }
232 
233  // This class renders the items in the tag names JList component as JCheckbox components.
234  private class TagsNamesListCellRenderer extends JCheckBox implements ListCellRenderer<String> {
235  private static final long serialVersionUID = 1L;
236 
237  @Override
238  public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
239  if (value != null) {
240  setEnabled(list.isEnabled());
241  setSelected(tagNameSelections.get(value));
242  setFont(list.getFont());
243  setBackground(list.getBackground());
244  setForeground(list.getForeground());
245  setText(value);
246  return this;
247  }
248  return new JLabel();
249  }
250  }
251 
257  @SuppressWarnings("unchecked")
258  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
259  private void initComponents() {
260 
261  jScrollPane1 = new javax.swing.JScrollPane();
262  tagNamesListBox = new javax.swing.JList<>();
263  selectAllButton = new javax.swing.JButton();
264  deselectAllButton = new javax.swing.JButton();
265  jLabel1 = new javax.swing.JLabel();
266  hashSetsComboBox = new javax.swing.JComboBox<>();
267  configureHashDatabasesButton = new javax.swing.JButton();
268  jLabel2 = new javax.swing.JLabel();
269  jAllTagsCheckBox = new javax.swing.JCheckBox();
270 
271  jScrollPane1.setViewportView(tagNamesListBox);
272 
273  org.openide.awt.Mnemonics.setLocalizedText(selectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.selectAllButton.text")); // NOI18N
274  selectAllButton.addActionListener(new java.awt.event.ActionListener() {
275  public void actionPerformed(java.awt.event.ActionEvent evt) {
276  selectAllButtonActionPerformed(evt);
277  }
278  });
279 
280  org.openide.awt.Mnemonics.setLocalizedText(deselectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.deselectAllButton.text")); // NOI18N
281  deselectAllButton.addActionListener(new java.awt.event.ActionListener() {
282  public void actionPerformed(java.awt.event.ActionEvent evt) {
283  deselectAllButtonActionPerformed(evt);
284  }
285  });
286 
287  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel1.text")); // NOI18N
288 
289  hashSetsComboBox.addActionListener(new java.awt.event.ActionListener() {
290  public void actionPerformed(java.awt.event.ActionEvent evt) {
291  hashSetsComboBoxActionPerformed(evt);
292  }
293  });
294 
295  org.openide.awt.Mnemonics.setLocalizedText(configureHashDatabasesButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.configureHashDatabasesButton.text")); // NOI18N
296  configureHashDatabasesButton.addActionListener(new java.awt.event.ActionListener() {
297  public void actionPerformed(java.awt.event.ActionEvent evt) {
298  configureHashDatabasesButtonActionPerformed(evt);
299  }
300  });
301 
302  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel2.text")); // NOI18N
303 
304  org.openide.awt.Mnemonics.setLocalizedText(jAllTagsCheckBox, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jAllTagsCheckBox.text")); // NOI18N
305  jAllTagsCheckBox.addActionListener(new java.awt.event.ActionListener() {
306  public void actionPerformed(java.awt.event.ActionEvent evt) {
307  jAllTagsCheckBoxActionPerformed(evt);
308  }
309  });
310 
311  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
312  this.setLayout(layout);
313  layout.setHorizontalGroup(
314  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
315  .addGroup(layout.createSequentialGroup()
316  .addContainerGap()
317  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
318  .addGroup(layout.createSequentialGroup()
319  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
320  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
321  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
322  .addComponent(deselectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
323  .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
324  .addGroup(layout.createSequentialGroup()
325  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326  .addComponent(jAllTagsCheckBox)
327  .addComponent(jLabel1)
328  .addGroup(layout.createSequentialGroup()
329  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
330  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
331  .addComponent(configureHashDatabasesButton))
332  .addComponent(jLabel2))
333  .addGap(0, 0, Short.MAX_VALUE)))
334  .addContainerGap())
335  );
336 
337  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deselectAllButton, selectAllButton});
338 
339  layout.setVerticalGroup(
340  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
341  .addGroup(layout.createSequentialGroup()
342  .addComponent(jLabel1)
343  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
344  .addComponent(jAllTagsCheckBox)
345  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
346  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
347  .addGroup(layout.createSequentialGroup()
348  .addComponent(selectAllButton)
349  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
350  .addComponent(deselectAllButton))
351  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
352  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
353  .addComponent(jLabel2)
354  .addGap(4, 4, 4)
355  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
356  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
357  .addComponent(configureHashDatabasesButton))
358  .addContainerGap())
359  );
360  }// </editor-fold>//GEN-END:initComponents
361 
362  private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
363  selectAllTags(true);
364  }//GEN-LAST:event_selectAllButtonActionPerformed
365 
366  private void hashSetsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hashSetsComboBoxActionPerformed
367  selectedHashSetName = (String)hashSetsComboBox.getSelectedItem();
368  }//GEN-LAST:event_hashSetsComboBoxActionPerformed
369 
370  private void deselectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
371  selectAllTags(false);
372  }//GEN-LAST:event_deselectAllButtonActionPerformed
373 
374  private void configureHashDatabasesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureHashDatabasesButtonActionPerformed
375  HashLookupSettingsPanel configPanel = new HashLookupSettingsPanel();
376  configPanel.load();
377  if (JOptionPane.showConfirmDialog(this, configPanel, "Hash Set Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
378  configPanel.store();
379  populateHashSetComponents();
380  } else {
381  configPanel.cancel();
382  populateHashSetComponents();
383  }
384  }//GEN-LAST:event_configureHashDatabasesButtonActionPerformed
385 
386  private void selectAllTags(boolean select) {
387  Boolean state = Boolean.TRUE;
388  if (!select) {
389  state = Boolean.FALSE;
390  }
391  for (TagName tagName : tagNames) {
392  tagNameSelections.put(tagName.getDisplayName(), state);
393  }
394  tagNamesListBox.repaint();
395  }
396  private void jAllTagsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAllTagsCheckBoxActionPerformed
397  selectAllTags(true);
398  }//GEN-LAST:event_jAllTagsCheckBoxActionPerformed
399 
400  // Variables declaration - do not modify//GEN-BEGIN:variables
401  private javax.swing.JButton configureHashDatabasesButton;
402  private javax.swing.JButton deselectAllButton;
403  private javax.swing.JComboBox<String> hashSetsComboBox;
404  private javax.swing.JCheckBox jAllTagsCheckBox;
405  private javax.swing.JLabel jLabel1;
406  private javax.swing.JLabel jLabel2;
407  private javax.swing.JScrollPane jScrollPane1;
408  private javax.swing.JButton selectAllButton;
409  private javax.swing.JList<String> tagNamesListBox;
410  // End of variables declaration//GEN-END:variables
411 }
Component getListCellRendererComponent(JList<?extends String > list, String value, int index, boolean isSelected, boolean cellHasFocus)

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