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

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.