Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashLookupModuleSettingsPanel.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.modules.hashdatabase;
20 
21 import java.awt.Component;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.logging.Level;
27 import javax.swing.JLabel;
28 import javax.swing.JScrollPane;
29 import javax.swing.JTable;
30 import javax.swing.table.AbstractTableModel;
31 import javax.swing.table.DefaultTableCellRenderer;
32 import javax.swing.table.TableColumn;
33 import org.apache.commons.lang.StringUtils;
37 import org.sleuthkit.datamodel.TskCoreException;
39 
43 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
44 public final class HashLookupModuleSettingsPanel extends IngestModuleIngestJobSettingsPanel implements PropertyChangeListener {
45 
46  private static final long serialVersionUID = 1L;
47  private final HashDbManager hashDbManager = HashDbManager.getInstance();
48  private final List<HashSetModel> hashSetModels = new ArrayList<>();
49  private final HashSetsTableModel hashSetsTableModel = new HashSetsTableModel(hashSetModels);
50 
51  HashLookupModuleSettingsPanel(HashLookupModuleSettings settings) {
52  initializeHashSetModels(settings);
53  initComponents();
54  customizeComponents(settings);
55  }
56 
57  private void initializeHashSetModels(HashLookupModuleSettings settings) {
58  List<HashDb> hashDbs = validSetsOnly(hashDbManager.getAllHashSets());
59  hashSetModels.clear();
60  for (HashDb db : hashDbs) {
61  hashSetModels.add(new HashSetModel(db, settings.isHashSetEnabled(db), isHashDbValid(db)));
62  }
63  }
64 
65  private void customizeComponents(HashLookupModuleSettings settings) {
66  customizeHashSetsTable(hashDbsScrollPane, hashTable, hashSetsTableModel);
67  alwaysCalcHashesCheckbox.setSelected(settings.shouldCalculateHashes());
68  hashDbManager.addPropertyChangeListener(this);
69  alwaysCalcHashesCheckbox.setText("<html>" + org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.text") + "</html>"); // NOI18N NON-NLS
70  }
71 
72  private void customizeHashSetsTable(JScrollPane scrollPane, JTable table, HashSetsTableModel tableModel) {
73  table.setModel(tableModel);
74  table.setTableHeader(null);
75  table.setRowSelectionAllowed(false);
76  final int width1 = scrollPane.getPreferredSize().width;
77  hashTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
78  TableColumn column;
79  for (int i = 0; i < table.getColumnCount(); i++) {
80  column = table.getColumnModel().getColumn(i);
81  if (i == 0) {
82  column.setPreferredWidth(((int) (width1 * 0.07)));
83  } else {
84  column.setCellRenderer(new HashSetTableCellRenderer());
85  column.setPreferredWidth(((int) (width1 * 0.92)));
86  }
87  }
88  }
89 
90  @Override
91  public void propertyChange(PropertyChangeEvent event) {
92  if (event.getPropertyName().equals(HashDbManager.SetEvt.DB_ADDED.name())
93  || event.getPropertyName().equals(HashDbManager.SetEvt.DB_DELETED.name())
94  || event.getPropertyName().equals(HashDbManager.SetEvt.DB_INDEXED.name())) {
95  update();
96  }
97  }
98 
99  @Override
101  List<HashDb> enabledHashSets = new ArrayList<>();
102  List<HashDb> disabledHashSets = new ArrayList<>();
103  addHashSets(hashSetModels, enabledHashSets, disabledHashSets);
104  return new HashLookupModuleSettings(alwaysCalcHashesCheckbox.isSelected(),
105  enabledHashSets, disabledHashSets);
106  }
107 
108  private void addHashSets(List<HashSetModel> hashSetModels, List<HashDb> enabledHashSets, List<HashDb> disabledHashSets) {
109  for (HashSetModel model : hashSetModels) {
110  if (model.isEnabled() && model.isValid()) {
111  enabledHashSets.add(model.getDatabase());
112  } else {
113  disabledHashSets.add(model.getDatabase());
114  }
115  }
116  }
117 
118  void update() {
119  updateHashSetModels();
120  hashSetsTableModel.fireTableDataChanged();
121  }
122 
123  private List<HashDb> validSetsOnly(List<HashDb> hashDbs) {
124  List<HashDb> validDbs = new ArrayList<>();
125  for (HashDb db : hashDbs) {
126  try {
127  if (db.isValid()) {
128  validDbs.add(db);
129  }
130  } catch (TskCoreException ex) {
131  Logger.getLogger(HashLookupModuleSettingsPanel.class.getName()).log(Level.SEVERE, "Error checking validity for hash set (name = " + db.getHashSetName() + ")", ex); //NON-NLS
132  }
133  }
134  return validDbs;
135  }
136 
137  void updateHashSetModels() {
138  List<HashDb> hashDbs = validSetsOnly(hashDbManager.getAllHashSets());
139 
140  List<HashDb> hashDatabases = new ArrayList<>(hashDbs);
141 
142  // Update the hash sets and detect deletions.
143  List<HashSetModel> deletedHashSetModels = new ArrayList<>();
144  for (HashSetModel model : hashSetModels) {
145  boolean foundDatabase = false;
146  for (HashDb db : hashDatabases) {
147  if (model.getDatabase().equals(db)) {
148  model.setValid(isHashDbValid(db));
149  hashDatabases.remove(db);
150  foundDatabase = true;
151  break;
152  }
153  }
154  if (!foundDatabase) {
155  deletedHashSetModels.add(model);
156  }
157  }
158 
159  // Remove the deleted hash sets.
160  for (HashSetModel model : deletedHashSetModels) {
161  hashSetModels.remove(model);
162  }
163 
164  // Add any new hash sets. All new sets are enabled by default.
165  for (HashDb db : hashDatabases) {
166  hashSetModels.add(new HashSetModel(db, true, isHashDbValid(db)));
167  }
168  }
169 
170  void reset(HashLookupModuleSettings newSettings) {
171  initializeHashSetModels(newSettings);
172  alwaysCalcHashesCheckbox.setSelected(newSettings.shouldCalculateHashes());
173  hashSetsTableModel.fireTableDataChanged();
174  }
175 
176  private boolean isHashDbValid(HashDb hashDb) {
177  boolean isValid = false;
178  try {
179  isValid = hashDb.isValid();
180  } catch (TskCoreException ex) {
181  Logger.getLogger(HashLookupModuleSettingsPanel.class.getName()).log(Level.SEVERE, "Error checking validity for hash set (name = " + hashDb.getHashSetName() + ")", ex); //NON-NLS
182  }
183  return isValid;
184  }
185 
186  private static final class HashSetModel {
187 
188  private final HashDb db;
189  private boolean valid;
190  private boolean enabled;
191 
192  HashSetModel(HashDb db, boolean enabled, boolean valid) {
193  this.db = db;
194  this.enabled = enabled;
195  this.valid = valid;
196  }
197 
198  HashDb getDatabase() {
199  return db;
200  }
201 
202  String getName() {
203  return db.getDisplayName();
204  }
205 
206  String getFormattedName() {
207  String knownTypeName = (db != null && db.getKnownFilesType() != null) ? db.getKnownFilesType().getDisplayName() : "";
208  if (!StringUtils.isBlank(knownTypeName)) {
209  knownTypeName = String.format(" (%s)", knownTypeName);
210  }
211 
212  String displayName = db != null ? db.getDisplayName() : "";
213  return displayName + knownTypeName;
214  }
215 
216  void setEnabled(boolean enabled) {
217  this.enabled = enabled;
218  }
219 
220  boolean isEnabled() {
221  return enabled;
222  }
223 
224  void setValid(boolean valid) {
225  this.valid = valid;
226  }
227 
228  boolean isValid() {
229  return valid;
230  }
231  }
232 
236  private static final class HashSetTableCellRenderer extends DefaultTableCellRenderer{
237 
238  private static final long serialVersionUID = 1L;
239  @Override
241  JTable table, Object value,
242  boolean isSelected, boolean hasFocus,
243  int row, int column) {
244  JLabel label = (JLabel)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
245  label.setToolTipText(label.getText());
246  return label;
247  }
248 
249  }
250 
251  private static final class HashSetsTableModel extends AbstractTableModel {
252 
253  private static final long serialVersionUID = 1L;
254  private final List<HashSetModel> hashSets;
255 
256  HashSetsTableModel(List<HashSetModel> hashSets) {
257  this.hashSets = hashSets;
258  }
259 
260  @Override
261  public int getRowCount() {
262  return hashSets.size();
263  }
264 
265  @Override
266  public int getColumnCount() {
267  return 2;
268  }
269 
270  @Override
271  public Object getValueAt(int rowIndex, int columnIndex) {
272  if (columnIndex == 0) {
273  return hashSets.get(rowIndex).isEnabled();
274  } else {
275  return hashSets.get(rowIndex).getFormattedName();
276  }
277  }
278 
279  @Override
280  public boolean isCellEditable(int rowIndex, int columnIndex) {
281  return (columnIndex == 0 && hashSets.get(rowIndex).isValid());
282  }
283 
284  @Override
285  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
286  if (columnIndex == 0) {
287  hashSets.get(rowIndex).setEnabled((Boolean) aValue);
288  }
289  }
290 
291  @Override
292  public Class<?> getColumnClass(int c) {
293  return getValueAt(0, c).getClass();
294  }
295  }
296 
302  @SuppressWarnings("unchecked")
303  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
304  private void initComponents() {
305 
306  hashDbsLabel = new javax.swing.JLabel();
307  hashDbsScrollPane = new javax.swing.JScrollPane();
308  hashTable = new javax.swing.JTable();
309  alwaysCalcHashesCheckbox = new javax.swing.JCheckBox();
310 
311  setPreferredSize(new java.awt.Dimension(292, 150));
312 
313  hashDbsLabel.setText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.hashDbsLabel.text")); // NOI18N
314 
315  hashDbsScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
316 
317  hashTable.setBackground(new java.awt.Color(240, 240, 240));
318  hashTable.setShowHorizontalLines(false);
319  hashTable.setShowVerticalLines(false);
320  hashDbsScrollPane.setViewportView(hashTable);
321 
322  alwaysCalcHashesCheckbox.setText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.text")); // NOI18N
323  alwaysCalcHashesCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.toolTipText")); // NOI18N
324  alwaysCalcHashesCheckbox.setMaximumSize(new java.awt.Dimension(290, 35));
325  alwaysCalcHashesCheckbox.setMinimumSize(new java.awt.Dimension(290, 35));
326  alwaysCalcHashesCheckbox.setPreferredSize(new java.awt.Dimension(271, 35));
327  alwaysCalcHashesCheckbox.setVerticalAlignment(javax.swing.SwingConstants.TOP);
328  alwaysCalcHashesCheckbox.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
329 
330  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
331  this.setLayout(layout);
332  layout.setHorizontalGroup(
333  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
334  .addGroup(layout.createSequentialGroup()
335  .addContainerGap()
336  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
337  .addGroup(layout.createSequentialGroup()
338  .addComponent(hashDbsLabel)
339  .addGap(0, 0, Short.MAX_VALUE))
340  .addGroup(layout.createSequentialGroup()
341  .addGap(10, 10, 10)
342  .addComponent(hashDbsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE))
343  .addComponent(alwaysCalcHashesCheckbox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
344  .addContainerGap())
345  );
346  layout.setVerticalGroup(
347  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
348  .addGroup(layout.createSequentialGroup()
349  .addGap(2, 2, 2)
350  .addComponent(hashDbsLabel)
351  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
352  .addComponent(hashDbsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
353  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
354  .addComponent(alwaysCalcHashesCheckbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
355  .addContainerGap())
356  );
357  }// </editor-fold>//GEN-END:initComponents
358 
359  // Variables declaration - do not modify//GEN-BEGIN:variables
360  private javax.swing.JCheckBox alwaysCalcHashesCheckbox;
361  private javax.swing.JLabel hashDbsLabel;
362  private javax.swing.JScrollPane hashDbsScrollPane;
363  private javax.swing.JTable hashTable;
364  // End of variables declaration//GEN-END:variables
365 }
synchronized void addPropertyChangeListener(PropertyChangeListener listener)
void addHashSets(List< HashSetModel > hashSetModels, List< HashDb > enabledHashSets, List< HashDb > disabledHashSets)
void customizeHashSetsTable(JScrollPane scrollPane, JTable table, HashSetsTableModel tableModel)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.