/*
 *  Internal frame for agent elements inspection
 *
 *  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 jTMass.TMass;
import jTMass.core.jALLL.jTObjects.TConstants;
import jTMass.core.jAMass.TAgent;
import jTMass.core.jAMass.TAgentEvent;
import java.awt.Color;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.table.TableColumn;


public class TAgentInternalFrame extends javax.swing.JInternalFrame {

    TAgent PAgent;
    TMass PTMass;
    TConnectionsPanel PConnectionsPanel;

    void setAgentState(){
        TIntentionText.setText(PAgent.getPlan());
        TPlansText.setText(PAgent.getBeliefBase().getPlanS());
        int state=PAgent.getAgentState();
        Color color=PAgent.getStateColor();
        setBorder(BorderFactory.createLineBorder(color));
        setBackground(color);

        String beliefs="";
        for(int i=1;i<=TConstants._NORegisters;i++){
            if(i-1==PAgent.getActiveRegIndex())
                beliefs=beliefs.concat("*");
            beliefs=beliefs.concat("Reg"+i+": "+PAgent.getRegister(i).getContent()+"\n");
        }

        beliefs=beliefs.concat(PAgent.getBeliefBase().getBeliefs());
        beliefs=beliefs.concat(PAgent.getKBCU().getDFSS());
        TBeliefsText.setText(beliefs);
        TInputText.setText(PAgent.getInputBuffer().getContent());
    }

    public void updateConnections(){
        PConnectionsPanel.setConnections();
    }
    
    public void updateAgentState(){
        setAgentState();
    }

    public void refreshHistory(){
        //if (!(TMass.PRunning)){
            Vector columnNames=new Vector();
            columnNames.add("Activity");
            columnNames.add("ExecTime");
            columnNames.add("Params");
            columnNames.add("Result");
            LinkedList<TAgentEvent> activityList=PAgent.getActivityHistory();
            TMassTableModel activityModel=new TMassTableModel(columnNames,activityList.size());
            int i=0;

            for(TAgentEvent activity:activityList){
                activityModel.setValueAt(activity.getActivityName(),i,0);
                activityModel.setValueAt(activity.getExecTime(),i,1);
                activityModel.setValueAt(activity.getParams(),i,2);
                activityModel.setValueAt(activity.getResult(),i,3);
                i++;
            }
            TActivityHistoryTable.setModel(activityModel);
            TableColumn col = TActivityHistoryTable.getColumnModel().getColumn(0);
            col.setPreferredWidth(125);
            col.setMinWidth(100);
            col.setMaxWidth(320);
            col = TActivityHistoryTable.getColumnModel().getColumn(1);
            col.setPreferredWidth(55);
            col.setMinWidth(30);
            col.setMaxWidth(120);
      //  }
    }

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

    public TAgentInternalFrame(TAgent agent, TMass tmass) {
        initComponents();
        this.setTitle(agent.getElementName());
        PAgent=agent;
        PTMass=tmass;
        setAgentState();
        refreshHistory();
        PConnectionsPanel=new TConnectionsPanel(agent,tmass);
        TAgentTable.add(PConnectionsPanel,1);
        TAgentTable.setTitleAt(1, "Connections");

    }

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

        popupMenu1 = new java.awt.PopupMenu();
        menuItem1 = new java.awt.MenuItem();
        menuItem2 = new java.awt.MenuItem();
        TAgentTable = new javax.swing.JTabbedPane();
        TAgentPanel = new javax.swing.JPanel();
        jGeneralSplit = new javax.swing.JSplitPane();
        jBeliefsPlansSplit = new javax.swing.JSplitPane();
        jBeliefsPane = new javax.swing.JScrollPane();
        TBeliefsText = new javax.swing.JTextPane();
        jPlansPane = new javax.swing.JScrollPane();
        TInputText = new javax.swing.JTextPane();
        jPlanInputSplit = new javax.swing.JSplitPane();
        jPlanPane = new javax.swing.JScrollPane();
        TIntentionText = new javax.swing.JTextPane();
        jInputPane = new javax.swing.JScrollPane();
        TPlansText = new javax.swing.JTextPane();
        TAgentHistoryPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TActivityHistoryTable = new javax.swing.JTable();

        popupMenu1.setLabel("popupMenu1");
        popupMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popupMenu1ActionPerformed(evt);
            }
        });

        menuItem1.setLabel("menuItem1");
        popupMenu1.add(menuItem1);

        menuItem2.setLabel("menuItem2");
        popupMenu1.add(menuItem2);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setPreferredSize(new java.awt.Dimension(450, 340));

        TAgentTable.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        TAgentTable.setFocusCycleRoot(true);

        jGeneralSplit.setDividerLocation(110);
        jGeneralSplit.setDividerSize(2);
        jGeneralSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jGeneralSplit.setResizeWeight(0.5);
        jGeneralSplit.setAlignmentY(0.5F);

        jBeliefsPlansSplit.setDividerLocation(220);
        jBeliefsPlansSplit.setDividerSize(2);
        jBeliefsPlansSplit.setResizeWeight(0.5);
        jBeliefsPlansSplit.setAlignmentY(0.5F);

        TBeliefsText.setEditable(false);
        TBeliefsText.setFont(new java.awt.Font("Tahoma", 0, 10));
        jBeliefsPane.setViewportView(TBeliefsText);

        jBeliefsPlansSplit.setLeftComponent(jBeliefsPane);

        TInputText.setEditable(false);
        TInputText.setFont(new java.awt.Font("Tahoma", 0, 10));
        jPlansPane.setViewportView(TInputText);

        jBeliefsPlansSplit.setRightComponent(jPlansPane);

        jGeneralSplit.setRightComponent(jBeliefsPlansSplit);

        jPlanInputSplit.setDividerLocation(36);
        jPlanInputSplit.setDividerSize(2);
        jPlanInputSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jPlanInputSplit.setResizeWeight(0.2);
        jPlanInputSplit.setAlignmentY(0.5F);

        TIntentionText.setEditable(false);
        TIntentionText.setFont(new java.awt.Font("Tahoma", 0, 10));
        jPlanPane.setViewportView(TIntentionText);

        jPlanInputSplit.setTopComponent(jPlanPane);

        TPlansText.setEditable(false);
        TPlansText.setFont(new java.awt.Font("Tahoma", 0, 10));
        jInputPane.setViewportView(TPlansText);

        jPlanInputSplit.setRightComponent(jInputPane);

        jGeneralSplit.setLeftComponent(jPlanInputSplit);

        javax.swing.GroupLayout TAgentPanelLayout = new javax.swing.GroupLayout(TAgentPanel);
        TAgentPanel.setLayout(TAgentPanelLayout);
        TAgentPanelLayout.setHorizontalGroup(
            TAgentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jGeneralSplit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
        );
        TAgentPanelLayout.setVerticalGroup(
            TAgentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jGeneralSplit, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
        );

        TAgentTable.addTab("State", TAgentPanel);

        TActivityHistoryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Activity", "ExecTime", "Content", "Result"
            }
        ));
        jScrollPane1.setViewportView(TActivityHistoryTable);
        TActivityHistoryTable.getColumnModel().getColumn(0).setPreferredWidth(200);
        TActivityHistoryTable.getColumnModel().getColumn(1).setMinWidth(50);
        TActivityHistoryTable.getColumnModel().getColumn(1).setPreferredWidth(80);
        TActivityHistoryTable.getColumnModel().getColumn(1).setMaxWidth(120);
        TActivityHistoryTable.getColumnModel().getColumn(2).setPreferredWidth(220);
        TActivityHistoryTable.getColumnModel().getColumn(3).setPreferredWidth(220);

        javax.swing.GroupLayout TAgentHistoryPanelLayout = new javax.swing.GroupLayout(TAgentHistoryPanel);
        TAgentHistoryPanel.setLayout(TAgentHistoryPanelLayout);
        TAgentHistoryPanelLayout.setHorizontalGroup(
            TAgentHistoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
        );
        TAgentHistoryPanelLayout.setVerticalGroup(
            TAgentHistoryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
        );

        TAgentTable.addTab("History", TAgentHistoryPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TAgentTable, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(TAgentTable, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
        );

        TAgentTable.getAccessibleContext().setAccessibleName("AgentState");

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

    private void popupMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popupMenu1ActionPerformed
 
    }//GEN-LAST:event_popupMenu1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TActivityHistoryTable;
    private javax.swing.JPanel TAgentHistoryPanel;
    private javax.swing.JPanel TAgentPanel;
    private javax.swing.JTabbedPane TAgentTable;
    private javax.swing.JTextPane TBeliefsText;
    private javax.swing.JTextPane TInputText;
    private javax.swing.JTextPane TIntentionText;
    private javax.swing.JTextPane TPlansText;
    private javax.swing.JScrollPane jBeliefsPane;
    private javax.swing.JSplitPane jBeliefsPlansSplit;
    private javax.swing.JSplitPane jGeneralSplit;
    private javax.swing.JScrollPane jInputPane;
    private javax.swing.JSplitPane jPlanInputSplit;
    private javax.swing.JScrollPane jPlanPane;
    private javax.swing.JScrollPane jPlansPane;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.MenuItem menuItem1;
    private java.awt.MenuItem menuItem2;
    private java.awt.PopupMenu popupMenu1;
    // End of variables declaration//GEN-END:variables

}
