/*
 * LoadPanel.java
 *
 * Created on December 1, 2004, 12:36 PM by John Kloetzli
 */

package DDMT.core;

import javax.swing.*;
import java.util.Vector;
import java.io.*;
import javax.swing.DefaultListModel;
/**
 *
 * @author  John Kloetzli
 */
public class ProjectSelectionPanel extends DDMT.core.DefaultPanel {
        
    //File that is pointing to the default directory for the current project.
    File currProject;
    
    //List model that holds the projects
    DefaultListModel listModel;
    
    /** Creates new form LoadPanel */
    public ProjectSelectionPanel(DDMT.core.MainPanel.ControlClass cClass) {
        controlClass = cClass;
        
        initComponents();
        
        //Print welcome message
        this.PrintWelcome();
        
        //Detect the projects that are available
        this.DetectProjects();
        
        //Set the name of this panel to appear in the main windows tab
        this.setName("Project Selection");

    }
    
    /** Prints out a welcome message to the user**/
    public void PrintWelcome()
    {
        this.DisplayFile(this.jTextArea1, new String("Files\\Core\\Welcome.txt"));
        
        //Since this is before any project could be selected, set the open button to disabled.
        jButton2.disable();
    }
    
    /**public method that tells this panel to sleep until further needed.  Sleep
     *means not to take up a lot of memory and certantly not to run any 
     *processes.
     */
    public void sleep()
    {
        
    }
    
    /**
     *DisplayFilel will take a text area and a string file name and display that 
     *file into the text area.  Any errors are outputted to the ControlClass's 
     *ErrorStream.
     */
    private void DisplayFile( JTextArea textArea, String filename)
    {
        //Clear out the text area.
        textArea.setText("");
        
        //Print out the welcome message
        File myFile = null;
        try{
            // Create a File
            myFile = new File(filename);

            // Create a Character Input Stream
            FileReader inStream = new FileReader(myFile);
        
            // Filter the Input Stream - buffers characters for efficiency
            BufferedReader in = new BufferedReader(inStream);

            //Display all the data
            String line = new String(in.readLine());
            while( line != null)
            {
                textArea.append( "\n"+line );
                line = in.readLine();
            }
        }catch( Exception ex){
            controlClass.showError("Error opening file: "+myFile.getAbsolutePath());
        } 
    }
    
    /**This method will detect what projects are currently available, and 
     *save a list of them.
     */
    private void DetectProjects()
    {
        //Put the projects on the list
        listModel = new DefaultListModel();
        
        //Go to the projects directory and count the sub-dirs.
        File file = new File("Files\\Projects");
        File[] names = file.listFiles();
        for( int i = 0; i<names.length; i++)
        {
            if( names[i].isDirectory() )
            {
                listModel.addElement(names[i].getName());
            }
        }
        
        //Add the model to the list
        jList1.setModel(listModel);
    }
    
    private void openFile()
    {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.showOpenDialog( this );
    }
    
    /** 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();
        jButton2 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jSeparator2 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jTextArea1 = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jTextArea2 = new javax.swing.JTextArea();

        jPanel1.setPreferredSize(new java.awt.Dimension(150, 400));
        jButton2.setText("Open");
        jButton2.setPreferredSize(new java.awt.Dimension(70, 23));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.add(jButton2);

        jPanel7.setPreferredSize(new java.awt.Dimension(70, 23));
        jPanel1.add(jPanel7);

        jLabel1.setText("Current Projects");
        jPanel1.add(jLabel1);

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

        jPanel8.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jPanel8.setPreferredSize(new java.awt.Dimension(120, 255));
        jScrollPane1.setBorder(null);
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        jPanel8.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel8);

        jPanel1.add(jSeparator2);

        add(jPanel1, java.awt.BorderLayout.WEST);

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

        jPanel3.setPreferredSize(new java.awt.Dimension(5, 5));
        jPanel2.add(jPanel3, java.awt.BorderLayout.WEST);

        jPanel4.setPreferredSize(new java.awt.Dimension(5, 5));
        jPanel2.add(jPanel4, java.awt.BorderLayout.EAST);

        jPanel5.setPreferredSize(new java.awt.Dimension(5, 5));
        jPanel2.add(jPanel5, java.awt.BorderLayout.NORTH);

        jPanel6.setPreferredSize(new java.awt.Dimension(5, 5));
        jPanel2.add(jPanel6, java.awt.BorderLayout.SOUTH);

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

        jPanel9.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jTextArea1.setEditable(false);
        jPanel9.add(jTextArea1);

        jPanel9.add(jSeparator1);

        jTextArea2.setEditable(false);
        jPanel9.add(jTextArea2);

        jPanel2.add(jPanel9, java.awt.BorderLayout.CENTER);

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

    }//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //Set the current project
        controlClass.setProjectName(((String)jList1.getSelectedValue()));
        controlClass.nodes = null;
        controlClass.showError("Project "+((String)jList1.getSelectedValue())+" loaded.");
        controlClass.showLastPanel();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
        
        //If there is no item highlighted, then do nothing
        if( jList1.getSelectedValue() != null)
        {
            //Enable the open button
            jButton2.enable();
            
            //see which item is highlighted
            currProject = new File("Files\\Projects\\"+(String)jList1.getSelectedValue()+"\\description.txt");
                
            //Look for a description file for the project
            this.DisplayFile(jTextArea2, new String(currProject.getPath()));

        }
    }//GEN-LAST:event_jList1MouseClicked

    /**This is the event for the New button clicked.*/    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    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.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration//GEN-END:variables
    
}
