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

/*
 * MainScreen.java
 *
 * Created on 2009.03.20., 14:13:36
 */
package setup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import packagehandling.ZipExtractor;

/**
 *
 * @author Esta
 */
public class MainScreen extends javax.swing.JFrame {

    /** Creates new form MainScreen */
    public MainScreen() {
        initComponents();
        if (FileLocations.isLinux()) {
            this.destinationField.setText(System.getenv("HOME") + "/XMLCalculator");
        } else if (FileLocations.isWindows()) {
            this.destinationField.setText(System.getenv("ProgramFiles") + "\\XMLCalculator");
        }
    }

    /** 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() {

        confPanel = new javax.swing.JPanel();
        destinationLabel = new javax.swing.JLabel();
        destinationField = new javax.swing.JTextField();
        browseButton = new javax.swing.JButton();
        noteLabel = new javax.swing.JLabel();
        install = new javax.swing.JLabel();
        overwrite = new javax.swing.JLabel();
        thanx = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        closebutton = new javax.swing.JButton();
        startButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("setup/setupLanguages"); // NOI18N
        setTitle(bundle.getString("WinTitle")); // NOI18N
        setBounds(new java.awt.Rectangle(50, 50, 509, 394));
        setMaximizedBounds(new java.awt.Rectangle(50, 50, 529, 439));
        setMinimumSize(new java.awt.Dimension(509, 394));
        setResizable(false);

        confPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("SetupTitle"))); // NOI18N

        destinationLabel.setText(bundle.getString("destinationLabel")); // NOI18N

        browseButton.setText(bundle.getString("browse")); // NOI18N
        browseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButtonActionPerformed(evt);
            }
        });

        noteLabel.setText(bundle.getString("warning")); // NOI18N

        install.setText(bundle.getString("install")); // NOI18N

        overwrite.setText(bundle.getString("overwrite")); // NOI18N

        thanx.setText(bundle.getString("thankYou")); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setText(bundle.getString("warranty")); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        closebutton.setText(bundle.getString("close")); // NOI18N
        closebutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closebuttonActionPerformed(evt);
            }
        });

        startButton.setText(bundle.getString("start")); // NOI18N
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout confPanelLayout = new javax.swing.GroupLayout(confPanel);
        confPanel.setLayout(confPanelLayout);
        confPanelLayout.setHorizontalGroup(
            confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(confPanelLayout.createSequentialGroup()
                .addGroup(confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(confPanelLayout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(jScrollPane1))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, confPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(startButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(closebutton))
                            .addGroup(confPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(destinationField, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(browseButton))
                            .addGroup(confPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(noteLabel))
                            .addGroup(confPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(install))
                            .addGroup(confPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(overwrite))))
                    .addGroup(confPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(thanx))
                    .addGroup(confPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(destinationLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        confPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {browseButton, closebutton, startButton});

        confPanelLayout.setVerticalGroup(
            confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(confPanelLayout.createSequentialGroup()
                .addComponent(destinationLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(noteLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(install)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(overwrite)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(thanx)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(confPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closebutton)
                    .addComponent(startButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        confPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {browseButton, closebutton, startButton});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(confPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(confPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closebuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closebuttonActionPerformed
        System.exit(0);
    }//GEN-LAST:event_closebuttonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        boolean test = true;
        if (FileLocations.isWindows()) {
            String archive = null;
            String to = null;
            try {
                archive = "InstallData.zip";
                to = System.getenv("TEMP");
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("TEMP") + "\\XmlCalcSettings.zip";
                to = FileLocations.getSettingsDir();
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("TEMP") + "\\XmlCalcProgram.zip";
                to = this.destinationField.getText();
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("TEMP") + "\\XmlCalcLib.zip";
                to = this.destinationField.getText() + "\\lib";
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                if (test) {
                    this.startButton.setEnabled(false);
                    JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("OK"), JOptionPane.INFORMATION_MESSAGE);
                    JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("info"));
                    dialog.setVisible(true);
                } else {
                    JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("failed"), JOptionPane.OK_OPTION);
                    JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                    dialog.setVisible(true);
                }
            } catch (FileNotFoundException ex) {
                JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive, JOptionPane.OK_OPTION);
                JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                dialog.setVisible(true);
            } catch (IOException ex) {
                String msg = null;
                if (ex.getMessage().equalsIgnoreCase("CANT_WRITE_DIR")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + to;
                } else if (ex.getMessage().equalsIgnoreCase("CANT_READ_ARCHIVE")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive;
                } else if (ex.getMessage().equalsIgnoreCase("ARCHIVE_NOT_FOUND")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive;
                } else {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage());
                }
                JOptionPane optionPane = new JOptionPane(msg, JOptionPane.OK_OPTION);
                JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                dialog.setVisible(true);
            }
        } else if (FileLocations.isLinux()) {
            String archive = null;
            String to = null;
            try {
                archive = "InstallData.zip";
                to = System.getenv("HOME") + "/.XMLCalculator/Temp";
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("HOME") + "/.XMLCalculator/Temp/XmlCalcSettings.zip";
                to = FileLocations.getSettingsDir();
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("HOME") + "/.XMLCalculator/Temp/XmlCalcProgram.zip";
                to = this.destinationField.getText();
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                archive = System.getenv("HOME") + "/.XMLCalculator/Temp/XmlCalcLib.zip";
                to = this.destinationField.getText() + "/lib";
                test = test && ZipExtractor.doUnPack(new File(archive), new File(to));
                if (test) {
                    this.startButton.setEnabled(false);
                    JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("OK"), JOptionPane.INFORMATION_MESSAGE);
                    JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("info"));
                    dialog.setVisible(true);
                } else {
                    JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("failed"), JOptionPane.OK_OPTION);
                    JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                    dialog.setVisible(true);
                }
            } catch (FileNotFoundException ex) {
                JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive, JOptionPane.OK_OPTION);
                JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                dialog.setVisible(true);
            } catch (IOException ex) {
                String msg = null;
                if (ex.getMessage().equalsIgnoreCase("CANT_WRITE_DIR")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + to;
                } else if (ex.getMessage().equalsIgnoreCase("CANT_READ_ARCHIVE")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive;
                } else if (ex.getMessage().equalsIgnoreCase("ARCHIVE_NOT_FOUND")) {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage()) + archive;
                } else {
                    msg = java.util.ResourceBundle.getBundle("setup/setupLanguages").getString(ex.getMessage());
                }
                JOptionPane optionPane = new JOptionPane(msg, JOptionPane.OK_OPTION);
                JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
                dialog.setVisible(true);
            }
        } else {
            JOptionPane optionPane = new JOptionPane(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("NotSupportedOS"), JOptionPane.OK_OPTION);
            JDialog dialog = optionPane.createDialog(java.util.ResourceBundle.getBundle("setup/setupLanguages").getString("error"));
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(this.destinationLabel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            this.destinationField.setText(file.getAbsolutePath());
        } else {
        }
    }//GEN-LAST:event_browseButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton;
    private javax.swing.JButton closebutton;
    private javax.swing.JPanel confPanel;
    private javax.swing.JTextField destinationField;
    private javax.swing.JLabel destinationLabel;
    private javax.swing.JLabel install;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel noteLabel;
    private javax.swing.JLabel overwrite;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel thanx;
    // End of variables declaration//GEN-END:variables
}
