19 package org.sleuthkit.autopsy.modules.hashdatabase;
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import javax.swing.JLabel;
29 import javax.swing.JOptionPane;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextArea;
32 import javax.swing.SwingWorker;
33 import org.openide.util.NbBundle;
41 @SuppressWarnings(
"PMD.SingularField")
45 private boolean disposeParent =
false;
47 private final List<HashEntry>
hashes;
65 this.hashes =
new ArrayList<>();
66 this.invalidHashes =
new ArrayList<>();
67 this.md5Pattern = Pattern.compile(
"^[a-fA-F0-9]{32}$");
68 this.parentRef = parent;
74 setLocationRelativeTo(parent);
82 final void addHashValuesToDatabase() {
83 parentRef.enableAddHashValuesToDatabaseDialog(
false);
84 new SwingWorker<Object, Void>() {
87 protected Object doInBackground() throws Exception {
90 getHashesFromTextArea(text);
94 if (!invalidHashes.isEmpty()) {
99 for (String invalidHash : invalidHashes) {
100 errorMessage = errorMessage + invalidHash +
"\n";
102 showErrorsButton.setVisible(
true);
103 showErrorsButton.requestFocus();
104 }
else if (hashes.isEmpty()) {
105 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.noHashesToAdd"));
110 okButton.requestFocus();
111 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.success", hashes.size()));
113 disposeParent =
true;
114 }
catch (TskCoreException ex) {
115 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash"));
117 errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash.msg");
118 errorMessage = ex.toString();
119 showErrorsButton.setVisible(
true);
120 showErrorsButton.requestFocus();
135 okButton.setEnabled(
true);
136 addingHashesToDatabaseProgressBar.setIndeterminate(
false);
137 addingHashesToDatabaseProgressBar.setValue(addingHashesToDatabaseProgressBar.getMaximum());
140 addingHashesToDatabaseProgressBar.setStringPainted(
true);
141 addingHashesToDatabaseProgressBar.setForeground(
new Color(50, 205, 50));
142 addingHashesToDatabaseProgressBar.setString(
"");
145 addingHashesToDatabaseProgressBar.setStringPainted(
true);
146 addingHashesToDatabaseProgressBar.setForeground(
new Color(178, 34, 34));
147 addingHashesToDatabaseProgressBar.setString(
"");
160 String[] linesInTextArea = text.split(
"\\r?\\n");
162 for (String hashEntry : linesInTextArea) {
163 hashEntry = hashEntry.trim();
164 Matcher m = md5Pattern.matcher(hashEntry);
167 hashes.add(
new HashEntry(null, m.group(0), null, null, null));
169 if (!hashEntry.isEmpty()) {
170 invalidHashes.add(hashEntry);
181 @SuppressWarnings(
"unchecked")
183 private
void initComponents() {
185 addingHashesToDatabaseProgressBar =
new javax.swing.JProgressBar();
186 okButton =
new javax.swing.JButton();
187 statusLabel =
new javax.swing.JLabel();
188 showErrorsButton =
new javax.swing.JButton();
190 setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
193 addingHashesToDatabaseProgressBar.setIndeterminate(
true);
196 okButton.setEnabled(
false);
197 okButton.addActionListener(
new java.awt.event.ActionListener() {
198 public void actionPerformed(java.awt.event.ActionEvent evt) {
199 okButtonActionPerformed(evt);
206 showErrorsButton.setVisible(
false);
207 showErrorsButton.addActionListener(
new java.awt.event.ActionListener() {
208 public void actionPerformed(java.awt.event.ActionEvent evt) {
209 showErrorsButtonActionPerformed(evt);
213 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
214 getContentPane().setLayout(layout);
215 layout.setHorizontalGroup(
216 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
217 .addGroup(layout.createSequentialGroup()
219 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
220 .addGroup(layout.createSequentialGroup()
221 .addComponent(statusLabel)
222 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
223 .addComponent(showErrorsButton))
224 .addGroup(layout.createSequentialGroup()
225 .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
226 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
227 .addComponent(okButton)))
230 layout.setVerticalGroup(
231 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232 .addGroup(layout.createSequentialGroup()
234 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
235 .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
236 .addComponent(okButton))
237 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
238 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239 .addComponent(statusLabel)
240 .addComponent(showErrorsButton))
241 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
248 parentRef.enableAddHashValuesToDatabaseDialog(
true);
256 JLabel textLabel =
new JLabel(errorTitle);
257 JTextArea textArea =
new JTextArea(errorMessage);
258 textArea.setEditable(
false);
259 JScrollPane scrollPane =
new JScrollPane(textArea);
260 scrollPane.setPreferredSize(
new Dimension(250, 100));
261 Object[] jOptionPaneComponents = {textLabel, scrollPane};
262 JOptionPane.showMessageDialog(
this, jOptionPaneComponents,
"Error:\n", JOptionPane.OK_OPTION);
javax.swing.JProgressBar addingHashesToDatabaseProgressBar
void okButtonActionPerformed(java.awt.event.ActionEvent evt)
final List< String > invalidHashes
void display(Component parent)
javax.swing.JButton showErrorsButton
javax.swing.JLabel statusLabel
void showErrorsButtonActionPerformed(java.awt.event.ActionEvent evt)
void getHashesFromTextArea(String text)
javax.swing.JButton okButton
void finish(boolean success)
final AddHashValuesToDatabaseDialog parentRef
final List< HashEntry > hashes
abstract void addHashes(Content content)