Autopsy  4.6.0
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 class AddTaggedHashesToHashDbConfigPanel extends javax.swing.JPanel {
50 
51  private static final long serialVersionUID = 1L;
52  private List<TagName> tagNames;
53  private final Map<String, Boolean> tagNameSelections = new LinkedHashMap<>();
54  private final TagNamesListModel tagsNamesListModel = new TagNamesListModel();
55  private final TagsNamesListCellRenderer tagsNamesRenderer = new TagsNamesListCellRenderer();
56  private HashDb selectedHashSet = null;
57 
58  AddTaggedHashesToHashDbConfigPanel() {
59  initComponents();
60  customizeComponents();
61  }
62 
63  private void customizeComponents() {
64  populateTagNameComponents();
65  populateHashSetComponents();
66  }
67 
68  private void populateTagNameComponents() {
69  // Get the tag names in use for the current case.
70  try {
71  tagNames = Case.getOpenCase().getServices().getTagsManager().getTagNamesInUse();
72  } catch (TskCoreException ex) {
73  Logger.getLogger(AddTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex);
74  JOptionPane.showMessageDialog(this, "Error getting tag names for case.", "Tag Names Not Found", JOptionPane.ERROR_MESSAGE);
75  } catch (NoCurrentCaseException ex) {
76  Logger.getLogger(AddTaggedHashesToHashDbConfigPanel.class.getName()).log(Level.SEVERE, "Exception while getting open case.", ex);
77  JOptionPane.showMessageDialog(this, "Error getting tag names for case.", "Exception while getting open case.", JOptionPane.ERROR_MESSAGE);
78  }
79 
80  // Mark the tag names as unselected. Note that tagNameSelections is a
81  // LinkedHashMap so that order is preserved and the tagNames and tagNameSelections
82  // containers are "parallel" containers.
83  for (TagName tagName : tagNames) {
84  tagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
85  }
86 
87  // Set up the tag names JList component to be a collection of check boxes
88  // for selecting tag names. The mouse click listener updates tagNameSelections
89  // to reflect user choices.
90  tagNamesListBox.setModel(tagsNamesListModel);
91  tagNamesListBox.setCellRenderer(tagsNamesRenderer);
92  tagNamesListBox.setVisibleRowCount(-1);
93  tagNamesListBox.addMouseListener(new MouseAdapter() {
94  @Override
95  public void mousePressed(MouseEvent evt) {
96  JList<?> list = (JList) evt.getSource();
97  int index = list.locationToIndex(evt.getPoint());
98  if (index > -1) {
99  String value = tagsNamesListModel.getElementAt(index);
100  tagNameSelections.put(value, !tagNameSelections.get(value));
101  list.repaint();
102  }
103  }
104  });
105  }
106 
107  private void populateHashSetComponents() {
108  // Clear the components because this method is called both during construction
109  // and when the user changes the hash set configuration.
110  hashSetsComboBox.removeAllItems();
111 
112  // Get the updateable hash databases and add their hash set names to the
113  // JComboBox component.
114  List<HashDb> updateableHashSets = HashDbManager.getInstance().getUpdateableHashSets();
115  if (!updateableHashSets.isEmpty()) {
116  for (HashDb hashDb : updateableHashSets) {
117  hashSetsComboBox.addItem(hashDb);
118  }
119  hashSetsComboBox.setEnabled(true);
120  } else {
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 
264  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deselectAllButton, selectAllButton});
265 
266  layout.setVerticalGroup(
267  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
268  .addGroup(layout.createSequentialGroup()
269  .addContainerGap()
270  .addComponent(jLabel1)
271  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
272  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
273  .addGroup(layout.createSequentialGroup()
274  .addComponent(selectAllButton)
275  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
276  .addComponent(deselectAllButton))
277  .addComponent(jScrollPane1))
278  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
279  .addComponent(jLabel2)
280  .addGap(4, 4, 4)
281  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
282  .addComponent(hashSetsComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
283  .addComponent(configureHashDatabasesButton))
284  .addContainerGap())
285  );
286  }// </editor-fold>//GEN-END:initComponents
287 
288  private void selectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllButtonActionPerformed
289  for (TagName tagName : tagNames) {
290  tagNameSelections.put(tagName.getDisplayName(), Boolean.TRUE);
291  }
292  tagNamesListBox.repaint();
293  }//GEN-LAST:event_selectAllButtonActionPerformed
294 
295  private void hashSetsComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hashSetsComboBoxActionPerformed
296  selectedHashSet = (HashDb)hashSetsComboBox.getSelectedItem();
297  }//GEN-LAST:event_hashSetsComboBoxActionPerformed
298 
299  private void deselectAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deselectAllButtonActionPerformed
300  for (TagName tagName : tagNames) {
301  tagNameSelections.put(tagName.getDisplayName(), Boolean.FALSE);
302  }
303  tagNamesListBox.repaint();
304  }//GEN-LAST:event_deselectAllButtonActionPerformed
305 
306  private void configureHashDatabasesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureHashDatabasesButtonActionPerformed
307  HashLookupSettingsPanel configPanel = new HashLookupSettingsPanel();
308  configPanel.load();
309  if (JOptionPane.showConfirmDialog(this, configPanel, "Hash Set Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == JOptionPane.OK_OPTION) {
310  configPanel.store();
311  populateHashSetComponents();
312  } else {
313  configPanel.cancel();
314  populateHashSetComponents();
315  }
316  }//GEN-LAST:event_configureHashDatabasesButtonActionPerformed
317 
318  // Variables declaration - do not modify//GEN-BEGIN:variables
319  private javax.swing.JButton configureHashDatabasesButton;
320  private javax.swing.JButton deselectAllButton;
321  private javax.swing.JComboBox<HashDb> hashSetsComboBox;
322  private javax.swing.JLabel jLabel1;
323  private javax.swing.JLabel jLabel2;
324  private javax.swing.JScrollPane jScrollPane1;
325  private javax.swing.JButton selectAllButton;
326  private javax.swing.JList<String> tagNamesListBox;
327  // End of variables declaration//GEN-END:variables
328 }
Component getListCellRendererComponent(JList<?extends String > list, String value, int index, boolean isSelected, boolean cellHasFocus)

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.