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.apache.commons.lang3.StringUtils;
34 import org.openide.util.NbBundle;
42 @SuppressWarnings(
"PMD.SingularField")
46 private boolean disposeParent =
false;
48 private final List<HashEntry>
hashes;
52 private static final Pattern HASH_LINE_PATTERN = Pattern.compile(
"^([a-fA-F0-9]{32})(,(.*))?$");
54 private static final int HASH_GROUP = 1;
56 private static final int COMMENT_GROUP = 3;
73 this.hashes =
new ArrayList<>();
74 this.invalidHashes =
new ArrayList<>();
75 this.parentRef = parent;
81 setLocationRelativeTo(parent);
89 final void addHashValuesToDatabase() {
90 parentRef.enableAddHashValuesToDatabaseDialog(
false);
91 new SwingWorker<Object, Void>() {
94 protected Object doInBackground() throws Exception {
97 getHashesFromTextArea(text);
101 if (!invalidHashes.isEmpty()) {
106 for (String invalidHash : invalidHashes) {
107 errorMessage = errorMessage + invalidHash +
"\n";
109 showErrorsButton.setVisible(
true);
110 showErrorsButton.requestFocus();
111 }
else if (hashes.isEmpty()) {
112 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.noHashesToAdd"));
117 okButton.requestFocus();
118 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.success", hashes.size()));
120 disposeParent =
true;
121 }
catch (TskCoreException ex) {
122 statusLabel.setText(NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash"));
124 errorTitle = NbBundle.getMessage(AddHashValuesToDatabaseProgressDialog.class,
"AddHashValuesToDatabaseProgressDialog.addHashValuesToDatabase.errorAddingValidHash.msg");
125 errorMessage = ex.toString();
126 showErrorsButton.setVisible(
true);
127 showErrorsButton.requestFocus();
142 okButton.setEnabled(
true);
143 addingHashesToDatabaseProgressBar.setIndeterminate(
false);
144 addingHashesToDatabaseProgressBar.setValue(addingHashesToDatabaseProgressBar.getMaximum());
147 addingHashesToDatabaseProgressBar.setStringPainted(
true);
148 addingHashesToDatabaseProgressBar.setForeground(
new Color(50, 205, 50));
149 addingHashesToDatabaseProgressBar.setString(
"");
152 addingHashesToDatabaseProgressBar.setStringPainted(
true);
153 addingHashesToDatabaseProgressBar.setForeground(
new Color(178, 34, 34));
154 addingHashesToDatabaseProgressBar.setString(
"");
167 String[] linesInTextArea = text.split(
"\\r?\\n");
169 for (String hashEntry : linesInTextArea) {
170 hashEntry = hashEntry.trim();
171 Matcher m = HASH_LINE_PATTERN.matcher(hashEntry);
173 String hash = m.group(HASH_GROUP);
176 String comment = StringUtils.isNotBlank(m.group(COMMENT_GROUP)) ?
177 m.group(COMMENT_GROUP).trim() : null;
179 hashes.add(
new HashEntry(null, hash, null, null, comment));
181 if (!hashEntry.isEmpty()) {
182 invalidHashes.add(hashEntry);
193 @SuppressWarnings(
"unchecked")
195 private
void initComponents() {
197 addingHashesToDatabaseProgressBar =
new javax.swing.JProgressBar();
198 okButton =
new javax.swing.JButton();
199 statusLabel =
new javax.swing.JLabel();
200 showErrorsButton =
new javax.swing.JButton();
202 setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
205 addingHashesToDatabaseProgressBar.setIndeterminate(
true);
208 okButton.setEnabled(
false);
209 okButton.addActionListener(
new java.awt.event.ActionListener() {
210 public void actionPerformed(java.awt.event.ActionEvent evt) {
211 okButtonActionPerformed(evt);
218 showErrorsButton.setVisible(
false);
219 showErrorsButton.addActionListener(
new java.awt.event.ActionListener() {
220 public void actionPerformed(java.awt.event.ActionEvent evt) {
221 showErrorsButtonActionPerformed(evt);
225 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
226 getContentPane().setLayout(layout);
227 layout.setHorizontalGroup(
228 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
229 .addGroup(layout.createSequentialGroup()
231 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232 .addGroup(layout.createSequentialGroup()
233 .addComponent(statusLabel)
234 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
235 .addComponent(showErrorsButton))
236 .addGroup(layout.createSequentialGroup()
237 .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
238 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
239 .addComponent(okButton)))
242 layout.setVerticalGroup(
243 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
244 .addGroup(layout.createSequentialGroup()
246 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
247 .addComponent(addingHashesToDatabaseProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
248 .addComponent(okButton))
249 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
251 .addComponent(statusLabel)
252 .addComponent(showErrorsButton))
253 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
260 parentRef.enableAddHashValuesToDatabaseDialog(
true);
268 JLabel textLabel =
new JLabel(errorTitle);
269 JTextArea textArea =
new JTextArea(errorMessage);
270 textArea.setEditable(
false);
271 JScrollPane scrollPane =
new JScrollPane(textArea);
272 scrollPane.setPreferredSize(
new Dimension(250, 100));
273 Object[] jOptionPaneComponents = {textLabel, scrollPane};
274 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)