19 package org.sleuthkit.autopsy.logicalimager.configuration;
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonIOException;
24 import com.google.gson.JsonSyntaxException;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.nio.charset.StandardCharsets;
31 import javax.swing.JFileChooser;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.event.DocumentEvent;
35 import javax.swing.event.DocumentListener;
36 import javax.swing.filechooser.FileFilter;
37 import javax.swing.filechooser.FileNameExtensionFilter;
38 import org.openide.util.NbBundle;
43 @SuppressWarnings(
"PMD.SingularField")
44 final class ConfigVisualPanel1 extends JPanel {
46 private LogicalImagerConfig config;
47 private String configFilename;
48 private boolean newFile =
true;
53 ConfigVisualPanel1() {
55 configFileTextField.getDocument().addDocumentListener(
new MyDocumentListener(
this));
59 "ConfigVisualPanel1.selectConfigurationFile=Select configuration file"
62 public String getName() {
63 return Bundle.ConfigVisualPanel1_selectConfigurationFile();
72 private void initComponents() {
74 buttonGroup1 =
new javax.swing.ButtonGroup();
75 jLabel1 =
new javax.swing.JLabel();
76 configFileTextField =
new javax.swing.JTextField();
77 browseButton =
new javax.swing.JButton();
79 org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class,
"ConfigVisualPanel1.jLabel1.text_1"));
81 configFileTextField.setEditable(
false);
82 configFileTextField.setText(
org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class,
"ConfigVisualPanel1.configFileTextField.text_1"));
84 org.openide.awt.Mnemonics.setLocalizedText(browseButton,
org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class,
"ConfigVisualPanel1.browseButton.text"));
85 browseButton.setToolTipText(
org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class,
"ConfigVisualPanel1.browseButton.toolTipText"));
86 browseButton.addActionListener(
new java.awt.event.ActionListener() {
87 public void actionPerformed(java.awt.event.ActionEvent evt) {
88 browseButtonActionPerformed(evt);
92 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
93 this.setLayout(layout);
94 layout.setHorizontalGroup(
95 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
96 .addGroup(layout.createSequentialGroup()
98 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
99 .addComponent(jLabel1)
100 .addComponent(configFileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
101 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
102 .addComponent(browseButton)
105 layout.setVerticalGroup(
106 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
107 .addGroup(layout.createSequentialGroup()
108 .addGap(116, 116, 116)
109 .addComponent(jLabel1)
110 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
111 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
112 .addComponent(configFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
113 .addComponent(browseButton))
114 .addContainerGap(141, Short.MAX_VALUE))
119 "ConfigVisualPanel1.chooseFileTitle=Select a Logical Imager configuration"
121 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
122 chooseFile(Bundle.ConfigVisualPanel1_chooseFileTitle());
126 "ConfigVisualPanel1.fileNameExtensionFilter=Configuration JSON File",
127 "ConfigVisualPanel1.invalidConfigJson=Invalid config JSON: ",
128 "ConfigVisualPanel1.configurationError=Configuration error",
130 private void chooseFile(String title) {
131 final String jsonExt =
".json";
132 JFileChooser fileChooser =
new JFileChooser();
133 fileChooser.setDialogTitle(title);
134 fileChooser.setDragEnabled(
false);
135 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
136 FileFilter filter =
new FileNameExtensionFilter(Bundle.ConfigVisualPanel1_fileNameExtensionFilter(),
new String[] {
"json"});
137 fileChooser.setFileFilter(filter);
138 fileChooser.setSelectedFile(
new File(
"logical-imager-config.json"));
139 fileChooser.setMultiSelectionEnabled(
false);
140 if (fileChooser.showOpenDialog(
this) == JFileChooser.APPROVE_OPTION) {
141 String path = fileChooser.getSelectedFile().getPath();
142 if (
new File(path).exists()) {
144 loadConfigFile(path);
145 configFilename = path;
146 configFileTextField.setText(path);
148 }
catch (JsonIOException | JsonSyntaxException | IOException ex) {
149 JOptionPane.showMessageDialog(
this,
150 Bundle.ConfigVisualPanel1_invalidConfigJson() + ex.getMessage() ,
151 Bundle.ConfigVisualPanel1_configurationError(),
152 JOptionPane.ERROR_MESSAGE);
155 if (!path.endsWith(jsonExt)) {
158 configFilename = path;
159 configFileTextField.setText(path);
160 config =
new LogicalImagerConfig();
167 private javax.swing.JButton browseButton;
168 private javax.swing.ButtonGroup buttonGroup1;
169 private javax.swing.JTextField configFileTextField;
170 private javax.swing.JLabel jLabel1;
175 "ConfigVisualPanel1.configFileIsEmpty=Configuration file {0} is empty",
177 private void loadConfigFile(String path)
throws FileNotFoundException, JsonIOException, JsonSyntaxException, IOException {
178 try (FileInputStream is =
new FileInputStream(path)) {
179 InputStreamReader reader =
new InputStreamReader(is, StandardCharsets.UTF_8);
180 GsonBuilder gsonBuilder =
new GsonBuilder().setPrettyPrinting();
181 gsonBuilder.registerTypeAdapter(LogicalImagerConfig.class,
new LogicalImagerConfigDeserializer());
182 Gson gson = gsonBuilder.create();
183 config = gson.fromJson(reader, LogicalImagerConfig.class);
184 if (config == null) {
186 throw new IOException(Bundle.ConfigVisualPanel1_configFileIsEmpty(path));
191 LogicalImagerConfig getConfig() {
195 String getConfigFilename() {
196 return configFilename;
199 boolean isNewFile() {
203 void setConfigFilename(String filename) {
204 configFileTextField.setText(filename);
207 boolean isPanelValid() {
208 return (newFile || !configFileTextField.getText().isEmpty());
216 private final ConfigVisualPanel1
panel;
238 panel.firePropertyChange(
"UPDATE_UI",
false,
true);
void insertUpdate(DocumentEvent e)
void removeUpdate(DocumentEvent e)
final ConfigVisualPanel1 panel
void changedUpdate(DocumentEvent e)