Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashDbSearchPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.Color;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.KeyAdapter;
25 import java.awt.event.KeyEvent;
26 import java.util.ArrayList;
27 
28 import org.openide.util.NbBundle;
30 import javax.swing.JOptionPane;
31 import javax.swing.table.DefaultTableModel;
32 import javax.swing.text.AttributeSet;
33 import javax.swing.text.BadLocationException;
34 import javax.swing.text.PlainDocument;
36 
40 class HashDbSearchPanel extends javax.swing.JPanel implements ActionListener {
41 
42  private static final Logger logger = Logger.getLogger(HashDbSearchPanel.class.getName());
43  private static HashDbSearchPanel instance;
44 
48  public static HashDbSearchPanel getDefault() {
49  if (instance == null) {
50  instance = new HashDbSearchPanel();
51  }
52  return instance;
53  }
54 
58  public void refresh() {
59  boolean running = IngestManager.getInstance().isIngestRunning();
60  if (running) {
61  titleLabel.setForeground(Color.red);
62  titleLabel.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.titleText.ingestOngoing"));
63  } else {
64  titleLabel.setForeground(Color.black);
65  titleLabel.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.titleLabel.text"));
66  }
67  hashField.setEditable(!running);
68  searchButton.setEnabled(!running);
69  addButton.setEnabled(!running);
70  removeButton.setEnabled(!running);
71  hashTable.setEnabled(!running);
72  hashLabel.setEnabled(!running);
73  saveBox.setEnabled(!running);
74  }
75 
79  private HashDbSearchPanel() {
80  setName(HashDbPanelSearchAction.ACTION_NAME);
81  initComponents();
82  customInit();
83  }
84 
85  final void customInit() {
86  addButton.addActionListener(this);
87  removeButton.addActionListener(this);
88  errorField.setVisible(false);
89  hashField.requestFocus();
90  // Don't let the user input more characters than in an MD5 hash
91  hashField.setDocument(new PlainDocument() {
92  @Override
93  public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
94  if ((this.getLength() + str.length()) <= 32) {
95  super.insertString(offset, str, a);
96  }
97  }
98  });
99  // Pressing enter adds the hash
100  hashField.addKeyListener(new KeyAdapter() {
101  @Override
102  public void keyPressed(KeyEvent e) {
103  if (e.getKeyChar() == KeyEvent.VK_ENTER) {
104  addButton.doClick();
105  }
106  }
107  });
108  // Pressing delete removes the selected rows
109  hashTable.addKeyListener(new KeyAdapter() {
110  @Override
111  public void keyPressed(KeyEvent e) {
112  if (e.getKeyChar() == KeyEvent.VK_DELETE) {
113  removeButton.doClick();
114  }
115  }
116  });
117  }
118 
119  void addSearchActionListener(ActionListener l) {
120  for (ActionListener al : searchButton.getActionListeners()) {
121  searchButton.removeActionListener(al);
122  }
123  searchButton.addActionListener(l);
124  }
125 
126  void addCancelActionListener(ActionListener l) {
127  cancelButton.addActionListener(l);
128  }
129 
135  @SuppressWarnings("rawtypes")
136  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
137  private void initComponents() {
138 
139  jScrollPane1 = new javax.swing.JScrollPane();
140  hashTable = new javax.swing.JTable();
141  hashField = new javax.swing.JTextField();
142  addButton = new javax.swing.JButton();
143  hashLabel = new javax.swing.JLabel();
144  searchButton = new javax.swing.JButton();
145  removeButton = new javax.swing.JButton();
146  jSeparator1 = new javax.swing.JSeparator();
147  titleLabel = new javax.swing.JLabel();
148  errorField = new javax.swing.JLabel();
149  saveBox = new javax.swing.JCheckBox();
150  cancelButton = new javax.swing.JButton();
151 
152  hashTable.setModel(new javax.swing.table.DefaultTableModel(
153  new Object [][] {
154 
155  },
156  new String [] {
157  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.hashTable.defaultModel.title.text")
158  }
159  ) {
160  Class[] types = new Class [] {
161  java.lang.String.class
162  };
163  boolean[] canEdit = new boolean [] {
164  false
165  };
166 
167  public Class getColumnClass(int columnIndex) {
168  return types [columnIndex];
169  }
170 
171  public boolean isCellEditable(int rowIndex, int columnIndex) {
172  return canEdit [columnIndex];
173  }
174  });
175  jScrollPane1.setViewportView(hashTable);
176  hashTable.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashTable.columnModel.title0")); // NOI18N
177 
178  hashField.setText(org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashField.text")); // NOI18N
179 
180  org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.addButton.text")); // NOI18N
181 
182  org.openide.awt.Mnemonics.setLocalizedText(hashLabel, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashLabel.text")); // NOI18N
183 
184  org.openide.awt.Mnemonics.setLocalizedText(searchButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.searchButton.text")); // NOI18N
185 
186  org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.removeButton.text")); // NOI18N
187 
188  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.titleLabel.text")); // NOI18N
189 
190  errorField.setForeground(new java.awt.Color(255, 0, 0));
191  org.openide.awt.Mnemonics.setLocalizedText(errorField, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.errorField.text")); // NOI18N
192 
193  org.openide.awt.Mnemonics.setLocalizedText(saveBox, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.saveBox.text")); // NOI18N
194  saveBox.addActionListener(new java.awt.event.ActionListener() {
195  public void actionPerformed(java.awt.event.ActionEvent evt) {
196  saveBoxActionPerformed(evt);
197  }
198  });
199 
200  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.cancelButton.text")); // NOI18N
201 
202  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
203  this.setLayout(layout);
204  layout.setHorizontalGroup(
205  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
206  .addGroup(layout.createSequentialGroup()
207  .addContainerGap()
208  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
209  .addComponent(jScrollPane1)
210  .addGroup(layout.createSequentialGroup()
211  .addComponent(hashLabel)
212  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
213  .addComponent(hashField))
214  .addComponent(jSeparator1)
215  .addGroup(layout.createSequentialGroup()
216  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
217  .addComponent(titleLabel)
218  .addGroup(layout.createSequentialGroup()
219  .addGap(61, 61, 61)
220  .addComponent(addButton)
221  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
222  .addComponent(removeButton)
223  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
224  .addComponent(saveBox)))
225  .addGap(0, 0, Short.MAX_VALUE))
226  .addGroup(layout.createSequentialGroup()
227  .addComponent(errorField)
228  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
229  .addComponent(searchButton)
230  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
231  .addComponent(cancelButton)))
232  .addContainerGap())
233  );
234  layout.setVerticalGroup(
235  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
236  .addGroup(layout.createSequentialGroup()
237  .addContainerGap()
238  .addComponent(titleLabel)
239  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
240  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
241  .addGap(18, 18, 18)
242  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
243  .addComponent(hashLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
244  .addComponent(hashField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
245  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
246  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
247  .addComponent(addButton)
248  .addComponent(removeButton)
249  .addComponent(saveBox))
250  .addGap(18, 18, 18)
251  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
252  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
253  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
254  .addComponent(searchButton)
255  .addComponent(errorField)
256  .addComponent(cancelButton))
257  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
258  );
259  }// </editor-fold>//GEN-END:initComponents
260 
261  private void saveBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBoxActionPerformed
262  // TODO add your handling code here:
263  }//GEN-LAST:event_saveBoxActionPerformed
264 
265  // Variables declaration - do not modify//GEN-BEGIN:variables
266  private javax.swing.JButton addButton;
267  private javax.swing.JButton cancelButton;
268  private javax.swing.JLabel errorField;
269  private javax.swing.JTextField hashField;
270  private javax.swing.JLabel hashLabel;
271  private javax.swing.JTable hashTable;
272  private javax.swing.JScrollPane jScrollPane1;
273  private javax.swing.JSeparator jSeparator1;
274  private javax.swing.JButton removeButton;
275  private javax.swing.JCheckBox saveBox;
276  private javax.swing.JButton searchButton;
277  private javax.swing.JLabel titleLabel;
278  // End of variables declaration//GEN-END:variables
279 
280  @Override
281  public void actionPerformed(ActionEvent e) {
282  if (e.getSource().equals(addButton)) {
283  add();
284  } else if (e.getSource().equals(removeButton)) {
285  remove();
286  }
287  }
288 
293  boolean search() {
294  // Check if any hashed have been entered
295  if (hashTable.getRowCount() != 0) {
296  // Make sure at least 1 file has an md5 hash
297  if (HashDbSearcher.countFilesMd5Hashed() > 0) {
298  return doSearch();
299  } else {
300  JOptionPane.showMessageDialog(null,
301  NbBundle.getMessage(this.getClass(),
302  "HashDbSearchPanel.noFilesHaveMD5HashMsg"),
303  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.dlgMsg.title"),
304  JOptionPane.ERROR_MESSAGE);
305  return false;
306  }
307  } else {
308  errorField.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.noHashesAddedMsg"));
309  errorField.setVisible(true);
310  return false;
311  }
312  }
313 
314  private boolean doSearch() {
315  errorField.setVisible(false);
316  // Get all the rows in the table
317  int numRows = hashTable.getRowCount();
318  ArrayList<String> hashes = new ArrayList<String>();
319  for (int i = 0; i < numRows; i++) {
320  hashes.add((String) hashTable.getValueAt(i, 0));
321  }
322  // Start a new thread and find the hashes
323  HashDbSearchThread hashThread = new HashDbSearchThread(hashes);
324  hashThread.execute();
325  return true;
326  }
327 
331  void add() {
332  errorField.setVisible(false);
333  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
334  String hash = hashField.getText();
335  if (!hash.equals("")) {
336  if (hash.matches("[a-fA-F0-9]{32}")) {
337  for (int i = 0; i < model.getRowCount(); i++) {
338  if (model.getValueAt(i, 0).equals(hashField.getText())) {
339  hashField.setText("");
340  errorField.setText(
341  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.hashAlreadyAddedMsg"));
342  errorField.setVisible(true);
343  errorField.setVisible(true);
344  return;
345  }
346  }
347  model.addRow(new String[]{hash});
348  hashField.setText(""); // wipe the field
349  } else {
350  errorField.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.invalidMD5HashMsg"));
351  errorField.setVisible(true);
352  }
353  }
354  hashField.requestFocus(); // select the field to type in
355  }
356 
360  void remove() {
361  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
362  int rows[] = hashTable.getSelectedRows();
363  // Loop backwards to delete highest row index first, otherwise
364  // index numbers change and the wrong rows are deleted
365  for (int i = rows.length - 1; i >= 0; i--) {
366  model.removeRow(rows[i]);
367  }
368  }
369 
373  void clear() {
374  if (!saveBox.isSelected()) {
375  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
376  int numRows = hashTable.getRowCount();
377  for (int i = numRows - 1; i >= 0; i--) {
378  model.removeRow(i);
379  }
380  }
381  errorField.setVisible(false);
382  hashField.setText("");
383  }
384 }

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