Autopsy  4.15.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.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  setText(TagUtils.getDecoratedTagDisplayName(value));
225  return this;
226  }
227  return new JLabel();
228  }
229  }
230 
236  @SuppressWarnings("unchecked")
237  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
238  private void initComponents() {
239 
240  jScrollPane1 = new javax.swing.JScrollPane();
241  tagNamesListBox = new javax.swing.JList<>();
242  selectAllButton = new javax.swing.JButton();
243  deselectAllButton = new javax.swing.JButton();
244  jLabel1 = new javax.swing.JLabel();
245  hashSetsComboBox = new javax.swing.JComboBox<>();
246  configureHashDatabasesButton = new javax.swing.JButton();
247  jLabel2 = new javax.swing.JLabel();
248  jAllTagsCheckBox = new javax.swing.JCheckBox();
249 
250  jScrollPane1.setViewportView(tagNamesListBox);
251 
252  org.openide.awt.Mnemonics.setLocalizedText(selectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.selectAllButton.text")); // NOI18N
253  selectAllButton.addActionListener(new java.awt.event.ActionListener() {
254  public void actionPerformed(java.awt.event.ActionEvent evt) {
255  selectAllButtonActionPerformed(evt);
256  }
257  });
258 
259  org.openide.awt.Mnemonics.setLocalizedText(deselectAllButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.deselectAllButton.text")); // NOI18N
260  deselectAllButton.addActionListener(new java.awt.event.ActionListener() {
261  public void actionPerformed(java.awt.event.ActionEvent evt) {
262  deselectAllButtonActionPerformed(evt);
263  }
264  });
265 
266  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel1.text")); // NOI18N
267 
268  hashSetsComboBox.addActionListener(new java.awt.event.ActionListener() {
269  public void actionPerformed(java.awt.event.ActionEvent evt) {
270  hashSetsComboBoxActionPerformed(evt);
271  }
272  });
273 
274  org.openide.awt.Mnemonics.setLocalizedText(configureHashDatabasesButton, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.configureHashDatabasesButton.text")); // NOI18N
275  configureHashDatabasesButton.addActionListener(new java.awt.event.ActionListener() {
276  public void actionPerformed(java.awt.event.ActionEvent evt) {
277  configureHashDatabasesButtonActionPerformed(evt);
278  }
279  });
280 
281  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jLabel2.text")); // NOI18N
282 
283  org.openide.awt.Mnemonics.setLocalizedText(jAllTagsCheckBox, org.openide.util.NbBundle.getMessage(SaveTaggedHashesToHashDbConfigPanel.class, "SaveTaggedHashesToHashDbConfigPanel.jAllTagsCheckBox.text")); // NOI18N
284  jAllTagsCheckBox.addActionListener(new java.awt.event.ActionListener() {
285  public void actionPerformed(java.awt.event.ActionEvent evt) {
286  jAllTagsCheckBoxActionPerformed(evt);
287  }
288  });
289 
290  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
291  this.setLayout(layout);
292  layout.setHorizontalGroup(
293  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
294  .addGroup(layout.createSequentialGroup()
295  .addContainerGap()
296  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
297  .addGroup(layout.createSequentialGroup()
298  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
299  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
300  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
301  .addComponent(deselectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
302  .addComponent(selectAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
303  .addGroup(layout.createSequentialGroup()
304  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
305  .addComponent(jAllTagsCheckBox)
306  .addComponent(jLabel1)
307  .addGroup(layout.createSequentialGroup()
308  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
309  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
310  .addComponent(configureHashDatabasesButton))
311  .addComponent(jLabel2))
312  .addGap(0, 0, Short.MAX_VALUE)))
313  .addContainerGap())
314  );
315 
316  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deselectAllButton, selectAllButton});
317 
318  layout.setVerticalGroup(
319  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
320  .addGroup(layout.createSequentialGroup()
321  .addComponent(jLabel1)
322  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
323  .addComponent(jAllTagsCheckBox)
324  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
325  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326  .addGroup(layout.createSequentialGroup()
327  .addComponent(selectAllButton)
328  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
329  .addComponent(deselectAllButton))
330  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
331  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
332  .addComponent(jLabel2)
333  .addGap(4, 4, 4)
334  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
335  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
336  .addComponent(configureHashDatabasesButton))
337  .addContainerGap())
338  );
339  }// </editor-fold>//GEN-END:initComponents
340 
341  private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
342  selectAllTags(true);
343  }//GEN-LAST:event_selectAllButtonActionPerformed
344 
345  private void hashSetsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hashSetsComboBoxActionPerformed
346  selectedHashSetName = (String) hashSetsComboBox.getSelectedItem();
347  }//GEN-LAST:event_hashSetsComboBoxActionPerformed
348 
349  private void deselectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
350  selectAllTags(false);
351  }//GEN-LAST:event_deselectAllButtonActionPerformed
352 
353  private void configureHashDatabasesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureHashDatabasesButtonActionPerformed
354  HashLookupSettingsPanel configPanel = new HashLookupSettingsPanel();
355  configPanel.load();
356  if (JOptionPane.showConfirmDialog(this, configPanel, "Hash Set Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
357  configPanel.store();
358  populateHashSetComponents();
359  } else {
360  configPanel.cancel();
361  populateHashSetComponents();
362  }
363  }//GEN-LAST:event_configureHashDatabasesButtonActionPerformed
364 
365  private void selectAllTags(boolean select) {
366  Boolean state = Boolean.TRUE;
367  if (!select) {
368  state = Boolean.FALSE;
369  }
370  for (TagName tagName : tagNames) {
371  tagNameSelections.put(tagName, state);
372  }
373  tagNamesListBox.repaint();
374  }
375  private void jAllTagsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAllTagsCheckBoxActionPerformed
376  selectAllTags(true);
377  }//GEN-LAST:event_jAllTagsCheckBoxActionPerformed
378 
379  // Variables declaration - do not modify//GEN-BEGIN:variables
380  private javax.swing.JButton configureHashDatabasesButton;
381  private javax.swing.JButton deselectAllButton;
382  private javax.swing.JComboBox<String> hashSetsComboBox;
383  private javax.swing.JCheckBox jAllTagsCheckBox;
384  private javax.swing.JLabel jLabel1;
385  private javax.swing.JLabel jLabel2;
386  private javax.swing.JScrollPane jScrollPane1;
387  private javax.swing.JButton selectAllButton;
388  private javax.swing.JList<TagName> tagNamesListBox;
389  // End of variables declaration//GEN-END:variables
390 }
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-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.