/*
 * NewProjectSetupPanel.java
 *
 * Created on January 5, 2005, 11:50 AM
 */

package DDMT.core;

import javax.swing.*;
import java.io.*;
import java.io.Writer.*;

/**
 *
 * @author  John Kloetzli
 */
public class NewProjectSetupPanel extends DDMT.core.DefaultPanel{
    
    
    /** Creates new form NewProjectSetupPanel */
    public NewProjectSetupPanel(DDMT.core.MainPanel.ControlClass cClass) {
        
        controlClass = cClass;
        
        initComponents();
        
        //Set the name of this panel to appear in the tab
        this.setName("New Project Setup");

    }
        
    /** 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.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();

        jPanel1.setPreferredSize(new java.awt.Dimension(50, 50));
        add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.Y_AXIS));

        jPanel5.setPreferredSize(new java.awt.Dimension(200, 300));
        jLabel1.setText("Enter project name below");
        jPanel8.add(jLabel1);

        jPanel5.add(jPanel8);

        jTextField1.setColumns(20);
        jTextField1.setMaximumSize(new java.awt.Dimension(500000, 20));
        jTextField1.setMinimumSize(new java.awt.Dimension(171, 20));
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jPanel5.add(jTextField1);

        jPanel2.setPreferredSize(new java.awt.Dimension(50, 10));
        jPanel5.add(jPanel2);

        jLabel2.setText("Enter the project description below");
        jPanel9.add(jLabel2);

        jPanel5.add(jPanel9);

        jTextArea1.setColumns(30);
        jTextArea1.setRows(10);
        jTextArea1.setLineWrap(true);
        jPanel5.add(jTextArea1);

        add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel4.setPreferredSize(new java.awt.Dimension(50, 50));
        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        jPanel3.setPreferredSize(new java.awt.Dimension(25, 25));
        jPanel3.setMaximumSize(new java.awt.Dimension(50000, 25));
        jPanel3.setMinimumSize(new java.awt.Dimension(25, 25));
        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel3.add(jButton1);

        jPanel4.add(jPanel3, java.awt.BorderLayout.SOUTH);

        add(jPanel4, java.awt.BorderLayout.SOUTH);

        jPanel6.setPreferredSize(new java.awt.Dimension(25, 25));
        add(jPanel6, java.awt.BorderLayout.EAST);

        jPanel7.setPreferredSize(new java.awt.Dimension(25, 25));
        add(jPanel7, java.awt.BorderLayout.WEST);

    }//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Check if the name is valid
        String name = new String(jTextField1.getText());
        
        //GO to the projects directory and make sure that there isn't already a project of this name.
        try{
            File file = new File("Files\\Projects");
            File[] names = file.listFiles();
            for( int i = 0; i<names.length; i++) {
                String temp = new String(names[i].getName().toLowerCase());
                if( names[i].isDirectory() && temp.equals(name.toLowerCase()) ) {
                    throw new Exception();
                }
            }
            
            //Set the current project
            controlClass.setProjectName(name);
            
            //Create the directory with the description file
            File directory = new File(new String(file.getAbsolutePath()+"\\"+name));
            File description = new File( new String(directory.getAbsolutePath()+"\\description.txt"));
            directory.mkdir();
            description.createNewFile();
            
            //create the data file
            File data = new File(new String(directory.getAbsolutePath())+"\\Data" );
            data.mkdir();
            
            //Output the description
            FileWriter out = new FileWriter(description);
            BufferedWriter outStream = new BufferedWriter(out);
            String text = jTextArea1.getText();
            int totalLines = jTextArea1.getLineCount();
            for (int i=0; i < totalLines; i++) {
                
                //Get the first line
                int start = jTextArea1.getLineStartOffset(i);
                int end   = jTextArea1.getLineEndOffset(i);
                String line = text.substring(start, end);
                
                //Write out that line
                for( int j = 0; j<line.length(); j++) {
                    outStream.write((int)line.charAt(j));
                }
                outStream.newLine();
            }
            outStream.flush();
            outStream.close();
            out.close();
            
            //Catch all exceptions
        }catch(IOException ex){
            controlClass.showError("Error:Unknown file output error!");
            controlClass.showError(ex.toString());
        }catch(Exception ex) {
            controlClass.showError("Error:A project named '"+name+"' already exists.Please choose a different name.");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    
    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        //
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
