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

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.