/*
 * 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;


import java.io.File;
import java.util.List;

import javax.swing.JDialog;

import org.wandora.application.Wandora;
import org.wandora.application.gui.UIConstants;
import org.wandora.application.gui.filechooser.TopicMapFileChooser;
import org.wandora.topicmap.layered.Layer;

/**
 *
 * @author  olli
 */
public class DiffToolConfigPanel extends javax.swing.JPanel {
    

	private static final long serialVersionUID = 1L;
	
	
	public static final int MODE_FILE=1;
    public static final int MODE_LAYER=2;
    public static final int MODE_PROJECT=3;
    public static final int MODE_NONE=99;
    
    protected Wandora wandora;
    
    protected JDialog parentDialog;
    
    protected boolean cancelled=true;
    
    /** Creates new form DiffToolConfigPanel */
    public DiffToolConfigPanel(Wandora wandora,JDialog parentDialog) {
        initComponents();
        this.wandora=wandora;
        this.parentDialog=parentDialog;
        List<Layer> layers=wandora.getTopicMap().getLayers();
        for(Layer l : layers){
            layerComboBox1.addItem(l.getName());
            layerComboBox2.addItem(l.getName());
        }
        map1GroupChanged();
        map2GroupChanged();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        map1Group = new javax.swing.ButtonGroup();
        map2Group = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        map1FileButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        map1LayerButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        map1ProjectButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        layerComboBox1 = new org.wandora.application.gui.simple.SimpleComboBox();
        layerComboBox1.setEditable(false);
        fileTextField1 = new org.wandora.application.gui.simple.SimpleField();
        fileButton1 = new org.wandora.application.gui.simple.SimpleButton();
        jPanel2 = new javax.swing.JPanel();
        map2FileButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        map2LayerButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        map2ProjectButton = new org.wandora.application.gui.simple.SimpleRadioButton();
        layerComboBox2 = new org.wandora.application.gui.simple.SimpleComboBox();
        layerComboBox2.setEditable(false);
        fileTextField2 = new org.wandora.application.gui.simple.SimpleField();
        fileButton2 = new org.wandora.application.gui.simple.SimpleButton();
        formatComboBox = new org.wandora.application.gui.simple.SimpleComboBox();
        formatComboBox.setEditable(false);
        jLabel1 = new org.wandora.application.gui.simple.SimpleLabel();
        buttonsPanel = new javax.swing.JPanel();
        okButton = new org.wandora.application.gui.simple.SimpleButton();
        cancelButton = new org.wandora.application.gui.simple.SimpleButton();

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

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Topic map 1", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, UIConstants.plainFont));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        map1Group.add(map1FileButton);
        map1FileButton.setSelected(true);
        map1FileButton.setText("File");
        map1FileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map1FileButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(map1FileButton, gridBagConstraints);

        map1Group.add(map1LayerButton);
        map1LayerButton.setText("Layer");
        map1LayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map1LayerButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(map1LayerButton, gridBagConstraints);

        map1Group.add(map1ProjectButton);
        map1ProjectButton.setText("Layer stack");
        map1ProjectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map1ProjectButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(map1ProjectButton, gridBagConstraints);

        layerComboBox1.setPreferredSize(new java.awt.Dimension(29, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(layerComboBox1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(fileTextField1, gridBagConstraints);

        fileButton1.setText("Browse");
        fileButton1.setMargin(new java.awt.Insets(0, 7, 0, 7));
        fileButton1.setPreferredSize(new java.awt.Dimension(69, 25));
        fileButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileButton1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel1.add(fileButton1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 3, 5);
        add(jPanel1, gridBagConstraints);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Topic Map 2", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, UIConstants.plainFont));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        map2Group.add(map2FileButton);
        map2FileButton.setSelected(true);
        map2FileButton.setText("File");
        map2FileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map2FileButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(map2FileButton, gridBagConstraints);

        map2Group.add(map2LayerButton);
        map2LayerButton.setText("Layer");
        map2LayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map2LayerButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(map2LayerButton, gridBagConstraints);

        map2Group.add(map2ProjectButton);
        map2ProjectButton.setText("Layer stack");
        map2ProjectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                map2ProjectButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(map2ProjectButton, gridBagConstraints);

        layerComboBox2.setPreferredSize(new java.awt.Dimension(29, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(layerComboBox2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(fileTextField2, gridBagConstraints);

        fileButton2.setText("Browse");
        fileButton2.setMargin(new java.awt.Insets(0, 7, 0, 7));
        fileButton2.setPreferredSize(new java.awt.Dimension(69, 25));
        fileButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileButton2ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 3);
        jPanel2.add(fileButton2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 5);
        add(jPanel2, gridBagConstraints);

        formatComboBox.setPreferredSize(new java.awt.Dimension(29, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 7);
        add(formatComboBox, gridBagConstraints);

        jLabel1.setText("Format");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(3, 10, 3, 5);
        add(jLabel1, gridBagConstraints);

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

        okButton.setText("Compare");
        okButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
        okButton.setMaximumSize(new java.awt.Dimension(70, 25));
        okButton.setMinimumSize(new java.awt.Dimension(70, 25));
        okButton.setPreferredSize(new java.awt.Dimension(70, 25));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(okButton, new java.awt.GridBagConstraints());

        cancelButton.setText("Cancel");
        cancelButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
        cancelButton.setMaximumSize(new java.awt.Dimension(70, 25));
        cancelButton.setMinimumSize(new java.awt.Dimension(70, 25));
        cancelButton.setPreferredSize(new java.awt.Dimension(70, 25));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        buttonsPanel.add(cancelButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 5, 3);
        add(buttonsPanel, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    public int getMap1Mode(){
        if(map1FileButton.isSelected()) return MODE_FILE;
        else if(map1LayerButton.isSelected()) return MODE_LAYER;
        else if(map1ProjectButton.isSelected()) return MODE_PROJECT;
        else return MODE_NONE;
    }
    public int getMap2Mode(){
        if(map2FileButton.isSelected()) return MODE_FILE;
        else if(map2LayerButton.isSelected()) return MODE_LAYER;
        else if(map2ProjectButton.isSelected()) return MODE_PROJECT;
        else return MODE_NONE;        
    }
    public String getMap1Value(){
        if(map1FileButton.isSelected()) return fileTextField1.getText();
        else if(map1LayerButton.isSelected()) return layerComboBox1.getSelectedItem().toString();
        else return null;
    }
    public String getMap2Value(){
        if(map2FileButton.isSelected()) return fileTextField2.getText();
        else if(map2LayerButton.isSelected()) return layerComboBox2.getSelectedItem().toString();
        else return null;        
    }
    
    public void addFormat(String format){
        formatComboBox.addItem(format);
    }
    
    public String getFormat(){
        return formatComboBox.getSelectedItem().toString();
    }
    
    private void map1GroupChanged(){
        fileTextField1.setEnabled(map1FileButton.isSelected());
        fileButton1.setEnabled(map1FileButton.isSelected());
        layerComboBox1.setEnabled(map1LayerButton.isSelected());
    }
    private void map2GroupChanged(){
        fileTextField2.setEnabled(map2FileButton.isSelected());
        fileButton2.setEnabled(map2FileButton.isSelected());
        layerComboBox2.setEnabled(map2LayerButton.isSelected());        
    }
    
    private void map1FileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map1FileButtonActionPerformed
        map1GroupChanged();
    }//GEN-LAST:event_map1FileButtonActionPerformed

    private void map1LayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map1LayerButtonActionPerformed
        map1GroupChanged();
    }//GEN-LAST:event_map1LayerButtonActionPerformed

    private void map1ProjectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map1ProjectButtonActionPerformed
        map1GroupChanged();
    }//GEN-LAST:event_map1ProjectButtonActionPerformed

    private void map2FileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map2FileButtonActionPerformed
        map2GroupChanged();
    }//GEN-LAST:event_map2FileButtonActionPerformed

    private void map2LayerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map2LayerButtonActionPerformed
        map2GroupChanged();
    }//GEN-LAST:event_map2LayerButtonActionPerformed

    private void map2ProjectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_map2ProjectButtonActionPerformed
        map2GroupChanged();
    }//GEN-LAST:event_map2ProjectButtonActionPerformed

    private String browseFile(){
        TopicMapFileChooser chooser=new TopicMapFileChooser();
        if(chooser.open(wandora, "Select")==TopicMapFileChooser.APPROVE_OPTION){
            File file = chooser.getSelectedFile();        
            return file.getAbsolutePath();
        }
        else return null;
    }
    
    private void fileButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileButton1ActionPerformed
        String f=browseFile();
        if(f!=null) fileTextField1.setText(f);
    }//GEN-LAST:event_fileButton1ActionPerformed

    private void fileButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileButton2ActionPerformed
        String f=browseFile();
        if(f!=null) fileTextField2.setText(f);        
    }//GEN-LAST:event_fileButton2ActionPerformed

    public boolean wasCancelled(){
        return cancelled;
    }
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        cancelled=false;
        if(parentDialog!=null) parentDialog.setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        cancelled=true;
        if(parentDialog!=null) parentDialog.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton fileButton1;
    private javax.swing.JButton fileButton2;
    private javax.swing.JTextField fileTextField1;
    private javax.swing.JTextField fileTextField2;
    private javax.swing.JComboBox formatComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox layerComboBox1;
    private javax.swing.JComboBox layerComboBox2;
    private javax.swing.JRadioButton map1FileButton;
    private javax.swing.ButtonGroup map1Group;
    private javax.swing.JRadioButton map1LayerButton;
    private javax.swing.JRadioButton map1ProjectButton;
    private javax.swing.JRadioButton map2FileButton;
    private javax.swing.ButtonGroup map2Group;
    private javax.swing.JRadioButton map2LayerButton;
    private javax.swing.JRadioButton map2ProjectButton;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    
}
