/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package edu.uth.sbmi.mco.java.app;

import com.formdev.flatlaf.FlatLightLaf;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import edu.uth.sbmi.mco.java.McoJavaMain;
import edu.uth.sbmi.mco.java.models.ClassData;
import java.awt.event.ItemEvent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tuan
 */
public class McoApp extends javax.swing.JFrame {

    private McoJavaMain mco = null;

    private List<ClassData> model_card_concepts = null;
    private List<ClassData> algorithms = null;
    private List<ClassData> licenses = null;
    private List<ClassData> doc_parts = null;
    
    private DefaultListModel<String> list_model = new DefaultListModel<String>();

    /**
     * Creates new form McoApp
     */
    public McoApp() {
        initComponents();

        this.setLocationRelativeTo(null);

        statusLabel.setText("Loading ontology model interface. Please wait....");
    }

    public void setupOntologyInterface() {
        //this.setEnabled(false);
        mco = McoJavaMain.getInstance();
    }
    
    public McoJavaMain getMCO_Interface(){
        return mco;
    }

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

        jSeparator2 = new javax.swing.JSeparator();
        annotationGroup = new javax.swing.ButtonGroup();
        tablePopup = new javax.swing.JPopupMenu();
        deleteTablePopup = new javax.swing.JMenuItem();
        editTablePopup = new javax.swing.JMenuItem();
        tabPanel = new javax.swing.JTabbedPane();
        AuthoringPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblEncoded = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDocumentation = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        btnDelete = new javax.swing.JButton();
        cbAnnotations = new javax.swing.JComboBox<>();
        btnAnnotate = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listAnnotation = new javax.swing.JList<>();
        btnEncode = new javax.swing.JButton();
        btnExport = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        lblToolText = new javax.swing.JLabel();
        statusPanel = new javax.swing.JPanel();
        statusLabel = new javax.swing.JLabel();
        rbModelCardConcepts = new javax.swing.JRadioButton();
        rbAlgorithm = new javax.swing.JRadioButton();
        rbLicenses = new javax.swing.JRadioButton();
        btnClearItem = new javax.swing.JButton();
        rbMisc_doc_parts = new javax.swing.JRadioButton();

        deleteTablePopup.setText("Delete");
        tablePopup.add(deleteTablePopup);

        editTablePopup.setText("Edit...");
        tablePopup.add(editTablePopup);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MCRO Demo App Prototype");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                onWindowOpened(evt);
            }
        });

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

            },
            new String [] {
                "Documentation", "Annotation", "IRI"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblEncoded.setComponentPopupMenu(tablePopup);
        jScrollPane1.setViewportView(tblEncoded);

        jLabel1.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
        jLabel1.setText("Enter text for the model");

        txtDocumentation.setColumns(20);
        txtDocumentation.setRows(5);
        jScrollPane2.setViewportView(txtDocumentation);

        btnDelete.setText("Delete");

        cbAnnotations.setEditable(true);
        cbAnnotations.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbAnnotationsItemStateChanged(evt);
            }
        });

        btnAnnotate.setText("Annotate");
        btnAnnotate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnnotateActionPerformed(evt);
            }
        });

        listAnnotation.setModel(list_model);
        jScrollPane3.setViewportView(listAnnotation);

        btnEncode.setText("Encode");
        btnEncode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncodeActionPerformed(evt);
            }
        });

        btnExport.setText("Export");
        btnExport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExportActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N
        jLabel2.setText("Select an annotation for the text");

        lblToolText.setFont(new java.awt.Font("Helvetica Neue", 2, 13)); // NOI18N
        lblToolText.setText("<html></html>");

        statusLabel.setFont(new java.awt.Font("Helvetica Neue", 1, 13)); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
        );

        annotationGroup.add(rbModelCardConcepts);
        rbModelCardConcepts.setSelected(true);
        rbModelCardConcepts.setText("model card concept");
        rbModelCardConcepts.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rbModelCardConceptsItemStateChanged(evt);
            }
        });

        annotationGroup.add(rbAlgorithm);
        rbAlgorithm.setText("algorithms");
        rbAlgorithm.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rbAlgorithmItemStateChanged(evt);
            }
        });

        annotationGroup.add(rbLicenses);
        rbLicenses.setText("licenses");
        rbLicenses.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rbLicensesItemStateChanged(evt);
            }
        });

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

        annotationGroup.add(rbMisc_doc_parts);
        rbMisc_doc_parts.setText("misc document parts");
        rbMisc_doc_parts.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                rbMisc_doc_partsItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout AuthoringPanelLayout = new javax.swing.GroupLayout(AuthoringPanel);
        AuthoringPanel.setLayout(AuthoringPanelLayout);
        AuthoringPanelLayout.setHorizontalGroup(
            AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(AuthoringPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AuthoringPanelLayout.createSequentialGroup()
                        .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(statusPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(AuthoringPanelLayout.createSequentialGroup()
                                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 538, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(AuthoringPanelLayout.createSequentialGroup()
                                        .addComponent(cbAnnotations, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnAnnotate)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblToolText, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AuthoringPanelLayout.createSequentialGroup()
                                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AuthoringPanelLayout.createSequentialGroup()
                                        .addComponent(btnClearItem)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnEncode)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(AuthoringPanelLayout.createSequentialGroup()
                        .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(AuthoringPanelLayout.createSequentialGroup()
                                .addComponent(btnDelete)
                                .addGap(45, 45, 45)
                                .addComponent(btnExport))
                            .addGroup(AuthoringPanelLayout.createSequentialGroup()
                                .addComponent(rbModelCardConcepts)
                                .addGap(18, 18, 18)
                                .addComponent(rbAlgorithm)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(rbLicenses)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbMisc_doc_parts)))
                        .addGap(0, 577, Short.MAX_VALUE))))
        );
        AuthoringPanelLayout.setVerticalGroup(
            AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AuthoringPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbModelCardConcepts)
                    .addComponent(rbAlgorithm)
                    .addComponent(rbLicenses)
                    .addComponent(rbMisc_doc_parts))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbAnnotations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAnnotate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblToolText)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEncode)
                    .addComponent(btnClearItem))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AuthoringPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDelete)
                    .addComponent(btnExport))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabPanel.addTab("Authoring", AuthoringPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPanel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabPanel))
        );

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

    private void btnAnnotateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnnotateActionPerformed
        // TODO add your handling code here:
        
        if(cbAnnotations.getSelectedIndex() == -1){
            JOptionPane.showMessageDialog(null, "Select an annotation!", "", JOptionPane.WARNING_MESSAGE);
            return;
        }
        
        Object selectedItem = cbAnnotations.getSelectedItem();
        
        
        if(rbAlgorithm.isSelected()){
            
            this.algorithms.forEach(l->{
                if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                   
                    list_model.addElement(selectedItem.toString() + " | " + l.getClass_iri().toString());
                }
                
            });
            
        }
        else if (this.rbLicenses.isSelected()){
            
            this.licenses.forEach(l->{
               
                if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    
                    list_model.addElement(selectedItem.toString() +" | " + l.getClass_iri().toString());
                }
                
            });
            
        }
        else if (this.rbModelCardConcepts.isSelected()){
            
            this.model_card_concepts.forEach(l->{
            if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    
                    list_model.addElement(selectedItem.toString() +" | " + l.getClass_iri().toString());
                }
            });
            
        }
        else if (this.rbMisc_doc_parts.isSelected()){
            
            this.doc_parts.forEach(l->{
            if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    
                    list_model.addElement(selectedItem.toString() +" | " + l.getClass_iri().toString());
                }
            });
            
        }
        
    }//GEN-LAST:event_btnAnnotateActionPerformed

    private void btnEncodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncodeActionPerformed
        // TODO add your handling code here:

        if (txtDocumentation.getText().trim().length() == 0) {

            JOptionPane.showMessageDialog(null, "No text data", "", JOptionPane.WARNING_MESSAGE);

            return;
        }

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

        DefaultTableModel tableModel = (DefaultTableModel) tblEncoded.getModel();
        
        StringBuilder annotate = new StringBuilder();
        StringBuilder IRIs = new StringBuilder();
        for(int i=0; i < list_model.getSize(); i++){
            if(i !=0){
                annotate.append(",");
                IRIs.append(",");
            }
            String target = list_model.getElementAt(i);
            String annotation_string = target.substring(0, target.indexOf("|"));
            String iri_string = target.substring(target.indexOf("|")+1);
            
            annotate.append(annotation_string.trim());
            IRIs.append(iri_string.trim());
            
        }
        
        Object[] rowdata = {text, annotate.toString(), IRIs.toString()};
        tableModel.addRow(rowdata);

        list_model.removeAllElements();
        txtDocumentation.setText("");
    }//GEN-LAST:event_btnEncodeActionPerformed

    private void onWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_onWindowOpened
        // TODO add your handling code here:

        
        LoadOntologyInterface loader = new LoadOntologyInterface();
        loader.setParent(this);
        loader.start();

    }//GEN-LAST:event_onWindowOpened

    private void rbModelCardConceptsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rbModelCardConceptsItemStateChanged
        // TODO add your handling code here:
        
        populateComboList(model_card_concepts);
    }//GEN-LAST:event_rbModelCardConceptsItemStateChanged

    private void rbAlgorithmItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rbAlgorithmItemStateChanged
        // TODO add your handling code here:
        populateComboList(algorithms);
    }//GEN-LAST:event_rbAlgorithmItemStateChanged

    private void rbLicensesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rbLicensesItemStateChanged
        // TODO add your handling code here:
        populateComboList(licenses);
    }//GEN-LAST:event_rbLicensesItemStateChanged

    private void cbAnnotationsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbAnnotationsItemStateChanged
        // TODO add your handling code here:
        
        
        if(evt.getStateChange() == ItemEvent.SELECTED){
            getClassDataTip(cbAnnotations.getSelectedItem());
        }
        
    }//GEN-LAST:event_cbAnnotationsItemStateChanged

    private void btnClearItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearItemActionPerformed
        // TODO add your handling code here:
        int selectedIndex = listAnnotation.getSelectedIndex();
        
        if(selectedIndex != -1){
            list_model.remove(selectedIndex);
        }
        else{
           JOptionPane.showMessageDialog(null, "Select an item to clear!", "", JOptionPane.WARNING_MESSAGE); 
        }
        
        
    }//GEN-LAST:event_btnClearItemActionPerformed

    private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExportActionPerformed
        // TODO add your handling code here:
        
       
        
        ExportToArtifactProcess exportProcess = new ExportToArtifactProcess();
        exportProcess.setParent(this);
        exportProcess.setOntologyManager();
        exportProcess.start();
    }//GEN-LAST:event_btnExportActionPerformed

    private void rbMisc_doc_partsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_rbMisc_doc_partsItemStateChanged
        // TODO add your handling code here:
        
        populateComboList(this.doc_parts);
    }//GEN-LAST:event_rbMisc_doc_partsItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            //UIManager.setLookAndFeel( new FlatDarkLaf() );

            UIManager.setLookAndFeel(new FlatLightLaf());
            /*for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
             */
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(McoApp.class.getName()).log(Level.SEVERE, null, ex);
        }

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new McoApp().setVisible(true);
            }
        });
    }
    
    public void populateComboList(List<ClassData> list){
        
        if(cbAnnotations.getItemCount()>0){
            cbAnnotations.removeAllItems();
        }
        
        for(ClassData cd : list){
            
            if(cd.getHuman_friendly_name() == null || cd.getHuman_friendly_name().length()<1){
                cbAnnotations.addItem(cd.getClass_iri().getFragment()); 
            }
            else{
                cbAnnotations.addItem(cd.getHuman_friendly_name());
            }
            
        }
        
        
    }

    private void getClassDataTip(Object selectedItem) {
        
        if(rbAlgorithm.isSelected()){
            this.algorithms.forEach(l->{
                if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    lblToolText.setText("<html>" + l.getTip_text() + "<html>");
                }
                
            });
        }
        
        if(rbLicenses.isSelected()){
            
            this.licenses.forEach(l->{
                

                
                if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    lblToolText.setText("<html>" + l.getTip_text() + "<html>");
                }
                
            });
            
        }
        
        
        if(rbModelCardConcepts.isSelected()){

            this.model_card_concepts.forEach(l->{
            if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    lblToolText.setText("<html>" + l.getTip_text() + "<html>");
                }
            });
            

        }
        
        if (this.rbMisc_doc_parts.isSelected()){
            
            this.doc_parts.forEach(l->{
            if(l.getHuman_friendly_name().equals(selectedItem.toString()))
                {
                    lblToolText.setText("<html>" + l.getTip_text() + "<html>");
                }
            });
            
        }
        
        
    }

    class LoadOntologyInterface extends Thread {

        private McoApp parent = null;
        
        @Override
        public void run() {
            
            
            LoadingOntologyModal modal = new LoadingOntologyModal(parent, false);
            
            modal.setLocationRelativeTo(parent);
            modal.setVisible(true);
            
            modal.setStatusText("setting up the ontology library..");
            setupOntologyInterface();
            modal.setStatusText("retrieving the model card concepts..");
            model_card_concepts = mco.get_model_card_concepts();
            modal.setStatusText("retrieving the algorithms..");
            algorithms = mco.get_algorithms();
            modal.setStatusText("retrieving the licenses..");
            licenses = mco.get_licenses();
            
            modal.setStatusText("retrieving misc document parts...");
            doc_parts = mco.get_doc_parts();

            modal.setStatusText("Done.");
            statusLabel.setText("Ready");
            
            populateComboList(model_card_concepts);
            
            modal.setVisible(false);
        }
        
        public void setParent(McoApp _parent){
            this.parent = _parent;
        }

    }
    
    class ExportToArtifactProcess extends Thread{
        
        /*
        -panel for customization and metadata (author information)
        -
        */
        
        private McoApp parent = null;
        
        private McoJavaMain mco = null;
        
        public void setParent(McoApp _parent){
            this.parent = _parent;
            
        }
        
        @Override
        public void run(){
            
            SaveOntologyModal modal = new SaveOntologyModal(parent, false);
            
            modal.setLocationRelativeTo(parent);
            modal.setModelCardOntologyInterface(parent.getMCO_Interface());
            modal.setVisible(true);
            

            
            modal.setInstanceTableData(parent.getInstanceDataTable());
           
            
            
        }
        
        
        

        private void setOntologyManager() {
            
            mco = parent.getMCO_Interface();
        }
        
    }
    
    private Multimap<String, String> getInstanceDataTable(){
        
        Multimap<String, String> instance_data = ArrayListMultimap.create();
        
        
        
        DefaultTableModel tableModel = (DefaultTableModel) tblEncoded.getModel();
        
        for(int i=0; i < tableModel.getRowCount(); i++){
   
            String index_key = tableModel.getValueAt(i, 0).toString();
            
            String[] class_values = tableModel.getValueAt(i, 1).toString().replaceAll("\\[", "").replaceAll("\\]", "").split(",");
            
            String [] iri_values = tableModel.getValueAt(i, 2).toString().replaceAll("\\[", "").replaceAll("\\]", "").split(",");
            
            
            StringBuilder values = new StringBuilder();
            for(int x=0 ; x < class_values.length; x++){
                if(x !=0){
                    values.append(",");
                }
                values.append(class_values[x] + " | " + iri_values[x]);
            }
            
            instance_data.put(index_key, values.toString());
            
        }
        
        return instance_data;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AuthoringPanel;
    private javax.swing.ButtonGroup annotationGroup;
    private javax.swing.JButton btnAnnotate;
    private javax.swing.JButton btnClearItem;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEncode;
    private javax.swing.JButton btnExport;
    private javax.swing.JComboBox<String> cbAnnotations;
    private javax.swing.JMenuItem deleteTablePopup;
    private javax.swing.JMenuItem editTablePopup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblToolText;
    private javax.swing.JList<String> listAnnotation;
    private javax.swing.JRadioButton rbAlgorithm;
    private javax.swing.JRadioButton rbLicenses;
    private javax.swing.JRadioButton rbMisc_doc_parts;
    private javax.swing.JRadioButton rbModelCardConcepts;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTabbedPane tabPanel;
    private javax.swing.JPopupMenu tablePopup;
    public javax.swing.JTable tblEncoded;
    private javax.swing.JTextArea txtDocumentation;
    // End of variables declaration//GEN-END:variables
}
