20 package org.sleuthkit.autopsy.modules.hashdatabase;
22 import java.awt.Dimension;
23 import java.awt.Toolkit;
25 import java.io.IOException;
26 import java.util.logging.Level;
28 import org.openide.util.NbBundle;
30 import javax.swing.JFileChooser;
31 import javax.swing.JOptionPane;
32 import javax.swing.JFrame;
33 import org.apache.commons.io.FilenameUtils;
44 final class HashDbCreateDatabaseDialog
extends javax.swing.JDialog {
46 private static final String DEFAULT_FILE_NAME = NbBundle
47 .getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.defaultFileName");
48 private JFileChooser fileChooser = null;
49 private HashDb newHashDb = null;
56 HashDbCreateDatabaseDialog() {
57 super(
new JFrame(), NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.createHashDbMsg"),
true);
67 HashDb getHashDatabase() {
71 private void initFileChooser() {
72 fileChooser =
new JFileChooser() {
74 public void approveSelection() {
75 File selectedFile = getSelectedFile();
76 if (!FilenameUtils.getExtension(selectedFile.getName()).equalsIgnoreCase(HashDbManager.getHashDatabaseFileExtension())) {
77 if (JOptionPane.showConfirmDialog(
this,
78 NbBundle.getMessage(
this.getClass(),
79 "HashDbCreateDatabaseDialog.hashDbMustHaveFileExtensionMsg",
80 HashDbManager.getHashDatabaseFileExtension()),
81 NbBundle.getMessage(
this.getClass(),
82 "HashDbCreateDatabaseDialog.fileNameErr"),
83 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
88 if (selectedFile.exists()) {
89 if (JOptionPane.showConfirmDialog(
this,
90 NbBundle.getMessage(
this.getClass(),
91 "HashDbCreateDatabaseDialog.fileNameAlreadyExistsMsg"),
92 NbBundle.getMessage(
this.getClass(),
93 "HashDbCreateDatabaseDialog.fileExistsErr"),
94 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
99 super.approveSelection();
102 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
103 fileChooser.setDragEnabled(
false);
104 fileChooser.setMultiSelectionEnabled(
false);
107 private void display() {
108 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
109 setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
118 @SuppressWarnings(
"unchecked")
120 private
void initComponents() {
122 buttonGroup1 =
new javax.swing.ButtonGroup();
123 saveAsButton =
new javax.swing.JButton();
124 cancelButton =
new javax.swing.JButton();
125 knownRadioButton =
new javax.swing.JRadioButton();
126 knownBadRadioButton =
new javax.swing.JRadioButton();
127 jLabel1 =
new javax.swing.JLabel();
128 hashSetNameTextField =
new javax.swing.JTextField();
129 jLabel2 =
new javax.swing.JLabel();
130 sendIngestMessagesCheckbox =
new javax.swing.JCheckBox();
131 jLabel3 =
new javax.swing.JLabel();
132 databasePathTextField =
new javax.swing.JTextField();
133 okButton =
new javax.swing.JButton();
135 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
137 org.openide.awt.Mnemonics.setLocalizedText(saveAsButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.saveAsButton.text"));
138 saveAsButton.addActionListener(
new java.awt.event.ActionListener() {
139 public void actionPerformed(java.awt.event.ActionEvent evt) {
140 saveAsButtonActionPerformed(evt);
144 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.cancelButton.text"));
145 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
146 public void actionPerformed(java.awt.event.ActionEvent evt) {
147 cancelButtonActionPerformed(evt);
151 buttonGroup1.add(knownRadioButton);
152 org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.knownRadioButton.text"));
153 knownRadioButton.addActionListener(
new java.awt.event.ActionListener() {
154 public void actionPerformed(java.awt.event.ActionEvent evt) {
155 knownRadioButtonActionPerformed(evt);
159 buttonGroup1.add(knownBadRadioButton);
160 knownBadRadioButton.setSelected(
true);
161 org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.knownBadRadioButton.text"));
162 knownBadRadioButton.addActionListener(
new java.awt.event.ActionListener() {
163 public void actionPerformed(java.awt.event.ActionEvent evt) {
164 knownBadRadioButtonActionPerformed(evt);
168 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel1.text"));
170 hashSetNameTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.hashSetNameTextField.text"));
172 org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel2.text"));
174 sendIngestMessagesCheckbox.setSelected(
true);
175 org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.sendIngestMessagesCheckbox.text"));
176 sendIngestMessagesCheckbox.addActionListener(
new java.awt.event.ActionListener() {
177 public void actionPerformed(java.awt.event.ActionEvent evt) {
178 sendIngestMessagesCheckboxActionPerformed(evt);
182 org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel3.text"));
184 databasePathTextField.setEditable(
false);
185 databasePathTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.databasePathTextField.text"));
187 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.okButton.text"));
188 okButton.addActionListener(
new java.awt.event.ActionListener() {
189 public void actionPerformed(java.awt.event.ActionEvent evt) {
190 okButtonActionPerformed(evt);
194 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
195 getContentPane().setLayout(layout);
196 layout.setHorizontalGroup(
197 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
198 .addGroup(layout.createSequentialGroup()
200 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
201 .addComponent(sendIngestMessagesCheckbox)
202 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
203 .addGap(0, 0, Short.MAX_VALUE)
204 .addComponent(okButton)
205 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
206 .addComponent(cancelButton))
207 .addComponent(jLabel2)
208 .addGroup(layout.createSequentialGroup()
210 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
211 .addComponent(knownRadioButton)
212 .addComponent(knownBadRadioButton)))
213 .addGroup(layout.createSequentialGroup()
214 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
215 .addGroup(layout.createSequentialGroup()
216 .addComponent(jLabel1)
217 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
218 .addComponent(databasePathTextField))
219 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
220 .addComponent(jLabel3)
221 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
222 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)))
223 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
224 .addComponent(saveAsButton)))
228 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
new java.awt.Component[] {cancelButton, okButton});
230 layout.setVerticalGroup(
231 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
234 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
235 .addComponent(jLabel3)
236 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
237 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
238 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
239 .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
240 .addComponent(saveAsButton)
241 .addComponent(jLabel1))
243 .addComponent(jLabel2)
244 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
245 .addComponent(knownRadioButton)
246 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
247 .addComponent(knownBadRadioButton)
248 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
249 .addComponent(sendIngestMessagesCheckbox)
251 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
252 .addComponent(cancelButton)
253 .addComponent(okButton))
254 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
260 private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
261 sendIngestMessagesCheckbox.setSelected(
false);
262 sendIngestMessagesCheckbox.setEnabled(
false);
265 private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
266 sendIngestMessagesCheckbox.setSelected(
true);
267 sendIngestMessagesCheckbox.setEnabled(
true);
270 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
274 private void saveAsButtonActionPerformed(java.awt.event.ActionEvent evt) {
276 StringBuilder path =
new StringBuilder();
277 if (!hashSetNameTextField.getText().isEmpty()) {
278 path.append(hashSetNameTextField.getText());
281 path.append(DEFAULT_FILE_NAME);
283 path.append(
".").append(HashDbManager.getHashDatabaseFileExtension());
284 fileChooser.setSelectedFile(
new File(path.toString()));
285 if (fileChooser.showSaveDialog(
this) == JFileChooser.APPROVE_OPTION) {
286 File databaseFile = fileChooser.getSelectedFile();
287 databasePathTextField.setText(databaseFile.getCanonicalPath());
290 catch (IOException ex) {
291 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING,
"Couldn't get selected file path.", ex);
295 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
299 if (hashSetNameTextField.getText().isEmpty()) {
300 JOptionPane.showMessageDialog(
this,
301 NbBundle.getMessage(
this.getClass(),
302 "HashDbCreateDatabaseDialog.mustEnterHashSetNameMsg"),
303 NbBundle.getMessage(
this.getClass(),
304 "HashDbCreateDatabaseDialog.createHashDbErr"),
305 JOptionPane.ERROR_MESSAGE);
309 if (databasePathTextField.getText().isEmpty()) {
310 JOptionPane.showMessageDialog(
this,
311 NbBundle.getMessage(
this.getClass(),
312 "HashDbCreateDatabaseDialog.mustEnterHashDbPathMsg"),
313 NbBundle.getMessage(
this.getClass(),
314 "HashDbCreateDatabaseDialog.createHashDbErr"),
315 JOptionPane.ERROR_MESSAGE);
320 if (knownRadioButton.isSelected()) {
321 type = KnownFilesType.KNOWN;
324 type = KnownFilesType.KNOWN_BAD;
327 String errorMessage = NbBundle
328 .getMessage(this.getClass(),
"HashDbCreateDatabaseDialog.errMsg.hashDbCreationErr");
331 newHashDb = HashDbManager.getInstance().addNewHashDatabaseInternal(hashSetNameTextField.getText(), fileChooser.getSelectedFile().getCanonicalPath(),
true, sendIngestMessagesCheckbox.isSelected(), type);
333 catch (IOException ex) {
334 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
335 JOptionPane.showMessageDialog(
this,
336 NbBundle.getMessage(
this.getClass(),
337 "HashDbCreateDatabaseDialog.cannotCreateFileAtLocMsg"),
338 NbBundle.getMessage(
this.getClass(),
339 "HashDbCreateDatabaseDialog.createHashDbErr"),
340 JOptionPane.ERROR_MESSAGE);
343 catch (HashDbManagerException ex) {
344 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
345 JOptionPane.showMessageDialog(
this,
347 NbBundle.getMessage(this.getClass(),
348 "HashDbCreateDatabaseDialog.createHashDbErr"),
349 JOptionPane.ERROR_MESSAGE);
352 catch (TskCoreException ex) {
353 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.SEVERE, errorMessage, ex);
354 JOptionPane.showMessageDialog(
this,
355 NbBundle.getMessage(
this.getClass(),
356 "HashDbCreateDatabaseDialog.failedToCreateHashDbMsg"),
357 NbBundle.getMessage(
this.getClass(),
358 "HashDbCreateDatabaseDialog.createHashDbErr"),
359 JOptionPane.ERROR_MESSAGE);
366 private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {
371 private javax.swing.ButtonGroup buttonGroup1;
372 private javax.swing.JButton cancelButton;
373 private javax.swing.JTextField databasePathTextField;
374 private javax.swing.JTextField hashSetNameTextField;
375 private javax.swing.JLabel jLabel1;
376 private javax.swing.JLabel jLabel2;
377 private javax.swing.JLabel jLabel3;
378 private javax.swing.JRadioButton knownBadRadioButton;
379 private javax.swing.JRadioButton knownRadioButton;
380 private javax.swing.JButton okButton;
381 private javax.swing.JButton saveAsButton;
382 private javax.swing.JCheckBox sendIngestMessagesCheckbox;