/*
 *  Internal frame for platform 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.TObject;
import jTMass.core.jAMass.TAgent;
import jTMass.core.jLoaders.TModelLoader;
import jTMass.core.jTMassEnvironment.TEnvironment;
import jTMass.core.jTMassModel.TModelConnection;
import jTMass.core.jTMassPlatform.TPlatform;
import jTMass.core.jTMassPlatform.jTServices.TASAsynchronousService;
import jTMass.core.jTMassPlatform.jTServices.TAService;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.table.TableColumn;

public class TPlatformsInternalFrame extends javax.swing.JInternalFrame {

    TPlatform PPlatform;
    TMass PTMass;
    TConnectionsPanel PConnectionsPanel;

    public void setSlotsTable(){
        Vector columnNames=new Vector();
        columnNames.add("Slot");
        columnNames.add("Element");
        columnNames.add("CNT");
        TMassTableModel slotsData=new TMassTableModel(columnNames,PPlatform.getnoSlots()+PPlatform.getnoEnvironments());

        TModelConnection con;
        int s=1;
        for(int i=2;
                i<PPlatform.getnoEnvironments()+2;i++){
            con=PPlatform.getOutputConnection(i);
            slotsData.setValueAt("ESLOT "+s++, i-2, 0);
            if(con.getInputElement()!=null)
            if(con.getInputElement() instanceof TEnvironment){
                slotsData.setValueAt(con.getInputElement().getElementName(), i-2, 1);
            slotsData.setValueAt(con.getConnectionTime(),i-2,2);
          }
        }

        s=1;
        for(int i=2+PPlatform.getnoEnvironments();
            i<2+PPlatform.getnoEnvironments()+PPlatform.getnoSlots();i++){
            con=PPlatform.getOutputConnection(i);
            slotsData.setValueAt("ASLOT "+s++, i-2, 0);
            if(con.getInputElement()!=null)
            if(con.getInputElement() instanceof TAgent){
                slotsData.setValueAt(con.getInputElement().getElementName(), i-2, 1);
                slotsData.setValueAt(con.getConnectionTime(),i-2,2);
            }
        }

        TPSlotsTable.setModel(slotsData);
        TableColumn col = TPSlotsTable.getColumnModel().getColumn(2);
        col.setPreferredWidth(55);
        col.setMinWidth(30);
        col.setMaxWidth(120);
    }

    public void setActiveServicesTable(){
        Vector columnNames=new Vector();
        columnNames.add("Service");
        columnNames.add("Executed");
        columnNames.add("Client");
        TMassTableModel servicesModel=new TMassTableModel(columnNames,PPlatform.getPCU().getnoActiveServices());
        int i=0;
        LinkedList<TASAsynchronousService> services=PPlatform.getPCU().getActiveServices();
        for(TASAsynchronousService service:services){
             servicesModel.setValueAt(service.getServiceName(),i,0);
             servicesModel.setValueAt(service.getInitTime(),i,1);
             servicesModel.setValueAt(service.getClientName(),i,2);
             i++;
         }

         TPRunningServicesTable.setModel(servicesModel);
         TableColumn col = TPRunningServicesTable.getColumnModel().getColumn(0);
         col.setPreferredWidth(100);
         col.setMinWidth(30);
         col.setMaxWidth(200);
         col = TPRunningServicesTable.getColumnModel().getColumn(1);
         col.setPreferredWidth(55);
         col.setMinWidth(30);
         col.setMaxWidth(120);
    }

    public void setServicesTable(){
        Vector columnNames=new Vector();
        columnNames.add("Service Name");
        columnNames.add("Class");
        Hashtable<String, Constructor> services=PPlatform.getPCU().getServices();
        TMassTableModel servicesModel=new TMassTableModel(columnNames,services.size());
        int i=0;
        String classPath;

        for(String service:services.keySet()){
            servicesModel.setValueAt(service,i,0);
            classPath=services.get(service).getName();
            classPath=classPath.replaceAll(TModelLoader._ServicesPath, "");
            servicesModel.setValueAt(classPath,i,1);
            i++;
        }

        TPServicesTable.setModel(servicesModel);
        TableColumn col = TPServicesTable.getColumnModel().getColumn(0);
        col.setPreferredWidth(100);
        col.setMinWidth(30);
        col.setMaxWidth(200);
    }

    public void setVariablesTable(){
        Vector columnNames=new Vector();
        columnNames.add("Variable");
        columnNames.add("Value");
        Hashtable<String,TObject> variables=PPlatform.getVariables();
        TMassTableModel variablesModel=new TMassTableModel(columnNames,variables.size());
        int i=0;
         for(String variable:variables.keySet()){
            variablesModel.setValueAt(variable, i, 0);
            variablesModel.setValueAt(variables.get(variable).getContent(), i, 1);
            i++;
         }
          TPVariablesTable.setModel(variablesModel);
         TableColumn col = TPVariablesTable.getColumnModel().getColumn(0);
         col.setPreferredWidth(55);
         col.setMinWidth(30);
         col.setMaxWidth(200);
    }

    public void refreshHistory(){
        if(!(TMass.PRunning)){
            Vector columnNames=new Vector();
            columnNames.add("Service");
            columnNames.add("Executed");
            columnNames.add("Finished");
            columnNames.add("Client");
            TMassTableModel servicesModel=new TMassTableModel(columnNames,PPlatform.getServiceHistory().size());
            int i=0;
                for(TAService service:PPlatform.getServiceHistory()){
                    servicesModel.setValueAt(service.getServiceName(),i,0);
                    servicesModel.setValueAt(service.getInitTime(),i,1);
                    if(service instanceof TASAsynchronousService)
                        servicesModel.setValueAt(((TASAsynchronousService)service).getFinishTime(),i,2);

                    servicesModel.setValueAt(service.getClientName(),i,3);
                    i++;
                }
            TServiceHistoryTable.setModel(servicesModel);
            TableColumn col = TServiceHistoryTable.getColumnModel().getColumn(0);
            col.setPreferredWidth(100);
            col.setMinWidth(30);
            col.setMaxWidth(200);
        }
    }


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

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

    public TPlatformsInternalFrame(TPlatform platform, TMass tmass) {
        initComponents();
        PConnectionsPanel=new TConnectionsPanel(platform,tmass);
        PPlatformTabbedPane.add(PConnectionsPanel,1);
        PPlatformTabbedPane.setTitleAt(1, "Connections");
        this.setTitle(platform.getElementName());
        PPlatform=platform;
        PTMass=tmass;
        setSlotsTable();
        setActiveServicesTable();
        setServicesTable();
        refreshHistory();
        setVariablesTable();
    }

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

        PPlatformTabbedPane = new javax.swing.JTabbedPane();
        PPlatformPanel = new javax.swing.JPanel();
        TPSMainSplit = new javax.swing.JSplitPane();
        TPSUpperSplit = new javax.swing.JSplitPane();
        TPSTPanel = new javax.swing.JScrollPane();
        TPSlotsTable = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        TPServicesTable = new javax.swing.JTable();
        TPSLowerSplit = new javax.swing.JSplitPane();
        jScrollPane6 = new javax.swing.JScrollPane();
        TPRunningServicesTable = new javax.swing.JTable();
        jScrollPane7 = new javax.swing.JScrollPane();
        TPVariablesTable = new javax.swing.JTable();
        PPlatformItneractionHistory = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        TServiceHistoryTable = new javax.swing.JTable();

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

        TPSMainSplit.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        TPSMainSplit.setDividerLocation(95);
        TPSMainSplit.setDividerSize(2);
        TPSMainSplit.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        TPSMainSplit.setResizeWeight(0.5);

        TPSUpperSplit.setDividerSize(2);
        TPSUpperSplit.setResizeWeight(0.5);

        TPSlotsTable.setFont(new java.awt.Font("Tahoma", 0, 10));
        TPSlotsTable.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 [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TPSTPanel.setViewportView(TPSlotsTable);

        TPSUpperSplit.setLeftComponent(TPSTPanel);

        TPServicesTable.setFont(new java.awt.Font("Tahoma", 0, 10));
        TPServicesTable.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 [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane5.setViewportView(TPServicesTable);

        TPSUpperSplit.setRightComponent(jScrollPane5);

        TPSMainSplit.setTopComponent(TPSUpperSplit);

        TPSLowerSplit.setDividerSize(2);
        TPSLowerSplit.setResizeWeight(0.5);

        TPRunningServicesTable.setFont(new java.awt.Font("Tahoma", 0, 10));
        TPRunningServicesTable.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 [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane6.setViewportView(TPRunningServicesTable);

        TPSLowerSplit.setRightComponent(jScrollPane6);

        TPVariablesTable.setFont(new java.awt.Font("Tahoma", 0, 10));
        TPVariablesTable.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 [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane7.setViewportView(TPVariablesTable);

        TPSLowerSplit.setLeftComponent(jScrollPane7);

        TPSMainSplit.setRightComponent(TPSLowerSplit);

        javax.swing.GroupLayout PPlatformPanelLayout = new javax.swing.GroupLayout(PPlatformPanel);
        PPlatformPanel.setLayout(PPlatformPanelLayout);
        PPlatformPanelLayout.setHorizontalGroup(
            PPlatformPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 449, Short.MAX_VALUE)
            .addGroup(PPlatformPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(TPSMainSplit, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE))
        );
        PPlatformPanelLayout.setVerticalGroup(
            PPlatformPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 260, Short.MAX_VALUE)
            .addGroup(PPlatformPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(TPSMainSplit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
        );

        PPlatformTabbedPane.addTab("State", PPlatformPanel);

        TServiceHistoryTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Service", "ExecTime", "FinishTime", "Client"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Long.class, java.lang.Long.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane3.setViewportView(TServiceHistoryTable);

        javax.swing.GroupLayout PPlatformItneractionHistoryLayout = new javax.swing.GroupLayout(PPlatformItneractionHistory);
        PPlatformItneractionHistory.setLayout(PPlatformItneractionHistoryLayout);
        PPlatformItneractionHistoryLayout.setHorizontalGroup(
            PPlatformItneractionHistoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
        );
        PPlatformItneractionHistoryLayout.setVerticalGroup(
            PPlatformItneractionHistoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
        );

        PPlatformTabbedPane.addTab("History", PPlatformItneractionHistory);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(PPlatformTabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(PPlatformTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
        );

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


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PPlatformItneractionHistory;
    private javax.swing.JPanel PPlatformPanel;
    private javax.swing.JTabbedPane PPlatformTabbedPane;
    private javax.swing.JTable TPRunningServicesTable;
    private javax.swing.JSplitPane TPSLowerSplit;
    private javax.swing.JSplitPane TPSMainSplit;
    private javax.swing.JScrollPane TPSTPanel;
    private javax.swing.JSplitPane TPSUpperSplit;
    private javax.swing.JTable TPServicesTable;
    private javax.swing.JTable TPSlotsTable;
    private javax.swing.JTable TPVariablesTable;
    private javax.swing.JTable TServiceHistoryTable;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    // End of variables declaration//GEN-END:variables

}
