/*
 * 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/>.
 *
 * 
 * NewTopicMapPanel.java
 *
 * Created on 21. marraskuuta 2005, 10:35
 */

package org.wandora.application.gui;


import java.awt.event.KeyEvent;

import org.wandora.application.Wandora;
import org.wandora.application.gui.simple.SimpleField;
import org.wandora.topicmap.TopicMap;
import org.wandora.topicmap.TopicMapConfigurationPanel;
import org.wandora.topicmap.TopicMapException;
import org.wandora.topicmap.TopicMapType;
import org.wandora.utils.Delegate;
import org.wandora.utils.Options;


/**
 *
 * @author  olli
 */
public class NewTopicMapPanel extends javax.swing.JPanel {
	
	private static final long serialVersionUID = 1L;
	
    
    private Wandora wandora;
    private Delegate<Object,NewTopicMapPanel> okDelegate;
    private Delegate<Object,NewTopicMapPanel> cancelDelegate;
    private TopicMapConfigurationPanel confPanel=null;
    private boolean acceptNamePropositions = true;
    
    
    /** Creates new form NewTopicMapPanel */
    public NewTopicMapPanel(Wandora wandora,Delegate<Object,NewTopicMapPanel> okDelegate,Delegate<Object,NewTopicMapPanel> cancelDelegate) {
        this.wandora=wandora;
        this.okDelegate=okDelegate;
        this.cancelDelegate=cancelDelegate;
        initComponents();
        
        typeComboBox.setEditable(false);
        
        Options options = wandora.getOptions();
        if(options != null) {
            int i=0;
            while(options.get("layers.layer["+i+"]") != null) {
                String layerTypeClassName = options.get("layers.layer["+i+"]");
                try {
                    if(layerTypeClassName != null) {
                        layerTypeClassName = layerTypeClassName.trim();
                        if(layerTypeClassName.length() > 0) {
                            Class layerTypeClass = Class.forName(layerTypeClassName);
                            if(layerTypeClass != null) {
                                Object layerType = layerTypeClass.newInstance();
                                if(layerType instanceof TopicMapType) {
                                    typeComboBox.addItem(layerType);
                                }
                            }
                        }
                    }
                }
                catch(Exception e) {
                    System.out.println("Warning: Can't create topic map layer type with class name '"+layerTypeClassName+"'.");
                    e.printStackTrace();
                }
                i++;
            }
        }
        
        if(okDelegate==null) okButton.setVisible(false);
        if(cancelDelegate==null) cancelButton.setVisible(false);
        //this.setName("NewTopicMapPanel");
    }
    
    public String getName(){
        return nameTextField.getText();
    }
    
    public TopicMap createTopicMap() throws TopicMapException {
        TopicMapType type=(TopicMapType)typeComboBox.getSelectedItem();
        Object params=confPanel.getParameters();
        TopicMap tm=type.createTopicMap(params);
        return tm;
    }
    
    public void proposeName(String proposedName) {
        if(acceptNamePropositions) nameTextField.setText(proposedName);
    }
    
    
    /** 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;

        layerNameLabel = new org.wandora.application.gui.simple.SimpleLabel();
        nameTextField = new SimpleField();
        layerTypeLabel = new org.wandora.application.gui.simple.SimpleLabel();
        typeComboBox = new org.wandora.application.gui.simple.SimpleComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        confContainerPanel = new javax.swing.JPanel();
        jSeparator2 = new javax.swing.JSeparator();
        buttonPanel = new javax.swing.JPanel();
        okButton = new org.wandora.application.gui.simple.SimpleButton();
        cancelButton = new org.wandora.application.gui.simple.SimpleButton();

        setName("newLayerPanel"); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        layerNameLabel.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(8, 7, 0, 5);
        add(layerNameLabel, gridBagConstraints);

        nameTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                rejectNamePropositionsAfterEdit(evt);
            }
        });
        nameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameTextFieldKeyReleased(evt);
            }
        });
        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(8, 5, 0, 7);
        add(nameTextField, gridBagConstraints);

        layerTypeLabel.setText("Type");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 5, 5);
        add(layerTypeLabel, gridBagConstraints);

        typeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                typeComboBoxActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 7);
        add(typeComboBox, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 2, 7);
        add(jSeparator1, gridBagConstraints);

        confContainerPanel.setName("confPanel"); // NOI18N
        confContainerPanel.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(confContainerPanel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 7, 0, 7);
        add(jSeparator2, gridBagConstraints);

        buttonPanel.setLayout(new java.awt.GridLayout(1, 0, 3, 0));

        okButton.setText("OK");
        okButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        okButton.setMaximumSize(new java.awt.Dimension(70, 23));
        okButton.setMinimumSize(new java.awt.Dimension(70, 23));
        okButton.setPreferredSize(new java.awt.Dimension(70, 23));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

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

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

    private void rejectNamePropositionsAfterEdit(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rejectNamePropositionsAfterEdit
        acceptNamePropositions = false;
    }//GEN-LAST:event_rejectNamePropositionsAfterEdit

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        cancelDelegate.invoke(this);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        okDelegate.invoke(this);
    }//GEN-LAST:event_okButtonActionPerformed

    private void typeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_typeComboBoxActionPerformed
        TopicMapType type=(TopicMapType)typeComboBox.getSelectedItem();
        confContainerPanel.removeAll();
        confPanel=type.getConfigurationPanel(wandora, wandora.getOptions());
        confContainerPanel.add(confPanel);
        confContainerPanel.revalidate();
        confContainerPanel.repaint();
    }//GEN-LAST:event_typeComboBoxActionPerformed

    private void nameTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER) {
            okButtonActionPerformed(null);
        }
    }//GEN-LAST:event_nameTextFieldKeyReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel confContainerPanel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel layerNameLabel;
    private javax.swing.JLabel layerTypeLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox typeComboBox;
    // End of variables declaration//GEN-END:variables
 
}
