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 java.awt.Color;
 
   25 import java.awt.Cursor;
 
   27 import java.io.IOException;
 
   28 import java.nio.file.Path;
 
   29 import java.nio.file.Paths;
 
   30 import java.util.Arrays;
 
   31 import java.util.List;
 
   32 import java.util.logging.Level;
 
   33 import javax.swing.JOptionPane;
 
   34 import org.apache.commons.io.FileUtils;
 
   35 import org.apache.commons.io.FilenameUtils;
 
   36 import org.openide.util.NbBundle;
 
   43 class ConfigVisualPanel3 
extends javax.swing.JPanel {
 
   45     private static final Logger logger = Logger.getLogger(ConfigVisualPanel3.class.getName());
 
   46     private static final String SAVED_LOGICAL_IMAGER = 
"SAVED_LOGICAL_IMAGER";
 
   47     private static final long serialVersionUID = 1L;
 
   48     private boolean hasBeenSaved = 
false;
 
   49     private String configFilename;
 
   50     private LogicalImagerConfig config;
 
   55     @NbBundle.Messages({
"ConfigVisualPanel3.copyStatus.notSaved=File has not been saved",
 
   56         "ConfigVisualPanel3.copyStatus.savingInProgress=Saving file, please wait",
 
   57         "ConfigVisualPanel3.copyStatus.saved=Saved",
 
   58         "ConfigVisualPanel3.copyStatus.error=Unable to save file"})
 
   59     ConfigVisualPanel3() {
 
   63     final void resetPanel() {
 
   65         configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_notSaved());
 
   66         executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_notSaved());
 
   70         "ConfigVisualPanel3.saveConfigurationFile=Save imager" 
   73     public String getName() {
 
   74         return Bundle.ConfigVisualPanel3_saveConfigurationFile();
 
   91         "# {0} - configFilename",
 
   92         "ConfigVisualPanel3.failedToSaveConfigMsg=Failed to save configuration file: {0}",
 
   94         "ConfigVisualPanel3.reason=\nReason: {0}",
 
   95         "ConfigVisualPanel3.failedToSaveExeMsg=Failed to save tsk_logical_imager.exe file" 
   97     void saveConfigFile() {
 
   98         boolean saveSuccess = 
true;
 
   99         executableStatusLabel.setForeground(Color.BLACK);
 
  100         configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_savingInProgress());
 
  101         executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_savingInProgress());
 
  102         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
 
  103         GsonBuilder gsonBuilder = 
new GsonBuilder()
 
  105                 .excludeFieldsWithoutExposeAnnotation()
 
  106                 .disableHtmlEscaping();
 
  107         Gson gson = gsonBuilder.create();
 
  108         String toJson = gson.toJson(config);
 
  110             List<String> lines = Arrays.asList(toJson.split(
"\\n"));
 
  111             FileUtils.writeLines(
new File(configFilename), 
"UTF-8", lines, System.getProperty(
"line.separator")); 
 
  112             configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_saved());
 
  113         } 
catch (IOException ex) {
 
  115             configStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
 
  116             configStatusLabel.setForeground(Color.RED);
 
  117             JOptionPane.showMessageDialog(
this, Bundle.ConfigVisualPanel3_failedToSaveConfigMsg(configFilename)
 
  118                     + Bundle.ConfigVisualPanel3_reason(ex.getMessage()));
 
  119         } 
catch (JsonIOException jioe) {
 
  121             executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
 
  122             executableStatusLabel.setForeground(Color.RED);
 
  123             logger.log(Level.SEVERE, 
"Failed to save configuration file: " + configFilename, jioe); 
 
  124             JOptionPane.showMessageDialog(
this, Bundle.ConfigVisualPanel3_failedToSaveConfigMsg(configFilename)
 
  125                     + Bundle.ConfigVisualPanel3_reason(jioe.getMessage()));
 
  128             writeTskLogicalImagerExe(Paths.get(configFilename).getParent());
 
  129             executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_saved());
 
  130         } 
catch (IOException ex) {
 
  132             executableStatusLabel.setText(Bundle.ConfigVisualPanel3_copyStatus_error());
 
  133             executableStatusLabel.setForeground(Color.RED);
 
  134             logger.log(Level.SEVERE, 
"Failed to save tsk_logical_imager.exe file", ex); 
 
  135             JOptionPane.showMessageDialog(
this, Bundle.ConfigVisualPanel3_failedToSaveExeMsg()
 
  136                     + Bundle.ConfigVisualPanel3_reason(ex.getMessage()));
 
  140             firePropertyChange(SAVED_LOGICAL_IMAGER, 
false, 
true); 
 
  142         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
 
  153         "ConfigVisualPanel3.errorMsg.cannotFindLogicalImager=Cannot locate logical imager, cannot copy to destination" 
  155     private void writeTskLogicalImagerExe(Path destDir) 
throws IOException {
 
  156         File logicalImagerExe = getLogicalImagerExe();
 
  157         if (logicalImagerExe != null && logicalImagerExe.exists()) {
 
  158             FileUtils.copyFileToDirectory(getLogicalImagerExe(), destDir.toFile());
 
  160             throw new IOException(Bundle.ConfigVisualPanel3_errorMsg_cannotFindLogicalImager());
 
  170     static String getSavedEventName() {
 
  171         return SAVED_LOGICAL_IMAGER;
 
  179     @SuppressWarnings(
"unchecked")
 
  181     private 
void initComponents() {
 
  183         javax.swing.JButton saveButton = 
new javax.swing.JButton();
 
  184         descriptionScrollPane = 
new javax.swing.JScrollPane();
 
  185         descriptionTextArea = 
new javax.swing.JTextArea();
 
  186         javax.swing.JLabel configLabel = 
new javax.swing.JLabel();
 
  187         configStatusLabel = 
new javax.swing.JLabel();
 
  188         javax.swing.JLabel executableLabel = 
new javax.swing.JLabel();
 
  189         executableStatusLabel = 
new javax.swing.JLabel();
 
  191         org.openide.awt.Mnemonics.setLocalizedText(saveButton, 
org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, 
"ConfigVisualPanel3.saveButton.text")); 
 
  192         saveButton.addActionListener(
new java.awt.event.ActionListener() {
 
  193             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  194                 saveButtonActionPerformed(evt);
 
  198         descriptionTextArea.setEditable(
false);
 
  199         descriptionTextArea.setBackground(
new java.awt.Color(240, 240, 240));
 
  200         descriptionTextArea.setColumns(20);
 
  201         descriptionTextArea.setLineWrap(
true);
 
  202         descriptionTextArea.setRows(5);
 
  203         descriptionTextArea.setWrapStyleWord(
true);
 
  204         descriptionTextArea.setEnabled(
false);
 
  205         descriptionScrollPane.setViewportView(descriptionTextArea);
 
  207         org.openide.awt.Mnemonics.setLocalizedText(configLabel, 
org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, 
"ConfigVisualPanel3.configLabel.text")); 
 
  209         org.openide.awt.Mnemonics.setLocalizedText(executableLabel, 
org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, 
"ConfigVisualPanel3.executableLabel.text")); 
 
  211         org.openide.awt.Mnemonics.setLocalizedText(executableStatusLabel, 
org.openide.util.NbBundle.getMessage(ConfigVisualPanel3.class, 
"ConfigVisualPanel3.executableStatusLabel.text")); 
 
  213         javax.swing.GroupLayout layout = 
new javax.swing.GroupLayout(
this);
 
  214         this.setLayout(layout);
 
  215         layout.setHorizontalGroup(
 
  216             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  217             .addGroup(layout.createSequentialGroup()
 
  219                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  220                     .addGroup(layout.createSequentialGroup()
 
  221                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, 
false)
 
  222                             .addComponent(configLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
  223                             .addComponent(executableLabel))
 
  224                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  225                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
 
  226                             .addComponent(configStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
 
  227                             .addComponent(executableStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE))
 
  228                         .addGap(0, 10, Short.MAX_VALUE))
 
  229                     .addGroup(layout.createSequentialGroup()
 
  230                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  231                             .addComponent(descriptionScrollPane)
 
  232                             .addGroup(layout.createSequentialGroup()
 
  233                                 .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
 
  234                                 .addGap(0, 0, Short.MAX_VALUE)))
 
  235                         .addContainerGap())))
 
  237         layout.setVerticalGroup(
 
  238             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  239             .addGroup(layout.createSequentialGroup()
 
  241                 .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
  243                 .addComponent(saveButton)
 
  245                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, 
false)
 
  246                     .addComponent(configLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 
  247                     .addComponent(configStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
 
  248                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  249                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
  250                     .addComponent(executableLabel)
 
  251                     .addComponent(executableStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
 
  252                 .addContainerGap(100, Short.MAX_VALUE))
 
  256     private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  262     private javax.swing.JLabel configStatusLabel;
 
  263     private javax.swing.JScrollPane descriptionScrollPane;
 
  264     private javax.swing.JTextArea descriptionTextArea;
 
  265     private javax.swing.JLabel executableStatusLabel;
 
  275         "# {0} - configurationLocation",
 
  276         "ConfigVisualPanel3.description.text=Press Save to write the imaging tool and configuration file to the destination.\nDestination: {0}" 
  278     void setConfigInfoForSaving(String configFile, LogicalImagerConfig config) {
 
  279         this.configFilename = configFile;
 
  280         this.config = config;
 
  281         descriptionTextArea.setText(Bundle.ConfigVisualPanel3_description_text(FilenameUtils.getFullPath(configFilename)));