Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
EamDbSettingsDialog.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2020 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 package org.sleuthkit.autopsy.centralrepository.optionspanel;
20 
21 import java.awt.Color;
22 import java.awt.Component;
23 import java.awt.Cursor;
24 import java.awt.HeadlessException;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.Serializable;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.logging.Level;
32 import javax.swing.JDialog;
33 import javax.swing.JFileChooser;
34 import javax.swing.JFrame;
35 import javax.swing.JLabel;
36 import javax.swing.JList;
37 import javax.swing.JOptionPane;
38 import javax.swing.JTextField;
39 import javax.swing.ListCellRenderer;
40 import javax.swing.SwingUtilities;
41 import javax.swing.event.DocumentEvent;
42 import javax.swing.event.DocumentListener;
43 import javax.swing.filechooser.FileFilter;
44 import org.openide.util.NbBundle;
45 import org.openide.util.NbBundle.Messages;
46 import org.openide.windows.TopComponent;
47 import org.openide.windows.WindowManager;
56 
60 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
61 public class EamDbSettingsDialog extends JDialog {
62 
63  private static final Logger logger = Logger.getLogger(EamDbSettingsDialog.class.getName());
64 
65  private static final long serialVersionUID = 1L;
66 
71  private class DbChoiceRenderer extends JLabel implements ListCellRenderer<CentralRepoDbChoice>, Serializable {
72 
73  private static final long serialVersionUID = 1L;
74 
75  @Override
76  public Component getListCellRendererComponent(
77  JList<? extends CentralRepoDbChoice> list, CentralRepoDbChoice value,
78  int index, boolean isSelected, boolean cellHasFocus) {
79 
80  // disable cell if it is the db connection from multi user settings
81  // and that option is not enabled in multi user settings
82  setText(value.getTitle());
83  setEnabled(isDbChoiceSelectable(value));
84  return this;
85  }
86  }
87 
88  private final Collection<JTextField> textBoxes;
90  private final CentralRepoDbManager manager = new CentralRepoDbManager();
91  private final DbChoiceRenderer DB_CHOICE_RENDERER = new DbChoiceRenderer();
92 
94  this(null);
95  }
96 
97  private boolean isDbChoiceSelectable(CentralRepoDbChoice item) {
99  }
100 
104  @Messages({"EamDbSettingsDialog.title.text=Central Repository Database Configuration",
105  "EamDbSettingsDialog.lbSingleUserSqLite.text=SQLite should only be used by one examiner at a time.",
106  "EamDbSettingsDialog.lbDatabaseType.text=Database Type :",
107  "EamDbSettingsDialog.fcDatabasePath.title=Select location for central_repository.db"})
108  public EamDbSettingsDialog(CentralRepoDbChoice initialMenuItem) {
109  super((JFrame) WindowManager.getDefault().getMainWindow(),
110  Bundle.EamDbSettingsDialog_title_text(),
111  true);
112 
113  textBoxes = new ArrayList<>();
114  textBoxChangedListener = new TextBoxChangedListener();
115 
116  initComponents();
117  fcDatabasePath.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
118  fcDatabasePath.setAcceptAllFileFilterUsed(false);
119  fcDatabasePath.setDialogTitle(Bundle.EamDbSettingsDialog_fcDatabasePath_title());
120  fcDatabasePath.setFileFilter(new FileFilter() {
121  @Override
122  public boolean accept(File pathname) {
123  if (pathname.isDirectory()) {
124  return true;
125  }
126  return pathname.getName().equalsIgnoreCase(SqliteCentralRepoSettings.DEFAULT_DBNAME);
127  }
128 
129  @Override
130  public String getDescription() {
131  return "Directories and Central Repository databases";
132  }
133  });
134 
135  setupDbChoice(initialMenuItem);
136  valid();
137  display();
138  }
139 
140  private void setupDbChoice(CentralRepoDbChoice initialMenuItem) {
141  // setup initially selected item
142  CentralRepoDbChoice toSelect = (initialMenuItem == null)
143  ? (Arrays.asList(CentralRepoDbChoice.DB_CHOICES).contains(manager.getSelectedDbChoice()))
144  ? manager.getSelectedDbChoice()
146  : initialMenuItem;
147 
148  cbDatabaseType.setRenderer(DB_CHOICE_RENDERER);
149  changeDbSelection(toSelect);
150  }
151 
163  @NbBundle.Messages({"EamDbSettingsDialog.okButton.corruptDatabaseExists.title=Error Loading Central Repository Database",
164  "EamDbSettingsDialog.okButton.corruptDatabaseExists.message=Central Repository Database exists but is not the right format. Manually delete it or choose a different path (if applicable).",
165  "EamDbSettingsDialog.okButton.createDbDialog.title=Central Repository Database Does Not Exist",
166  "EamDbSettingsDialog.okButton.createDbDialog.message=Central Repository Database does not exist, would you like to create it?",
167  "EamDbSettingsDialog.okButton.databaseConnectionFailed.title=Central Repository Database Connection Failed",
168  "EamDbSettingsDialog.okButton.databaseConnectionFailed.message=Unable to connect to Central Repository Database. Please check your settings and try again.",
169  "EamDbSettingsDialog.okButton.createSQLiteDbError.message=Unable to create SQLite Central Repository Database, please ensure location exists and you have write permissions and try again.",
170  "EamDbSettingsDialog.okButton.createPostgresDbError.message=Unable to create Postgres Central Repository Database, please ensure address, port, and login credentials are correct for Postgres server and try again.",
171  "EamDbSettingsDialog.okButton.createDbError.title=Unable to Create Central Repository Database"})
172  private static boolean promptTestStatusWarnings(CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
174  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
175  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_message(),
176  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
177  JOptionPane.WARNING_MESSAGE);
178  } else if (manager.getStatus() == DatabaseTestResult.SCHEMA_INVALID) {
179  // There's an existing database or file, but it's not in our format.
180  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
181  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_message(),
182  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_title(),
183  JOptionPane.WARNING_MESSAGE);
184  } else if (manager.getStatus() == DatabaseTestResult.DB_DOES_NOT_EXIST) {
185  promptCreateDatabase(manager, dialog);
186  }
187 
188  return (manager.getStatus() == DatabaseTestResult.TESTED_OK);
189  }
190 
201  public static boolean promptCreateDatabase(CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
202  //database doesn't exist. do you want to create?
203  if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
204  Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
205  Bundle.EamDbSettingsDialog_okButton_createDbDialog_title(),
206  JOptionPane.YES_NO_OPTION)) {
207  try {
208  manager.createDb();
209 
210  } catch (CentralRepoException e) {
211  onPromptStatusError(manager);
212  return false;
213  }
214 
215  if (dialog != null) {
216  dialog.valid();
217  }
218  return true;
219  }
220 
221  return manager.testStatus() == DatabaseTestResult.TESTED_OK;
222  }
223 
232  private static void onPromptStatusError(CentralRepoDbManager manager1) {
233  // in the event that there is a failure to connect, notify user with corresponding message
234  String errorMessage = "";
235  if (manager1 == null || manager1.getSelectedDbChoice() == null) {
236  errorMessage = "";
238  errorMessage = Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message();
239  } else if (manager1.getSelectedDbChoice().getDbPlatform() == CentralRepoPlatforms.SQLITE) {
240  errorMessage = Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message();
241  }
242  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
243  errorMessage,
244  Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
245  JOptionPane.WARNING_MESSAGE);
246  }
247 
253  @SuppressWarnings("unchecked")
254  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
255  private void initComponents() {
256  java.awt.GridBagConstraints gridBagConstraints;
257 
258  fcDatabasePath = new javax.swing.JFileChooser();
259  pnButtons = new javax.swing.JPanel();
260  pnSQLiteSettings = new javax.swing.JPanel();
261  lbDatabasePath = new javax.swing.JLabel();
262  lbHostName = new javax.swing.JLabel();
263  tbDbHostname = new javax.swing.JTextField();
264  lbPort = new javax.swing.JLabel();
265  tbDbPort = new javax.swing.JTextField();
266  lbUserName = new javax.swing.JLabel();
267  tbDbUsername = new javax.swing.JTextField();
268  lbUserPassword = new javax.swing.JLabel();
269  jpDbPassword = new javax.swing.JPasswordField();
270  lbDatabaseType = new javax.swing.JLabel();
271  lbDatabaseDesc = new javax.swing.JLabel();
272  filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
273  dataBaseFileScrollPane = new javax.swing.JScrollPane();
274  dataBaseFileTextArea = new javax.swing.JTextArea();
275  pathPanel = new javax.swing.JPanel();
276  tfDatabasePath = new javax.swing.JTextField();
277  bnDatabasePathFileOpen = new javax.swing.JButton();
278  typePanel = new javax.swing.JPanel();
279  lbSingleUserSqLite = new javax.swing.JLabel();
280  cbDatabaseType = new javax.swing.JComboBox<>();
281  bnCancel = new javax.swing.JButton();
282  bnOk = new javax.swing.JButton();
283 
284  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
285  setResizable(false);
286 
287  javax.swing.GroupLayout pnButtonsLayout = new javax.swing.GroupLayout(pnButtons);
288  pnButtons.setLayout(pnButtonsLayout);
289  pnButtonsLayout.setHorizontalGroup(
290  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
291  .addGap(0, 0, Short.MAX_VALUE)
292  );
293  pnButtonsLayout.setVerticalGroup(
294  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
295  .addGap(0, 25, Short.MAX_VALUE)
296  );
297 
298  pnSQLiteSettings.setBorder(javax.swing.BorderFactory.createEtchedBorder());
299  pnSQLiteSettings.setLayout(new java.awt.GridBagLayout());
300 
301  org.openide.awt.Mnemonics.setLocalizedText(lbDatabasePath, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabasePath.text")); // NOI18N
302  lbDatabasePath.setMaximumSize(new java.awt.Dimension(191, 16));
303  lbDatabasePath.setPreferredSize(new java.awt.Dimension(100, 14));
304  gridBagConstraints = new java.awt.GridBagConstraints();
305  gridBagConstraints.gridx = 0;
306  gridBagConstraints.gridy = 1;
307  gridBagConstraints.gridwidth = 2;
308  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
309  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
310  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
311  pnSQLiteSettings.add(lbDatabasePath, gridBagConstraints);
312 
313  org.openide.awt.Mnemonics.setLocalizedText(lbHostName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbHostName.text")); // NOI18N
314  lbHostName.setMaximumSize(new java.awt.Dimension(195, 16));
315  lbHostName.setPreferredSize(new java.awt.Dimension(110, 14));
316  gridBagConstraints = new java.awt.GridBagConstraints();
317  gridBagConstraints.gridx = 0;
318  gridBagConstraints.gridy = 3;
319  gridBagConstraints.gridwidth = 2;
320  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
321  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
322  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
323  pnSQLiteSettings.add(lbHostName, gridBagConstraints);
324 
325  tbDbHostname.setPreferredSize(new java.awt.Dimension(519, 20));
326  gridBagConstraints = new java.awt.GridBagConstraints();
327  gridBagConstraints.gridx = 2;
328  gridBagConstraints.gridy = 3;
329  gridBagConstraints.gridheight = 2;
330  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
331  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
332  gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 6);
333  pnSQLiteSettings.add(tbDbHostname, gridBagConstraints);
334 
335  org.openide.awt.Mnemonics.setLocalizedText(lbPort, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbPort.text")); // NOI18N
336  lbPort.setMaximumSize(new java.awt.Dimension(132, 16));
337  lbPort.setPreferredSize(new java.awt.Dimension(90, 14));
338  gridBagConstraints = new java.awt.GridBagConstraints();
339  gridBagConstraints.gridx = 0;
340  gridBagConstraints.gridy = 5;
341  gridBagConstraints.gridwidth = 2;
342  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
343  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
344  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
345  pnSQLiteSettings.add(lbPort, gridBagConstraints);
346 
347  tbDbPort.setPreferredSize(new java.awt.Dimension(519, 20));
348  gridBagConstraints = new java.awt.GridBagConstraints();
349  gridBagConstraints.gridx = 2;
350  gridBagConstraints.gridy = 5;
351  gridBagConstraints.gridheight = 2;
352  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
353  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
354  gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 6);
355  pnSQLiteSettings.add(tbDbPort, gridBagConstraints);
356 
357  org.openide.awt.Mnemonics.setLocalizedText(lbUserName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserName.text")); // NOI18N
358  lbUserName.setMaximumSize(new java.awt.Dimension(172, 16));
359  lbUserName.setPreferredSize(new java.awt.Dimension(100, 14));
360  gridBagConstraints = new java.awt.GridBagConstraints();
361  gridBagConstraints.gridx = 0;
362  gridBagConstraints.gridy = 7;
363  gridBagConstraints.gridwidth = 2;
364  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
365  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
366  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
367  pnSQLiteSettings.add(lbUserName, gridBagConstraints);
368 
369  tbDbUsername.setPreferredSize(new java.awt.Dimension(519, 20));
370  gridBagConstraints = new java.awt.GridBagConstraints();
371  gridBagConstraints.gridx = 2;
372  gridBagConstraints.gridy = 7;
373  gridBagConstraints.gridheight = 2;
374  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
375  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
376  gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 6);
377  pnSQLiteSettings.add(tbDbUsername, gridBagConstraints);
378 
379  org.openide.awt.Mnemonics.setLocalizedText(lbUserPassword, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserPassword.text")); // NOI18N
380  lbUserPassword.setMaximumSize(new java.awt.Dimension(194, 16));
381  lbUserPassword.setPreferredSize(new java.awt.Dimension(110, 14));
382  gridBagConstraints = new java.awt.GridBagConstraints();
383  gridBagConstraints.gridx = 0;
384  gridBagConstraints.gridy = 9;
385  gridBagConstraints.gridwidth = 2;
386  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
387  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
388  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
389  pnSQLiteSettings.add(lbUserPassword, gridBagConstraints);
390 
391  jpDbPassword.setPreferredSize(new java.awt.Dimension(519, 20));
392  gridBagConstraints = new java.awt.GridBagConstraints();
393  gridBagConstraints.gridx = 2;
394  gridBagConstraints.gridy = 9;
395  gridBagConstraints.gridheight = 2;
396  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
397  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
398  gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 6);
399  pnSQLiteSettings.add(jpDbPassword, gridBagConstraints);
400 
401  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseType, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseType.text")); // NOI18N
402  lbDatabaseType.setMaximumSize(new java.awt.Dimension(180, 14));
403  lbDatabaseType.setMinimumSize(new java.awt.Dimension(80, 14));
404  lbDatabaseType.setPreferredSize(new java.awt.Dimension(100, 14));
405  gridBagConstraints = new java.awt.GridBagConstraints();
406  gridBagConstraints.gridx = 0;
407  gridBagConstraints.gridy = 0;
408  gridBagConstraints.gridwidth = 2;
409  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
410  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
411  gridBagConstraints.insets = new java.awt.Insets(12, 14, 0, 0);
412  pnSQLiteSettings.add(lbDatabaseType, gridBagConstraints);
413 
414  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseDesc, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseDesc.text")); // NOI18N
415  lbDatabaseDesc.setMaximumSize(new java.awt.Dimension(182, 16));
416  lbDatabaseDesc.setPreferredSize(new java.awt.Dimension(100, 14));
417  gridBagConstraints = new java.awt.GridBagConstraints();
418  gridBagConstraints.gridx = 0;
419  gridBagConstraints.gridy = 11;
420  gridBagConstraints.gridwidth = 2;
421  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
422  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
423  gridBagConstraints.insets = new java.awt.Insets(7, 14, 0, 0);
424  pnSQLiteSettings.add(lbDatabaseDesc, gridBagConstraints);
425  gridBagConstraints = new java.awt.GridBagConstraints();
426  gridBagConstraints.gridx = 0;
427  gridBagConstraints.gridy = 12;
428  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
429  pnSQLiteSettings.add(filler1, gridBagConstraints);
430 
431  dataBaseFileScrollPane.setBorder(null);
432 
433  dataBaseFileTextArea.setEditable(false);
434  dataBaseFileTextArea.setBackground(new java.awt.Color(240, 240, 240));
435  dataBaseFileTextArea.setColumns(20);
436  dataBaseFileTextArea.setLineWrap(true);
437  dataBaseFileTextArea.setRows(3);
438  dataBaseFileScrollPane.setViewportView(dataBaseFileTextArea);
439 
440  gridBagConstraints = new java.awt.GridBagConstraints();
441  gridBagConstraints.gridx = 2;
442  gridBagConstraints.gridy = 11;
443  gridBagConstraints.gridheight = 3;
444  gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
445  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
446  gridBagConstraints.weightx = 1.0;
447  gridBagConstraints.weighty = 1.0;
448  gridBagConstraints.insets = new java.awt.Insets(7, 10, 15, 6);
449  pnSQLiteSettings.add(dataBaseFileScrollPane, gridBagConstraints);
450 
451  tfDatabasePath.setText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.text")); // NOI18N
452  tfDatabasePath.setToolTipText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.toolTipText")); // NOI18N
453  tfDatabasePath.setPreferredSize(new java.awt.Dimension(420, 23));
454 
455  org.openide.awt.Mnemonics.setLocalizedText(bnDatabasePathFileOpen, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnDatabasePathFileOpen.text")); // NOI18N
456  bnDatabasePathFileOpen.addActionListener(new java.awt.event.ActionListener() {
457  public void actionPerformed(java.awt.event.ActionEvent evt) {
458  bnDatabasePathFileOpenActionPerformed(evt);
459  }
460  });
461 
462  javax.swing.GroupLayout pathPanelLayout = new javax.swing.GroupLayout(pathPanel);
463  pathPanel.setLayout(pathPanelLayout);
464  pathPanelLayout.setHorizontalGroup(
465  pathPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
466  .addGroup(pathPanelLayout.createSequentialGroup()
467  .addGap(0, 0, 0)
468  .addComponent(tfDatabasePath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
469  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
470  .addComponent(bnDatabasePathFileOpen)
471  .addGap(0, 0, 0))
472  );
473  pathPanelLayout.setVerticalGroup(
474  pathPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
475  .addGroup(pathPanelLayout.createSequentialGroup()
476  .addGap(0, 0, 0)
477  .addGroup(pathPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
478  .addComponent(tfDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
479  .addComponent(bnDatabasePathFileOpen))
480  .addGap(0, 0, 0))
481  );
482 
483  gridBagConstraints = new java.awt.GridBagConstraints();
484  gridBagConstraints.gridx = 2;
485  gridBagConstraints.gridy = 1;
486  gridBagConstraints.gridheight = 2;
487  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
488  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
489  gridBagConstraints.insets = new java.awt.Insets(7, 10, 0, 6);
490  pnSQLiteSettings.add(pathPanel, gridBagConstraints);
491 
492  org.openide.awt.Mnemonics.setLocalizedText(lbSingleUserSqLite, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbSingleUserSqLite.text")); // NOI18N
493  lbSingleUserSqLite.setPreferredSize(new java.awt.Dimension(381, 14));
494 
495  cbDatabaseType.setModel(new javax.swing.DefaultComboBoxModel<>(org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepoDbChoice.DB_CHOICES));
496  cbDatabaseType.setPreferredSize(new java.awt.Dimension(120, 20));
497  cbDatabaseType.addActionListener(new java.awt.event.ActionListener() {
498  public void actionPerformed(java.awt.event.ActionEvent evt) {
499  cbDatabaseTypeActionPerformed(evt);
500  }
501  });
502 
503  javax.swing.GroupLayout typePanelLayout = new javax.swing.GroupLayout(typePanel);
504  typePanel.setLayout(typePanelLayout);
505  typePanelLayout.setHorizontalGroup(
506  typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
507  .addGroup(typePanelLayout.createSequentialGroup()
508  .addGap(0, 0, 0)
509  .addComponent(cbDatabaseType, 0, 210, Short.MAX_VALUE)
510  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
511  .addComponent(lbSingleUserSqLite, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
512  .addGap(0, 0, 0))
513  );
514  typePanelLayout.setVerticalGroup(
515  typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
516  .addGroup(typePanelLayout.createSequentialGroup()
517  .addGap(0, 0, 0)
518  .addGroup(typePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
519  .addComponent(cbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
520  .addComponent(lbSingleUserSqLite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
521  .addGap(0, 0, 0))
522  );
523 
524  gridBagConstraints = new java.awt.GridBagConstraints();
525  gridBagConstraints.gridx = 2;
526  gridBagConstraints.gridy = 0;
527  gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
528  gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
529  gridBagConstraints.insets = new java.awt.Insets(12, 10, 2, 0);
530  pnSQLiteSettings.add(typePanel, gridBagConstraints);
531 
532  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnCancel.text")); // NOI18N
533  bnCancel.setMaximumSize(new java.awt.Dimension(79, 23));
534  bnCancel.setMinimumSize(new java.awt.Dimension(79, 23));
535  bnCancel.setPreferredSize(new java.awt.Dimension(79, 23));
536  bnCancel.addActionListener(new java.awt.event.ActionListener() {
537  public void actionPerformed(java.awt.event.ActionEvent evt) {
538  bnCancelActionPerformed(evt);
539  }
540  });
541 
542  org.openide.awt.Mnemonics.setLocalizedText(bnOk, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnOk.text")); // NOI18N
543  bnOk.addActionListener(new java.awt.event.ActionListener() {
544  public void actionPerformed(java.awt.event.ActionEvent evt) {
545  bnOkActionPerformed(evt);
546  }
547  });
548 
549  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
550  getContentPane().setLayout(layout);
551  layout.setHorizontalGroup(
552  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
553  .addGroup(layout.createSequentialGroup()
554  .addContainerGap()
555  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
556  .addGroup(layout.createSequentialGroup()
557  .addComponent(pnButtons, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
558  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
559  .addComponent(bnOk)
560  .addGap(11, 11, 11)
561  .addComponent(bnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
562  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE))
563  .addContainerGap())
564  );
565 
566  layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bnCancel, bnOk});
567 
568  layout.setVerticalGroup(
569  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
570  .addGroup(layout.createSequentialGroup()
571  .addContainerGap()
572  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
573  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
574  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
575  .addComponent(pnButtons, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
576  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
577  .addComponent(bnOk)
578  .addComponent(bnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
579  .addContainerGap())
580  );
581 
582  pack();
583  }// </editor-fold>//GEN-END:initComponents
584 
585  private void customizeComponents() {
586  setTextPrompts();
587  setTextBoxListeners();
588  manager.clearStatus();
590  updatePostgresFields(false);
591  updateSqliteFields(true);
593  updatePostgresFields(true);
594  updateSqliteFields(false);
595  } else {
596  updatePostgresFields(false);
597  updateSqliteFields(false);
598  }
599 
600  displayDatabaseSettings(manager.getSelectedDbChoice());
601  }
602 
603  private void display() {
604  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
605  setVisible(true);
606  }
607 
608  @Messages({"EamDbSettingsDialog.chooserPath.failedToGetDbPathMsg=Selected database path is invalid. Try again."})
609  private void bnDatabasePathFileOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDatabasePathFileOpenActionPerformed
610  fcDatabasePath.setSelectedFile(new File(manager.getDbSettingsSqlite().getDbDirectory()));
611  if (fcDatabasePath.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
612  File databaseFile = fcDatabasePath.getSelectedFile();
613  if (databaseFile.isFile()) {
614  databaseFile = fcDatabasePath.getCurrentDirectory();
615  }
616  try {
617  tfDatabasePath.setText(databaseFile.getCanonicalPath());
618  tfDatabasePath.setCaretPosition(tfDatabasePath.getText().length());
619  valid();
620  } catch (IOException ex) {
621  logger.log(Level.SEVERE, "Failed to get path of selected database file", ex); // NON-NLS
622  }
623  }
624  }//GEN-LAST:event_bnDatabasePathFileOpenActionPerformed
625 
626  @NbBundle.Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
627  "EamDbSettingsDialog.okButton.errorMsg.text=Please restart Autopsy to begin using the new database platform.",
628  "EamDbSettingsDialog.okButton.connectionErrorMsg.text=Failed to connect to central repository database."})
629  private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOkActionPerformed
630  if (testStatusAndCreate(this, manager, this)) {
631  dispose();
632  }
633  }//GEN-LAST:event_bnOkActionPerformed
634 
646  public static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager) {
647  return testStatusAndCreate(parent, manager, null);
648  }
649 
663  private static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
664  closePersonasTopComponent();
665 
666  parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
667  manager.testStatus();
668 
669  if (dialog != null) {
670  dialog.valid();
671  }
672 
673  boolean testedOk = promptTestStatusWarnings(manager, dialog);
674  if (!testedOk) {
675  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
676  return false;
677  }
678 
679  try {
680  manager.saveNewCentralRepo();
681  } catch (CentralRepoException e) {
682  SwingUtilities.invokeLater(() -> {
683  JOptionPane.showMessageDialog(parent,
684  Bundle.EamDbSettingsDialog_okButton_errorMsg_text(),
685  Bundle.EamDbSettingsDialog_okButton_errorTitle_text(),
686  JOptionPane.WARNING_MESSAGE);
687  });
688 
689  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
690  return false;
691  }
692 
693  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
694  return true;
695  }
696 
697 
698 
702  private static void closePersonasTopComponent() {
703  SwingUtilities.invokeLater(() -> {
704  TopComponent personasWindow = WindowManager.getDefault().findTopComponent("PersonasTopComponent");
705  if (personasWindow != null && personasWindow.isOpened()) {
706  personasWindow.close();
707  }
708  });
709  }
710 
711 
719  public boolean wasConfigurationChanged() {
720  return manager.wasConfigurationChanged();
721  }
722 
723  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
724  dispose();
725  }//GEN-LAST:event_bnCancelActionPerformed
726 
727 
728  private void cbDatabaseTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDatabaseTypeActionPerformed
729  CentralRepoDbChoice selectedItem = (CentralRepoDbChoice) cbDatabaseType.getSelectedItem();
730  changeDbSelection(selectedItem);
731  }//GEN-LAST:event_cbDatabaseTypeActionPerformed
732 
733  private void changeDbSelection(CentralRepoDbChoice selectedItem) {
734  if (isDbChoiceSelectable(selectedItem)) {
735  manager.setSelctedDbChoice(selectedItem);
736  cbDatabaseType.setSelectedItem(selectedItem);
737  } else {
738  cbDatabaseType.setSelectedItem(manager.getSelectedDbChoice());
739  }
740 
741  customizeComponents();
742  }
743 
744  private void updateFullDbPath() {
745  dataBaseFileTextArea.setText(tfDatabasePath.getText() + File.separator + SqliteCentralRepoSettings.DEFAULT_DBNAME);
746  dataBaseFileTextArea.setCaretPosition(dataBaseFileTextArea.getText().length());
747  }
748 
750  boolean isSqlite = choice == CentralRepoDbChoice.SQLITE;
751  boolean isPostgres = choice == CentralRepoDbChoice.POSTGRESQL_CUSTOM;
752 
753  lbDatabasePath.setVisible(isSqlite);
754  tfDatabasePath.setVisible(isSqlite);
755  lbDatabaseDesc.setVisible(isSqlite);
756  dataBaseFileTextArea.setVisible(isSqlite);
757  lbSingleUserSqLite.setVisible(isSqlite);
758  bnDatabasePathFileOpen.setVisible(isSqlite);
759 
760  lbHostName.setVisible(isPostgres);
761  tbDbHostname.setVisible(isPostgres);
762  lbPort.setVisible(isPostgres);
763  tbDbPort.setVisible(isPostgres);
764  lbUserName.setVisible(isPostgres);
765  tbDbUsername.setVisible(isPostgres);
766  lbUserPassword.setVisible(isPostgres);
767  jpDbPassword.setVisible(isPostgres);
768  }
769 
773  @Messages({"EamDbSettingsDialog.textPrompt.hostnameOrIP=Hostname or IP Address",
774  "EamDbSettingsDialog.textPrompt.port=Port Number",
775  "EamDbSettingsDialog.textPrompt.dbName=Database Name",
776  "EamDbSettingsDialog.textPrompt.user=Database User",
777  "EamDbSettingsDialog.textPrompt.password=Database User's Password"})
778  private void setTextPrompts() {
779  Collection<TextPrompt> textPrompts = new ArrayList<>();
780  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_hostnameOrIP(), tbDbHostname));
781  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_port(), tbDbPort));
782  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_user(), tbDbUsername));
783  configureTextPrompts(textPrompts);
784  }
785 
786  private void updatePostgresFields(boolean enabled) {
787  tbDbHostname.setText(enabled ? manager.getDbSettingsPostgres().getHost() : "");
788  tbDbHostname.setEnabled(enabled);
789  tbDbPort.setText(enabled ? Integer.toString(manager.getDbSettingsPostgres().getPort()) : "");
790  tbDbPort.setEnabled(enabled);
791  tbDbUsername.setText(enabled ? manager.getDbSettingsPostgres().getUserName() : "");
792  tbDbUsername.setEnabled(enabled);
793  jpDbPassword.setText(enabled ? manager.getDbSettingsPostgres().getPassword() : "");
794  jpDbPassword.setEnabled(enabled);
795  }
796 
803  private void updateSqliteFields(boolean enabled) {
804  tfDatabasePath.setText(enabled ? manager.getDbSettingsSqlite().getDbDirectory() : "");
805  tfDatabasePath.setEnabled(enabled);
806  bnDatabasePathFileOpen.setEnabled(enabled);
807  }
808 
812  private void setTextBoxListeners() {
813  textBoxes.add(tfDatabasePath);
814  textBoxes.add(tbDbHostname);
815  textBoxes.add(tbDbPort);
816  // textBoxes.add(tbDbName);
817  textBoxes.add(tbDbUsername);
818  textBoxes.add(jpDbPassword);
819  addDocumentListeners(textBoxes, textBoxChangedListener);
820  }
821 
828  private static void configureTextPrompts(Collection<TextPrompt> textPrompts) {
829  float alpha = 0.9f; // Mostly opaque
830  for (TextPrompt textPrompt : textPrompts) {
831  textPrompt.setForeground(Color.LIGHT_GRAY);
832  textPrompt.changeAlpha(alpha);
833  }
834  }
835 
842  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
843  textFields.forEach((textField) -> {
844  textField.getDocument().addDocumentListener(listener);
845  });
846  }
847 
854  @Messages({"EamDbSettingsDialog.validation.incompleteFields=Fill in all values for the selected database."})
855  private boolean databaseFieldsArePopulated() {
856  boolean result = true;
858  result = !tbDbHostname.getText().trim().isEmpty()
859  && !tbDbPort.getText().trim().isEmpty()
860  // && !tbDbName.getText().trim().isEmpty()
861  && !tbDbUsername.getText().trim().isEmpty()
862  && 0 < jpDbPassword.getPassword().length;
863  } else if (manager.getSelectedDbChoice() == CentralRepoDbChoice.SQLITE) {
864  result = !tfDatabasePath.getText().trim().isEmpty();
865  }
866 
867  return result;
868  }
869 
875  private boolean checkFields() {
876  return databaseFieldsArePopulated()
877  && databaseSettingsAreValid();
878  }
879 
885  private boolean valid() {
886  return enableOkButton(checkFields());
887  }
888 
895  @Messages({"EamDbSettingsDialog.validation.finished=Click OK to save your database settings and return to the Options. Or select a different database type."})
896  private boolean enableOkButton(boolean isValidInput) {
897  if (isValidInput) {
898  bnOk.setEnabled(true);
899  } else {
900  bnOk.setEnabled(false);
901  }
902  return true;
903 
904  }
905 
911  private boolean databaseSettingsAreValid() {
912  try {
914  tbDbHostname.getText().trim(),
915  tbDbPort.getText().trim(),
916  tbDbUsername.getText().trim(),
917  tfDatabasePath.getText().trim(),
918  new String(jpDbPassword.getPassword()));
919  } catch (CentralRepoException | NumberFormatException | IllegalStateException e) {
920  return false;
921  }
922 
923  return true;
924  }
925 
930  private class TextBoxChangedListener implements DocumentListener {
931 
932  @Override
933  public void changedUpdate(DocumentEvent e) {
934  manager.clearStatus();
935  updateFullDbPath();
936  valid();
937  }
938 
939  @Override
940  public void insertUpdate(DocumentEvent e) {
941  manager.clearStatus();
942  updateFullDbPath();
943  valid();
944  }
945 
946  @Override
947  public void removeUpdate(DocumentEvent e) {
948  manager.clearStatus();
949  updateFullDbPath();
950  valid();
951 
952  }
953  }
954 
955 
956  // Variables declaration - do not modify//GEN-BEGIN:variables
957  private javax.swing.JButton bnCancel;
958  private javax.swing.JButton bnDatabasePathFileOpen;
959  private javax.swing.JButton bnOk;
961  private javax.swing.JScrollPane dataBaseFileScrollPane;
962  private javax.swing.JTextArea dataBaseFileTextArea;
963  private javax.swing.JFileChooser fcDatabasePath;
964  private javax.swing.Box.Filler filler1;
965  private javax.swing.JPasswordField jpDbPassword;
966  private javax.swing.JLabel lbDatabaseDesc;
967  private javax.swing.JLabel lbDatabasePath;
968  private javax.swing.JLabel lbDatabaseType;
969  private javax.swing.JLabel lbHostName;
970  private javax.swing.JLabel lbPort;
971  private javax.swing.JLabel lbSingleUserSqLite;
972  private javax.swing.JLabel lbUserName;
973  private javax.swing.JLabel lbUserPassword;
974  private javax.swing.JPanel pathPanel;
975  private javax.swing.JPanel pnButtons;
976  private javax.swing.JPanel pnSQLiteSettings;
977  private javax.swing.JTextField tbDbHostname;
978  private javax.swing.JTextField tbDbPort;
979  private javax.swing.JTextField tbDbUsername;
980  private javax.swing.JTextField tfDatabasePath;
981  private javax.swing.JPanel typePanel;
982  // End of variables declaration//GEN-END:variables
983 }
static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager, EamDbSettingsDialog dialog)
Component getListCellRendererComponent(JList<?extends CentralRepoDbChoice > list, CentralRepoDbChoice value, int index, boolean isSelected, boolean cellHasFocus)
static void addDocumentListeners(Collection< JTextField > textFields, TextBoxChangedListener listener)
static boolean promptTestStatusWarnings(CentralRepoDbManager manager, EamDbSettingsDialog dialog)
boolean testDatabaseSettingsAreValid(String tbDbHostname, String tbDbPort, String tbDbUsername, String tfDatabasePath, String jpDbPassword)
static void configureTextPrompts(Collection< TextPrompt > textPrompts)
static boolean promptCreateDatabase(CentralRepoDbManager manager, EamDbSettingsDialog dialog)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager)
javax.swing.JComboBox< org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepoDbChoice > cbDatabaseType

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.