Autopsy  4.14.0
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 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;
33 
37 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
38 final class XRYDataSourceProcessorConfigPanel extends JPanel {
39 
40  private static final long serialVersionUID = 1L;
41 
42  private static final String PROP_LAST_USED_PATH = "LAST_USED_PATH";
43  private static final String SETTINGS_CONTEXT = "XRYDataSourceProcessorConfigPanel_Settings";
44 
45  private static final XRYDataSourceProcessorConfigPanel INSTANCE =
46  new XRYDataSourceProcessorConfigPanel();
47 
48  //Used to communicate with the DSP infrastructure. This config
49  //panel will indicate when it is ready for an update.
50  private final PropertyChangeSupport pcs;
51 
56  private XRYDataSourceProcessorConfigPanel() {
57  initComponents();
58  pcs = new PropertyChangeSupport(this);
59  }
60 
64  private void setLastUsedPath(Path selection) {
65  Path parent = selection.getParent();
66  ModuleSettings.setConfigSetting(SETTINGS_CONTEXT,
67  PROP_LAST_USED_PATH, parent.toString());
68  }
69 
74  private Optional<Path> getLastUsedPath() {
75  String lastFolderPath = ModuleSettings.getConfigSetting(
76  SETTINGS_CONTEXT, PROP_LAST_USED_PATH);
77  if (StringUtils.isNotBlank(lastFolderPath)) {
78  Path lastPath = Paths.get(lastFolderPath);
79  if (Files.exists(lastPath)) {
80  return Optional.of(lastPath);
81  }
82  }
83  return Optional.empty();
84  }
85 
89  static XRYDataSourceProcessorConfigPanel getInstance() {
90  return INSTANCE;
91  }
92 
96  void clearErrorText() {
97  errorLabel.setText(null);
98  }
99 
103  void setErrorText(String text) {
104  errorLabel.setText(text);
105  }
106 
110  void clearSelectedFilePath() {
111  filePathTextField.setText(null);
112  }
113 
117  String getSelectedFilePath() {
118  return filePathTextField.getText();
119  }
120 
124  @Override
125  public synchronized void addPropertyChangeListener(PropertyChangeListener pcl) {
126  super.addPropertyChangeListener(pcl);
127  pcs.addPropertyChangeListener(pcl);
128  }
129 
135  @SuppressWarnings("unchecked")
136  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
137  private void initComponents() {
138 
139  filePathTextField = new javax.swing.JTextField();
140  fileBrowserButton = new javax.swing.JButton();
141  xrySelectFolderLabel = new javax.swing.JLabel();
142  errorLabel = new javax.swing.JLabel();
143 
144  filePathTextField.setEditable(false);
145  filePathTextField.setText(org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.filePathTextField.text")); // NOI18N
146 
147  org.openide.awt.Mnemonics.setLocalizedText(fileBrowserButton, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.fileBrowserButton.text")); // NOI18N
148  fileBrowserButton.addActionListener(new java.awt.event.ActionListener() {
149  public void actionPerformed(java.awt.event.ActionEvent evt) {
150  fileBrowserButtonActionPerformed(evt);
151  }
152  });
153 
154  org.openide.awt.Mnemonics.setLocalizedText(xrySelectFolderLabel, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.xrySelectFolderLabel.text")); // NOI18N
155 
156  errorLabel.setForeground(new java.awt.Color(255, 0, 0));
157  org.openide.awt.Mnemonics.setLocalizedText(errorLabel, org.openide.util.NbBundle.getMessage(XRYDataSourceProcessorConfigPanel.class, "XRYDataSourceProcessorConfigPanel.errorLabel.text")); // NOI18N
158 
159  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
160  this.setLayout(layout);
161  layout.setHorizontalGroup(
162  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
163  .addGroup(layout.createSequentialGroup()
164  .addContainerGap()
165  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
166  .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
167  .addComponent(xrySelectFolderLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
168  .addGroup(layout.createSequentialGroup()
169  .addComponent(filePathTextField)
170  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
171  .addComponent(fileBrowserButton)))
172  .addContainerGap())
173  );
174  layout.setVerticalGroup(
175  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
176  .addGroup(layout.createSequentialGroup()
177  .addContainerGap()
178  .addComponent(xrySelectFolderLabel)
179  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
180  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
181  .addComponent(filePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
182  .addComponent(fileBrowserButton))
183  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
184  .addComponent(errorLabel)
185  .addContainerGap(235, Short.MAX_VALUE))
186  );
187  }// </editor-fold>//GEN-END:initComponents
188 
193  private void fileBrowserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileBrowserButtonActionPerformed
194  JFileChooser fileChooser = new JFileChooser();
195  fileChooser.setMultiSelectionEnabled(false);
196  fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
197  Optional<Path> lastUsedPath = getLastUsedPath();
198  if(lastUsedPath.isPresent()) {
199  fileChooser.setCurrentDirectory(lastUsedPath.get().toFile());
200  }
201  int returnVal = fileChooser.showOpenDialog(this);
202  if (returnVal == JFileChooser.APPROVE_OPTION) {
203  File selection = fileChooser.getSelectedFile();
204  setLastUsedPath(selection.toPath());
205  filePathTextField.setText(selection.getAbsolutePath());
206 
207  //This will notify the wizard to revalidate the data source processor.
208  pcs.firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.UPDATE_UI.toString(), false, true);
209  }
210  }//GEN-LAST:event_fileBrowserButtonActionPerformed
211 
212 
213  // Variables declaration - do not modify//GEN-BEGIN:variables
214  private javax.swing.JLabel errorLabel;
215  private javax.swing.JButton fileBrowserButton;
216  private javax.swing.JTextField filePathTextField;
217  private javax.swing.JLabel xrySelectFolderLabel;
218  // End of variables declaration//GEN-END:variables
219 }

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.