/*
 *  Application main frame
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTGUI;

    import java.util.Hashtable;
    import java.io.File;
    import jTMass.TMass;
    import jTMass.core.jTMassModel.TModel;
    import jTMass.core.jTMassModel.TModelElement;
    import javax.swing.JInternalFrame;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;


public class TBackgroundFrame extends javax.swing.JFrame {

    public static int _SM_Time=1;
    public static int _SM_Steps=2;

    Hashtable<String,DefaultMutableTreeNode> PTypeTable=new Hashtable();
    TMass PTMass;
    int PFrameCount=0;
    DefaultMutableTreeNode PRootNode;
    DefaultTreeModel PTreeModel;
    Hashtable<TModelElement,JInternalFrame>
            PElementFrameTable=new Hashtable<TModelElement,JInternalFrame>();
    TModelElement PActualElement=null;

    // ===== TML FILE FILTER =====

    class TTMLFilter extends javax.swing.filechooser.FileFilter{

        @Override
        public String getDescription(){
            return("T-Mass Modelling Language files");
        }

        @Override
        public boolean accept(File f){
            if (f.isDirectory())
                return true;
            else
                if(f.isFile())
                    if (f.getName().endsWith(".tml"))
                        return true;
            return false;
        }

        public TTMLFilter(){
            super();
        }
    }

    // ========== TEXT AREA ==========

    public void printToTextArea(String message){
        PMainTextArea.append(message);
    }

   public void printlnToTextArea(String message){
        PMainTextArea.append(message+"\n");
   }

   // ========== FRAMES ==========

   public void simulationSarted(){
       TSimulateButton.setEnabled(false);
       TPauseButton.setEnabled(true);
   }

   public void simulationPaused(){
       TSimulateButton.setEnabled(true);
       TPauseButton.setEnabled(false);
   }


   public void enableComponents(){
        TModelMenu.setEnabled(true);
        TSimulationMenu.setEnabled(true);
        TShowElementButton.setEnabled(true);
        TSimulationSteps.setEnabled(true);
        TSimulateButton.setEnabled(true);
        TResetButton.setEnabled(true);
   }

   void showElementFrame(TModelElement element){
        if (!PElementFrameTable.containsKey(element)){
            JInternalFrame newFrame;
            newFrame=element.createElementFrame(PTMass);
            PElementFrameTable.put(element, (JInternalFrame)newFrame);
            newFrame.setBounds(10*(PFrameCount%10),10*(PFrameCount%10), 450, 300);
            PFrameCount++;
            newFrame.setDefaultCloseOperation(HIDE_ON_CLOSE);
            PDesktopPanel.add(newFrame);
            newFrame.show();
       }
       else{
            JInternalFrame internalFrame=PElementFrameTable.get(element);
            try{
              internalFrame.setVisible(true);
              internalFrame.setSelected(true);
            }catch(Exception e){
            TMass._Terminal.reportError("Frame setting visibility "+internalFrame);
         }
       }
   }

    void showElementFrame(){
        if(PActualElement!=null)
            showElementFrame(PActualElement);
    }

    public void eraseFrames(){
        for(JInternalFrame frame:PDesktopPanel.getAllFrames())
            frame.dispose();
        PElementFrameTable.clear();
    }

    // ===== PREFERENCES =====

    void initPreferencesDialog(){
        TFTerminate.setText(PTMass.getDeadline().toString());
    }

    void storePreferences(){
        Long deadline;
        try{
            deadline=Long.parseLong(TFTerminate.getText());
            PTMass.setDeadline(deadline);
        }
        catch(Exception e){
            TMass._Terminal.reportError("Wrong deadline format");
        }
    }

    // ===== READER =====

    public int getSimMode(){
        if(RBSteps.isSelected())
            return(_SM_Steps);
                    else
                        return(_SM_Time);
    }

    // ===== System loaders =====

    public void reload(){
        eraseFrames();
        simulationPaused();
        PTMass.reloadModel();
    }

    public void loadUniverse(){
        PTypeTable.clear();
        PRootNode.removeAllChildren();
        PTreeModel=new DefaultTreeModel(PRootNode);
        String eType,eName;
	TModel model=TMass.getModel();
	TModelElement element;
	model.resetElementList();
	element=model.getElement();
	DefaultMutableTreeNode type,node;
	while(element!=null){
             eName=element.getElementName();
             eType=element.getTypeName();
             node=new DefaultMutableTreeNode(eName);
             type=PTypeTable.get(eType);
             if(type==null){
                type=new DefaultMutableTreeNode(eType);
		PTypeTable.put(eType, type);
		PRootNode.add(type);
            }
            type.add(node);
            element=model.getElement();
        }

        PUniverseTree.setModel(PTreeModel);
    };

    // ===== CONSTRUCTOR =====

    public TBackgroundFrame(TMass tMass) {
        PTMass=tMass;
        PRootNode=new DefaultMutableTreeNode("Universe");
        PTreeModel=new DefaultTreeModel(PRootNode);
        initComponents();
        PUniverseTree.setModel(PTreeModel);
    }

    /** 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() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        AboutBox = new javax.swing.JDialog();
        ABPanel = new javax.swing.JPanel();
        TTMassLabel = new javax.swing.JLabel();
        TNameLabel = new javax.swing.JLabel();
        TToolNameLabel = new javax.swing.JLabel();
        TUniversityLabel = new javax.swing.JLabel();
        TFacultyLabel = new javax.swing.JLabel();
        TDepartmentLabel = new javax.swing.JLabel();
        TWebLabel = new javax.swing.JLabel();
        TEmailLabel = new javax.swing.JLabel();
        PFileChooser = new javax.swing.JFileChooser(new File("/*.tml"));
        TUniversePopup = new javax.swing.JPopupMenu();
        TUPShowElement = new javax.swing.JMenuItem();
        SimModeBG = new javax.swing.ButtonGroup();
        TPreferencesDialog = new javax.swing.JDialog();
        TPreferencesPanel = new javax.swing.JPanel();
        LSTermination = new javax.swing.JLabel();
        TFTerminate = new javax.swing.JTextField();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        PMainPanel = new javax.swing.JSplitPane();
        PMainScroll = new javax.swing.JScrollPane();
        PUniverseTree = new javax.swing.JTree();
        PTextSplit = new javax.swing.JSplitPane();
        PMainTextScroll = new javax.swing.JScrollPane();
        PMainTextArea = new javax.swing.JTextArea();
        PDesktopPanel = new javax.swing.JDesktopPane();
        jToolBar = new javax.swing.JToolBar();
        TShowElementButton = new javax.swing.JButton();
        TSimulationSteps = new javax.swing.JComboBox();
        RBSteps = new javax.swing.JRadioButton();
        RBTime = new javax.swing.JRadioButton();
        TSimulateButton = new javax.swing.JButton();
        TResetButton = new javax.swing.JButton();
        TPauseButton = new javax.swing.JButton();
        TMainMenuBar = new javax.swing.JMenuBar();
        TFileMenu = new javax.swing.JMenu();
        MILoadModel = new javax.swing.JMenuItem();
        MIQuit = new javax.swing.JMenuItem();
        TModelMenu = new javax.swing.JMenu();
        MIReloadModel = new javax.swing.JMenuItem();
        TSimulationMenu = new javax.swing.JMenu();
        MIRunSimulation = new javax.swing.JMenuItem();
        MIExecuteStep = new javax.swing.JMenuItem();
        MISimulationPreference = new javax.swing.JMenuItem();
        THelpMenu = new javax.swing.JMenu();
        MIAboutHelp = new javax.swing.JMenuItem();

        AboutBox.setTitle("About T-Mass(X)");
        AboutBox.setAlwaysOnTop(true);
        AboutBox.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.darkShadow"));
        AboutBox.setMinimumSize(new java.awt.Dimension(420, 300));
        AboutBox.setName("PAboutBox"); // NOI18N
        AboutBox.setResizable(false);

        TTMassLabel.setFont(new java.awt.Font("Tw Cen MT Condensed", 0, 36));
        TTMassLabel.setText("T-Mass(X)");
        TTMassLabel.setAutoscrolls(true);
        TTMassLabel.setBorder(new javax.swing.border.MatteBorder(null));

        TNameLabel.setFont(new java.awt.Font("Niagara Solid", 0, 18));
        TNameLabel.setText("(C) 2004-2011 Frantisek Zboril jr.");

        TToolNameLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        TToolNameLabel.setText("A Tool For Multiagent System Simulation");

        TUniversityLabel.setFont(new java.awt.Font("Niagara Solid", 0, 18));
        TUniversityLabel.setText("Brno University of Technology");

        TFacultyLabel.setFont(new java.awt.Font("Niagara Solid", 0, 18));
        TFacultyLabel.setText("Faculty of Information Technologies");

        TDepartmentLabel.setFont(new java.awt.Font("Niagara Solid", 0, 18));
        TDepartmentLabel.setText("Department of Intelligent Systems");

        TWebLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        TWebLabel.setText("http://www.fit.vutbr.cz/~zborilf/TMass");

        TEmailLabel.setText("email: zborilf@fit.vutbr.cz");

        javax.swing.GroupLayout ABPanelLayout = new javax.swing.GroupLayout(ABPanel);
        ABPanel.setLayout(ABPanelLayout);
        ABPanelLayout.setHorizontalGroup(
            ABPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ABPanelLayout.createSequentialGroup()
                .addGroup(ABPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ABPanelLayout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addGroup(ABPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TUniversityLabel)
                            .addComponent(TNameLabel)
                            .addComponent(TFacultyLabel)
                            .addComponent(TDepartmentLabel)
                            .addComponent(TEmailLabel)))
                    .addGroup(ABPanelLayout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(TTMassLabel))
                    .addGroup(ABPanelLayout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addGroup(ABPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TToolNameLabel)
                            .addComponent(TWebLabel))))
                .addContainerGap(141, Short.MAX_VALUE))
        );
        ABPanelLayout.setVerticalGroup(
            ABPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ABPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TTMassLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(TToolNameLabel)
                .addGap(2, 2, 2)
                .addComponent(TWebLabel)
                .addGap(18, 18, 18)
                .addComponent(TNameLabel)
                .addGap(1, 1, 1)
                .addComponent(TUniversityLabel)
                .addGap(1, 1, 1)
                .addComponent(TFacultyLabel)
                .addGap(1, 1, 1)
                .addComponent(TDepartmentLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TEmailLabel)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout AboutBoxLayout = new javax.swing.GroupLayout(AboutBox.getContentPane());
        AboutBox.getContentPane().setLayout(AboutBoxLayout);
        AboutBoxLayout.setHorizontalGroup(
            AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ABPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        AboutBoxLayout.setVerticalGroup(
            AboutBoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ABPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        PFileChooser.setFileFilter(new TTMLFilter());
        PFileChooser.setEnabled(false);

        TUniversePopup.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                onPopup(evt);
            }
        });

        TUPShowElement.setText("Show element");
        TUPShowElement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TUPShowElementActionPerformed(evt);
            }
        });
        TUniversePopup.add(TUPShowElement);

        TPreferencesDialog.setTitle("TMass-X Settings");
        TPreferencesDialog.setAlwaysOnTop(true);
        TPreferencesDialog.setMinimumSize(new java.awt.Dimension(320, 200));

        LSTermination.setText("Simulation Termination");

        TFTerminate.setText("0");

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");

        javax.swing.GroupLayout TPreferencesPanelLayout = new javax.swing.GroupLayout(TPreferencesPanel);
        TPreferencesPanel.setLayout(TPreferencesPanelLayout);
        TPreferencesPanelLayout.setHorizontalGroup(
            TPreferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TPreferencesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TPreferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(TPreferencesPanelLayout.createSequentialGroup()
                        .addComponent(OKButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CancelButton))
                    .addGroup(TPreferencesPanelLayout.createSequentialGroup()
                        .addComponent(LSTermination)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TFTerminate, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(181, Short.MAX_VALUE))
        );
        TPreferencesPanelLayout.setVerticalGroup(
            TPreferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TPreferencesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TPreferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TFTerminate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LSTermination))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 211, Short.MAX_VALUE)
                .addGroup(TPreferencesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton)))
        );

        javax.swing.GroupLayout TPreferencesDialogLayout = new javax.swing.GroupLayout(TPreferencesDialog.getContentPane());
        TPreferencesDialog.getContentPane().setLayout(TPreferencesDialogLayout);
        TPreferencesDialogLayout.setHorizontalGroup(
            TPreferencesDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TPreferencesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        TPreferencesDialogLayout.setVerticalGroup(
            TPreferencesDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TPreferencesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("T-Mass(X)");
        setMinimumSize(new java.awt.Dimension(400, 300));
        setName("PTMassFrame"); // NOI18N

        PMainPanel.setDividerLocation(185);
        PMainPanel.setDividerSize(3);

        PUniverseTree.setFont(new java.awt.Font("Courier New", 0, 11));
        PUniverseTree.setToolTipText("Shows universe element");
        PUniverseTree.setComponentPopupMenu(TUniversePopup);
        PUniverseTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PUniverseTreeMouseClicked(evt);
            }
        });
        PUniverseTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                PUniverseTreeValueChanged(evt);
            }
        });
        PMainScroll.setViewportView(PUniverseTree);

        PMainPanel.setLeftComponent(PMainScroll);

        PTextSplit.setDividerLocation(666);
        PTextSplit.setDividerSize(3);
        PTextSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        PMainTextScroll.setAutoscrolls(true);

        PMainTextArea.setBackground(new java.awt.Color(153, 255, 255));
        PMainTextArea.setColumns(20);
        PMainTextArea.setEditable(false);
        PMainTextArea.setFont(new java.awt.Font("Courier New", 0, 10));
        PMainTextArea.setRows(5);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, PMainTextScroll, org.jdesktop.beansbinding.ELProperty.create("${autoscrolls}"), PMainTextArea, org.jdesktop.beansbinding.BeanProperty.create("autoscrolls"));
        bindingGroup.addBinding(binding);

        PMainTextScroll.setViewportView(PMainTextArea);

        PTextSplit.setRightComponent(PMainTextScroll);

        PDesktopPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        PTextSplit.setLeftComponent(PDesktopPanel);

        PMainPanel.setRightComponent(PTextSplit);

        jToolBar.setRollover(true);
        jToolBar.setPreferredSize(new java.awt.Dimension(418, 25));

        TShowElementButton.setText("Show Element");
        TShowElementButton.setBorderPainted(false);
        TShowElementButton.setEnabled(false);
        TShowElementButton.setFocusable(false);
        TShowElementButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TShowElementButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TShowElementButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TShowElementButtonActionPerformed(evt);
            }
        });
        jToolBar.add(TShowElementButton);
        TShowElementButton.getAccessibleContext().setAccessibleName("TShowElementButton");

        TSimulationSteps.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Go!", "1", "2", "5", "10", "25", "50", "100", "500", "1000", "2000", "5000", "10000", "25000", "50000" }));
        TSimulationSteps.setEnabled(false);
        TSimulationSteps.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TSimulationStepsActionPerformed(evt);
            }
        });
        jToolBar.add(TSimulationSteps);

        SimModeBG.add(RBSteps);
        RBSteps.setText("Steps");
        RBSteps.setFocusable(false);
        RBSteps.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jToolBar.add(RBSteps);

        SimModeBG.add(RBTime);
        RBTime.setSelected(true);
        RBTime.setText("Time");
        RBTime.setFocusable(false);
        RBTime.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        RBTime.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        RBTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBTimeActionPerformed(evt);
            }
        });
        jToolBar.add(RBTime);

        TSimulateButton.setText("Simulate");
        TSimulateButton.setEnabled(false);
        TSimulateButton.setFocusable(false);
        TSimulateButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TSimulateButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TSimulateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TSimulateButtonActionPerformed(evt);
            }
        });
        jToolBar.add(TSimulateButton);

        TResetButton.setText("Reload");
        TResetButton.setEnabled(false);
        TResetButton.setFocusable(false);
        TResetButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TResetButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TResetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TResetButtonActionPerformed(evt);
            }
        });
        jToolBar.add(TResetButton);

        TPauseButton.setText("Pause        ");
        TPauseButton.setFocusable(false);
        TPauseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TPauseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TPauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TPauseButtonActionPerformed(evt);
            }
        });
        jToolBar.add(TPauseButton);

        TFileMenu.setText("File");

        MILoadModel.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        MILoadModel.setText("Load Model");
        MILoadModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MILoadModelActionPerformed(evt);
            }
        });
        TFileMenu.add(MILoadModel);

        MIQuit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        MIQuit.setText("Quit");
        MIQuit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MIQuitActionPerformed(evt);
            }
        });
        TFileMenu.add(MIQuit);

        TMainMenuBar.add(TFileMenu);

        TModelMenu.setText("Model");
        TModelMenu.setEnabled(false);

        MIReloadModel.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        MIReloadModel.setText("Reload Model");
        MIReloadModel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MIReloadModelActionPerformed(evt);
            }
        });
        TModelMenu.add(MIReloadModel);

        TMainMenuBar.add(TModelMenu);

        TSimulationMenu.setText("Simulation");
        TSimulationMenu.setEnabled(false);

        MIRunSimulation.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, 0));
        MIRunSimulation.setText("Run Simulation");
        MIRunSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MIRunSimulationActionPerformed(evt);
            }
        });
        TSimulationMenu.add(MIRunSimulation);

        MIExecuteStep.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, 0));
        MIExecuteStep.setText("Execute Step");
        MIExecuteStep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MIExecuteStepActionPerformed(evt);
            }
        });
        TSimulationMenu.add(MIExecuteStep);

        MISimulationPreference.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        MISimulationPreference.setText("Simulation Preferences");
        MISimulationPreference.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MISimulationPreferenceActionPerformed(evt);
            }
        });
        TSimulationMenu.add(MISimulationPreference);

        TMainMenuBar.add(TSimulationMenu);

        THelpMenu.setText("Help");

        MIAboutHelp.setText("About T-Mass(X)");
        MIAboutHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MIAboutHelpActionPerformed(evt);
            }
        });
        THelpMenu.add(MIAboutHelp);

        TMainMenuBar.add(THelpMenu);

        setJMenuBar(TMainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(PMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 1662, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1163, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 914, Short.MAX_VALUE))
        );

        bindingGroup.bind();

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

    private void MIQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MIQuitActionPerformed
        System.exit(1);
    }//GEN-LAST:event_MIQuitActionPerformed

    private void MIReloadModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MIReloadModelActionPerformed
        reload();
    }//GEN-LAST:event_MIReloadModelActionPerformed

    private void MIAboutHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MIAboutHelpActionPerformed
        AboutBox.setLocation(this.getX()+(this.getWidth()/2)-(AboutBox.getWidth()/2),
                this.getY()+(this.getHeight()/2)-(AboutBox.getHeight()/2));
        AboutBox.setVisible(true);
    }//GEN-LAST:event_MIAboutHelpActionPerformed

    private void PUniverseTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_PUniverseTreeValueChanged

		  DefaultMutableTreeNode elementNode=(DefaultMutableTreeNode)PUniverseTree.getLastSelectedPathComponent();
                  if(elementNode!=null)
                    if(elementNode.isLeaf()){
			  TModelElement element=
                            TMass.getModel().getElementByName(elementNode.toString());
                          PActualElement=element;			  
                 }else
                     PActualElement=null;
    }//GEN-LAST:event_PUniverseTreeValueChanged
    
                  private void MILoadModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MILoadModelActionPerformed
                    if(PFileChooser.showOpenDialog(this)==javax.swing.JFileChooser.APPROVE_OPTION){
                        PTMass.loadModel(PFileChooser.getSelectedFile().toString());
                        eraseFrames();
                        loadUniverse();
                    }
                  }//GEN-LAST:event_MILoadModelActionPerformed

                  private void TShowElementButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TShowElementButtonActionPerformed
                    showElementFrame();
                  }//GEN-LAST:event_TShowElementButtonActionPerformed

                  private void TSimulateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TSimulateButtonActionPerformed
                     PTMass.startSimulation(TSimulationSteps.getSelectedItem().toString());
                  }//GEN-LAST:event_TSimulateButtonActionPerformed

                  private void TSimulationStepsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TSimulationStepsActionPerformed

                  }//GEN-LAST:event_TSimulationStepsActionPerformed

                  private void PUniverseTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PUniverseTreeMouseClicked
                    if(evt.getClickCount()==2)
                            showElementFrame();
                  }//GEN-LAST:event_PUniverseTreeMouseClicked

                  private void TResetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TResetButtonActionPerformed
                     reload();
                  }//GEN-LAST:event_TResetButtonActionPerformed

                  private void onPopup(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_onPopup
                        if(PActualElement!=null)
                            this.TUPShowElement.setVisible(true);
                        else
                            this.TUPShowElement.setVisible(false);
                  }//GEN-LAST:event_onPopup

                  private void TUPShowElementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TUPShowElementActionPerformed
                        showElementFrame();
                  }//GEN-LAST:event_TUPShowElementActionPerformed

                  private void MIRunSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MIRunSimulationActionPerformed
                        PTMass.startSimulation("Go!");
                  }//GEN-LAST:event_MIRunSimulationActionPerformed

                  private void MIExecuteStepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MIExecuteStepActionPerformed
                        PTMass.startSimulation("1");
                  }//GEN-LAST:event_MIExecuteStepActionPerformed

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

                  private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
                      storePreferences();
                      TPreferencesDialog.hide();
                  }//GEN-LAST:event_OKButtonActionPerformed

                  private void MISimulationPreferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MISimulationPreferenceActionPerformed
                      initPreferencesDialog();
                      TPreferencesDialog.show();
                  }//GEN-LAST:event_MISimulationPreferenceActionPerformed

                  private void TPauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TPauseButtonActionPerformed
                      PTMass.pauseSimulation();
                  }//GEN-LAST:event_TPauseButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ABPanel;
    private javax.swing.JDialog AboutBox;
    private javax.swing.JButton CancelButton;
    private javax.swing.JLabel LSTermination;
    private javax.swing.JMenuItem MIAboutHelp;
    private javax.swing.JMenuItem MIExecuteStep;
    private javax.swing.JMenuItem MILoadModel;
    private javax.swing.JMenuItem MIQuit;
    private javax.swing.JMenuItem MIReloadModel;
    private javax.swing.JMenuItem MIRunSimulation;
    private javax.swing.JMenuItem MISimulationPreference;
    private javax.swing.JButton OKButton;
    private javax.swing.JDesktopPane PDesktopPanel;
    private javax.swing.JFileChooser PFileChooser;
    private javax.swing.JSplitPane PMainPanel;
    private javax.swing.JScrollPane PMainScroll;
    private javax.swing.JTextArea PMainTextArea;
    private javax.swing.JScrollPane PMainTextScroll;
    private javax.swing.JSplitPane PTextSplit;
    private javax.swing.JTree PUniverseTree;
    private javax.swing.JRadioButton RBSteps;
    private javax.swing.JRadioButton RBTime;
    private javax.swing.ButtonGroup SimModeBG;
    private javax.swing.JLabel TDepartmentLabel;
    private javax.swing.JLabel TEmailLabel;
    private javax.swing.JTextField TFTerminate;
    private javax.swing.JLabel TFacultyLabel;
    private javax.swing.JMenu TFileMenu;
    private javax.swing.JMenu THelpMenu;
    private javax.swing.JMenuBar TMainMenuBar;
    private javax.swing.JMenu TModelMenu;
    private javax.swing.JLabel TNameLabel;
    private javax.swing.JButton TPauseButton;
    private javax.swing.JDialog TPreferencesDialog;
    private javax.swing.JPanel TPreferencesPanel;
    private javax.swing.JButton TResetButton;
    private javax.swing.JButton TShowElementButton;
    private javax.swing.JButton TSimulateButton;
    private javax.swing.JMenu TSimulationMenu;
    private javax.swing.JComboBox TSimulationSteps;
    private javax.swing.JLabel TTMassLabel;
    private javax.swing.JLabel TToolNameLabel;
    private javax.swing.JMenuItem TUPShowElement;
    private javax.swing.JPopupMenu TUniversePopup;
    private javax.swing.JLabel TUniversityLabel;
    private javax.swing.JLabel TWebLabel;
    private javax.swing.JToolBar jToolBar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
