19 package org.sleuthkit.autopsy.modules.hashdatabase;
21 import java.awt.Dimension;
22 import java.awt.Toolkit;
24 import java.io.IOException;
25 import java.nio.file.Paths;
26 import java.util.logging.Level;
27 import javax.swing.JFileChooser;
28 import javax.swing.JFrame;
29 import javax.swing.JOptionPane;
30 import org.apache.commons.io.FilenameUtils;
31 import org.openide.util.NbBundle;
32 import org.openide.windows.WindowManager;
45 final class HashDbCreateDatabaseDialog
extends javax.swing.JDialog {
47 private static final String DEFAULT_FILE_NAME = NbBundle
48 .getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.defaultFileName");
49 private JFileChooser fileChooser = null;
50 private HashDb newHashDb = null;
51 private final static String LAST_FILE_PATH_KEY =
"HashDbCreate_Path";
58 HashDbCreateDatabaseDialog() {
59 super((JFrame) WindowManager.getDefault().getMainWindow(), NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.createHashDbMsg"),
true);
70 HashDb getHashDatabase() {
74 private void initFileChooser() {
75 fileChooser =
new JFileChooser() {
77 public void approveSelection() {
78 File selectedFile = getSelectedFile();
79 if (!FilenameUtils.getExtension(selectedFile.getName()).equalsIgnoreCase(HashDbManager.getHashDatabaseFileExtension())) {
80 if (JOptionPane.showConfirmDialog(
this,
81 NbBundle.getMessage(
this.getClass(),
82 "HashDbCreateDatabaseDialog.hashDbMustHaveFileExtensionMsg",
83 HashDbManager.getHashDatabaseFileExtension()),
84 NbBundle.getMessage(
this.getClass(),
85 "HashDbCreateDatabaseDialog.fileNameErr"),
86 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
91 if (selectedFile.exists()) {
92 if (JOptionPane.showConfirmDialog(
this,
93 NbBundle.getMessage(
this.getClass(),
94 "HashDbCreateDatabaseDialog.fileNameAlreadyExistsMsg"),
95 NbBundle.getMessage(
this.getClass(),
96 "HashDbCreateDatabaseDialog.fileExistsErr"),
97 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
102 super.approveSelection();
105 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
106 fileChooser.setDragEnabled(
false);
107 fileChooser.setMultiSelectionEnabled(
false);
110 private void display() {
111 Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
112 setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
121 @SuppressWarnings(
"unchecked")
123 private
void initComponents() {
125 buttonGroup1 =
new javax.swing.ButtonGroup();
126 saveAsButton =
new javax.swing.JButton();
127 cancelButton =
new javax.swing.JButton();
128 knownRadioButton =
new javax.swing.JRadioButton();
129 knownBadRadioButton =
new javax.swing.JRadioButton();
130 jLabel1 =
new javax.swing.JLabel();
131 hashSetNameTextField =
new javax.swing.JTextField();
132 jLabel2 =
new javax.swing.JLabel();
133 sendIngestMessagesCheckbox =
new javax.swing.JCheckBox();
134 jLabel3 =
new javax.swing.JLabel();
135 databasePathTextField =
new javax.swing.JTextField();
136 okButton =
new javax.swing.JButton();
138 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
140 org.openide.awt.Mnemonics.setLocalizedText(saveAsButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.saveAsButton.text"));
141 saveAsButton.addActionListener(
new java.awt.event.ActionListener() {
142 public void actionPerformed(java.awt.event.ActionEvent evt) {
143 saveAsButtonActionPerformed(evt);
147 org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.cancelButton.text"));
148 cancelButton.addActionListener(
new java.awt.event.ActionListener() {
149 public void actionPerformed(java.awt.event.ActionEvent evt) {
150 cancelButtonActionPerformed(evt);
154 buttonGroup1.add(knownRadioButton);
155 org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.knownRadioButton.text"));
156 knownRadioButton.addActionListener(
new java.awt.event.ActionListener() {
157 public void actionPerformed(java.awt.event.ActionEvent evt) {
158 knownRadioButtonActionPerformed(evt);
162 buttonGroup1.add(knownBadRadioButton);
163 knownBadRadioButton.setSelected(
true);
164 org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.knownBadRadioButton.text"));
165 knownBadRadioButton.addActionListener(
new java.awt.event.ActionListener() {
166 public void actionPerformed(java.awt.event.ActionEvent evt) {
167 knownBadRadioButtonActionPerformed(evt);
171 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel1.text"));
173 hashSetNameTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.hashSetNameTextField.text"));
175 org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel2.text"));
177 sendIngestMessagesCheckbox.setSelected(
true);
178 org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.sendIngestMessagesCheckbox.text"));
179 sendIngestMessagesCheckbox.addActionListener(
new java.awt.event.ActionListener() {
180 public void actionPerformed(java.awt.event.ActionEvent evt) {
181 sendIngestMessagesCheckboxActionPerformed(evt);
185 org.openide.awt.Mnemonics.setLocalizedText(jLabel3,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.jLabel3.text"));
187 databasePathTextField.setEditable(
false);
188 databasePathTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.databasePathTextField.text"));
190 org.openide.awt.Mnemonics.setLocalizedText(okButton,
org.openide.util.NbBundle.getMessage(HashDbCreateDatabaseDialog.class,
"HashDbCreateDatabaseDialog.okButton.text"));
191 okButton.addActionListener(
new java.awt.event.ActionListener() {
192 public void actionPerformed(java.awt.event.ActionEvent evt) {
193 okButtonActionPerformed(evt);
197 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(getContentPane());
198 getContentPane().setLayout(layout);
199 layout.setHorizontalGroup(
200 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
201 .addGroup(layout.createSequentialGroup()
203 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
204 .addComponent(sendIngestMessagesCheckbox)
205 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
206 .addGap(0, 0, Short.MAX_VALUE)
207 .addComponent(okButton)
208 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
209 .addComponent(cancelButton))
210 .addComponent(jLabel2)
211 .addGroup(layout.createSequentialGroup()
213 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
214 .addComponent(knownRadioButton)
215 .addComponent(knownBadRadioButton)))
216 .addGroup(layout.createSequentialGroup()
217 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
218 .addGroup(layout.createSequentialGroup()
219 .addComponent(jLabel1)
220 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
221 .addComponent(databasePathTextField))
222 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
223 .addComponent(jLabel3)
224 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
225 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)))
226 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
227 .addComponent(saveAsButton)))
231 layout.linkSize(javax.swing.SwingConstants.HORIZONTAL,
new java.awt.Component[] {cancelButton, okButton});
233 layout.setVerticalGroup(
234 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
235 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
237 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
238 .addComponent(jLabel3)
239 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
240 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
241 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
242 .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
243 .addComponent(saveAsButton)
244 .addComponent(jLabel1))
246 .addComponent(jLabel2)
247 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
248 .addComponent(knownRadioButton)
249 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
250 .addComponent(knownBadRadioButton)
251 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
252 .addComponent(sendIngestMessagesCheckbox)
254 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
255 .addComponent(cancelButton)
256 .addComponent(okButton))
257 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
263 private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
264 sendIngestMessagesCheckbox.setSelected(
false);
265 sendIngestMessagesCheckbox.setEnabled(
false);
268 private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
269 sendIngestMessagesCheckbox.setSelected(
true);
270 sendIngestMessagesCheckbox.setEnabled(
true);
273 private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
277 private void saveAsButtonActionPerformed(java.awt.event.ActionEvent evt) {
279 String lastBaseDirectory = Paths.get(PlatformUtil.getUserConfigDirectory(),
"HashDatabases").toString();
280 if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
281 lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY);
283 StringBuilder path =
new StringBuilder();
284 path.append(lastBaseDirectory);
285 File hashDbFolder =
new File(path.toString());
287 if (!hashDbFolder.exists()){
288 hashDbFolder.mkdir();
290 if (!hashSetNameTextField.getText().isEmpty()) {
291 path.append(File.separator).append(hashSetNameTextField.getText());
293 path.append(File.separator).append(DEFAULT_FILE_NAME);
295 path.append(
".").append(HashDbManager.getHashDatabaseFileExtension());
296 fileChooser.setSelectedFile(
new File(path.toString()));
297 if (fileChooser.showSaveDialog(
this) == JFileChooser.APPROVE_OPTION) {
298 File databaseFile = fileChooser.getSelectedFile();
299 databasePathTextField.setText(databaseFile.getCanonicalPath());
300 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
302 }
catch (IOException ex) {
303 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING,
"Couldn't get selected file path.", ex);
307 private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
311 if (hashSetNameTextField.getText().isEmpty()) {
312 JOptionPane.showMessageDialog(
this,
313 NbBundle.getMessage(
this.getClass(),
314 "HashDbCreateDatabaseDialog.mustEnterHashSetNameMsg"),
315 NbBundle.getMessage(
this.getClass(),
316 "HashDbCreateDatabaseDialog.createHashDbErr"),
317 JOptionPane.ERROR_MESSAGE);
321 if (databasePathTextField.getText().isEmpty()) {
322 JOptionPane.showMessageDialog(
this,
323 NbBundle.getMessage(
this.getClass(),
324 "HashDbCreateDatabaseDialog.mustEnterHashDbPathMsg"),
325 NbBundle.getMessage(
this.getClass(),
326 "HashDbCreateDatabaseDialog.createHashDbErr"),
327 JOptionPane.ERROR_MESSAGE);
332 if (knownRadioButton.isSelected()) {
333 type = KnownFilesType.KNOWN;
335 type = KnownFilesType.KNOWN_BAD;
338 String errorMessage = NbBundle
339 .getMessage(this.getClass(),
"HashDbCreateDatabaseDialog.errMsg.hashDbCreationErr");
341 newHashDb = HashDbManager.getInstance().addNewHashDatabaseNoSave(hashSetNameTextField.getText(), fileChooser.getSelectedFile().getCanonicalPath(),
true, sendIngestMessagesCheckbox.isSelected(), type);
342 }
catch (IOException ex) {
343 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
344 JOptionPane.showMessageDialog(
this,
345 NbBundle.getMessage(
this.getClass(),
346 "HashDbCreateDatabaseDialog.cannotCreateFileAtLocMsg"),
347 NbBundle.getMessage(
this.getClass(),
348 "HashDbCreateDatabaseDialog.createHashDbErr"),
349 JOptionPane.ERROR_MESSAGE);
351 }
catch (HashDbManagerException ex) {
352 Logger.getLogger(HashDbCreateDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
353 JOptionPane.showMessageDialog(
this,
355 NbBundle.getMessage(this.getClass(),
356 "HashDbCreateDatabaseDialog.createHashDbErr"),
357 JOptionPane.ERROR_MESSAGE);
364 private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {
369 private javax.swing.ButtonGroup buttonGroup1;
370 private javax.swing.JButton cancelButton;
371 private javax.swing.JTextField databasePathTextField;
372 private javax.swing.JTextField hashSetNameTextField;
373 private javax.swing.JLabel jLabel1;
374 private javax.swing.JLabel jLabel2;
375 private javax.swing.JLabel jLabel3;
376 private javax.swing.JRadioButton knownBadRadioButton;
377 private javax.swing.JRadioButton knownRadioButton;
378 private javax.swing.JButton okButton;
379 private javax.swing.JButton saveAsButton;
380 private javax.swing.JCheckBox sendIngestMessagesCheckbox;