/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * reportPanel.java
 *
 * Created on Feb 21, 2012, 12:13:14 PM
 */
package org.sleuthkit.autopsy.report;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Alex
 */
public class reportPanel extends javax.swing.JPanel {

    /** Creates new form reportPanel */
    public reportPanel(String report) {
        initComponents();
        setReportWindow(report);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jOptionPane1 = new javax.swing.JOptionPane();
        jButton1 = new javax.swing.JButton();
        saveReport = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jButton1.setText(org.openide.util.NbBundle.getMessage(reportPanel.class, "reportPanel.jButton1.text")); // NOI18N

        saveReport.setText(org.openide.util.NbBundle.getMessage(reportPanel.class, "reportPanel.saveReport.text")); // NOI18N
        saveReport.setActionCommand(org.openide.util.NbBundle.getMessage(reportPanel.class, "reportPanel.saveReport.actionCommand")); // NOI18N
        saveReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveReportActionPerformed(evt);
            }
        });

        jLabel1.setText(org.openide.util.NbBundle.getMessage(reportPanel.class, "reportPanel.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 128, Short.MAX_VALUE)
                        .addComponent(saveReport)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(saveReport))
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleName("");
        getAccessibleContext().setAccessibleParent(this);
    }// </editor-fold>//GEN-END:initComponents

private void saveReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveReportActionPerformed
  
    saveReportAction();
}//GEN-LAST:event_saveReportActionPerformed
  /**
     * Sets the listener for the OK button
     *
     * @param e  The action listener
     */
    public void setjButton1ActionListener(ActionListener e){
       jButton1.addActionListener(e);
    }
    public void getLink(HyperlinkEvent evt){
        try{
         
         String str = evt.getDescription();
       //  jEditorPane1.scrollToReference(str.substring(1));
        }
        catch(Exception e){
            String whater = "";
        }
    }
    public void setjEditorPane1EventListener(HyperlinkListener evt){
       // jEditorPane1.addHyperlinkListener(evt);
    }
    
    private void setReportWindow(String report)
    {
     //   jEditorPane1.setText(report);
     //   jEditorPane1.setCaretPosition(0);
    }
    
    public void setFinishedReportText(){
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        String reportText = "Report was sucessfully generated at " + dateFormat.format(date) + ".";
        jLabel1.setText(reportText);
    }
    
    
    private void saveReportAction(){
        
        int option = jFileChooser1.showOpenDialog(this);  
        if(option == jFileChooser1.APPROVE_OPTION){  
        if(jFileChooser1.getSelectedFile()!=null){  
        String path = jFileChooser1.getSelectedFile().toString();
        exportReport(path);
        }
        }
    }
    
    private void exportReport(String path){
        
       String htmlpath = reportUtils.changeExtension(path, ".html");
       String xmlpath = reportUtils.changeExtension(path, ".xml");
       String xlspath = reportUtils.changeExtension(path, ".xlsx");
         try {
                   Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlpath), "UTF-8"));
                   
                 // FileOutputStream out = new FileOutputStream(htmlpath);
                  out.write(reportHTML.formatted_header.toString());
                  out.flush();
                  out.close();
                  
                  //xls report
                   FileOutputStream fos = new FileOutputStream(xlspath);
                   reportXLS.wb.write(fos);
                   fos.close();
                  
                  FileOutputStream xmlout = new FileOutputStream(xmlpath);
                  XMLOutputter serializer = new XMLOutputter();
                  serializer.output(reportXML.xmldoc, xmlout);
                  xmlout.flush();
                  xmlout.close();
                   jOptionPane1.showMessageDialog(this, "Report has been successfully saved!");
                }
            catch (IOException e) {
              System.err.println(e);
                }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JOptionPane jOptionPane1;
    private javax.swing.JButton saveReport;
    // End of variables declaration//GEN-END:variables


}

@@ -5181,12 +5181,12 @@
 show
-Open
+Save
 Dial
@@ -5194,18 +5194,16 @@
 g(this);
-  
 %0A       

use save instead of open dialog