Autopsy  4.19.3
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.DefaultListModel;
33 import javax.swing.JCheckBox;
34 import javax.swing.JLabel;
35 import javax.swing.JList;
36 import javax.swing.JOptionPane;
37 import javax.swing.ListCellRenderer;
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<TagName, Boolean> tagNameSelections = new LinkedHashMap<>();
58  private DefaultListModel<TagName> tagsNamesListModel = new DefaultListModel<>();
59  private TagsNamesListCellRenderer tagsNamesRenderer = new TagsNamesListCellRenderer();
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.addMouseListener(new MouseAdapter() {
71  @Override
72  public void mousePressed(MouseEvent evt) {
73  if (jAllTagsCheckBox.isSelected()) {
74  return;
75  }
76  JList<?> list = (JList) evt.getSource();
77  int index = list.locationToIndex(evt.getPoint());
78  if (index > -1) {
79  TagName tagName = tagsNamesListModel.getElementAt(index);
80  tagNameSelections.put(tagName, !tagNameSelections.get(tagName));
81  list.repaint();
82  }
83  }
84  });
85 
86  this.jAllTagsCheckBox.addItemListener(new ItemListener() {
87  @Override
88  public void itemStateChanged(ItemEvent e) {
89  tagNamesListBox.setEnabled(!jAllTagsCheckBox.isSelected());
90  selectAllButton.setEnabled(!jAllTagsCheckBox.isSelected());
91  deselectAllButton.setEnabled(!jAllTagsCheckBox.isSelected());
92  selectAllTags(jAllTagsCheckBox.isSelected());
93  }
94  });
95  }
96 
97  HashesReportModuleSettings getConfiguration() {
98  return new HashesReportModuleSettings(jAllTagsCheckBox.isSelected(), selectedHashSetName);
99  }
100 
101  void setConfiguration(HashesReportModuleSettings settings) {
102  // Need to reset tags. User may have opened a different case or
103  // there may not be a case open any more (Command Line wizard).
104  customizeComponents();
105 
106  // update tag selection
107  jAllTagsCheckBox.setSelected(settings.isExportAllTags());
108  if (settings.isExportAllTags()) {
109  selectAllTags(true);
110  }
111 
112  // update hash database selection
113  if (settings.getHashDbName() != null) {
114  populateHashSetComponents();
115  hashSetsComboBox.setSelectedItem(settings.getHashDbName());
116  }
117  }
118 
119  private void customizeComponents() {
120  tagsNamesListModel = new DefaultListModel<>();
121  tagsNamesRenderer = new TagsNamesListCellRenderer();
122  populateTagNameComponents();
123 
124  tagNamesListBox.setModel(tagsNamesListModel);
125  tagNamesListBox.setCellRenderer(tagsNamesRenderer);
126  tagNamesListBox.setVisibleRowCount(-1);
127 
128  populateHashSetComponents();
129  }
130 
131  private void populateTagNameComponents() {
132  // Get the tag names in use for the current case.
133  tagNames = new ArrayList<>();
134  Map<TagName, Boolean> updatedTagNameSelections = new LinkedHashMap<>();
135  try {
136  // There may not be a case open when configuring report modules for Command Line execution
137  tagNames = Case.getCurrentCaseThrows().getServices().getTagsManager().getTagNamesInUse();
138  for (TagName tagName : tagNames) {
139  tagsNamesListModel.addElement(tagName);
140  }
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) != null && Objects.equals(tagNameSelections.get(tagName), Boolean.TRUE)) {
155  updatedTagNameSelections.put(tagName, Boolean.TRUE);
156  } else {
157  updatedTagNameSelections.put(tagName, 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)) {
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 renders the items in the tag names JList component as JCheckbox components.
212  private class TagsNamesListCellRenderer extends JCheckBox implements ListCellRenderer<TagName> {
213 
214  private static final long serialVersionUID = 1L;
215 
216  @Override
217  public Component getListCellRendererComponent(JList<? extends TagName> list, TagName value, int index, boolean isSelected, boolean cellHasFocus) {
218  if (value != null) {
219  setEnabled(list.isEnabled());
220  setSelected(tagNameSelections.get(value));
221  setFont(list.getFont());
222  setBackground(list.getBackground());
223  setForeground(list.getForeground());
224 
225  String text = TagUtils.getDecoratedTagDisplayName(value);
226  setText(text);
227  this.setToolTipText(text);
228  return this;
229  }
230  return new JLabel();
231  }
232  }
233 
239  @SuppressWarnings("unchecked")
240  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
241  private void initComponents() {
242 
243  jScrollPane1 = new javax.swing.JScrollPane();
244  tagNamesListBox = new javax.swing.JList<>();
245  selectAllButton = new javax.swing.JButton();
246  deselectAllButton = new javax.swing.JButton();
247  jLabel1 = new javax.swing.JLabel();
248  hashSetsComboBox = new javax.swing.JComboBox<>();
249  configureHashDatabasesButton = new javax.swing.JButton();
250  jLabel2 = new javax.swing.JLabel();
251  jAllTagsCheckBox = new javax.swing.JCheckBox();
252 
253  jScrollPane1.setViewportView(tagNamesListBox);
254 
255  org.openide.awt.Mnemonics.setLocalizedText(selectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.selectAllButton.text")); // NOI18N
256  selectAllButton.addActionListener(new java.awt.event.ActionListener() {
257  public void actionPerformed(java.awt.event.ActionEvent evt) {
258  selectAllButtonActionPerformed(evt);
259  }
260  });
261 
262  org.openide.awt.Mnemonics.setLocalizedText(deselectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.deselectAllButton.text")); // NOI18N
263  deselectAllButton.addActionListener(new java.awt.event.ActionListener() {
264  public void actionPerformed(java.awt.event.ActionEvent evt) {
265  deselectAllButtonActionPerformed(evt);
266  }
267  });
268 
269  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel1.text")); // NOI18N
270 
271  hashSetsComboBox.addActionListener(new java.awt.event.ActionListener() {
272  public void actionPerformed(java.awt.event.ActionEvent evt) {
273  hashSetsComboBoxActionPerformed(evt);
274  }
275  });
276 
277  org.openide.awt.Mnemonics.setLocalizedText(configureHashDatabasesButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.configureHashDatabasesButton.text")); // NOI18N
278  configureHashDatabasesButton.addActionListener(new java.awt.event.ActionListener() {
279  public void actionPerformed(java.awt.event.ActionEvent evt) {
280  configureHashDatabasesButtonActionPerformed(evt);
281  }
282  });
283 
284  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel2.text")); // NOI18N
285 
286  org.openide.awt.Mnemonics.setLocalizedText(jAllTagsCheckBox, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jAllTagsCheckBox.text")); // NOI18N
287  jAllTagsCheckBox.addActionListener(new java.awt.event.ActionListener() {
288  public void actionPerformed(java.awt.event.ActionEvent evt) {
289  jAllTagsCheckBoxActionPerformed(evt);
290  }
291  });
292 
293  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
294  this.setLayout(layout);
295  layout.setHorizontalGroup(
296  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
297  .addGroup(layout.createSequentialGroup()
298  .addContainerGap()
299  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
300  .addGroup(layout.createSequentialGroup()
301  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
302  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
303  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
304  .addComponent(deselectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
305  .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
306  .addGroup(layout.createSequentialGroup()
307  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
308  .addComponent(jAllTagsCheckBox)
309  .addComponent(jLabel1)
310  .addGroup(layout.createSequentialGroup()
311  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
312  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
313  .addComponent(configureHashDatabasesButton))
314  .addComponent(jLabel2))
315  .addGap(0, 0, Short.MAX_VALUE)))
316  .addContainerGap())
317  );
318 
319  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deselectAllButton, selectAllButton});
320 
321  layout.setVerticalGroup(
322  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
323  .addGroup(layout.createSequentialGroup()
324  .addComponent(jLabel1)
325  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
326  .addComponent(jAllTagsCheckBox)
327  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
328  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
329  .addGroup(layout.createSequentialGroup()
330  .addComponent(selectAllButton)
331  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
332  .addComponent(deselectAllButton))
333  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE))
334  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
335  .addComponent(jLabel2)
336  .addGap(4, 4, 4)
337  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
338  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
339  .addComponent(configureHashDatabasesButton))
340  .addContainerGap())
341  );
342  }// </editor-fold>//GEN-END:initComponents
343 
344  private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
345  selectAllTags(true);
346  }//GEN-LAST:event_selectAllButtonActionPerformed
347 
348  private void hashSetsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hashSetsComboBoxActionPerformed
349  selectedHashSetName = (String) hashSetsComboBox.getSelectedItem();
350  }//GEN-LAST:event_hashSetsComboBoxActionPerformed
351 
352  private void deselectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
353  selectAllTags(false);
354  }//GEN-LAST:event_deselectAllButtonActionPerformed
355 
356  private void configureHashDatabasesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureHashDatabasesButtonActionPerformed
357  HashLookupSettingsPanel configPanel = new HashLookupSettingsPanel();
358  configPanel.load();
359  if (JOptionPane.showConfirmDialog(this, configPanel, "Hash Set Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
360  configPanel.store();
361  populateHashSetComponents();
362  } else {
363  configPanel.cancel();
364  populateHashSetComponents();
365  }
366  }//GEN-LAST:event_configureHashDatabasesButtonActionPerformed
367 
368  private void selectAllTags(boolean select) {
369  Boolean state = Boolean.TRUE;
370  if (!select) {
371  state = Boolean.FALSE;
372  }
373  for (TagName tagName : tagNames) {
374  tagNameSelections.put(tagName, state);
375  }
376  tagNamesListBox.repaint();
377  }
378  private void jAllTagsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAllTagsCheckBoxActionPerformed
379  selectAllTags(true);
380  }//GEN-LAST:event_jAllTagsCheckBoxActionPerformed
381 
382  // Variables declaration - do not modify//GEN-BEGIN:variables
383  private javax.swing.JButton configureHashDatabasesButton;
384  private javax.swing.JButton deselectAllButton;
385  private javax.swing.JComboBox<String> hashSetsComboBox;
386  private javax.swing.JCheckBox jAllTagsCheckBox;
387  private javax.swing.JLabel jLabel1;
388  private javax.swing.JLabel jLabel2;
389  private javax.swing.JScrollPane jScrollPane1;
390  private javax.swing.JButton selectAllButton;
391  private javax.swing.JList<TagName> tagNamesListBox;
392  // End of variables declaration//GEN-END:variables
393 }
Component getListCellRendererComponent(JList<?extends TagName > list, TagName value, int index, boolean isSelected, boolean cellHasFocus)
static String getDecoratedTagDisplayName(TagName tagName)
Definition: TagUtils.java:55

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