Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashDbCreateDatabaseDialog.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 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 
20 package org.sleuthkit.autopsy.modules.hashdatabase;
21 
22 import java.awt.Dimension;
23 import java.awt.Toolkit;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.logging.Level;
27 
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;
38 
44 final class HashDbCreateDatabaseDialog extends javax.swing.JDialog {
45 
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;
50 
56  HashDbCreateDatabaseDialog() {
57  super(new JFrame(), NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.createHashDbMsg"), true);
58  initFileChooser();
59  initComponents();
60  display();
61  }
62 
67  HashDb getHashDatabase() {
68  return newHashDb;
69  }
70 
71  private void initFileChooser() {
72  fileChooser = new JFileChooser() {
73  @Override
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) {
84  cancelSelection();
85  }
86  return;
87  }
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) {
95  cancelSelection();
96  }
97  return;
98  }
99  super.approveSelection();
100  }
101  };
102  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
103  fileChooser.setDragEnabled(false);
104  fileChooser.setMultiSelectionEnabled(false);
105  }
106 
107  private void display() {
108  Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
109  setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
110  setVisible(true);
111  }
112 
118  @SuppressWarnings("unchecked")
119  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
120  private void initComponents() {
121 
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();
134 
135  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
136 
137  org.openide.awt.Mnemonics.setLocalizedText(saveAsButton, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.saveAsButton.text")); // NOI18N
138  saveAsButton.addActionListener(new java.awt.event.ActionListener() {
139  public void actionPerformed(java.awt.event.ActionEvent evt) {
140  saveAsButtonActionPerformed(evt);
141  }
142  });
143 
144  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.cancelButton.text")); // NOI18N
145  cancelButton.addActionListener(new java.awt.event.ActionListener() {
146  public void actionPerformed(java.awt.event.ActionEvent evt) {
147  cancelButtonActionPerformed(evt);
148  }
149  });
150 
151  buttonGroup1.add(knownRadioButton);
152  org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.knownRadioButton.text")); // NOI18N
153  knownRadioButton.addActionListener(new java.awt.event.ActionListener() {
154  public void actionPerformed(java.awt.event.ActionEvent evt) {
155  knownRadioButtonActionPerformed(evt);
156  }
157  });
158 
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")); // NOI18N
162  knownBadRadioButton.addActionListener(new java.awt.event.ActionListener() {
163  public void actionPerformed(java.awt.event.ActionEvent evt) {
164  knownBadRadioButtonActionPerformed(evt);
165  }
166  });
167 
168  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.jLabel1.text")); // NOI18N
169 
170  hashSetNameTextField.setText(org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.hashSetNameTextField.text")); // NOI18N
171 
172  org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.jLabel2.text")); // NOI18N
173 
174  sendIngestMessagesCheckbox.setSelected(true);
175  org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.sendIngestMessagesCheckbox.text")); // NOI18N
176  sendIngestMessagesCheckbox.addActionListener(new java.awt.event.ActionListener() {
177  public void actionPerformed(java.awt.event.ActionEvent evt) {
178  sendIngestMessagesCheckboxActionPerformed(evt);
179  }
180  });
181 
182  org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.jLabel3.text")); // NOI18N
183 
184  databasePathTextField.setEditable(false);
185  databasePathTextField.setText(org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.databasePathTextField.text")); // NOI18N
186 
187  org.openide.awt.Mnemonics.setLocalizedText(okButton, org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class, "HashDbCreateDatabaseDialog.okButton.text")); // NOI18N
188  okButton.addActionListener(new java.awt.event.ActionListener() {
189  public void actionPerformed(java.awt.event.ActionEvent evt) {
190  okButtonActionPerformed(evt);
191  }
192  });
193 
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()
199  .addContainerGap()
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()
209  .addGap(20, 20, 20)
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)))
225  .addContainerGap())
226  );
227 
228  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
229 
230  layout.setVerticalGroup(
231  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
233  .addGap(2, 2, 2)
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))
242  .addGap(7, 7, 7)
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)
250  .addGap(26, 26, 26)
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))
255  );
256 
257  pack();
258  }// </editor-fold>//GEN-END:initComponents
259 
260  private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownRadioButtonActionPerformed
261  sendIngestMessagesCheckbox.setSelected(false);
262  sendIngestMessagesCheckbox.setEnabled(false);
263  }//GEN-LAST:event_knownRadioButtonActionPerformed
264 
265  private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_knownBadRadioButtonActionPerformed
266  sendIngestMessagesCheckbox.setSelected(true);
267  sendIngestMessagesCheckbox.setEnabled(true);
268  }//GEN-LAST:event_knownBadRadioButtonActionPerformed
269 
270  private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
271  dispose();
272  }//GEN-LAST:event_cancelButtonActionPerformed
273 
274  private void saveAsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsButtonActionPerformed
275  try {
276  StringBuilder path = new StringBuilder();
277  if (!hashSetNameTextField.getText().isEmpty()) {
278  path.append(hashSetNameTextField.getText());
279  }
280  else {
281  path.append(DEFAULT_FILE_NAME);
282  }
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());
288  }
289  }
290  catch (IOException ex) {
291  Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, "Couldn't get selected file path.", ex); //NON-NLS
292  }
293  }//GEN-LAST:event_saveAsButtonActionPerformed
294 
295  private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
296  // Note that the error handlers in this method call return without disposing of the
297  // dialog to allow the user to try again, if desired.
298 
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);
306  return;
307  }
308 
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);
316  return;
317  }
318 
319  KnownFilesType type;
320  if (knownRadioButton.isSelected()) {
321  type = KnownFilesType.KNOWN;
322  }
323  else {
324  type = KnownFilesType.KNOWN_BAD;
325  }
326 
327  String errorMessage = NbBundle
328  .getMessage(this.getClass(), "HashDbCreateDatabaseDialog.errMsg.hashDbCreationErr");
329  try
330  {
331  newHashDb = HashDbManager.getInstance().addNewHashDatabaseInternal(hashSetNameTextField.getText(), fileChooser.getSelectedFile().getCanonicalPath(), true, sendIngestMessagesCheckbox.isSelected(), type);
332  }
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);
341  return;
342  }
343  catch (HashDbManagerException ex) {
344  Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
345  JOptionPane.showMessageDialog(this,
346  ex.getMessage(),
347  NbBundle.getMessage(this.getClass(),
348  "HashDbCreateDatabaseDialog.createHashDbErr"),
349  JOptionPane.ERROR_MESSAGE);
350  return;
351  }
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);
360  return;
361  }
362 
363  dispose();
364  }//GEN-LAST:event_okButtonActionPerformed
365 
366  private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendIngestMessagesCheckboxActionPerformed
367  // TODO add your handling code here:
368  }//GEN-LAST:event_sendIngestMessagesCheckboxActionPerformed
369 
370  // Variables declaration - do not modify//GEN-BEGIN:variables
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;
383  // End of variables declaration//GEN-END:variables
384 }

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.