/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 
 */



package org.wandora.application.tools.extractors.umbel;

import java.awt.Cursor;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import org.json.JSONObject;
import org.wandora.application.Wandora;
import org.wandora.application.gui.simple.SimpleButton;
import org.wandora.application.gui.simple.SimpleLabel;

/**
 *
 * @author akivela
 */


public class UmbelSearchConceptSelector extends javax.swing.JDialog {

    private boolean wasAccepted = false;
    private ArrayList<JSONObject> data = null;
    private JTable dataTable = null;
    private UmbelConceptTableModel dataModel = null;
    
    
    /**
     * Creates new form UmbelSearchConceptSelector
     */
    public UmbelSearchConceptSelector(ArrayList<JSONObject> data, Wandora wandora) {
        super(wandora, true);
        this.data = data;
        initComponents();
        wasAccepted = false;
        updateDataTable();
        setTitle("Search and import Umbel concepts");
    }

    public void open(Wandora wandora) {
        wasAccepted = false;
        this.setSize(950, 500);
        if(wandora != null) wandora.centerWindow(this);
        this.setVisible(true);
    }
    
    
    private void updateDataTable() {
        dataTable = new JTable();
        dataTable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        if(data != null) {
            dataModel = new UmbelConceptTableModel(data);
            dataTable.setModel(dataModel);
            dataTable.setRowSorter(new TableRowSorter(dataModel));

            dataTable.setColumnSelectionAllowed(false);
            dataTable.setRowSelectionAllowed(true);
            dataTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

            TableColumn column = null;
            for (int i=0; i < dataTable.getColumnCount(); i++) {
                column = dataTable.getColumnModel().getColumn(i);
                column.setPreferredWidth(dataModel.getColumnWidth(i));
            }
            tableScrollPane.setViewportView(dataTable);
        }
    }
    
    
        
        
    public boolean wasAccepted() {
        return wasAccepted;
    }
    
    
    
    public ArrayList<JSONObject> getSelection() {
        ArrayList<JSONObject> selection = new ArrayList<JSONObject>();
        int[] indexes = dataTable.getSelectedRows();
        for(int i=0; i<indexes.length; i++) {
            selection.add(data.get(dataTable.convertRowIndexToModel(indexes[i])));
        }
        return selection;
    }
    
    
    
    
    
    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        containerPanel = new javax.swing.JPanel();
        headerPanel = new javax.swing.JPanel();
        headerLabel = new SimpleLabel();
        tablePanel = new javax.swing.JPanel();
        tableScrollPane = new javax.swing.JScrollPane();
        buttonPanel = new javax.swing.JPanel();
        buttonFillerPanel = new javax.swing.JPanel();
        importButton = new SimpleButton();
        cancelButton = new SimpleButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        containerPanel.setLayout(new java.awt.GridBagLayout());

        headerPanel.setLayout(new java.awt.GridBagLayout());

        headerLabel.setText("<html>Select which Umbel concepts you would like to import to Wandora. Keep SHIFT or CTRL key pressed to select multiple concepts. After selection press import button.</html>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        headerPanel.add(headerLabel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 8, 0);
        containerPanel.add(headerPanel, gridBagConstraints);

        tablePanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        tablePanel.add(tableScrollPane, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        containerPanel.add(tablePanel, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridBagLayout());

        buttonFillerPanel.setPreferredSize(new java.awt.Dimension(254, 8));

        javax.swing.GroupLayout buttonFillerPanelLayout = new javax.swing.GroupLayout(buttonFillerPanel);
        buttonFillerPanel.setLayout(buttonFillerPanelLayout);
        buttonFillerPanelLayout.setHorizontalGroup(
            buttonFillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
        );
        buttonFillerPanelLayout.setVerticalGroup(
            buttonFillerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 8, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        buttonPanel.add(buttonFillerPanel, gridBagConstraints);

        importButton.setText("Import");
        importButton.setMargin(new java.awt.Insets(2, 4, 2, 4));
        importButton.setPreferredSize(new java.awt.Dimension(80, 23));
        importButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        buttonPanel.add(importButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.setMargin(new java.awt.Insets(2, 4, 2, 4));
        cancelButton.setPreferredSize(new java.awt.Dimension(80, 23));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(cancelButton, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        containerPanel.add(buttonPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(containerPanel, gridBagConstraints);

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

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        wasAccepted = false;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importButtonActionPerformed
        wasAccepted = true;
        this.setVisible(false);
    }//GEN-LAST:event_importButtonActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonFillerPanel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JButton importButton;
    private javax.swing.JPanel tablePanel;
    private javax.swing.JScrollPane tableScrollPane;
    // End of variables declaration//GEN-END:variables


    // -------------------------------------------------------------------------
    // -------------------------------------------------------------------------
    // -------------------------------------------------------------------------
    
    
    public class UmbelConceptTableModel extends DefaultTableModel implements TableModel {
        ArrayList<JSONObject> d = null;

        public UmbelConceptTableModel(ArrayList<JSONObject> m) {
            d = m;
        }


        @Override
        public int getRowCount() {
            if(d == null) return 0;
            return d.size();
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public String getColumnName(int columnIndex) {
            switch(columnIndex) {
                case 0: return "uri";
                case 1: return "pref-label";
                case 2: return "description";
                default: return "Illegal column";
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if(d == null) return null;
            if(rowIndex >= 0 && rowIndex < d.size()) {
                if(columnIndex >= 0 && columnIndex < 6) {
                    try {
                        JSONObject v = d.get(rowIndex);
                        switch(columnIndex) {
                            case 0: return v.get("uri").toString();
                            case 1: return v.get("pref-label").toString();
                            case 2: return v.get("description").toString();
                        }
                    }
                    catch(Exception e) {
                        return "";
                    }
                }
            }
            return null;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

        }


        public int getColumnWidth(int col) {
            switch(col) {
                case 0: return 200;
                case 1: return 100;
                case 2: return 400;
                default: return 100;
            }
        }

    }


}
