Autopsy  4.19.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
XRYDataSourceProcessorConfigPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2021 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.datasourceprocessors.xry;
20 
21 import java.beans.PropertyChangeListener;
22 import java.beans.PropertyChangeSupport;
23 import java.io.File;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Optional;
28 import javax.swing.JFileChooser;
29 import javax.swing.JPanel;
30 import org.apache.commons.lang3.StringUtils;
34 
38 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
39 final class XRYDataSourceProcessorConfigPanel extends JPanel {
40 
41  private static final long serialVersionUID = 1L;
42 
43  private static final String PROP_LAST_USED_PATH = "LAST_USED_PATH";
44  private static final String SETTINGS_CONTEXT = "XRYDataSourceProcessorConfigPanel_Settings";
45 
46  private static final XRYDataSourceProcessorConfigPanel INSTANCE =
47  new XRYDataSourceProcessorConfigPanel();
48 
49  //Used to communicate with the DSP infrastructure. This config
50  //panel will indicate when it is ready for an update.
51  private final PropertyChangeSupport pcs;
52 
53  private final JFileChooserFactory chooserHelper = new JFileChooserFactory();
54 
59  private XRYDataSourceProcessorConfigPanel() {
60  initComponents();
61  pcs = new PropertyChangeSupport(this);
62  }
63 
67  private void setLastUsedPath(Path selection) {
68  Path parent = selection.getParent();
69  ModuleSettings.setConfigSetting(SETTINGS_CONTEXT,
70  PROP_LAST_USED_PATH, parent.toString());
71  }
72 
77  private Optional<Path> getLastUsedPath() {
78  String lastFolderPath = ModuleSettings.getConfigSetting(
79  SETTINGS_CONTEXT, PROP_LAST_USED_PATH);
80  if (StringUtils.isNotBlank(lastFolderPath)) {
81  Path lastPath = Paths.get(lastFolderPath);
82  if (Files.exists(lastPath)) {
83  return Optional.of(lastPath);
84  }
85  }
86  return Optional.empty();
87  }
88 
92  static XRYDataSourceProcessorConfigPanel getInstance() {
93  return INSTANCE;
94  }
95 
99  void clearErrorText() {
100  errorLabel.setText(null);
101  }
102 
106  void setErrorText(String text) {
107  errorLabel.setText(text);
108  }
109 
113  void clearSelectedFilePath() {
114  filePathTextField.setText(null);
115  }
116 
120  String getSelectedFilePath() {
121  return filePathTextField.getText();
122  }
123 
127  @Override
128  public synchronized void addPropertyChangeListener(PropertyChangeListener pcl) {
129  super.addPropertyChangeListener(pcl);
130  pcs.addPropertyChangeListener(pcl);
131  }
132 
138  @SuppressWarnings("unchecked")
139  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
140  private void initComponents() {
141 
142  filePathTextField = new javax.swing.JTextField();
143  fileBrowserButton = new javax.swing.JButton();
144  xrySelectFolderLabel = new javax.swing.JLabel();
145  errorLabel = new javax.swing.JLabel();
146 
147  filePathTextField.setEditable(false);
148  filePathTextField.setText(org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.filePathTextField.text")); // NOI18N
149 
150  org.openide.awt.Mnemonics.setLocalizedText(fileBrowserButton, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.fileBrowserButton.text")); // NOI18N
151  fileBrowserButton.addActionListener(new java.awt.event.ActionListener() {
152  public void actionPerformed(java.awt.event.ActionEvent evt) {
153  fileBrowserButtonActionPerformed(evt);
154  }
155  });
156 
157  org.openide.awt.Mnemonics.setLocalizedText(xrySelectFolderLabel, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.xrySelectFolderLabel.text")); // NOI18N
158 
159  errorLabel.setForeground(new java.awt.Color(255, 0, 0));
160  org.openide.awt.Mnemonics.setLocalizedText(errorLabel, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.errorLabel.text")); // NOI18N
161 
162  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
163  this.setLayout(layout);
164  layout.setHorizontalGroup(
165  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
166  .addGroup(layout.createSequentialGroup()
167  .addContainerGap()
168  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
169  .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
170  .addComponent(xrySelectFolderLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
171  .addGroup(layout.createSequentialGroup()
172  .addComponent(filePathTextField)
173  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
174  .addComponent(fileBrowserButton)))
175  .addContainerGap())
176  );
177  layout.setVerticalGroup(
178  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
179  .addGroup(layout.createSequentialGroup()
180  .addContainerGap()
181  .addComponent(xrySelectFolderLabel)
182  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
183  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
184  .addComponent(filePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
185  .addComponent(fileBrowserButton))
186  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
187  .addComponent(errorLabel)
188  .addContainerGap(235, Short.MAX_VALUE))
189  );
190  }// </editor-fold>//GEN-END:initComponents
191 
196  private void fileBrowserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileBrowserButtonActionPerformed
197  JFileChooser fileChooser = chooserHelper.getChooser();
198  fileChooser.setMultiSelectionEnabled(false);
199  fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
200  Optional<Path> lastUsedPath = getLastUsedPath();
201  if(lastUsedPath.isPresent()) {
202  fileChooser.setCurrentDirectory(lastUsedPath.get().toFile());
203  }
204  int returnVal = fileChooser.showOpenDialog(this);
205  if (returnVal == JFileChooser.APPROVE_OPTION) {
206  File selection = fileChooser.getSelectedFile();
207  setLastUsedPath(selection.toPath());
208  filePathTextField.setText(selection.getAbsolutePath());
209 
210  //This will notify the wizard to revalidate the data source processor.
211  pcs.firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.UPDATE_UI.toString(), false, true);
212  }
213  }//GEN-LAST:event_fileBrowserButtonActionPerformed
214 
215 
216  // Variables declaration - do not modify//GEN-BEGIN:variables
217  private javax.swing.JLabel errorLabel;
218  private javax.swing.JButton fileBrowserButton;
219  private javax.swing.JTextField filePathTextField;
220  private javax.swing.JLabel xrySelectFolderLabel;
221  // End of variables declaration//GEN-END:variables
222 }

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.