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

/*
 * NewJFrame.java
 *
 * Created on Aug 9, 2009, 3:41:02 PM
 */

package swingpackage;

import CommonFunctions.debugPrinter;
import database.Context;
import java.util.Enumeration;
import javax.swing.JOptionPane;
import mdot.Main;
import structures.Constants;
import structures.HashContext;
import structures.HashEntryDoubleVector;
import structures.RoadSignEntry;

/**
 *
 * @author baba
 */
public class TreeView2 extends javax.swing.JFrame {

    public static boolean busy = false;
    private String scalarVectorSelected = "";
    private String valueTypeSelected = "";
    private String SCALAR_STRING = "Scalar";
    private String VECTOR_STRING = "Vector";
    private String STRING_STRING = "String";
    private String DOUBLE_STRING = "Double";
    private static HashContext dataFromWindow = null;
    /** Creates new form NewJFrame */
    public TreeView2() {
        initComponents();
        createNodes();
        InitializeComboBoxes();
    }

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

        txtAreaContext = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        txtValue = new javax.swing.JTextField();
        cboScalarVector = new javax.swing.JComboBox();
        cboValueType = new javax.swing.JComboBox();
        txtKey = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtRemoveKey = new javax.swing.JTextField();
        btnRemove = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        btnAdd1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        btnAdd2 = new javax.swing.JButton();
        txtAddVehicle = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cbVehicles = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("IntelliDrive Debugger");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        txtAreaContext.setColumns(20);
        txtAreaContext.setEditable(false);
        txtAreaContext.setRows(5);

        jLabel1.setText("Vehicle ID:");

        jLabel2.setText("Key:");

        jLabel3.setText("Value Type:");

        jLabel4.setText("Scalar/Vector:");

        jLabel5.setText("Value:");

        btnAdd.setText("Add Key:Value");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        txtValue.setText("jTextField1");

        cboScalarVector.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboScalarVector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboScalarVectorActionPerformed(evt);
            }
        });

        cboValueType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboValueType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboValueTypeActionPerformed(evt);
            }
        });

        txtKey.setText("jTextField1");

        jLabel6.setText("Key:");

        btnRemove.setText("Remove Key");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnAdd1.setText("Remove Vehicle");
        btnAdd1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdd1ActionPerformed(evt);
            }
        });

        jLabel7.setText("Vehicle ID:");

        btnAdd2.setText("Add Vehicle");
        btnAdd2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdd2ActionPerformed(evt);
            }
        });

        txtAddVehicle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAddVehicleActionPerformed(evt);
            }
        });

        jLabel8.setText("Vehicle ID:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel9.setText("Add/Remove Vehicles");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel10.setText("Add/Remove Keys");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(txtAreaContext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 328, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel9)
                            .add(jLabel1)
                            .add(jLabel7))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, btnAdd1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.LEADING, btnAdd2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(txtAddVehicle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel10)
                            .add(jLabel8)
                            .add(jLabel2)
                            .add(jLabel5)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel6))
                        .add(36, 36, 36)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btnRemove, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtRemoveKey, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cboScalarVector, 0, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cboValueType, 0, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtValue, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, txtKey, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btnAdd, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, cbVehicles, 0, 160, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel9)
                        .add(10, 10, 10)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAdd1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel7)
                            .add(txtAddVehicle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAdd2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel10)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cbVehicles, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel8))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtKey, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel5))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cboValueType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel3))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cboScalarVector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnAdd)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(txtRemoveKey, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel6))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnRemove))
                    .add(txtAreaContext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 528, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    private void txtAddVehicleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAddVehicleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAddVehicleActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        String keyName = txtKey.getText().trim();
        String value = txtValue.getText().trim();

        if(cbVehicles.getSelectedItem().toString().equals(Main.meString))
        {
        if (valueTypeSelected.equals(STRING_STRING)) {
            if (keyName != null && value != null) {
                if (!keyName.equals("") && !value.equals("")) {
                    if (!Main.getMe().ContainsKey(keyName)) {
                        Main.getMe().setObjectAsString(keyName, value);
                        UpdateTree();
                    } else {
                        JOptionPane.showConfirmDialog(this, "Duplicate Key",
                                "Error", JOptionPane.DEFAULT_OPTION);
                    }
                } else {
                    JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        } else {
            if (keyName != null && value != null) {
                if (!keyName.equals("") && !value.equals("")) {
                    if (!Main.getMe().ContainsKey(keyName)) {
                        if (scalarVectorSelected.equals(SCALAR_STRING)) {
                            try {
                                Main.getMe().setObjectAsDouble(keyName, Double.parseDouble(value));
                                UpdateTree();
                            } catch (Exception ex) {
                                JOptionPane.showConfirmDialog(this, "Value has to be Double",
                                        "Error", JOptionPane.DEFAULT_OPTION);
                            }
                        } else {
                            try {
                                Main.getMe().setObjectAsDoubleVectorwithException(keyName, value);
                                UpdateTree();
                            } catch (Exception ex) {
                                JOptionPane.showConfirmDialog(this, "Value has to be Double Vector delimited by \",\"",
                                        "Error", JOptionPane.DEFAULT_OPTION);
                            }
                        }
                    } else {
                        JOptionPane.showConfirmDialog(this, "Duplicate Key",
                                "Error", JOptionPane.DEFAULT_OPTION);
                    }
                } else {
                    JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        }
        }
        else
        {
            HashContext hash = Main.getContext().get(cbVehicles.getSelectedItem().toString());
             if (valueTypeSelected.equals(STRING_STRING)) {
            if (keyName != null && value != null) {
                if (!keyName.equals("") && !value.equals("")) {
                    if (!hash.ContainsKey(keyName)) {
                        hash.setObjectAsString(keyName, value);
                        UpdateTree();
                    } else {
                        JOptionPane.showConfirmDialog(this, "Duplicate Key",
                                "Error", JOptionPane.DEFAULT_OPTION);
                    }
                } else {
                    JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        } else {


            if (keyName != null && value != null) {
                if (!keyName.equals("") && !value.equals("")) {
                    if (!hash.ContainsKey(keyName)) {
                        if (scalarVectorSelected.equals(SCALAR_STRING)) {
                            try {
                                hash.setObjectAsDouble(keyName, Double.parseDouble(value));
                                UpdateTree();
                            } catch (Exception ex) {
                                JOptionPane.showConfirmDialog(this, "Value has to be Double",
                                        "Error", JOptionPane.DEFAULT_OPTION);
                            }
                        } else {
                            try {
                                hash.setObjectAsDoubleVectorwithException(keyName, value);
                                UpdateTree();
                            } catch (Exception ex) {
                                JOptionPane.showConfirmDialog(this, "Value has to be Double Vector delimited by \",\"",
                                        "Error", JOptionPane.DEFAULT_OPTION);
                            }
                        }
                    } else {
                        JOptionPane.showConfirmDialog(this, "Duplicate Key",
                                "Error", JOptionPane.DEFAULT_OPTION);
                    }
                } else {
                    JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "key/value pair cannot be null",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        }


        }

        txtKey.setText("");
        txtValue.setText("");
        txtKey.requestFocus();
}                                      

    private void cboScalarVectorActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        if (cboScalarVector.getSelectedItem() != null) {
            scalarVectorSelected = cboScalarVector.getSelectedItem().toString();
            debugPrinter.printLine("selected Index = " + scalarVectorSelected);
        }
}                                               

    private void cboValueTypeActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        if (cboValueType.getSelectedItem() != null) {
            valueTypeSelected = cboValueType.getSelectedItem().toString();
            if (valueTypeSelected.equals(STRING_STRING)) {
                cboScalarVector.removeAllItems();
                cboScalarVector.addItem(SCALAR_STRING);
                scalarVectorSelected = SCALAR_STRING;
            } else {
                cboScalarVector.removeAllItems();
                cboScalarVector.addItem(SCALAR_STRING);
                cboScalarVector.addItem(VECTOR_STRING);
                scalarVectorSelected = SCALAR_STRING;
            }
            debugPrinter.printLine("selected Index = " + valueTypeSelected);
        }
}                                            

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String removeKey = txtRemoveKey.getText().trim();

        //Check if we are deleting from ME or CONTEXT
        System.out.println(cbVehicles.getSelectedItem().toString());

        if(cbVehicles.getSelectedItem().toString().equals(Main.meString))
        {
        //We are removing from me


        if (removeKey != null) {
            if (!removeKey.equals("")) {
                if (!removeKey.equals(Constants.HASH_ID)) {
                    Main.getMe().removeObject(removeKey);
                    Enumeration<String> enKeys = Main.getContext().keys();
                    while (enKeys.hasMoreElements()) {
                        String key = enKeys.nextElement();
                        Main.getContext().get(key).removeObject(removeKey);
                    }
                    UpdateTree();
                }else{//CAN'T REMOVE ID
                    JOptionPane.showConfirmDialog(this, "Cannot remove NAME",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "No key to remove",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        } else {
            JOptionPane.showConfirmDialog(this, "No key to remove",
                    "Error", JOptionPane.DEFAULT_OPTION);
        }
        }
        else
        {
        //We are removing from CONTEXT
            HashContext hash = Main.getContext().get(cbVehicles.getSelectedItem().toString());

            if (removeKey != null) {
            if (!removeKey.equals("")) {
                if (!removeKey.equals(Constants.HASH_ID)) {

                    hash.removeObject(removeKey);
                    
                    UpdateTree();
                }else{//CAN'T REMOVE ID
                    JOptionPane.showConfirmDialog(this, "Cannot remove NAME",
                            "Error", JOptionPane.DEFAULT_OPTION);
                }
            } else {
                JOptionPane.showConfirmDialog(this, "No key to remove",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        } else {
            JOptionPane.showConfirmDialog(this, "No key to remove",
                    "Error", JOptionPane.DEFAULT_OPTION);
        }


        }

        txtRemoveKey.setText("");
        txtRemoveKey.requestFocus();

}                                         

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        // TODO add your handling code here:
        Main.treeView = null;
    }                                  

    private void btnAdd1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int index = jComboBox1.getSelectedIndex();

        if(jComboBox1.getSelectedItem().equals(Main.meString))
        {
            JOptionPane.showConfirmDialog(this, "Cannot remove ME",
                            "Error", JOptionPane.DEFAULT_OPTION);
        }
        else
        {
            //Find out if vehicle was "DYNAMIC SIGN"
              Enumeration<RoadSignEntry> en = Context.dynamicRoadSignVector.elements();
                    while(en.hasMoreElements())
                    {
                        RoadSignEntry tmpRoadSign = en.nextElement();
                        if(tmpRoadSign.getDynamicid().equals(jComboBox1.getSelectedItem().toString()))
                            Context.dynamicRoadSignVector.remove(tmpRoadSign);
                    }


            Main.getContext().remove(jComboBox1.getSelectedItem().toString());
            UpdateTree();
        }

        jComboBox1.removeItemAt(index);
        cbVehicles.removeItemAt(index);


    }                                       

    private void btnAdd2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:

       

        String text = txtAddVehicle.getText().trim();

        if( text != null)
        {
            if(!text.equals(""))
            {

                if(!text.equals(Main.meString))
                {
                    //put this new hashtable into the context
                    HashContext tmpHash = new HashContext();
                    tmpHash.setObjectAsDouble(Constants.HASH_TYPE, Constants.TYPE_VEHICLE);
                    tmpHash.setObjectAsString(Constants.HASH_LATITUDE, "45.409275");
                    tmpHash.setObjectAsString(Constants.HASH_LONGITUDE, "-86.829253");
                    tmpHash.setObjectAsString(Constants.HASH_BEARING, "N");

                    Main.getContext().put(text, tmpHash);
                    UpdateTree();
                }
                else
                {
                    JOptionPane.showConfirmDialog(this, "duplicate vehicle id",
                        "Error", JOptionPane.DEFAULT_OPTION);
                }

            }
            else
            {
                 JOptionPane.showConfirmDialog(this, "vehicle id cannot be empty",
                        "Error", JOptionPane.DEFAULT_OPTION);
            }
        }
        else{

            JOptionPane.showConfirmDialog(this, "vehicle id cannot be null",
                        "Error", JOptionPane.DEFAULT_OPTION);

        }

        txtAddVehicle.setText("");
        txtAddVehicle.requestFocus();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TreeView2 tree = new TreeView2();
                Main.treeView = tree;
                tree.setVisible(true);
            }
        });
    }

    public void UpdateTree() {
        createNodes();
    }

    private void checkComboBox(String key)
    {
        boolean inBox = false;

        for(int i=0; i< jComboBox1.getItemCount(); i++)
        {
            if(key.equals(jComboBox1.getItemAt(i).toString())){
                inBox = true;
                break;
            }
        }

        if(inBox == false)
        {
            jComboBox1.addItem(key);
            cbVehicles.addItem(key);
        }

        return;

    }

    private void createNodes() {
        String txtString = "";
        Enumeration<String> enKeys = Main.getContext().keys();
        while (enKeys.hasMoreElements()) {
            String key = enKeys.nextElement();


            checkComboBox(key);

            HashContext vehicle = Main.getContext().get(key);
            txtString = txtString + key + "\n";
            Enumeration<String> enValues = vehicle.keys();
            while (enValues.hasMoreElements()) {
                String keyValue = enValues.nextElement();
                double value = vehicle.GetObjectAsDouble(keyValue);
                if (value != -1) {
                    txtString = txtString + "  => " + keyValue + " : " + value + "\n";
                } else {
                    HashEntryDoubleVector valueVector = vehicle.GetObjectAsDoubleVector(keyValue);
                    if (valueVector != null) {
                        txtString = txtString + "  => " + keyValue + " : " + valueVector.toString() + "\n";
                    } else {
                        String valueString = vehicle.GetObjectAsString(keyValue);
                        txtString = txtString + "  => " + keyValue + " : " + valueString + "\n";
                    }
                }
            }
            txtString = txtString + "\n";
        }
        enKeys = Main.getMe().keys();
        txtString = txtString + Main.meString + "\n";
        checkComboBox(Main.meString);

        while (enKeys.hasMoreElements()) {
            String key = enKeys.nextElement();
            //System.out.println("key  = "+key);
            double value = Main.getMe().GetObjectAsDouble(key);
            if (value != -1) {
                txtString = txtString + "  => " + key + " : " + value + "\n";
            } else {
                HashEntryDoubleVector valueVector = Main.getMe().GetObjectAsDoubleVector(key);
                if (valueVector != null) {
                    txtString = txtString + "  => " + key + " : " + valueVector.toString() + "\n";
                } else {
                    String valueString = Main.getMe().GetObjectAsString(key);
                    txtString = txtString + "  => " + key + " : " + valueString + "\n";
                    //System.out.println("******************************************************************");
                    //System.out.println(txtString);
                }
            }
        }
        txtAreaContext.setText(txtString);
    }

    private void InitializeComboBoxes() {
        cboScalarVector.removeAllItems();
        cboScalarVector.addItem(SCALAR_STRING);
        cboScalarVector.addItem(VECTOR_STRING);
        scalarVectorSelected = SCALAR_STRING;

        cboValueType.removeAllItems();
        cboValueType.addItem(STRING_STRING);
        cboValueType.addItem(DOUBLE_STRING);
        valueTypeSelected = STRING_STRING;

        txtKey.setText("");
        txtValue.setText("");
        txtRemoveKey.setText("");
    }

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnAdd1;
    private javax.swing.JButton btnAdd2;
    private javax.swing.JButton btnRemove;
    private javax.swing.JComboBox cbVehicles;
    private javax.swing.JComboBox cboScalarVector;
    private javax.swing.JComboBox cboValueType;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtAddVehicle;
    private javax.swing.JTextArea txtAreaContext;
    private javax.swing.JTextField txtKey;
    private javax.swing.JTextField txtRemoveKey;
    private javax.swing.JTextField txtValue;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the dataFromWindow
     */
    public HashContext getDataFromWindow() {
        return dataFromWindow;
    }

    /**
     * @param dataFromWindow the dataFromWindow to set
     */
    public static void setDataFromWindow(HashContext tmpdataFromWindow) {
        dataFromWindow = tmpdataFromWindow;
    }

}
