Autopsy  4.11.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ConfigVisualPanel1.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-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.logicalimager.configuration;
20 
21 import com.google.gson.Gson;
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonIOException;
24 import com.google.gson.JsonSyntaxException;
25 import java.io.File;
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;
39 
43 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
44 final class ConfigVisualPanel1 extends JPanel {
45 
46  private LogicalImagerConfig config;
47  private String configFilename;
48  private boolean newFile = true;
49 
53  ConfigVisualPanel1() {
54  initComponents();
55  configFileTextField.getDocument().addDocumentListener(new MyDocumentListener(this));
56  }
57 
58  @NbBundle.Messages({
59  "ConfigVisualPanel1.selectConfigurationFile=Select configuration file"
60  })
61  @Override
62  public String getName() {
63  return Bundle.ConfigVisualPanel1_selectConfigurationFile();
64  }
65 
71  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
72  private void initComponents() {
73 
74  buttonGroup1 = new javax.swing.ButtonGroup();
75  jLabel1 = new javax.swing.JLabel();
76  configFileTextField = new javax.swing.JTextField();
77  browseButton = new javax.swing.JButton();
78 
79  org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class, "ConfigVisualPanel1.jLabel1.text_1")); // NOI18N
80 
81  configFileTextField.setEditable(false);
82  configFileTextField.setText(org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class, "ConfigVisualPanel1.configFileTextField.text_1")); // NOI18N
83 
84  org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class, "ConfigVisualPanel1.browseButton.text")); // NOI18N
85  browseButton.setToolTipText(org.openide.util.NbBundle.getMessage(ConfigVisualPanel1.class, "ConfigVisualPanel1.browseButton.toolTipText")); // NOI18N
86  browseButton.addActionListener(new java.awt.event.ActionListener() {
87  public void actionPerformed(java.awt.event.ActionEvent evt) {
88  browseButtonActionPerformed(evt);
89  }
90  });
91 
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()
97  .addGap(38, 38, 38)
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)
103  .addContainerGap())
104  );
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))
115  );
116  }// </editor-fold>//GEN-END:initComponents
117 
118  @NbBundle.Messages({
119  "ConfigVisualPanel1.chooseFileTitle=Select a Logical Imager configuration"
120  })
121  private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
122  chooseFile(Bundle.ConfigVisualPanel1_chooseFileTitle());
123  }//GEN-LAST:event_browseButtonActionPerformed
124 
125  @NbBundle.Messages({
126  "ConfigVisualPanel1.fileNameExtensionFilter=Configuration JSON File",
127  "ConfigVisualPanel1.invalidConfigJson=Invalid config JSON: ",
128  "ConfigVisualPanel1.configurationError=Configuration error",
129  })
130  private void chooseFile(String title) {
131  final String jsonExt = ".json"; // NON-NLS
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"}); // NON-NLS
137  fileChooser.setFileFilter(filter);
138  fileChooser.setSelectedFile(new File("logical-imager-config.json")); // default
139  fileChooser.setMultiSelectionEnabled(false);
140  if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
141  String path = fileChooser.getSelectedFile().getPath();
142  if (new File(path).exists()) {
143  try {
144  loadConfigFile(path);
145  configFilename = path;
146  configFileTextField.setText(path);
147  newFile = false;
148  } catch (JsonIOException | JsonSyntaxException | IOException ex) {
149  JOptionPane.showMessageDialog(this,
150  Bundle.ConfigVisualPanel1_invalidConfigJson() + ex.getMessage() ,
151  Bundle.ConfigVisualPanel1_configurationError(),
152  JOptionPane.ERROR_MESSAGE);
153  }
154  } else {
155  if (!path.endsWith(jsonExt)) {
156  path += jsonExt;
157  }
158  configFilename = path;
159  configFileTextField.setText(path);
160  config = new LogicalImagerConfig();
161  newFile = true;
162  }
163  }
164  }
165 
166  // Variables declaration - do not modify//GEN-BEGIN:variables
167  private javax.swing.JButton browseButton;
168  private javax.swing.ButtonGroup buttonGroup1;
169  private javax.swing.JTextField configFileTextField;
170  private javax.swing.JLabel jLabel1;
171  // End of variables declaration//GEN-END:variables
172 
173  @NbBundle.Messages({
174  "# {0} - filename",
175  "ConfigVisualPanel1.configFileIsEmpty=Configuration file {0} is empty",
176  })
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) {
185  // This happens if the file is empty. Gson doesn't call the deserializer and doesn't throw any exception.
186  throw new IOException(Bundle.ConfigVisualPanel1_configFileIsEmpty(path));
187  }
188  }
189  }
190 
191  LogicalImagerConfig getConfig() {
192  return config;
193  }
194 
195  String getConfigFilename() {
196  return configFilename;
197  }
198 
199  boolean isNewFile() {
200  return newFile;
201  }
202 
203  void setConfigFilename(String filename) {
204  configFileTextField.setText(filename);
205  }
206 
207  boolean isPanelValid() {
208  return (newFile || !configFileTextField.getText().isEmpty());
209  }
210 
214  private static class MyDocumentListener implements DocumentListener {
215 
216  private final ConfigVisualPanel1 panel;
217 
218  MyDocumentListener(ConfigVisualPanel1 aThis) {
219  this.panel = aThis;
220  }
221 
222  @Override
223  public void insertUpdate(DocumentEvent e) {
224  fireChange();
225  }
226 
227  @Override
228  public void removeUpdate(DocumentEvent e) {
229  fireChange();
230  }
231 
232  @Override
233  public void changedUpdate(DocumentEvent e) {
234  fireChange();
235  }
236 
237  private void fireChange() {
238  panel.firePropertyChange("UPDATE_UI", false, true); // NON-NLS
239  }
240  }
241 
242 }

Copyright © 2012-2018 Basis Technology. Generated on: Fri Jun 21 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.