/*
 * AgentTypesPanel.java
 *
 * Created on January 10, 2005, 10:40 AM
 */
//NodeClassMatching.txt
package DDMT.core;

import javax.swing.DefaultListModel;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.regex.*;
import DDMT.Nodes.*;
import java.text.*;
import java.util.Vector;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.*;

/**
 *
 * @author  John Kloetzli
 */
public class AgentTypesPanel extends DDMT.core.DefaultPanel {
    
    //List model that holds the agent types
    private DefaultListModel classNamesModel;
    
    //List model that holds the Node Type - Class Name match.
    private DefaultListModel nodeClassModel;
    
    //List model that holds the current network locations
    private DefaultListModel locationModel;
    
    //List model that holds the node - network location matches.
    private DefaultListModel networkNodeModel;

    //Pattern for outputting decimal numbers
    private String floatPattern = new String("#####.########");
    private DecimalFormat floatFormat = new DecimalFormat(floatPattern);
    
    //Pattern for a node definition
    //Node Number, Network Location, Data Location, Node Type, X-value, Y-value, Neighbor1, Neighbor1 delay, Neighbor2, ...
    private Pattern nodePattern = Pattern.compile("\\d+\\s\\S+\\s\\S+\\s\\S+\\s\\d+\\s\\d+\\s[\\d+\\s\\d+\\p{Punct}\\d+\\s]*");
    private Matcher nodeMatcher;
    
    //Define patterns for integers, strings, doubles, first string, and last string
    private Pattern integerP = Pattern.compile("\\d+");
    private Pattern doubleP = Pattern.compile("\\d+\\p{Punct}\\d+");
    private Pattern stringP = Pattern.compile("\\S+\\s");
    private Pattern alphaStringP = Pattern.compile("[a-zA-Z]\\S*");
    private Pattern neighborP = Pattern.compile("\\s\\d+\\s\\d+\\p{Punct}\\d+");
    private Pattern fileP = Pattern.compile("\\S+\\s\\S+");
    private Pattern firstP = Pattern.compile("\\A\\S+");
    private Pattern lastP = Pattern.compile("\\S+\\Z");
    private Matcher fileM;
    private Matcher firstM;
    private Matcher lastM;
    private Matcher integerM;
    private Matcher doubleM;
    private Matcher stringM;
    private Matcher alphaStringM;
    private Matcher neighborM;
    
    //Agent to query through
    private MonitorAgent agent;
    
    //the node types - class name array
    String[][] nodeTypes;
    
    /** Creates new form AgentTypesPanel */
    public AgentTypesPanel(DDMT.core.MainPanel.ControlClass cClass) {
        initComponents();
        
        controlClass = cClass;
        
        //Set the name of this panel to appear in the main window's tab pane
        this.setName("Agent Types");
        
        //Set up the agent reference
        agent = controlClass.getAgent();
    }
    
    /**
     *Method for distributing all the settings that each node should know
     *
     */
     private void distributeSettings()
     {
         nodeTypes = new String[2][nodeClassModel.size()];
         for( int i = 0; i< nodeClassModel.size(); i++ )
         {
             String[] element = ((String[])nodeClassModel.elementAt(i));
             nodeTypes[0][i] = element[0];
             nodeTypes[1][i] = element[1];
         }
     }
     
    /**
     *Method for pinging the network.  Will ask for a list of all node-bearing 
     *agents running
     */
    private void pingNetwork()
    {        
        if( controlClass.nodes != null )
        {   
            //Get the current node matching
            this.distributeSettings();
            
            //Generate the data for the Node - Network Location list
            networkNodeModel = new DefaultListModel();
            for( int i = 0; i< controlClass.nodes.length; i++)
            {
                //System.out.println(i);
                String tmp = controlClass.nodes[i].getType();
                for( int j = 0; j< nodeTypes[0].length; j++)
                {
                    if( controlClass.nodes[i].getType().equals( nodeTypes[0][j]))
                    {
                        if( !nodeTypes[1][j].equals( "None") )
                        {
                            networkNodeModel.addElement(new String[]{controlClass.nodes[i].getNumber()+" ("+nodeTypes[1][j]+")",controlClass.nodes[i].getNetworkLocation() });
                        }else
                        {
                            networkNodeModel.addElement(new String[]{controlClass.nodes[i].getNumber()+" ("+controlClass.nodes[i].getType()+")",controlClass.nodes[i].getNetworkLocation() });
                        }
                        break;
                    }
                }
            }
            jList4.setCellRenderer(new DoubleCellRenderer());
            jList4.setModel( networkNodeModel );
            
            //Query the DF for currently running agents.
            controlClass.getAgent().search();

            //pause
            try{
                Thread.sleep(500);
            }catch(Exception ex)
            {
                System.out.println("Error polling for agents");
            }

            //Check for answers
            Vector dfd = controlClass.getAgent().find();
            locationModel = new DefaultListModel();
            for( int i = 0; i<dfd.size(); i++)
            {
                locationModel.addElement((String)dfd.elementAt(i));
            }
            jList2.setModel(locationModel);
        }
    }
    
    /**
     *Method for loading the known agent types from the Nodes.txt file.
     */
    private void loadAgentTypes()
    {
        if( controlClass.nodes == null )
        {
            System.out.println("loading data again...");
            //Open the Nodes.txt file
            File nodesFile = null;
            try{
                nodesFile = new File("Files\\Projects\\" + controlClass.getProjectName() + "\\nodes.txt");

                //Load the nodes from the file
                controlClass.fileLoader.loadNodes(nodesFile);
            }catch(IOException ex)
            {
                controlClass.showError("Agent Types: Error: File "+nodesFile.getPath()+" could not be opened!");
            }catch(NullPointerException ex)
            {
            }catch(Exception ex)
            {
                controlClass.showError("Agent Types: Error: Could not read file "+nodesFile.getPath()+".");
            }
        }
        //Load the agent types matching into the list model
        nodeClassModel = new DefaultListModel();
        File file = null;
        try{
            file = new File("Files\\Projects\\"+controlClass.getProjectName()+"\\NodeClassMatching.txt");
        }catch(Exception ex)
        {
            controlClass.showError("Agent Types: Error: No project selected!  Please select a project from the 'Project Selection' tab.");
            return;
        }
        if( file.exists() )
        {
            //Read in the file
            String[] types = null;
            String[] classes = null;
            String[][] temp = this.readFile( file ); 
            if( temp != null)
            {
                types = temp[0];
                classes = temp[1];
                for( int j = 0; j<temp[0].length; j++)
                {
                    nodeClassModel.addElement(new String[]{temp[1][j],temp[0][j]});
                }
            }
        }else{
            
            //Generate the file
            file.delete();
            try{
                for( int i = 0; i<controlClass.nodes.length; i++)
                {
                    boolean found = false;
                    for( int j = 0; j< nodeClassModel.size(); j++ )
                    {
                        //If this element is in the model, then stop
                        if( (((String)((Object[])nodeClassModel.elementAt(j))[0]).equals( controlClass.nodes[i].getType() )) )
                        {
                            found = true;
                            break;
                        }
                    }
                    
                    if( !found )
                    {
                        nodeClassModel.addElement(new String[]{controlClass.nodes[i].getType(),"None"});
                    }
                }
            }catch(NullPointerException ex)
            {
                return;
            }
        }
        jList3.setCellRenderer(new DoubleCellRenderer());
        jList3.setModel(nodeClassModel);
        
        //Load the known node class types        
        try{
            //Read in the file
            file = new File("Files\\Core\\NodeClasses.txt");
            FileReader reader = new FileReader(file);
            BufferedReader bReader = new BufferedReader(reader);
            
            String line = bReader.readLine();
            classNamesModel = new DefaultListModel();
            while(line != null)
            {
                classNamesModel.addElement(line);
                line = bReader.readLine();
            }
            jList1.setModel(classNamesModel);
            
            reader.close();
            bReader.close();
        }catch(Exception ex)
        {
        }
    }
    
    /**
     *Method takes two arrays of strings and a file and reads the file as a 
     *matched pair of data items, which it puts into the arrays.
     */
    private String[][] readFile( File file )
    {
        String[][] array = null;
         try{   
                //Read from the file
                file.createNewFile();
                FileReader reader = new FileReader(file);
                BufferedReader bReader = new BufferedReader(reader);
                String line = bReader.readLine();
                
                //First line contains the number of pairs
                array = new String[2][Integer.parseInt(line)];
                
                //Read in all the pairs
                line = bReader.readLine();
                int j = 0;
                while(line != null )
                {
                    fileM = fileP.matcher(line);
                    if( fileM.matches() )
                    {
                        //Set up matchers
                        firstM = firstP.matcher(line);
                        lastM = lastP.matcher(line);
                        
                        //Find the pair
                        firstM.find();
                        array[0][j] = firstM.group();
                        lastM.find();
                        array[1][j] = lastM.group();
                    }
                    line = bReader.readLine();
                    j++;
                }
                
                //Close the readers
                reader.close();
                bReader.close();
                
            }catch(Exception ex)
            {
                controlClass.showError("Agent Types: Error: Unable to read file "+file.getPath()+". Refresh this panel to try again.");
                file.delete();
                ex.printStackTrace();
            }
        
        return array;
    }
    
    /**
     *Method that will tell the panel to save all it's data to files so that other
     *panels can then use it.  The file that this panel saves is nodes.txt, which
     *holds a list of all the nodes in the system.
     */
    public void save()
    {   
        //Remove the old file 
        File file = new File("Files\\Projects\\"+controlClass.getProjectName()+"\\partitionTypes.txt");
        if( file.exists() )
            file.delete();
        
        //Define some patterns used below
        Pattern firstP = Pattern.compile("\\A\\S+");
        Pattern lastP = Pattern.compile("\\S+\\Z");
        Matcher firstM;
        Matcher lastM;
        
        //Save the current node-networklocation matching
        int max = networkNodeModel.getSize();
        for( int i = 0; i< max; i++ )
        {
            String input1 = ((String[])networkNodeModel.elementAt(i))[0];
            String input2 = ((String[])networkNodeModel.elementAt(i))[1];
            firstM = firstP.matcher(input1);
            firstM.find();
            int nodeNum = Integer.parseInt( firstM.group() );
            
            controlClass.nodes[nodeNum].setNetworkLocation(input2);
        }
        
        try
        {
            
            //Clear out the old files if they exist
            if( controlClass.getProjectName() == null )
            {
                throw new Exception();
            }
            File nodesFile = new File("Files\\Projects\\" + controlClass.getProjectName() + "\\nodes.txt");
            if( nodesFile.exists() )
                nodesFile.delete();
            nodesFile.createNewFile();
            
            //Save the nodes file
            controlClass.fileLoader.saveNodes(nodesFile);
        }catch( IOException ex)
        {
            controlClass.showError("Agent Types: Error: Couldn't save topology information to file!");
        }catch( NullPointerException ex )
        {
            controlClass.showError("Agent Types: Error: No Project selected!  Could not save file!");
        }catch( Exception x)
        {
            controlClass.showError("Agent Types: Error: Unknown Exception");
        }
        
        //Save the current matching between node types and classes
        try
        {
            file = new File("Files\\Projects\\"+controlClass.getProjectName()+"\\NodeClassMatching.txt");
            FileWriter writer = new FileWriter(file);
            BufferedWriter bWriter = new BufferedWriter(writer);
            
            max = nodeClassModel.getSize();
            bWriter.write(""+max);
            for( int i = 0; i< max; i++ )
            {
                String input1 = ((String[])nodeClassModel.elementAt(i))[0];
                String input2 = ((String[])nodeClassModel.elementAt(i))[1];
                firstM = lastP.matcher(input1);
                lastM = firstP.matcher(input2);
                firstM.find();
                lastM.find();
                bWriter.newLine();
                bWriter.write(lastM.group() + " "+firstM.group());
            }
            bWriter.flush();
            bWriter.close();
            writer.close();
            
        }catch(Exception ex)
        {
            controlClass.showError("Agent Types: Error: Could not open file "+file.getPath()+" for writing.");
            file.delete();
        }
    }
    
    /**
     *Method for refreshing the data on the panel.
     */
    public void refresh()
    {
        this.loadAgentTypes();
        this.pingNetwork();
    }
    
    /** 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
        jPanel12 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel27 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel13 = new javax.swing.JPanel();
        jPanel15 = new javax.swing.JPanel();
        jPanel17 = new javax.swing.JPanel();
        jPanel26 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jPanel18 = new javax.swing.JPanel();
        jPanel25 = new javax.swing.JPanel();
        jPanel16 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jPanel19 = new javax.swing.JPanel();
        jPanel24 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jPanel11 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jPanel21 = new javax.swing.JPanel();
        jPanel22 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jSeparator3 = new javax.swing.JSeparator();
        jPanel14 = new javax.swing.JPanel();
        jPanel23 = new javax.swing.JPanel();

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

        jPanel1.setPreferredSize(new java.awt.Dimension(25, 25));
        jPanel1.setMaximumSize(new java.awt.Dimension(25, 25));
        jPanel1.setMinimumSize(new java.awt.Dimension(25, 25));
        jPanel27.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 0));

        jButton3.setText("Refresh");
        jButton3.setPreferredSize(new java.awt.Dimension(85, 23));
        jButton3.setFocusCycleRoot(true);
        jButton3.setMaximumSize(new java.awt.Dimension(85, 23));
        jButton3.setMinimumSize(new java.awt.Dimension(85, 23));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel27.add(jButton3);

        jButton10.setText("Save");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jPanel27.add(jButton10);

        jPanel1.add(jPanel27, java.awt.BorderLayout.CENTER);

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

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

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

        jPanel3.setPreferredSize(new java.awt.Dimension(150, 150));
        jPanel3.setMaximumSize(new java.awt.Dimension(150, 150));
        jPanel3.setMinimumSize(new java.awt.Dimension(150, 150));
        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.Y_AXIS));

        jPanel5.setPreferredSize(new java.awt.Dimension(150, 150));
        jPanel5.setMaximumSize(new java.awt.Dimension(555555555, 5555555));
        jPanel5.setMinimumSize(new java.awt.Dimension(300, 225));
        jPanel6.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Known Agent Classes");
        jPanel6.add(jLabel1, java.awt.BorderLayout.NORTH);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(110, 150));
        jScrollPane1.setViewportView(jList1);

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

        jPanel13.setPreferredSize(new java.awt.Dimension(150, 10));
        jPanel13.setMaximumSize(new java.awt.Dimension(150, 10));
        jPanel13.setMinimumSize(new java.awt.Dimension(150, 10));
        jPanel6.add(jPanel13, java.awt.BorderLayout.SOUTH);

        jPanel6.add(jPanel15, java.awt.BorderLayout.EAST);

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

        jPanel17.setMaximumSize(new java.awt.Dimension(10, 10));
        jPanel6.add(jPanel17, java.awt.BorderLayout.SOUTH);

        jPanel6.add(jPanel26, java.awt.BorderLayout.SOUTH);

        jPanel5.add(jPanel6);

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

        jLabel2.setText("Network Locations");
        jPanel7.add(jLabel2, java.awt.BorderLayout.NORTH);

        jScrollPane2.setPreferredSize(new java.awt.Dimension(110, 130));
        jScrollPane2.setViewportView(jList2);

        jPanel7.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel18, java.awt.BorderLayout.EAST);

        jPanel7.add(jPanel25, java.awt.BorderLayout.SOUTH);

        jPanel5.add(jPanel7);

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

        jPanel3.add(jPanel16, java.awt.BorderLayout.WEST);

        jPanel2.add(jPanel3, java.awt.BorderLayout.WEST);

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

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

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

        jLabel3.setText("BRITE Node Name and Class Name");
        jLabel3.setPreferredSize(new java.awt.Dimension(269, 15));
        jPanel10.add(jLabel3, java.awt.BorderLayout.NORTH);

        jScrollPane3.setPreferredSize(new java.awt.Dimension(400, 150));
        jScrollPane3.setViewportView(jList3);

        jPanel10.add(jScrollPane3, java.awt.BorderLayout.CENTER);

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

        jPanel19.setMaximumSize(new java.awt.Dimension(10, 10));
        jPanel10.add(jPanel19, java.awt.BorderLayout.SOUTH);

        jButton4.setText("Set");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel24.add(jButton4);

        jButton5.setText("Clear");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jPanel24.add(jButton5);

        jButton6.setText("Load...");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jPanel24.add(jButton6);

        jPanel10.add(jPanel24, java.awt.BorderLayout.SOUTH);

        jPanel9.add(jPanel10);

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

        jLabel4.setText("    Node and Network Location");
        jPanel11.add(jLabel4, java.awt.BorderLayout.NORTH);

        jScrollPane4.setPreferredSize(new java.awt.Dimension(400, 150));
        jScrollPane4.setViewportView(jList4);

        jPanel11.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jPanel11.add(jPanel21, java.awt.BorderLayout.WEST);

        jButton7.setText("Set");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jPanel22.add(jButton7);

        jButton8.setText("Clear");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jPanel22.add(jButton8);

        jButton9.setText("Load...");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jPanel22.add(jButton9);

        jPanel11.add(jPanel22, java.awt.BorderLayout.SOUTH);

        jPanel9.add(jPanel11);

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

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

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

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

        jPanel2.add(jPanel8, java.awt.BorderLayout.SOUTH);

        jPanel2.add(jPanel14, java.awt.BorderLayout.NORTH);

        jPanel2.add(jPanel23, java.awt.BorderLayout.EAST);

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

    }//GEN-END:initComponents

    /**
     *Method for 'Load' under Node and Network Location matching list clicked
     */
    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        //Open a file chooser
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this.getParent());
        
        
        //Get the return file, if applicable
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String[][] array = this.readFile( file );
            String[] types = array[0];
            String[] classes = array[1];
            
            try{
                //Make sure that the file was the right length
                //System.out.println(types.length + "  "+controlClass.nodes.length);
                if( types.length != controlClass.nodes.length )
                {
                    throw new Exception();
                }
                
                //Load the data into the lists
                networkNodeModel = new DefaultListModel();
                for( int i = 0; i<types.length; i++)
                {
                    networkNodeModel.addElement(new String[]{array[0][i] + " ("+controlClass.nodes[i].getType()+" )", array[1][i] });    
                    //System.out.println(controlClass.nodes[i].getType());
                }
                jList4.setCellRenderer(new DoubleCellRenderer());
                jList4.setModel(networkNodeModel);
            }catch(Exception ex)
            {
                controlClass.showError("Agent Types: Error: Wrong number of items in file "+file.getPath()+"!");
            }
        }
    }//GEN-LAST:event_jButton9ActionPerformed
    /**
     *Method for 'Save' clicked
     */
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        this.save();
        
        controlClass.showError("Agent Types: All data saved");
    }//GEN-LAST:event_jButton10ActionPerformed

    /**
     *Method for 'Clear' under the node - locations matching list
     */
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        //Reset the selection to 'None'
        Object[] tmp = jList4.getSelectedValues();
        int[] index = ((int[])jList4.getSelectedIndices());
        for( int i = 0; i<tmp.length; i++)
        {
            try
            {
                networkNodeModel.setElementAt(new String[]{ ((String[])networkNodeModel.getElementAt(index[i]))[0] ,"None"}, index[i]);
            }catch( Exception ex)
            {
            }
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    /**
     *Method for 'Set' under the node - locations matching list
     */
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        //Replace the location with the selected one
        Object[] tmp = jList4.getSelectedValues();
        int[] index = ((int[])jList4.getSelectedIndices());
        for( int i = 0; i<tmp.length; i++)
        {
            try
            {
                String temp = (String)jList2.getSelectedValue();
                if(temp.equals("null"))
                {
                    
                }else
                {
                    networkNodeModel.setElementAt( new String[]{ ((String[])networkNodeModel.elementAt(index[i]))[0] , temp } , index[i]);
                }
            }catch( Exception ex)
            {
            }
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    /**
     *Method for 'Load..' under the node type - class matching list
     */
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        //Open a file chooser
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this.getParent());
        
        //Get the return file, if applicable
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String[][] array = this.readFile( file );
            String[] types = array[0];
            String[] classes = array[1];
            
            try
            {
                //Check for the correct number of pairs
                if( types.length != nodeClassModel.getSize())
                {
                    throw new Exception();
                }
                
                //Load the data into the lists
                nodeClassModel = new DefaultListModel();
                for( int i = 0; i<types.length; i++)
                {
                    nodeClassModel.addElement(new String[]{ array[0][i] , array[1][i]});
                }
            }catch(Exception ex)
            {
                controlClass.showError("Agent Types: Error: Wrong number of items in file "+file.getPath()+"!");
            }
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     *Method for 'Clear' under the node type - class matching list
     */
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        //Reset the selection to 'none'
        Object[] tmp = jList3.getSelectedValues();
        int[] index = ((int[])jList3.getSelectedIndices());
        for( int i = 0; i<tmp.length; i++)
        {
            try
            {
                nodeClassModel.setElementAt( new String[]{ ((String[])nodeClassModel.getElementAt(index[i]))[0],"None"} , index[i]);
            }catch( Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        this.pingNetwork();
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
     *Method for 'Set' under the node type - class matching list
     */
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        //Replace the class with the selected one
        Object[] tmp = jList3.getSelectedValues();
        int[] index = ((int[])jList3.getSelectedIndices());
        for( int i = 0; i<tmp.length; i++)
        {
            try
            {
                String temp = (String)jList1.getSelectedValue();
                if(temp != null && temp.equals("null"))
                {
                    
                }else if( temp != null)
                {
                    nodeClassModel.setElementAt( new String[]{ ((String[])nodeClassModel.getElementAt(index[i]))[0] , temp}, index[i]);
                }
                
            }catch( Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
        this.pingNetwork();
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     *Method for "Refresh" pressed.
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //re-ping network
        this.loadAgentTypes();
        this.pingNetwork();
    }//GEN-LAST:event_jButton3ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JList jList4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel21;
    private javax.swing.JPanel jPanel22;
    private javax.swing.JPanel jPanel23;
    private javax.swing.JPanel jPanel24;
    private javax.swing.JPanel jPanel25;
    private javax.swing.JPanel jPanel26;
    private javax.swing.JPanel jPanel27;
    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.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator3;
    // End of variables declaration//GEN-END:variables
 
     /**
     *Cell renderer for the list.  Shows two strings seperated by a two-way arrow.
     */
    public class DoubleCellRenderer extends JPanel implements ListCellRenderer {
            
        //The left label
        JLabel left;
        
        //The right label
        JLabel right;
        
        //the center label
        JLabel center;
        
        /**
         *Method to create a new cell renderer
         */
        DoubleCellRenderer() 
        {
            setLayout( new GridLayout());
            left = new JLabel();
            right = new JLabel();
            center = new JLabel();
            left.setOpaque(true);
            right.setOpaque(true);
            center.setOpaque(true);
            add(left);
            add(center);
            add(right);
        }
      
        /**
         *Method to get one component of the cell renderer
         */
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
        {
            //The first string
            String leftS = ((String[])value)[0];
            String rightS = ((String[])value)[1];
            left.setText(leftS);
            right.setText(rightS);
            center.setIcon(new ImageIcon("Files\\Core\\Icons\\arrow.gif"));
            
            if (isSelected) 
            {
                right.setBackground( list.getSelectionBackground());
                right.setForeground( list.getSelectionForeground());
                left.setBackground( list.getSelectionBackground());
                left.setForeground( list.getSelectionForeground());
                center.setBackground( list.getSelectionBackground());
                center.setForeground( list.getSelectionForeground());
                right.setEnabled(true);
                left.setEnabled(true);
                center.setEnabled(true);
            } else {
                right.setBackground(list.getBackground());
                right.setForeground(list.getForeground());
                left.setBackground(list.getBackground());
                left.setForeground(list.getForeground());
                center.setBackground(list.getBackground());
                center.setForeground(list.getForeground());
                right.setEnabled(true);
                left.setEnabled(true);
                center.setEnabled(true);
            }
            
            if( !list.isEnabled() )
            {
                right.setBackground(list.getBackground());
                right.setEnabled(false);
                left.setBackground(list.getBackground());
                left.setEnabled(false);
                center.setBackground(list.getBackground());
                center.setEnabled(false);
            }
            
            setEnabled(list.isEnabled());
            setFont(list.getFont());
            return this;
        }
    }
}
