Autopsy  4.14.0
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.netbeans.spi.options.OptionsPanelController;
45 import org.openide.util.NbBundle;
46 import org.openide.util.NbBundle.Messages;
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 
70  private class DbChoiceRenderer extends JLabel implements ListCellRenderer<CentralRepoDbChoice>, Serializable {
71  private static final long serialVersionUID = 1L;
72 
73  @Override
74  public Component getListCellRendererComponent(
75  JList<? extends CentralRepoDbChoice> list, CentralRepoDbChoice value,
76  int index, boolean isSelected, boolean cellHasFocus) {
77 
78  // disable cell if it is the db connection from multi user settings
79  // and that option is not enabled in multi user settings
80  setText(value.getTitle());
81  setEnabled(isDbChoiceSelectable(value));
82  return this;
83  }
84  }
85 
86 
87  private final Collection<JTextField> textBoxes;
89  private final CentralRepoDbManager manager = new CentralRepoDbManager();
90  private final DbChoiceRenderer DB_CHOICE_RENDERER = new DbChoiceRenderer();
91 
93  this(null);
94  }
95 
96  private boolean isDbChoiceSelectable(CentralRepoDbChoice item) {
98  }
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 
141  private void setupDbChoice(CentralRepoDbChoice initialMenuItem) {
142  // setup initially selected item
143  CentralRepoDbChoice toSelect = (initialMenuItem == null) ?
144  (Arrays.asList(CentralRepoDbChoice.DB_CHOICES).contains(manager.getSelectedDbChoice())) ?
145  manager.getSelectedDbChoice() :
147  initialMenuItem;
148 
149  cbDatabaseType.setRenderer(DB_CHOICE_RENDERER);
150  changeDbSelection(toSelect);
151  }
152 
153 
154 
161  @NbBundle.Messages({"EamDbSettingsDialog.okButton.corruptDatabaseExists.title=Error Loading Central Repository Database",
162  "EamDbSettingsDialog.okButton.corruptDatabaseExists.message=Central Repository Database exists but is not the right format. Manually delete it or choose a different path (if applicable).",
163  "EamDbSettingsDialog.okButton.createDbDialog.title=Central Repository Database Does Not Exist",
164  "EamDbSettingsDialog.okButton.createDbDialog.message=Central Repository Database does not exist, would you like to create it?",
165  "EamDbSettingsDialog.okButton.databaseConnectionFailed.title=Central Repository Database Connection Failed",
166  "EamDbSettingsDialog.okButton.databaseConnectionFailed.message=Unable to connect to Central Repository Database. Please check your settings and try again.",
167  "EamDbSettingsDialog.okButton.createSQLiteDbError.message=Unable to create SQLite Central Repository Database, please ensure location exists and you have write permissions and try again.",
168  "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.",
169  "EamDbSettingsDialog.okButton.createDbError.title=Unable to Create Central Repository Database"})
170  private static boolean promptTestStatusWarnings(CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
172  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
173  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_message(),
174  Bundle.EamDbSettingsDialog_okButton_databaseConnectionFailed_title(),
175  JOptionPane.WARNING_MESSAGE);
176  } else if (manager.getStatus() == DatabaseTestResult.SCHEMA_INVALID) {
177  // There's an existing database or file, but it's not in our format.
178  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
179  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_message(),
180  Bundle.EamDbSettingsDialog_okButton_corruptDatabaseExists_title(),
181  JOptionPane.WARNING_MESSAGE);
182  } else if (manager.getStatus() == DatabaseTestResult.DB_DOES_NOT_EXIST) {
183  promptCreateDatabase(manager, dialog);
184  }
185 
186  return (manager.getStatus() == DatabaseTestResult.TESTED_OK);
187  }
188 
189 
199  public static boolean promptCreateDatabase(CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
200  //database doesn't exist. do you want to create?
201  if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(WindowManager.getDefault().getMainWindow(),
202  Bundle.EamDbSettingsDialog_okButton_createDbDialog_message(),
203  Bundle.EamDbSettingsDialog_okButton_createDbDialog_title(),
204  JOptionPane.YES_NO_OPTION)) {
205  try {
206  manager.createDb();
207 
208  } catch (CentralRepoException e) {
209  onPromptStatusError(manager);
210  return false;
211  }
212 
213  if (dialog != null) {
214  dialog.valid();
215  }
216  return true;
217  }
218 
219  return manager.testStatus() == DatabaseTestResult.TESTED_OK;
220  }
221 
222 
228  private static void onPromptStatusError(CentralRepoDbManager manager1) {
229  // in the event that there is a failure to connect, notify user with corresponding message
230  String errorMessage = "";
231  if (manager1 == null || manager1.getSelectedDbChoice() == null) {
232  errorMessage = "";
234  errorMessage = Bundle.EamDbSettingsDialog_okButton_createPostgresDbError_message();
235  } else if (manager1.getSelectedDbChoice().getDbPlatform() == CentralRepoPlatforms.SQLITE) {
236  errorMessage = Bundle.EamDbSettingsDialog_okButton_createSQLiteDbError_message();
237  }
238  JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(),
239  errorMessage,
240  Bundle.EamDbSettingsDialog_okButton_createDbError_title(),
241  JOptionPane.WARNING_MESSAGE);
242  }
243 
244 
250  @SuppressWarnings("unchecked")
251  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
252  private void initComponents() {
253 
254  fcDatabasePath = new javax.swing.JFileChooser();
255  pnButtons = new javax.swing.JPanel();
256  bnCancel = new javax.swing.JButton();
257  bnOk = new javax.swing.JButton();
258  pnSQLiteSettings = new javax.swing.JPanel();
259  lbDatabasePath = new javax.swing.JLabel();
260  tfDatabasePath = new javax.swing.JTextField();
261  bnDatabasePathFileOpen = new javax.swing.JButton();
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  cbDatabaseType = new javax.swing.JComboBox<>();
271  lbSingleUserSqLite = new javax.swing.JLabel();
272  lbDatabaseType = new javax.swing.JLabel();
273  lbDatabaseDesc = new javax.swing.JLabel();
274  filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
275  dataBaseFileScrollPane = new javax.swing.JScrollPane();
276  dataBaseFileTextArea = new javax.swing.JTextArea();
277 
278  setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
279  setResizable(false);
280 
281  org.openide.awt.Mnemonics.setLocalizedText(bnCancel, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnCancel.text")); // NOI18N
282  bnCancel.setMaximumSize(new java.awt.Dimension(79, 23));
283  bnCancel.setMinimumSize(new java.awt.Dimension(79, 23));
284  bnCancel.setPreferredSize(new java.awt.Dimension(79, 23));
285  bnCancel.addActionListener(new java.awt.event.ActionListener() {
286  public void actionPerformed(java.awt.event.ActionEvent evt) {
287  bnCancelActionPerformed(evt);
288  }
289  });
290 
291  org.openide.awt.Mnemonics.setLocalizedText(bnOk, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnOk.text")); // NOI18N
292  bnOk.addActionListener(new java.awt.event.ActionListener() {
293  public void actionPerformed(java.awt.event.ActionEvent evt) {
294  bnOkActionPerformed(evt);
295  }
296  });
297 
298  javax.swing.GroupLayout pnButtonsLayout = new javax.swing.GroupLayout(pnButtons);
299  pnButtons.setLayout(pnButtonsLayout);
300  pnButtonsLayout.setHorizontalGroup(
301  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
302  .addGroup(pnButtonsLayout.createSequentialGroup()
303  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
304  .addComponent(bnOk)
305  .addGap(11, 11, 11)
306  .addComponent(bnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
307  .addContainerGap())
308  );
309 
310  pnButtonsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {bnCancel, bnOk});
311 
312  pnButtonsLayout.setVerticalGroup(
313  pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
314  .addGroup(pnButtonsLayout.createSequentialGroup()
315  .addGap(0, 0, 0)
316  .addGroup(pnButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
317  .addComponent(bnOk)
318  .addComponent(bnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
319  .addGap(0, 0, 0))
320  );
321 
322  pnSQLiteSettings.setBorder(javax.swing.BorderFactory.createEtchedBorder());
323 
324  org.openide.awt.Mnemonics.setLocalizedText(lbDatabasePath, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabasePath.text")); // NOI18N
325  lbDatabasePath.setPreferredSize(new java.awt.Dimension(80, 14));
326 
327  tfDatabasePath.setText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.text")); // NOI18N
328  tfDatabasePath.setToolTipText(org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.tfDatabasePath.toolTipText")); // NOI18N
329  tfDatabasePath.setPreferredSize(new java.awt.Dimension(420, 23));
330 
331  org.openide.awt.Mnemonics.setLocalizedText(bnDatabasePathFileOpen, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.bnDatabasePathFileOpen.text")); // NOI18N
332  bnDatabasePathFileOpen.addActionListener(new java.awt.event.ActionListener() {
333  public void actionPerformed(java.awt.event.ActionEvent evt) {
334  bnDatabasePathFileOpenActionPerformed(evt);
335  }
336  });
337 
338  org.openide.awt.Mnemonics.setLocalizedText(lbHostName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbHostName.text")); // NOI18N
339  lbHostName.setPreferredSize(new java.awt.Dimension(80, 14));
340 
341  tbDbHostname.setPreferredSize(new java.awt.Dimension(509, 20));
342 
343  org.openide.awt.Mnemonics.setLocalizedText(lbPort, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbPort.text")); // NOI18N
344  lbPort.setPreferredSize(new java.awt.Dimension(80, 14));
345 
346  tbDbPort.setPreferredSize(new java.awt.Dimension(509, 20));
347 
348  org.openide.awt.Mnemonics.setLocalizedText(lbUserName, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserName.text")); // NOI18N
349  lbUserName.setPreferredSize(new java.awt.Dimension(80, 14));
350 
351  tbDbUsername.setPreferredSize(new java.awt.Dimension(509, 20));
352 
353  org.openide.awt.Mnemonics.setLocalizedText(lbUserPassword, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbUserPassword.text")); // NOI18N
354  lbUserPassword.setPreferredSize(new java.awt.Dimension(80, 14));
355 
356  jpDbPassword.setPreferredSize(new java.awt.Dimension(509, 20));
357 
358  cbDatabaseType.setModel(new javax.swing.DefaultComboBoxModel<>(org.sleuthkit.autopsy.centralrepository.datamodel.CentralRepoDbChoice.DB_CHOICES));
359  cbDatabaseType.setPreferredSize(new java.awt.Dimension(120, 20));
360  cbDatabaseType.addActionListener(new java.awt.event.ActionListener() {
361  public void actionPerformed(java.awt.event.ActionEvent evt) {
362  cbDatabaseTypeActionPerformed(evt);
363  }
364  });
365 
366  org.openide.awt.Mnemonics.setLocalizedText(lbSingleUserSqLite, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbSingleUserSqLite.text")); // NOI18N
367  lbSingleUserSqLite.setPreferredSize(new java.awt.Dimension(381, 14));
368 
369  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseType, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseType.text")); // NOI18N
370  lbDatabaseType.setMaximumSize(new java.awt.Dimension(80, 14));
371  lbDatabaseType.setMinimumSize(new java.awt.Dimension(80, 14));
372  lbDatabaseType.setPreferredSize(new java.awt.Dimension(80, 14));
373 
374  org.openide.awt.Mnemonics.setLocalizedText(lbDatabaseDesc, org.openide.util.NbBundle.getMessage(EamDbSettingsDialog.class, "EamDbSettingsDialog.lbDatabaseDesc.text")); // NOI18N
375  lbDatabaseDesc.setPreferredSize(new java.awt.Dimension(80, 14));
376 
377  dataBaseFileScrollPane.setBorder(null);
378 
379  dataBaseFileTextArea.setEditable(false);
380  dataBaseFileTextArea.setBackground(new java.awt.Color(240, 240, 240));
381  dataBaseFileTextArea.setColumns(20);
382  dataBaseFileTextArea.setLineWrap(true);
383  dataBaseFileTextArea.setRows(3);
384  dataBaseFileScrollPane.setViewportView(dataBaseFileTextArea);
385 
386  javax.swing.GroupLayout pnSQLiteSettingsLayout = new javax.swing.GroupLayout(pnSQLiteSettings);
387  pnSQLiteSettings.setLayout(pnSQLiteSettingsLayout);
388  pnSQLiteSettingsLayout.setHorizontalGroup(
389  pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
390  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
391  .addContainerGap()
392  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
393  .addComponent(lbHostName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
394  .addComponent(lbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
395  .addComponent(lbDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
396  .addComponent(lbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
397  .addComponent(lbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
398  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
399  .addComponent(lbDatabaseDesc, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
400  .addComponent(lbUserPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
401  .addGap(10, 10, 10)
402  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
403  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
404  .addComponent(tfDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
405  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
406  .addComponent(bnDatabasePathFileOpen))
407  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
408  .addComponent(cbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
409  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
410  .addComponent(lbSingleUserSqLite, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))
411  .addComponent(jpDbPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
412  .addComponent(tbDbUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
413  .addComponent(tbDbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
414  .addComponent(tbDbHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
415  .addComponent(dataBaseFileScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 509, javax.swing.GroupLayout.PREFERRED_SIZE))
416  .addContainerGap())
417  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
418  .addGap(55, 55, 55)
419  .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
420  .addGap(0, 0, Short.MAX_VALUE))
421  );
422  pnSQLiteSettingsLayout.setVerticalGroup(
423  pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
424  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
425  .addGap(6, 6, 6)
426  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
427  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
428  .addComponent(cbDatabaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
429  .addComponent(lbSingleUserSqLite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
430  .addComponent(lbDatabaseType, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
431  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
432  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
433  .addComponent(lbDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
434  .addComponent(tfDatabasePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
435  .addComponent(bnDatabasePathFileOpen))
436  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
437  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
438  .addComponent(tbDbHostname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
439  .addComponent(lbHostName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
440  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
441  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
442  .addComponent(tbDbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
443  .addComponent(lbPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
444  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
445  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
446  .addComponent(tbDbUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
447  .addComponent(lbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
448  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
449  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
450  .addComponent(jpDbPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
451  .addComponent(lbUserPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
452  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
453  .addGroup(pnSQLiteSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
454  .addGroup(pnSQLiteSettingsLayout.createSequentialGroup()
455  .addComponent(lbDatabaseDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
456  .addGap(18, 18, 18)
457  .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
458  .addComponent(dataBaseFileScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
459  .addContainerGap())
460  );
461 
462  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
463  getContentPane().setLayout(layout);
464  layout.setHorizontalGroup(
465  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
466  .addGroup(layout.createSequentialGroup()
467  .addContainerGap()
468  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
469  .addComponent(pnButtons, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
470  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
471  .addContainerGap())
472  );
473  layout.setVerticalGroup(
474  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
475  .addGroup(layout.createSequentialGroup()
476  .addContainerGap()
477  .addComponent(pnSQLiteSettings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
478  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
479  .addComponent(pnButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
480  .addContainerGap())
481  );
482 
483  pack();
484  }// </editor-fold>//GEN-END:initComponents
485 
486  private void customizeComponents() {
487  setTextPrompts();
488  setTextBoxListeners();
489  manager.clearStatus();
491  updatePostgresFields(false);
492  updateSqliteFields(true);
493  }
495  updatePostgresFields(true);
496  updateSqliteFields(false);
497  }
498  else {
499  updatePostgresFields(false);
500  updateSqliteFields(false);
501  }
502 
503  displayDatabaseSettings(manager.getSelectedDbChoice());
504  }
505 
506  private void display() {
507  this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
508  setVisible(true);
509  }
510 
511  @Messages({"EamDbSettingsDialog.chooserPath.failedToGetDbPathMsg=Selected database path is invalid. Try again."})
512  private void bnDatabasePathFileOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDatabasePathFileOpenActionPerformed
513  fcDatabasePath.setSelectedFile(new File(manager.getDbSettingsSqlite().getDbDirectory()));
514  if (fcDatabasePath.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
515  File databaseFile = fcDatabasePath.getSelectedFile();
516  if (databaseFile.isFile()) {
517  databaseFile = fcDatabasePath.getCurrentDirectory();
518  }
519  try {
520  tfDatabasePath.setText(databaseFile.getCanonicalPath());
521  tfDatabasePath.setCaretPosition(tfDatabasePath.getText().length());
522  valid();
523  } catch (IOException ex) {
524  logger.log(Level.SEVERE, "Failed to get path of selected database file", ex); // NON-NLS
525  }
526  }
527  }//GEN-LAST:event_bnDatabasePathFileOpenActionPerformed
528 
529  @NbBundle.Messages({"EamDbSettingsDialog.okButton.errorTitle.text=Restart Required.",
530  "EamDbSettingsDialog.okButton.errorMsg.text=Please restart Autopsy to begin using the new database platform.",
531  "EamDbSettingsDialog.okButton.connectionErrorMsg.text=Failed to connect to central repository database."})
532  private void bnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnOkActionPerformed
533  if (testStatusAndCreate(this, manager, this))
534  dispose();
535  }//GEN-LAST:event_bnOkActionPerformed
536 
537 
544  public static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager) {
545  return testStatusAndCreate(parent, manager, null);
546  }
547 
548 
556  private static boolean testStatusAndCreate(Component parent, CentralRepoDbManager manager, EamDbSettingsDialog dialog) {
557  parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
558  manager.testStatus();
559 
560  if (dialog != null)
561  dialog.valid();
562 
563  boolean testedOk = promptTestStatusWarnings(manager, dialog);
564  if (!testedOk) {
565  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
566  return false;
567  }
568 
569  try{
570  manager.saveNewCentralRepo();
571  }
572  catch (CentralRepoException e) {
573  SwingUtilities.invokeLater(() -> {
574  JOptionPane.showMessageDialog(parent,
575  Bundle.EamDbSettingsDialog_okButton_errorMsg_text(),
576  Bundle.EamDbSettingsDialog_okButton_errorTitle_text(),
577  JOptionPane.WARNING_MESSAGE);
578  });
579 
580  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
581  return false;
582  }
583 
584  parent.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
585  return true;
586  }
587 
588 
596  public boolean wasConfigurationChanged() {
597  return manager.wasConfigurationChanged();
598  }
599 
600  private void bnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnCancelActionPerformed
601  dispose();
602  }//GEN-LAST:event_bnCancelActionPerformed
603 
604 
605  private void cbDatabaseTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDatabaseTypeActionPerformed
606  CentralRepoDbChoice selectedItem = (CentralRepoDbChoice) cbDatabaseType.getSelectedItem();
607  changeDbSelection(selectedItem);
608  }//GEN-LAST:event_cbDatabaseTypeActionPerformed
609 
610  private void changeDbSelection(CentralRepoDbChoice selectedItem) {
611  if (isDbChoiceSelectable(selectedItem)) {
612  manager.setSelctedDbChoice(selectedItem);
613  cbDatabaseType.setSelectedItem(selectedItem);
614  }
615  else {
616  cbDatabaseType.setSelectedItem(manager.getSelectedDbChoice());
617  }
618 
619  customizeComponents();
620  }
621 
622  private void updateFullDbPath() {
623  dataBaseFileTextArea.setText(tfDatabasePath.getText() + File.separator + SqliteCentralRepoSettings.DEFAULT_DBNAME);
624  dataBaseFileTextArea.setCaretPosition(dataBaseFileTextArea.getText().length());
625  }
626 
628  boolean isSqlite = choice == CentralRepoDbChoice.SQLITE;
629  boolean isPostgres = choice == CentralRepoDbChoice.POSTGRESQL_CUSTOM;
630 
631  lbDatabasePath.setVisible(isSqlite);
632  tfDatabasePath.setVisible(isSqlite);
633  lbDatabaseDesc.setVisible(isSqlite);
634  dataBaseFileTextArea.setVisible(isSqlite);
635  lbSingleUserSqLite.setVisible(isSqlite);
636  bnDatabasePathFileOpen.setVisible(isSqlite);
637 
638  lbHostName.setVisible(isPostgres);
639  tbDbHostname.setVisible(isPostgres);
640  lbPort.setVisible(isPostgres);
641  tbDbPort.setVisible(isPostgres);
642  lbUserName.setVisible(isPostgres);
643  tbDbUsername.setVisible(isPostgres);
644  lbUserPassword.setVisible(isPostgres);
645  jpDbPassword.setVisible(isPostgres);
646  }
647 
651  @Messages({"EamDbSettingsDialog.textPrompt.hostnameOrIP=Hostname or IP Address",
652  "EamDbSettingsDialog.textPrompt.port=Port Number",
653  "EamDbSettingsDialog.textPrompt.dbName=Database Name",
654  "EamDbSettingsDialog.textPrompt.user=Database User",
655  "EamDbSettingsDialog.textPrompt.password=Database User's Password"})
656  private void setTextPrompts() {
657  Collection<TextPrompt> textPrompts = new ArrayList<>();
658  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_hostnameOrIP(), tbDbHostname));
659  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_port(), tbDbPort));
660  textPrompts.add(new TextPrompt(Bundle.EamDbSettingsDialog_textPrompt_user(), tbDbUsername));
661  configureTextPrompts(textPrompts);
662  }
663 
664  private void updatePostgresFields(boolean enabled) {
665  tbDbHostname.setText(enabled ? manager.getDbSettingsPostgres().getHost() : "");
666  tbDbHostname.setEnabled(enabled);
667  tbDbPort.setText(enabled ? Integer.toString(manager.getDbSettingsPostgres().getPort()) : "");
668  tbDbPort.setEnabled(enabled);
669  tbDbUsername.setText(enabled ? manager.getDbSettingsPostgres().getUserName() : "");
670  tbDbUsername.setEnabled(enabled);
671  jpDbPassword.setText(enabled ? manager.getDbSettingsPostgres().getPassword() : "");
672  jpDbPassword.setEnabled(enabled);
673  }
674 
681  private void updateSqliteFields(boolean enabled) {
682  tfDatabasePath.setText(enabled ? manager.getDbSettingsSqlite().getDbDirectory() : "");
683  tfDatabasePath.setEnabled(enabled);
684  bnDatabasePathFileOpen.setEnabled(enabled);
685  }
686 
690  private void setTextBoxListeners() {
691  textBoxes.add(tfDatabasePath);
692  textBoxes.add(tbDbHostname);
693  textBoxes.add(tbDbPort);
694  // textBoxes.add(tbDbName);
695  textBoxes.add(tbDbUsername);
696  textBoxes.add(jpDbPassword);
697  addDocumentListeners(textBoxes, textBoxChangedListener);
698  }
699 
706  private static void configureTextPrompts(Collection<TextPrompt> textPrompts) {
707  float alpha = 0.9f; // Mostly opaque
708  for (TextPrompt textPrompt : textPrompts) {
709  textPrompt.setForeground(Color.LIGHT_GRAY);
710  textPrompt.changeAlpha(alpha);
711  }
712  }
713 
720  private static void addDocumentListeners(Collection<JTextField> textFields, TextBoxChangedListener listener) {
721  textFields.forEach((textField) -> {
722  textField.getDocument().addDocumentListener(listener);
723  });
724  }
725 
732  @Messages({"EamDbSettingsDialog.validation.incompleteFields=Fill in all values for the selected database."})
733  private boolean databaseFieldsArePopulated() {
734  boolean result = true;
736  result = !tbDbHostname.getText().trim().isEmpty()
737  && !tbDbPort.getText().trim().isEmpty()
738  // && !tbDbName.getText().trim().isEmpty()
739  && !tbDbUsername.getText().trim().isEmpty()
740  && 0 < jpDbPassword.getPassword().length;
741  }
742  else if (manager.getSelectedDbChoice() == CentralRepoDbChoice.SQLITE) {
743  result = !tfDatabasePath.getText().trim().isEmpty();
744  }
745 
746  return result;
747  }
748 
754  private boolean checkFields() {
755  return databaseFieldsArePopulated()
756  && databaseSettingsAreValid();
757  }
758 
759 
765  private boolean valid() {
766  return enableOkButton(checkFields());
767  }
768 
775  @Messages({"EamDbSettingsDialog.validation.finished=Click OK to save your database settings and return to the Options. Or select a different database type."})
776  private boolean enableOkButton(boolean isValidInput) {
777  if (isValidInput) {
778  bnOk.setEnabled(true);
779  } else {
780  bnOk.setEnabled(false);
781  }
782  return true;
783 
784  }
785 
786 
787 
793  private boolean databaseSettingsAreValid() {
794  try {
796  tbDbHostname.getText().trim(),
797  tbDbPort.getText().trim(),
798  tbDbUsername.getText().trim(),
799  tfDatabasePath.getText().trim(),
800  new String(jpDbPassword.getPassword()));
801  }
802  catch (CentralRepoException | NumberFormatException | IllegalStateException e) {
803  return false;
804  }
805 
806  return true;
807  }
808 
813  private class TextBoxChangedListener implements DocumentListener {
814 
815  @Override
816  public void changedUpdate(DocumentEvent e) {
817  manager.clearStatus();
818  updateFullDbPath();
819  valid();
820  }
821 
822  @Override
823  public void insertUpdate(DocumentEvent e) {
824  manager.clearStatus();
825  updateFullDbPath();
826  valid();
827  }
828 
829  @Override
830  public void removeUpdate(DocumentEvent e) {
831  manager.clearStatus();
832  updateFullDbPath();
833  valid();
834 
835  }
836  }
837 
838 
839  // Variables declaration - do not modify//GEN-BEGIN:variables
840  private javax.swing.JButton bnCancel;
841  private javax.swing.JButton bnDatabasePathFileOpen;
842  private javax.swing.JButton bnOk;
844  private javax.swing.JScrollPane dataBaseFileScrollPane;
845  private javax.swing.JTextArea dataBaseFileTextArea;
846  private javax.swing.JFileChooser fcDatabasePath;
847  private javax.swing.Box.Filler filler1;
848  private javax.swing.JPasswordField jpDbPassword;
849  private javax.swing.JLabel lbDatabaseDesc;
850  private javax.swing.JLabel lbDatabasePath;
851  private javax.swing.JLabel lbDatabaseType;
852  private javax.swing.JLabel lbHostName;
853  private javax.swing.JLabel lbPort;
854  private javax.swing.JLabel lbSingleUserSqLite;
855  private javax.swing.JLabel lbUserName;
856  private javax.swing.JLabel lbUserPassword;
857  private javax.swing.JPanel pnButtons;
858  private javax.swing.JPanel pnSQLiteSettings;
859  private javax.swing.JTextField tbDbHostname;
860  private javax.swing.JTextField tbDbPort;
861  private javax.swing.JTextField tbDbUsername;
862  private javax.swing.JTextField tfDatabasePath;
863  // End of variables declaration//GEN-END:variables
864 }
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-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.